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