source: src/SynTree/Declaration.h@ 641c3d0

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 641c3d0 was 6b0b624, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

change #ifndef to #pragma once

  • Property mode set to 100644
File size: 13.8 KB
RevLine 
[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
[6b0b624]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Jul 22 09:52:59 2017
13// Update Count : 124
[0dd3a2f]14//
15
[6b0b624]16#pragma once
[51b73452]17
[294647b]18#include <string>
19
20#include "BaseSyntaxNode.h"
[51b73452]21#include "Mutator.h"
[294647b]22#include "Visitor.h"
23#include "SynTree.h"
[51b73452]24#include "Parser/LinkageSpec.h"
[68cd1ce]25#include "Parser/ParseNode.h"
[51b73452]26
[294647b]27class Declaration : public BaseSyntaxNode {
[17cd4eb]28 public:
[68fe077a]29 Declaration( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage );
[0dd3a2f]30 Declaration( const Declaration &other );
31 virtual ~Declaration();
32
[5f2f2d7]33 const std::string &get_name() const { return name; }
[0dd3a2f]34 void set_name( std::string newValue ) { name = newValue; }
[a7c90d4]35
[68fe077a]36 Type::StorageClasses get_storageClasses() const { return storageClasses; }
[a7c90d4]37
[8b7ee09]38 LinkageSpec::Spec get_linkage() const { return linkage; }
39 void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; }
[a7c90d4]40
[0dd3a2f]41 UniqueId get_uniqueId() const { return uniqueId; }
[a7c90d4]42
[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;
[68fe077a]57 Type::StorageClasses storageClasses;
[8b7ee09]58 LinkageSpec::Spec linkage;
[0dd3a2f]59 UniqueId uniqueId;
[8e9cbb2]60 bool extension = false;
[51b73452]61};
62
[17cd4eb]63class DeclarationWithType : public Declaration {
64 public:
[ddfd945]65 DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs );
[0dd3a2f]66 DeclarationWithType( const DeclarationWithType &other );
67 virtual ~DeclarationWithType();
[51b73452]68
[0dd3a2f]69 std::string get_mangleName() const { return mangleName; }
[58dd019]70 DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
[17cd4eb]71
[f326f99]72 std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
73
74 int get_scopeLevel() const { return scopeLevel; }
[58dd019]75 DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; }
76
77 ConstantExpr *get_asmName() const { return asmName; }
78 DeclarationWithType * set_asmName( ConstantExpr *newValue ) { asmName = newValue; return this; }
[f326f99]79
[f9cebb5]80 std::list< Attribute * >& get_attributes() { return attributes; }
81 const std::list< Attribute * >& get_attributes() const { return attributes; }
82
[ddfd945]83 Type::FuncSpecifiers get_funcSpec() const { return fs; }
84 //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; }
[dd020c0]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;
[ddfd945]99 Type::FuncSpecifiers fs;
[51b73452]100};
101
[17cd4eb]102class ObjectDecl : public DeclarationWithType {
[0dd3a2f]103 typedef DeclarationWithType Parent;
[17cd4eb]104 public:
[68fe077a]105 ObjectDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression *bitfieldWidth, Type *type, Initializer *init,
[ddfd945]106 const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
[0dd3a2f]107 ObjectDecl( const ObjectDecl &other );
108 virtual ~ObjectDecl();
109
110 virtual Type *get_type() const { return type; }
111 virtual void set_type(Type *newType) { type = newType; }
112
113 Initializer *get_init() const { return init; }
114 void set_init( Initializer *newValue ) { init = newValue; }
[58dd019]115
[0dd3a2f]116 Expression *get_bitfieldWidth() const { return bitfieldWidth; }
117 void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; }
118
119 virtual ObjectDecl *clone() const { return new ObjectDecl( *this ); }
120 virtual void accept( Visitor &v ) { v.visit( this ); }
[1db21619]121 virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[0dd3a2f]122 virtual void print( std::ostream &os, int indent = 0 ) const;
123 virtual void printShort( std::ostream &os, int indent = 0 ) const;
[17cd4eb]124 private:
[0dd3a2f]125 Type *type;
126 Initializer *init;
127 Expression *bitfieldWidth;
[51b73452]128};
129
[17cd4eb]130class FunctionDecl : public DeclarationWithType {
[0dd3a2f]131 typedef DeclarationWithType Parent;
[17cd4eb]132 public:
[68fe077a]133 FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements,
[ddfd945]134 const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
[0dd3a2f]135 FunctionDecl( const FunctionDecl &other );
136 virtual ~FunctionDecl();
[51b73452]137
[0dd3a2f]138 Type *get_type() const;
139 virtual void set_type(Type *);
[51b73452]140
[0dd3a2f]141 FunctionType *get_functionType() const { return type; }
142 void set_functionType( FunctionType *newValue ) { type = newValue; }
143 CompoundStmt *get_statements() const { return statements; }
144 void set_statements( CompoundStmt *newValue ) { statements = newValue; }
145
146 virtual FunctionDecl *clone() const { return new FunctionDecl( *this ); }
147 virtual void accept( Visitor &v ) { v.visit( this ); }
148 virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); }
149 virtual void print( std::ostream &os, int indent = 0 ) const;
150 virtual void printShort( std::ostream &os, int indent = 0 ) const;
[17cd4eb]151 private:
[0dd3a2f]152 FunctionType *type;
153 CompoundStmt *statements;
[51b73452]154};
155
[17cd4eb]156class NamedTypeDecl : public Declaration {
[0dd3a2f]157 typedef Declaration Parent;
[17cd4eb]158 public:
[68fe077a]159 NamedTypeDecl( const std::string &name, Type::StorageClasses scs, Type *type );
[46f6134]160 NamedTypeDecl( const NamedTypeDecl &other );
[0dd3a2f]161 virtual ~NamedTypeDecl();
162
163 Type *get_base() const { return base; }
164 void set_base( Type *newValue ) { base = newValue; }
165 std::list< TypeDecl* >& get_parameters() { return parameters; }
166 std::list< DeclarationWithType* >& get_assertions() { return assertions; }
167
[e39241b]168 virtual std::string typeString() const = 0;
169
[0dd3a2f]170 virtual NamedTypeDecl *clone() const = 0;
171 virtual void print( std::ostream &os, int indent = 0 ) const;
172 virtual void printShort( std::ostream &os, int indent = 0 ) const;
[17cd4eb]173 protected:
174 private:
[0dd3a2f]175 Type *base;
176 std::list< TypeDecl* > parameters;
177 std::list< DeclarationWithType* > assertions;
[51b73452]178};
179
[17cd4eb]180class TypeDecl : public NamedTypeDecl {
[0dd3a2f]181 typedef NamedTypeDecl Parent;
[17cd4eb]182 public:
[8f60f0b]183 enum Kind { Any, Dtype, Ftype, Ttype };
[2c57025]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 };
[51b73452]194
[67cf18c]195 TypeDecl( const std::string &name, Type::StorageClasses scs, Type *type, Kind kind, Type * init = nullptr );
[0dd3a2f]196 TypeDecl( const TypeDecl &other );
[67cf18c]197 virtual ~TypeDecl();
[17cd4eb]198
[0dd3a2f]199 Kind get_kind() const { return kind; }
[51b73452]200
[67cf18c]201 Type * get_init() const { return init; }
202 TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
203
[2c57025]204 bool isComplete() const { return kind == Any || sized; }
205 bool get_sized() const { return sized; }
206 TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
207
[e39241b]208 virtual std::string typeString() const;
[bdd0755]209 virtual std::string genTypeString() const;
[e39241b]210
[0dd3a2f]211 virtual TypeDecl *clone() const { return new TypeDecl( *this ); }
212 virtual void accept( Visitor &v ) { v.visit( this ); }
213 virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[67cf18c]214 virtual void print( std::ostream &os, int indent = 0 ) const;
215
[17cd4eb]216 private:
[0dd3a2f]217 Kind kind;
[67cf18c]218 Type * init;
[2c57025]219 bool sized;
[51b73452]220};
221
[17cd4eb]222class TypedefDecl : public NamedTypeDecl {
[0dd3a2f]223 typedef NamedTypeDecl Parent;
[17cd4eb]224 public:
[68fe077a]225 TypedefDecl( const std::string &name, Type::StorageClasses scs, Type *type ) : Parent( name, scs, type ) {}
[0dd3a2f]226 TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
[17cd4eb]227
[e39241b]228 virtual std::string typeString() const;
229
[0dd3a2f]230 virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); }
231 virtual void accept( Visitor &v ) { v.visit( this ); }
232 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[17cd4eb]233 private:
[51b73452]234};
235
[17cd4eb]236class AggregateDecl : public Declaration {
[0dd3a2f]237 typedef Declaration Parent;
[17cd4eb]238 public:
[fa4805f]239 AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
[0dd3a2f]240 AggregateDecl( const AggregateDecl &other );
241 virtual ~AggregateDecl();
[51b73452]242
[0dd3a2f]243 std::list<Declaration*>& get_members() { return members; }
244 std::list<TypeDecl*>& get_parameters() { return parameters; }
[17cd4eb]245
[c0aa336]246 std::list< Attribute * >& get_attributes() { return attributes; }
247 const std::list< Attribute * >& get_attributes() const { return attributes; }
248
[5d125e4]249 bool has_body() const { return body; }
250 AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
251
[0dd3a2f]252 virtual void print( std::ostream &os, int indent = 0 ) const;
253 virtual void printShort( std::ostream &os, int indent = 0 ) const;
[17cd4eb]254 protected:
[0dd3a2f]255 virtual std::string typeString() const = 0;
[17cd4eb]256
257 private:
[0dd3a2f]258 std::list<Declaration*> members;
259 std::list<TypeDecl*> parameters;
[5d125e4]260 bool body;
[c0aa336]261 std::list< Attribute * > attributes;
[51b73452]262};
263
[17cd4eb]264class StructDecl : public AggregateDecl {
[0dd3a2f]265 typedef AggregateDecl Parent;
[17cd4eb]266 public:
[6ea87486]267 StructDecl( const std::string &name, DeclarationNode::Aggregate kind = DeclarationNode::Struct, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ), kind( kind ), tagged( false ), parent_name( "" ) {}
268 StructDecl( const std::string &name, const std::string *parent, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ), kind( DeclarationNode::Struct ), tagged( true ), parent_name( parent ? *parent : "" ) {}
[0dd3a2f]269 StructDecl( const StructDecl &other ) : Parent( other ) {}
[17cd4eb]270
[409433da]271 bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
272 bool is_monitor() { return kind == DeclarationNode::Monitor; }
273 bool is_thread() { return kind == DeclarationNode::Thread; }
274
[6ea87486]275 // Tagged/Tree Structure Excetion
276 bool get_tagged() { return tagged; }
277 void set_tagged( bool newValue ) { tagged = newValue; }
278 bool has_parent() { return parent_name != ""; }
279 std::string get_parentName() { return parent_name; }
280
[0dd3a2f]281 virtual StructDecl *clone() const { return new StructDecl( *this ); }
282 virtual void accept( Visitor &v ) { v.visit( this ); }
283 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[17cd4eb]284 private:
[409433da]285 DeclarationNode::Aggregate kind;
[0dd3a2f]286 virtual std::string typeString() const;
[6ea87486]287
288 bool tagged;
289 std::string parent_name;
[51b73452]290};
291
[17cd4eb]292class UnionDecl : public AggregateDecl {
[0dd3a2f]293 typedef AggregateDecl Parent;
[17cd4eb]294 public:
[fa4805f]295 UnionDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
[0dd3a2f]296 UnionDecl( const UnionDecl &other ) : Parent( other ) {}
[17cd4eb]297
[0dd3a2f]298 virtual UnionDecl *clone() const { return new UnionDecl( *this ); }
299 virtual void accept( Visitor &v ) { v.visit( this ); }
300 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[17cd4eb]301 private:
[0dd3a2f]302 virtual std::string typeString() const;
[51b73452]303};
304
[17cd4eb]305class EnumDecl : public AggregateDecl {
[0dd3a2f]306 typedef AggregateDecl Parent;
[17cd4eb]307 public:
[fa4805f]308 EnumDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
[0dd3a2f]309 EnumDecl( const EnumDecl &other ) : Parent( other ) {}
[17cd4eb]310
[0dd3a2f]311 virtual EnumDecl *clone() const { return new EnumDecl( *this ); }
312 virtual void accept( Visitor &v ) { v.visit( this ); }
313 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[17cd4eb]314 private:
[0dd3a2f]315 virtual std::string typeString() const;
[51b73452]316};
317
[4040425]318class TraitDecl : public AggregateDecl {
[0dd3a2f]319 typedef AggregateDecl Parent;
[17cd4eb]320 public:
[c0aa336]321 TraitDecl( const std::string &name, const std::list< Attribute * > & attributes ) : Parent( name ) {
322 assertf( attributes.empty(), "attribute unsupported for traits" );
323 }
[4040425]324 TraitDecl( const TraitDecl &other ) : Parent( other ) {}
[17cd4eb]325
[4040425]326 virtual TraitDecl *clone() const { return new TraitDecl( *this ); }
[0dd3a2f]327 virtual void accept( Visitor &v ) { v.visit( this ); }
328 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[17cd4eb]329 private:
[0dd3a2f]330 virtual std::string typeString() const;
[51b73452]331};
332
[e994912]333class AsmDecl : public Declaration {
334 public:
335 AsmDecl( AsmStmt *stmt );
336 AsmDecl( const AsmDecl &other );
337 virtual ~AsmDecl();
338
339 AsmStmt *get_stmt() { return stmt; }
340 void set_stmt( AsmStmt *newValue ) { stmt = newValue; }
341
342 virtual AsmDecl *clone() const { return new AsmDecl( *this ); }
343 virtual void accept( Visitor &v ) { v.visit( this ); }
344 virtual AsmDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
345 virtual void print( std::ostream &os, int indent = 0 ) const;
346 virtual void printShort( std::ostream &os, int indent = 0 ) const;
347 private:
348 AsmStmt *stmt;
349};
350
[3906301]351std::ostream & operator<<( std::ostream & out, const Declaration * decl );
[2c57025]352std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
[baf7fee]353
[0dd3a2f]354// Local Variables: //
355// tab-width: 4 //
356// mode: c++ //
357// compile-command: "make install" //
358// End: //
Note: See TracBrowser for help on using the repository browser.