| 1 | // | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo | 
|---|
| 3 | // | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
| 5 | // file "LICENCE" distributed with Cforall. | 
|---|
| 6 | // | 
|---|
| 7 | // Declaration.h -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Fri Mar 17 16:05:08 2017 | 
|---|
| 13 | // Update Count     : 121 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #ifndef DECLARATION_H | 
|---|
| 17 | #define DECLARATION_H | 
|---|
| 18 |  | 
|---|
| 19 | #include <string> | 
|---|
| 20 |  | 
|---|
| 21 | #include "BaseSyntaxNode.h" | 
|---|
| 22 | #include "Mutator.h" | 
|---|
| 23 | #include "Visitor.h" | 
|---|
| 24 | #include "SynTree.h" | 
|---|
| 25 | #include "Parser/LinkageSpec.h" | 
|---|
| 26 | #include "Parser/ParseNode.h" | 
|---|
| 27 |  | 
|---|
| 28 | class Declaration : public BaseSyntaxNode { | 
|---|
| 29 | public: | 
|---|
| 30 | Declaration( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage ); | 
|---|
| 31 | Declaration( const Declaration &other ); | 
|---|
| 32 | virtual ~Declaration(); | 
|---|
| 33 |  | 
|---|
| 34 | const std::string &get_name() const { return name; } | 
|---|
| 35 | void set_name( std::string newValue ) { name = newValue; } | 
|---|
| 36 |  | 
|---|
| 37 | Type::StorageClasses get_storageClasses() const { return storageClasses; } | 
|---|
| 38 |  | 
|---|
| 39 | LinkageSpec::Spec get_linkage() const { return linkage; } | 
|---|
| 40 | void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; } | 
|---|
| 41 |  | 
|---|
| 42 | UniqueId get_uniqueId() const { return uniqueId; } | 
|---|
| 43 |  | 
|---|
| 44 | bool get_extension() const { return extension; } | 
|---|
| 45 | Declaration *set_extension( bool exten ) { extension = exten; return this; } | 
|---|
| 46 |  | 
|---|
| 47 | void fixUniqueId( void ); | 
|---|
| 48 | virtual Declaration *clone() const = 0; | 
|---|
| 49 | virtual void accept( Visitor &v ) = 0; | 
|---|
| 50 | virtual Declaration *acceptMutator( Mutator &m ) = 0; | 
|---|
| 51 | virtual void print( std::ostream &os, int indent = 0 ) const = 0; | 
|---|
| 52 | virtual void printShort( std::ostream &os, int indent = 0 ) const = 0; | 
|---|
| 53 |  | 
|---|
| 54 | static void dumpIds( std::ostream &os ); | 
|---|
| 55 | static Declaration *declFromId( UniqueId id ); | 
|---|
| 56 | private: | 
|---|
| 57 | std::string name; | 
|---|
| 58 | Type::StorageClasses storageClasses; | 
|---|
| 59 | LinkageSpec::Spec linkage; | 
|---|
| 60 | UniqueId uniqueId; | 
|---|
| 61 | bool extension = false; | 
|---|
| 62 | }; | 
|---|
| 63 |  | 
|---|
| 64 | class DeclarationWithType : public Declaration { | 
|---|
| 65 | public: | 
|---|
| 66 | DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs ); | 
|---|
| 67 | DeclarationWithType( const DeclarationWithType &other ); | 
|---|
| 68 | virtual ~DeclarationWithType(); | 
|---|
| 69 |  | 
|---|
| 70 | std::string get_mangleName() const { return mangleName; } | 
|---|
| 71 | DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; } | 
|---|
| 72 |  | 
|---|
| 73 | std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); } | 
|---|
| 74 |  | 
|---|
| 75 | int get_scopeLevel() const { return scopeLevel; } | 
|---|
| 76 | DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; } | 
|---|
| 77 |  | 
|---|
| 78 | ConstantExpr *get_asmName() const { return asmName; } | 
|---|
| 79 | DeclarationWithType * set_asmName( ConstantExpr *newValue ) { asmName = newValue; return this; } | 
|---|
| 80 |  | 
|---|
| 81 | std::list< Attribute * >& get_attributes() { return attributes; } | 
|---|
| 82 | const std::list< Attribute * >& get_attributes() const { return attributes; } | 
|---|
| 83 |  | 
|---|
| 84 | Type::FuncSpecifiers get_funcSpec() const { return fs; } | 
|---|
| 85 | //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; } | 
|---|
| 86 |  | 
|---|
| 87 | virtual DeclarationWithType *clone() const = 0; | 
|---|
| 88 | virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0; | 
|---|
| 89 |  | 
|---|
| 90 | virtual Type *get_type() const = 0; | 
|---|
| 91 | virtual void set_type(Type *) = 0; | 
|---|
| 92 | private: | 
|---|
| 93 | // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2 | 
|---|
| 94 | std::string mangleName; | 
|---|
| 95 | // need to remember the scope level at which the variable was declared, so that shadowed identifiers can be accessed | 
|---|
| 96 | int scopeLevel = 0; | 
|---|
| 97 |  | 
|---|
| 98 | ConstantExpr *asmName; | 
|---|
| 99 | std::list< Attribute * > attributes; | 
|---|
| 100 | Type::FuncSpecifiers fs; | 
|---|
| 101 | }; | 
|---|
| 102 |  | 
|---|
| 103 | class ObjectDecl : public DeclarationWithType { | 
|---|
| 104 | typedef DeclarationWithType Parent; | 
|---|
| 105 | public: | 
|---|
| 106 | ObjectDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression *bitfieldWidth, Type *type, Initializer *init, | 
|---|
| 107 | const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() ); | 
|---|
| 108 | ObjectDecl( const ObjectDecl &other ); | 
|---|
| 109 | virtual ~ObjectDecl(); | 
|---|
| 110 |  | 
|---|
| 111 | virtual Type *get_type() const { return type; } | 
|---|
| 112 | virtual void set_type(Type *newType) { type = newType; } | 
|---|
| 113 |  | 
|---|
| 114 | Initializer *get_init() const { return init; } | 
|---|
| 115 | void set_init( Initializer *newValue ) { init = newValue; } | 
|---|
| 116 |  | 
|---|
| 117 | Expression *get_bitfieldWidth() const { return bitfieldWidth; } | 
|---|
| 118 | void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; } | 
|---|
| 119 |  | 
|---|
| 120 | virtual ObjectDecl *clone() const { return new ObjectDecl( *this ); } | 
|---|
| 121 | virtual void accept( Visitor &v ) { v.visit( this ); } | 
|---|
| 122 | virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); } | 
|---|
| 123 | virtual void print( std::ostream &os, int indent = 0 ) const; | 
|---|
| 124 | virtual void printShort( std::ostream &os, int indent = 0 ) const; | 
|---|
| 125 | private: | 
|---|
| 126 | Type *type; | 
|---|
| 127 | Initializer *init; | 
|---|
| 128 | Expression *bitfieldWidth; | 
|---|
| 129 | }; | 
|---|
| 130 |  | 
|---|
| 131 | class FunctionDecl : public DeclarationWithType { | 
|---|
| 132 | typedef DeclarationWithType Parent; | 
|---|
| 133 | public: | 
|---|
| 134 | FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements, | 
|---|
| 135 | const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() ); | 
|---|
| 136 | FunctionDecl( const FunctionDecl &other ); | 
|---|
| 137 | virtual ~FunctionDecl(); | 
|---|
| 138 |  | 
|---|
| 139 | Type *get_type() const; | 
|---|
| 140 | virtual void set_type(Type *); | 
|---|
| 141 |  | 
|---|
| 142 | FunctionType *get_functionType() const { return type; } | 
|---|
| 143 | void set_functionType( FunctionType *newValue ) { type = newValue; } | 
|---|
| 144 | CompoundStmt *get_statements() const { return statements; } | 
|---|
| 145 | void set_statements( CompoundStmt *newValue ) { statements = newValue; } | 
|---|
| 146 |  | 
|---|
| 147 | virtual FunctionDecl *clone() const { return new FunctionDecl( *this ); } | 
|---|
| 148 | virtual void accept( Visitor &v ) { v.visit( this ); } | 
|---|
| 149 | virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); } | 
|---|
| 150 | virtual void print( std::ostream &os, int indent = 0 ) const; | 
|---|
| 151 | virtual void printShort( std::ostream &os, int indent = 0 ) const; | 
|---|
| 152 | private: | 
|---|
| 153 | FunctionType *type; | 
|---|
| 154 | CompoundStmt *statements; | 
|---|
| 155 | }; | 
|---|
| 156 |  | 
|---|
| 157 | class NamedTypeDecl : public Declaration { | 
|---|
| 158 | typedef Declaration Parent; | 
|---|
| 159 | public: | 
|---|
| 160 | NamedTypeDecl( const std::string &name, Type::StorageClasses scs, Type *type ); | 
|---|
| 161 | NamedTypeDecl( const NamedTypeDecl &other ); | 
|---|
| 162 | virtual ~NamedTypeDecl(); | 
|---|
| 163 |  | 
|---|
| 164 | Type *get_base() const { return base; } | 
|---|
| 165 | void set_base( Type *newValue ) { base = newValue; } | 
|---|
| 166 | std::list< TypeDecl* >& get_parameters() { return parameters; } | 
|---|
| 167 | std::list< DeclarationWithType* >& get_assertions() { return assertions; } | 
|---|
| 168 |  | 
|---|
| 169 | virtual NamedTypeDecl *clone() const = 0; | 
|---|
| 170 | virtual void print( std::ostream &os, int indent = 0 ) const; | 
|---|
| 171 | virtual void printShort( std::ostream &os, int indent = 0 ) const; | 
|---|
| 172 | protected: | 
|---|
| 173 | virtual std::string typeString() const = 0; | 
|---|
| 174 | private: | 
|---|
| 175 | Type *base; | 
|---|
| 176 | std::list< TypeDecl* > parameters; | 
|---|
| 177 | std::list< DeclarationWithType* > assertions; | 
|---|
| 178 | }; | 
|---|
| 179 |  | 
|---|
| 180 | class TypeDecl : public NamedTypeDecl { | 
|---|
| 181 | typedef NamedTypeDecl Parent; | 
|---|
| 182 | public: | 
|---|
| 183 | enum Kind { Any, Dtype, Ftype, Ttype }; | 
|---|
| 184 | /// Data extracted from a type decl | 
|---|
| 185 | struct Data { | 
|---|
| 186 | TypeDecl::Kind kind; | 
|---|
| 187 | bool isComplete; | 
|---|
| 188 | Data() : kind( (TypeDecl::Kind)-1 ), isComplete( false ) {} | 
|---|
| 189 | Data( TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {} | 
|---|
| 190 | Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {} | 
|---|
| 191 | bool operator==(const Data & other) const { return kind == other.kind && isComplete == other.isComplete; } | 
|---|
| 192 | bool operator!=(const Data & other) const { return !(*this == other);} | 
|---|
| 193 | }; | 
|---|
| 194 |  | 
|---|
| 195 | TypeDecl( const std::string &name, Type::StorageClasses scs, Type *type, Kind kind ); | 
|---|
| 196 | TypeDecl( const TypeDecl &other ); | 
|---|
| 197 |  | 
|---|
| 198 | Kind get_kind() const { return kind; } | 
|---|
| 199 |  | 
|---|
| 200 | bool isComplete() const { return kind == Any || sized; } | 
|---|
| 201 | bool get_sized() const { return sized; } | 
|---|
| 202 | TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; } | 
|---|
| 203 |  | 
|---|
| 204 | virtual TypeDecl *clone() const { return new TypeDecl( *this ); } | 
|---|
| 205 | virtual void accept( Visitor &v ) { v.visit( this ); } | 
|---|
| 206 | virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); } | 
|---|
| 207 | private: | 
|---|
| 208 | virtual std::string typeString() const; | 
|---|
| 209 | Kind kind; | 
|---|
| 210 | bool sized; | 
|---|
| 211 | }; | 
|---|
| 212 |  | 
|---|
| 213 | class TypedefDecl : public NamedTypeDecl { | 
|---|
| 214 | typedef NamedTypeDecl Parent; | 
|---|
| 215 | public: | 
|---|
| 216 | TypedefDecl( const std::string &name, Type::StorageClasses scs, Type *type ) : Parent( name, scs, type ) {} | 
|---|
| 217 | TypedefDecl( const TypedefDecl &other ) : Parent( other ) {} | 
|---|
| 218 |  | 
|---|
| 219 | virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); } | 
|---|
| 220 | virtual void accept( Visitor &v ) { v.visit( this ); } | 
|---|
| 221 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } | 
|---|
| 222 | private: | 
|---|
| 223 | virtual std::string typeString() const; | 
|---|
| 224 | }; | 
|---|
| 225 |  | 
|---|
| 226 | class AggregateDecl : public Declaration { | 
|---|
| 227 | typedef Declaration Parent; | 
|---|
| 228 | public: | 
|---|
| 229 | AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >() ); | 
|---|
| 230 | AggregateDecl( const AggregateDecl &other ); | 
|---|
| 231 | virtual ~AggregateDecl(); | 
|---|
| 232 |  | 
|---|
| 233 | std::list<Declaration*>& get_members() { return members; } | 
|---|
| 234 | std::list<TypeDecl*>& get_parameters() { return parameters; } | 
|---|
| 235 |  | 
|---|
| 236 | std::list< Attribute * >& get_attributes() { return attributes; } | 
|---|
| 237 | const std::list< Attribute * >& get_attributes() const { return attributes; } | 
|---|
| 238 |  | 
|---|
| 239 | bool has_body() const { return body; } | 
|---|
| 240 | AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; } | 
|---|
| 241 |  | 
|---|
| 242 | virtual void print( std::ostream &os, int indent = 0 ) const; | 
|---|
| 243 | virtual void printShort( std::ostream &os, int indent = 0 ) const; | 
|---|
| 244 | protected: | 
|---|
| 245 | virtual std::string typeString() const = 0; | 
|---|
| 246 |  | 
|---|
| 247 | private: | 
|---|
| 248 | std::list<Declaration*> members; | 
|---|
| 249 | std::list<TypeDecl*> parameters; | 
|---|
| 250 | bool body; | 
|---|
| 251 | std::list< Attribute * > attributes; | 
|---|
| 252 | }; | 
|---|
| 253 |  | 
|---|
| 254 | class StructDecl : public AggregateDecl { | 
|---|
| 255 | typedef AggregateDecl Parent; | 
|---|
| 256 | public: | 
|---|
| 257 | StructDecl( const std::string &name, DeclarationNode::Aggregate kind = DeclarationNode::Struct, const std::list< Attribute * > & attributes = std::list< class Attribute * >() ) : Parent( name, attributes ), kind( kind ) {} | 
|---|
| 258 | StructDecl( const StructDecl &other ) : Parent( other ) {} | 
|---|
| 259 |  | 
|---|
| 260 | bool is_coroutine() { return kind == DeclarationNode::Coroutine; } | 
|---|
| 261 | bool is_monitor() { return kind == DeclarationNode::Monitor; } | 
|---|
| 262 | bool is_thread() { return kind == DeclarationNode::Thread; } | 
|---|
| 263 |  | 
|---|
| 264 | virtual StructDecl *clone() const { return new StructDecl( *this ); } | 
|---|
| 265 | virtual void accept( Visitor &v ) { v.visit( this ); } | 
|---|
| 266 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } | 
|---|
| 267 | private: | 
|---|
| 268 | DeclarationNode::Aggregate kind; | 
|---|
| 269 | virtual std::string typeString() const; | 
|---|
| 270 | }; | 
|---|
| 271 |  | 
|---|
| 272 | class UnionDecl : public AggregateDecl { | 
|---|
| 273 | typedef AggregateDecl Parent; | 
|---|
| 274 | public: | 
|---|
| 275 | UnionDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >() ) : Parent( name, attributes ) {} | 
|---|
| 276 | UnionDecl( const UnionDecl &other ) : Parent( other ) {} | 
|---|
| 277 |  | 
|---|
| 278 | virtual UnionDecl *clone() const { return new UnionDecl( *this ); } | 
|---|
| 279 | virtual void accept( Visitor &v ) { v.visit( this ); } | 
|---|
| 280 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } | 
|---|
| 281 | private: | 
|---|
| 282 | virtual std::string typeString() const; | 
|---|
| 283 | }; | 
|---|
| 284 |  | 
|---|
| 285 | class EnumDecl : public AggregateDecl { | 
|---|
| 286 | typedef AggregateDecl Parent; | 
|---|
| 287 | public: | 
|---|
| 288 | EnumDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >() ) : Parent( name, attributes ) {} | 
|---|
| 289 | EnumDecl( const EnumDecl &other ) : Parent( other ) {} | 
|---|
| 290 |  | 
|---|
| 291 | virtual EnumDecl *clone() const { return new EnumDecl( *this ); } | 
|---|
| 292 | virtual void accept( Visitor &v ) { v.visit( this ); } | 
|---|
| 293 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } | 
|---|
| 294 | private: | 
|---|
| 295 | virtual std::string typeString() const; | 
|---|
| 296 | }; | 
|---|
| 297 |  | 
|---|
| 298 | class TraitDecl : public AggregateDecl { | 
|---|
| 299 | typedef AggregateDecl Parent; | 
|---|
| 300 | public: | 
|---|
| 301 | TraitDecl( const std::string &name, const std::list< Attribute * > & attributes ) : Parent( name ) { | 
|---|
| 302 | assertf( attributes.empty(), "attribute unsupported for traits" ); | 
|---|
| 303 | } | 
|---|
| 304 | TraitDecl( const TraitDecl &other ) : Parent( other ) {} | 
|---|
| 305 |  | 
|---|
| 306 | virtual TraitDecl *clone() const { return new TraitDecl( *this ); } | 
|---|
| 307 | virtual void accept( Visitor &v ) { v.visit( this ); } | 
|---|
| 308 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } | 
|---|
| 309 | private: | 
|---|
| 310 | virtual std::string typeString() const; | 
|---|
| 311 | }; | 
|---|
| 312 |  | 
|---|
| 313 | class AsmDecl : public Declaration { | 
|---|
| 314 | public: | 
|---|
| 315 | AsmDecl( AsmStmt *stmt ); | 
|---|
| 316 | AsmDecl( const AsmDecl &other ); | 
|---|
| 317 | virtual ~AsmDecl(); | 
|---|
| 318 |  | 
|---|
| 319 | AsmStmt *get_stmt() { return stmt; } | 
|---|
| 320 | void set_stmt( AsmStmt *newValue ) { stmt = newValue; } | 
|---|
| 321 |  | 
|---|
| 322 | virtual AsmDecl *clone() const { return new AsmDecl( *this ); } | 
|---|
| 323 | virtual void accept( Visitor &v ) { v.visit( this ); } | 
|---|
| 324 | virtual AsmDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); } | 
|---|
| 325 | virtual void print( std::ostream &os, int indent = 0 ) const; | 
|---|
| 326 | virtual void printShort( std::ostream &os, int indent = 0 ) const; | 
|---|
| 327 | private: | 
|---|
| 328 | AsmStmt *stmt; | 
|---|
| 329 | }; | 
|---|
| 330 |  | 
|---|
| 331 | std::ostream & operator<<( std::ostream & out, const Declaration * decl ); | 
|---|
| 332 | std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data ); | 
|---|
| 333 |  | 
|---|
| 334 | #endif // DECLARATION_H | 
|---|
| 335 |  | 
|---|
| 336 | // Local Variables: // | 
|---|
| 337 | // tab-width: 4 // | 
|---|
| 338 | // mode: c++ // | 
|---|
| 339 | // compile-command: "make install" // | 
|---|
| 340 | // End: // | 
|---|