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