source: src/SynTree/Declaration.h@ 8a13c47

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 8a13c47 was 07de76b, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

remove file TypeVar.h* and put TypeVar::Kind into TypeDecl, move LinkageSpec.* from directory Parse to SynTree

  • Property mode set to 100644
File size: 17.3 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
[312029a]11// Last Modified By : Peter A. Buhr
[07de76b]12// Last Modified On : Fri Dec 13 23:11:22 2019
13// Update Count : 157
[0dd3a2f]14//
15
[6b0b624]16#pragma once
[51b73452]17
[ea6332d]18#include <cassert> // for assertf
19#include <iosfwd> // for ostream
20#include <list> // for list
[1690778]21#include <unordered_map> // for unordered_map
[ea6332d]22#include <string> // for string, operator+, allocator, to_string
23
24#include "BaseSyntaxNode.h" // for BaseSyntaxNode
25#include "Mutator.h" // for Mutator
[07de76b]26#include "LinkageSpec.h" // for Spec, Cforall
[ea6332d]27#include "SynTree.h" // for UniqueId
28#include "SynTree/Type.h" // for Type, Type::StorageClasses, Type::Fu...
29#include "Visitor.h" // for Visitor
30
31class AsmStmt;
32class Attribute;
33class CompoundStmt;
34class ConstantExpr;
35class Expression;
36class Initializer;
37class TypeDecl;
[51b73452]38
[294647b]39class Declaration : public BaseSyntaxNode {
[17cd4eb]40 public:
[65cdc1e]41 std::string name;
42 LinkageSpec::Spec linkage;
43 bool extension = false;
44
[07de76b]45 Declaration( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage );
46 Declaration( const Declaration & other );
[0dd3a2f]47 virtual ~Declaration();
48
[07de76b]49 const std::string & get_name() const { return name; }
[0dd3a2f]50 void set_name( std::string newValue ) { name = newValue; }
[a7c90d4]51
[68fe077a]52 Type::StorageClasses get_storageClasses() const { return storageClasses; }
[a7c90d4]53
[8b7ee09]54 LinkageSpec::Spec get_linkage() const { return linkage; }
55 void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; }
[a7c90d4]56
[0dd3a2f]57 UniqueId get_uniqueId() const { return uniqueId; }
[a7c90d4]58
[8e9cbb2]59 bool get_extension() const { return extension; }
[07de76b]60 Declaration * set_extension( bool exten ) { extension = exten; return this; }
[0dd3a2f]61
62 void fixUniqueId( void );
[07de76b]63 virtual Declaration * clone() const override = 0;
[7870799]64 virtual void accept( Visitor & v ) override = 0;
65 virtual void accept( Visitor & v ) const override = 0;
[07de76b]66 virtual Declaration * acceptMutator( Mutator & m ) override = 0;
67 virtual void print( std::ostream & os, Indenter indent = {} ) const override = 0;
68 virtual void printShort( std::ostream & os, Indenter indent = {} ) const = 0;
[0dd3a2f]69
70 UniqueId uniqueId;
[1f93c2c]71 Type::StorageClasses storageClasses;
72 private:
[51b73452]73};
74
[17cd4eb]75class DeclarationWithType : public Declaration {
76 public:
[65cdc1e]77 // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2
78 std::string mangleName;
79 // need to remember the scope level at which the variable was declared, so that shadowed identifiers can be accessed
80 int scopeLevel = 0;
81
[07de76b]82 Expression * asmName;
[65cdc1e]83 std::list< Attribute * > attributes;
[3ed994e]84 bool isDeleted = false;
[65cdc1e]85
[07de76b]86 DeclarationWithType( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs );
87 DeclarationWithType( const DeclarationWithType & other );
[0dd3a2f]88 virtual ~DeclarationWithType();
[51b73452]89
[0dd3a2f]90 std::string get_mangleName() const { return mangleName; }
[58dd019]91 DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
[17cd4eb]92
[f326f99]93 std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
94
95 int get_scopeLevel() const { return scopeLevel; }
[58dd019]96 DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; }
97
[07de76b]98 Expression * get_asmName() const { return asmName; }
99 DeclarationWithType * set_asmName( Expression * newValue ) { asmName = newValue; return this; }
[f326f99]100
[f9cebb5]101 std::list< Attribute * >& get_attributes() { return attributes; }
102 const std::list< Attribute * >& get_attributes() const { return attributes; }
103
[ddfd945]104 Type::FuncSpecifiers get_funcSpec() const { return fs; }
105 //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; }
[dd020c0]106
[07de76b]107 virtual DeclarationWithType * clone() const override = 0;
108 virtual DeclarationWithType * acceptMutator( Mutator & m ) override = 0;
[17cd4eb]109
[9aaac6e9]110 virtual Type * get_type() const = 0;
[0dd3a2f]111 virtual void set_type(Type *) = 0;
[f9cebb5]112
[65cdc1e]113 private:
[ddfd945]114 Type::FuncSpecifiers fs;
[51b73452]115};
116
[17cd4eb]117class ObjectDecl : public DeclarationWithType {
[0dd3a2f]118 typedef DeclarationWithType Parent;
[17cd4eb]119 public:
[07de76b]120 Type * type;
121 Initializer * init;
122 Expression * bitfieldWidth;
[65cdc1e]123
[07de76b]124 ObjectDecl( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression * bitfieldWidth, Type * type, Initializer * init,
[ddfd945]125 const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
[07de76b]126 ObjectDecl( const ObjectDecl & other );
[0dd3a2f]127 virtual ~ObjectDecl();
128
[e149f77]129 virtual Type * get_type() const override { return type; }
[07de76b]130 virtual void set_type(Type * newType) override { type = newType; }
[0dd3a2f]131
[07de76b]132 Initializer * get_init() const { return init; }
133 void set_init( Initializer * newValue ) { init = newValue; }
[58dd019]134
[07de76b]135 Expression * get_bitfieldWidth() const { return bitfieldWidth; }
136 void set_bitfieldWidth( Expression * newValue ) { bitfieldWidth = newValue; }
[0dd3a2f]137
[96f9ef5]138 static ObjectDecl * newObject( const std::string & name, Type * type, Initializer * init );
139
[07de76b]140 virtual ObjectDecl * clone() const override { return new ObjectDecl( *this ); }
[7870799]141 virtual void accept( Visitor & v ) override { v.visit( this ); }
142 virtual void accept( Visitor & v ) const override { v.visit( this ); }
[07de76b]143 virtual DeclarationWithType * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
144 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
145 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
[51b73452]146};
147
[17cd4eb]148class FunctionDecl : public DeclarationWithType {
[0dd3a2f]149 typedef DeclarationWithType Parent;
[17cd4eb]150 public:
[07de76b]151 FunctionType * type;
152 CompoundStmt * statements;
[574894d]153 std::list< Expression * > withExprs;
[65cdc1e]154
[07de76b]155 FunctionDecl( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType * type, CompoundStmt * statements,
[ddfd945]156 const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
[07de76b]157 FunctionDecl( const FunctionDecl & other );
[0dd3a2f]158 virtual ~FunctionDecl();
[51b73452]159
[e149f77]160 virtual Type * get_type() const override { return type; }
161 virtual void set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); }
[51b73452]162
[9aaac6e9]163 FunctionType * get_functionType() const { return type; }
[07de76b]164 void set_functionType( FunctionType * newValue ) { type = newValue; }
165 CompoundStmt * get_statements() const { return statements; }
166 void set_statements( CompoundStmt * newValue ) { statements = newValue; }
[76f7fc7]167 bool has_body() const { return NULL != statements; }
[0dd3a2f]168
[75626a1]169 static FunctionDecl * newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements );
170
[07de76b]171 virtual FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
[7870799]172 virtual void accept( Visitor & v ) override { v.visit( this ); }
173 virtual void accept( Visitor & v ) const override { v.visit( this ); }
[07de76b]174 virtual DeclarationWithType * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
175 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
176 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
[51b73452]177};
178
[17cd4eb]179class NamedTypeDecl : public Declaration {
[0dd3a2f]180 typedef Declaration Parent;
[17cd4eb]181 public:
[07de76b]182 Type * base;
183 std::list< TypeDecl * > parameters;
184 std::list< DeclarationWithType * > assertions;
[65cdc1e]185
[07de76b]186 NamedTypeDecl( const std::string & name, Type::StorageClasses scs, Type * type );
187 NamedTypeDecl( const NamedTypeDecl & other );
[0dd3a2f]188 virtual ~NamedTypeDecl();
189
[07de76b]190 Type * get_base() const { return base; }
191 void set_base( Type * newValue ) { base = newValue; }
192 std::list< TypeDecl* > & get_parameters() { return parameters; }
193 std::list< DeclarationWithType * >& get_assertions() { return assertions; }
[0dd3a2f]194
[312029a]195 virtual const char * typeString() const = 0;
[e39241b]196
[07de76b]197 virtual NamedTypeDecl * clone() const override = 0;
198 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
199 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
[51b73452]200};
201
[17cd4eb]202class TypeDecl : public NamedTypeDecl {
[0dd3a2f]203 typedef NamedTypeDecl Parent;
[17cd4eb]204 public:
[07de76b]205 enum Kind { Dtype, Otype, Ftype, Ttype, NUMBER_OF_KINDS };
[65cdc1e]206
[07de76b]207 Kind kind;
[65cdc1e]208 bool sized;
[07de76b]209 Type * init;
[65cdc1e]210
[2c57025]211 /// Data extracted from a type decl
212 struct Data {
[07de76b]213 Kind kind;
[2c57025]214 bool isComplete;
[1f93c2c]215
[07de76b]216 Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
217 Data( const TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {}
[2c57025]218 Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {}
[07de76b]219 Data( const Data & d1, const Data & d2 )
220 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
[7a63486]221
[07de76b]222 bool operator==( const Data & other ) const { return kind == other.kind && isComplete == other.isComplete; }
223 bool operator!=( const Data & other ) const { return !(*this == other);}
[2c57025]224 };
[51b73452]225
[07de76b]226 TypeDecl( const std::string & name, Type::StorageClasses scs, Type * type, Kind kind, bool sized, Type * init = nullptr );
227 TypeDecl( const TypeDecl & other );
[67cf18c]228 virtual ~TypeDecl();
[17cd4eb]229
[0dd3a2f]230 Kind get_kind() const { return kind; }
[51b73452]231
[67cf18c]232 Type * get_init() const { return init; }
233 TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
234
[f0ecf9b]235 bool isComplete() const { return sized; }
[2c57025]236 bool get_sized() const { return sized; }
237 TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
238
[312029a]239 virtual const char * typeString() const override;
240 virtual const char * genTypeString() const;
[e39241b]241
[07de76b]242 virtual TypeDecl * clone() const override { return new TypeDecl( *this ); }
[7870799]243 virtual void accept( Visitor & v ) override { v.visit( this ); }
244 virtual void accept( Visitor & v ) const override { v.visit( this ); }
[07de76b]245 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
246 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
[51b73452]247};
248
[17cd4eb]249class TypedefDecl : public NamedTypeDecl {
[0dd3a2f]250 typedef NamedTypeDecl Parent;
[17cd4eb]251 public:
[07de76b]252 TypedefDecl( const std::string & name, CodeLocation location, Type::StorageClasses scs, Type * type, LinkageSpec::Spec spec = LinkageSpec::Cforall )
[0b0f1dd]253 : Parent( name, scs, type ) { set_linkage( spec ); this->location = location; }
254
[07de76b]255 TypedefDecl( const TypedefDecl & other ) : Parent( other ) {}
[17cd4eb]256
[312029a]257 virtual const char * typeString() const override;
[e39241b]258
[07de76b]259 virtual TypedefDecl * clone() const override { return new TypedefDecl( *this ); }
[7870799]260 virtual void accept( Visitor & v ) override { v.visit( this ); }
261 virtual void accept( Visitor & v ) const override { v.visit( this ); }
[07de76b]262 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
[17cd4eb]263 private:
[51b73452]264};
265
[17cd4eb]266class AggregateDecl : public Declaration {
[0dd3a2f]267 typedef Declaration Parent;
[17cd4eb]268 public:
[312029a]269 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
270 static const char * aggrString( Aggregate aggr );
271
[65cdc1e]272 std::list<Declaration*> members;
273 std::list<TypeDecl*> parameters;
274 bool body;
275 std::list< Attribute * > attributes;
[29f9e20]276 AggregateDecl * parent = nullptr;
[65cdc1e]277
[07de76b]278 AggregateDecl( const std::string & name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
279 AggregateDecl( const AggregateDecl & other );
[0dd3a2f]280 virtual ~AggregateDecl();
[51b73452]281
[0dd3a2f]282 std::list<Declaration*>& get_members() { return members; }
283 std::list<TypeDecl*>& get_parameters() { return parameters; }
[17cd4eb]284
[c0aa336]285 std::list< Attribute * >& get_attributes() { return attributes; }
286 const std::list< Attribute * >& get_attributes() const { return attributes; }
287
[5d125e4]288 bool has_body() const { return body; }
289 AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
290
[07de76b]291 virtual void print( std::ostream & os, Indenter indent = {} ) const override final;
292 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
[17cd4eb]293 protected:
[312029a]294 virtual const char * typeString() const = 0;
[51b73452]295};
296
[17cd4eb]297class StructDecl : public AggregateDecl {
[0dd3a2f]298 typedef AggregateDecl Parent;
[17cd4eb]299 public:
[07de76b]300 StructDecl( const std::string & name, Aggregate kind = Struct, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ), kind( kind ) {}
301 StructDecl( const StructDecl & other ) : Parent( other ), kind( other.kind ) {}
[17cd4eb]302
[312029a]303 bool is_coroutine() { return kind == Coroutine; }
304 bool is_monitor() { return kind == Monitor; }
305 bool is_thread() { return kind == Thread; }
[409433da]306
[07de76b]307 virtual StructDecl * clone() const override { return new StructDecl( *this ); }
[7870799]308 virtual void accept( Visitor & v ) override { v.visit( this ); }
309 virtual void accept( Visitor & v ) const override { v.visit( this ); }
[07de76b]310 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
[312029a]311 Aggregate kind;
[712348a]312 private:
[312029a]313 virtual const char * typeString() const override;
[51b73452]314};
315
[17cd4eb]316class UnionDecl : public AggregateDecl {
[0dd3a2f]317 typedef AggregateDecl Parent;
[17cd4eb]318 public:
[07de76b]319 UnionDecl( const std::string & name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
320 UnionDecl( const UnionDecl & other ) : Parent( other ) {}
[17cd4eb]321
[07de76b]322 virtual UnionDecl * clone() const override { return new UnionDecl( *this ); }
[7870799]323 virtual void accept( Visitor & v ) override { v.visit( this ); }
324 virtual void accept( Visitor & v ) const override { v.visit( this ); }
[07de76b]325 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
[17cd4eb]326 private:
[312029a]327 virtual const char * typeString() const override;
[51b73452]328};
329
[17cd4eb]330class EnumDecl : public AggregateDecl {
[0dd3a2f]331 typedef AggregateDecl Parent;
[17cd4eb]332 public:
[07de76b]333 EnumDecl( const std::string & name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
334 EnumDecl( const EnumDecl & other ) : Parent( other ) {}
[17cd4eb]335
[fc9153d]336 bool valueOf( Declaration * enumerator, long long int & value );
337
[07de76b]338 virtual EnumDecl * clone() const override { return new EnumDecl( *this ); }
[7870799]339 virtual void accept( Visitor & v ) override { v.visit( this ); }
340 virtual void accept( Visitor & v ) const override { v.visit( this ); }
[07de76b]341 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
[17cd4eb]342 private:
[1690778]343 std::unordered_map< std::string, long long int > enumValues;
[312029a]344 virtual const char * typeString() const override;
[51b73452]345};
346
[4040425]347class TraitDecl : public AggregateDecl {
[0dd3a2f]348 typedef AggregateDecl Parent;
[17cd4eb]349 public:
[07de76b]350 TraitDecl( const std::string & name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, attributes, linkage ) {
[c0aa336]351 assertf( attributes.empty(), "attribute unsupported for traits" );
352 }
[07de76b]353 TraitDecl( const TraitDecl & other ) : Parent( other ) {}
[17cd4eb]354
[07de76b]355 virtual TraitDecl * clone() const override { return new TraitDecl( *this ); }
[7870799]356 virtual void accept( Visitor & v ) override { v.visit( this ); }
357 virtual void accept( Visitor & v ) const override { v.visit( this ); }
[07de76b]358 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
[17cd4eb]359 private:
[312029a]360 virtual const char * typeString() const override;
[51b73452]361};
362
[e67991f]363class WithStmt : public Declaration {
364public:
365 std::list< Expression * > exprs;
366 Statement * stmt;
367
368 WithStmt( const std::list< Expression * > & exprs, Statement * stmt );
369 WithStmt( const WithStmt & other );
370 virtual ~WithStmt();
371
372 virtual WithStmt * clone() const override { return new WithStmt( *this ); }
373 virtual void accept( Visitor & v ) override { v.visit( this ); }
374 virtual void accept( Visitor & v ) const override { v.visit( this ); }
375 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
376 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
377 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override { print(os, indent); }
378};
379
[e994912]380class AsmDecl : public Declaration {
381 public:
[07de76b]382 AsmStmt * stmt;
[65cdc1e]383
[07de76b]384 AsmDecl( AsmStmt * stmt );
385 AsmDecl( const AsmDecl & other );
[e994912]386 virtual ~AsmDecl();
387
[07de76b]388 AsmStmt * get_stmt() { return stmt; }
389 void set_stmt( AsmStmt * newValue ) { stmt = newValue; }
[e994912]390
[07de76b]391 virtual AsmDecl * clone() const override { return new AsmDecl( *this ); }
[7870799]392 virtual void accept( Visitor & v ) override { v.visit( this ); }
393 virtual void accept( Visitor & v ) const override { v.visit( this ); }
[07de76b]394 virtual AsmDecl * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
395 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
396 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
[e994912]397};
398
[f6e3e34]399class StaticAssertDecl : public Declaration {
400public:
401 Expression * condition;
402 ConstantExpr * message; // string literal
403
404 StaticAssertDecl( Expression * condition, ConstantExpr * message );
405 StaticAssertDecl( const StaticAssertDecl & other );
406 virtual ~StaticAssertDecl();
407
408 virtual StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
[7870799]409 virtual void accept( Visitor & v ) override { v.visit( this ); }
410 virtual void accept( Visitor & v ) const override { v.visit( this ); }
[07de76b]411 virtual StaticAssertDecl * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
412 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
413 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
[f6e3e34]414};
415
[2c57025]416std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
[baf7fee]417
[0dd3a2f]418// Local Variables: //
419// tab-width: 4 //
420// mode: c++ //
421// compile-command: "make install" //
422// End: //
Note: See TracBrowser for help on using the repository browser.