Synopsis - Cross-Reference
File: /Synopsis/Parsers/Cxx/Types.hh1// 2// Copyright (C) 2002 Stephen Davies 3// Copyright (C) 2002 Stefan Seefeld 4// All rights reserved. 5// Licensed to the public under the terms of the GNU LGPL (>= 2), 6// see the file COPYING for details. 7// 8 9#ifndef Types_hh_ 10#define Types_hh_ 11 12#include <vector> 13#include <string> 14#include <typeinfo> 15#include "common.hh" 16#include "FakeGC.hh" 17 18// Forward declare ASG::Declaration 19namespace ASG 20{ 21class Declaration; 22class Parameter; 23} 24 25//. The Type hierarchy 26namespace Types 27{ 28// Forward declaration of the Types::Visitor class defined in this file 29class Visitor; 30 31// Forward decl of Named 32class Named; 33 34//. The base class of the Type hierarchy 35 class Type : public FakeGC::LightObject 36{ 37public: 38 //. A vector of Type objects 39 typedef std::vector<Type*> vector; 40 41 //. Typedef for modifier list 42 typedef std::vector<std::string> Mods; 43 44 //. Constructor 45 Type(); 46 47 //. Destructor. Note that Types::Type becomes abstract, unlike ASG::Declaration 48 virtual ~Type(); 49 50 //. Accept the given visitor 51 virtual void accept(Visitor*); 52}; 53 54//. Base class of types with Names 55class Named : public Type 56{ 57public: 58 //. A vector of Types::Named objects 59 typedef std::vector<Types::Named*> vector; 60 61 //. Constructor 62 Named(const ScopedName& name); 63 64 //. Destructor 65 ~Named(); 66 67 //. Accept the given visitor 68 virtual void accept(Visitor*); 69 70 // 71 // Attribute Methods 72 // 73 74 //. Constant version of name() 75 const ScopedName& name() const 76 { 77 return m_name; 78 } 79 80 //. Return the scoped ScopedName of this type 81 ScopedName& name() 82 { 83 return m_name; 84 } 85 86private: 87 //. The scoped ScopedName of this type 88 ScopedName m_name; 89}; 90 91 92//. Base types are the (implicitly declared) builtin types such as 'int', 'bool' etc 93class Base : public Named 94{ 95public: 96 //. Constructor 97 Base(const ScopedName& name); 98 99 //. Accept the given visitor 100 virtual void accept(Visitor*); 101 102}; 103 104 105//. Unknown type 106class Unknown : public Named 107{ 108public: 109 //. Constructor 110 Unknown(const ScopedName& name); 111 //. Accept the given visitor 112 virtual void accept(Visitor*); 113}; 114 115 116//. Template parameter dependent types have a possibly scoped name, but no 117//. known type. Member types of template-parameters will be scoped, eg: 118//. 119//. template<class T> class A { T::B b; } 120//. 121//. Here T::B is a depandant type, and so is T. 122class Dependent : public Named 123{ 124public: 125 //. Constructor 126 Dependent(const ScopedName& name); 127 //. Accept the given visitor 128 virtual void accept(Visitor*); 129}; 130 131 132 133//. Declared types have a name and a reference to their declaration 134class Declared : public Named 135{ 136public: 137 //. Constructor 138 Declared(const ScopedName& name, ASG::Declaration* decl); 139 //. Accept the given visitor 140 virtual void accept(Visitor*); 141 142 // 143 // Attribute Methods 144 // 145 146 //. Const version of declaration() 147 const ASG::Declaration* declaration() const 148 { 149 return m_decl; 150 } 151 152 //. Returns the Declaration referenced by this type 153 ASG::Declaration* declaration() 154 { 155 return m_decl; 156 } 157 158private: 159 //. The declaration referenced by this type 160 ASG::Declaration* m_decl; 161}; 162 163 164//. Template types are declared template types. They have a name, a 165//. declaration (which is an ASG::Class) and a vector of parameters 166//. declare this template. Each parameter (using ASG::Parameter) should be 167//. either the correct type for non-type parameters, or a Dependent for type 168//. parameters. In either case, there may be default values. 169class Template : public Declared 170{ 171public: 172 //. A vector of Types::Template objects 173 typedef std::vector<Types::Template*> vector; 174 175 //. A vector of Parameter objects 176 typedef std::vector<ASG::Parameter*> param_vector; 177 178 //. Constructor 179 Template(const ScopedName& name , ASG::Declaration* decl, const param_vector& params); 180 181 //. Accept the given visitor 182 virtual void accept(Visitor*); 183 184 // 185 // Attribute Methods 186 // 187 188 //. Constant version of parameters() 189 const param_vector& parameters() const 190 { 191 return m_params; 192 } 193 194 //. Returns the vector of parameter Types 195 param_vector& parameters() 196 { 197 return m_params; 198 } 199 200 //. Constant version of specializations() 201 const Template::vector& specializations() const 202 { 203 return m_specs; 204 } 205 206 //. Returns the vector of specializations for this template. 207 //. If this template is a specialization, the vector must be empty 208 Template::vector& specializations() 209 { 210 return m_specs; 211 } 212 213private: 214 //. The parameters 215 param_vector m_params; 216 217 //. The vector of specializations for this template 218 Template::vector m_specs; 219}; 220 221//. Type Modifier. This type has a nested type, and wraps it with 222//. modifiers such as const, reference, etc. 223class Modifier : public Type 224{ 225public: 226 //. Constructor 227 Modifier(Type* alias, const Mods& pre, const Mods& post); 228 229 //. Destructor 230 ~Modifier(); 231 232 //. Accept the given visitor 233 virtual void accept(Visitor*); 234 235 // 236 // Attribute Methods 237 // 238 239 //. Returns the alias (modified) Type object 240 Type* alias() 241 { 242 return m_alias; 243 } 244 245 //. Returns the premodifiers 246 Mods& pre() 247 { 248 return m_pre; 249 } 250 251 //. Returns the postmodifiers 252 Mods& post() 253 { 254 return m_post; 255 } 256 257private: 258 //. The alias type 259 Type* m_alias; 260 //. The pre and post modifiers 261 Mods m_pre, m_post; 262}; 263 264//. Type Array. This type adds array dimensions to a primary type 265class Array : public Type 266{ 267public: 268 //. Constructor 269 Array(Type* alias, const Mods& sizes); 270 271 //. Destructor 272 ~Array(); 273 274 //. Accept the given visitor 275 virtual void accept(Visitor*); 276 277 //. Returns the alias (modified) Type object 278 Type* alias() 279 { 280 return m_alias; 281 } 282 283 //. Returns the sizes 284 Mods& sizes() 285 { 286 return m_sizes; 287 } 288private: 289 //. The alias type 290 Type* m_alias; 291 //. The sizes 292 Mods m_sizes; 293}; 294 295 296//. Parameterized type. A parameterized type is an instantiation of a 297//. Template type with concrete Types as parameters. 298class Parameterized : public Type 299{ 300public: 301 //. Constructor 302 Parameterized(Named* templ, const Type::vector& params); 303 304 //. Destructor 305 ~Parameterized(); 306 307 //. Accept the given visitor 308 virtual void accept(Visitor*); 309 310 // 311 // Attribute Methods 312 // 313 314 //. Returns the Template type this is an instance of 315 Named* template_id() 316 { 317 return m_template; 318 } 319 320 //. Constant version of parameters() 321 const Type::vector& parameters() const 322 { 323 return m_params; 324 } 325 326 //. Returns the vector of parameter Types 327 Type::vector& parameters() 328 { 329 return m_params; 330 } 331 332private: 333 //. The Template object, either a Template or a Dependent. 334 Named* m_template; 335 //. The vector of parameter Types 336 Type::vector m_params; 337}; 338 339 340//. Function Pointer type. Function ptr types have a return type and a 341//. list of parameter types, along with a list of premodifiers. 342class FuncPtr : public Type 343{ 344public: 345 //. Constructor 346 FuncPtr(Type* ret, const Mods& premods, const Type::vector& params); 347 348 //. Destructor 349 ~FuncPtr(); 350 351 //. Accept the given visitor 352 virtual void accept(Visitor*); 353 354 // 355 // Attribute Methods 356 // 357 358 //. Returns the Return Type object 359 Type* return_type() 360 { 361 return m_return; 362 } 363 364 //. Const version of pre() 365 const Mods& pre() const 366 { 367 return m_premod; 368 } 369 370 //. Returns the premodifier vector 371 Mods& pre() 372 { 373 return m_premod; 374 } 375 376 //. Constant version of parameters() 377 const Type::vector& parameters() const 378 { 379 return m_params; 380 } 381 382 //. Returns the vector of parameter Types 383 Type::vector& parameters() 384 { 385 return m_params; 386 } 387 388private: 389 //. The Return Type 390 Type* m_return; 391 //. The premodifiers 392 Mods m_premod; 393 //. The parameters 394 Type::vector m_params; 395}; 396 397 398//. The Visitor base class 399class Visitor 400{ 401public: 402 // Virtual destructor makes abstract 403 virtual ~Visitor() = 0; 404 virtual void visit_type(Type*); 405 virtual void visit_unknown(Unknown*); 406 virtual void visit_modifier(Modifier*); 407 virtual void visit_array(Array*); 408 virtual void visit_named(Named*); 409 virtual void visit_base(Base*); 410 virtual void visit_dependent(Dependent*); 411 virtual void visit_declared(Declared*); 412 virtual void visit_template_type(Template*); 413 virtual void visit_parameterized(Parameterized*); 414 virtual void visit_func_ptr(FuncPtr*); 415}; 416 417//. The exception thrown by the special cast templates 418class wrong_type_cast : public std::exception 419{ 420public: 421 //. Constructor 422 wrong_type_cast() throw () 423 {} 424 //. Destructor 425 virtual ~wrong_type_cast() throw () 426 {} 427 //. Returns name of this class 428 virtual const char* what() const throw(); 429}; 430 431//. Casts Types::Type types to derived types safely. The cast is done using 432//. dynamic_cast, and wrong_type_cast is thrown upon failure. 433template <typename T> 434T* type_cast(Type* type_ptr) throw (wrong_type_cast) 435{ 436 if (T* derived_ptr = dynamic_cast<T*>(type_ptr)) 437 return derived_ptr; 438 throw wrong_type_cast(); 439} 440 441//. Casts Types::Named types to derived types safely. The cast is done using 442//. dynamic_cast, and wrong_type_cast is thrown upon failure. 443template <typename T> 444T* named_cast(Named* named_ptr) throw (wrong_type_cast) 445{ 446 if (T* derived_ptr = dynamic_cast<T*>(named_ptr)) 447 return derived_ptr; 448 throw wrong_type_cast(); 449} 450 451//. Safely extracts typed Declarations from Named types. The type is first 452//. safely cast to Types::Declared, then the declaration() safely cast to 453//. the template type. 454template <typename T> 455T* declared_cast(Named* named_ptr) throw (wrong_type_cast) 456{ 457 if (Declared* declared_ptr = dynamic_cast<Declared*>(named_ptr)) 458 if (ASG::Declaration* decl = declared_ptr->declaration()) 459 if (T* derived_ptr = dynamic_cast<T*>(decl)) 460 return derived_ptr; 461 throw wrong_type_cast(); 462} 463 464//. Safely extracts typed Declarations from Type types. The type is first 465//. safely cast to Types::Declared, then the declaration() safely cast to 466//. the template type. 467template <typename T> 468T* declared_cast(Type* type_ptr) throw (wrong_type_cast) 469{ 470 if (Declared* declared_ptr = dynamic_cast<Declared*>(type_ptr)) 471 if (ASG::Declaration* decl = declared_ptr->declaration()) 472 if (T* derived_ptr = dynamic_cast<T*>(decl)) 473 return derived_ptr; 474 throw wrong_type_cast(); 475} 476 477 478} // namespace Type 479 480#endif 481// vim: set ts=8 sts=4 sw=4 et: