source: src/SynTree/Declaration.h@ 6fa409e

new-env
Last change on this file since 6fa409e was 28f3a19, checked in by Aaron Moss <a3moss@…>, 7 years ago

Merge branch 'master' into with_gc

  • Property mode set to 100644
File size: 15.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
[e612146c]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Sep 3 19:24:06 2017
13// Update Count : 131
[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
21#include <string> // for string, operator+, allocator, to_string
22
23#include "BaseSyntaxNode.h" // for BaseSyntaxNode
24#include "Mutator.h" // for Mutator
25#include "Parser/LinkageSpec.h" // for Spec, Cforall
26#include "Parser/ParseNode.h" // for DeclarationNode, DeclarationNode::Ag...
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
[68fe077a]45 Declaration( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage );
[0dd3a2f]46 Declaration( const Declaration &other );
47
[5f2f2d7]48 const std::string &get_name() const { return name; }
[0dd3a2f]49 void set_name( std::string newValue ) { name = newValue; }
[a7c90d4]50
[68fe077a]51 Type::StorageClasses get_storageClasses() const { return storageClasses; }
[a7c90d4]52
[8b7ee09]53 LinkageSpec::Spec get_linkage() const { return linkage; }
54 void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; }
[a7c90d4]55
[0dd3a2f]56 UniqueId get_uniqueId() const { return uniqueId; }
[a7c90d4]57
[8e9cbb2]58 bool get_extension() const { return extension; }
59 Declaration *set_extension( bool exten ) { extension = exten; return this; }
[0dd3a2f]60
61 void fixUniqueId( void );
[fa16264]62 virtual Declaration *clone() const override = 0;
[e149f77]63 virtual void accept( Visitor &v ) override = 0;
[fa16264]64 virtual Declaration *acceptMutator( Mutator &m ) override = 0;
[50377a4]65 virtual void print( std::ostream &os, Indenter indent = {} ) const override = 0;
66 virtual void printShort( std::ostream &os, Indenter indent = {} ) const = 0;
[0dd3a2f]67
68 static void dumpIds( std::ostream &os );
69 static Declaration *declFromId( UniqueId id );
[65cdc1e]70
[17cd4eb]71 private:
[68fe077a]72 Type::StorageClasses storageClasses;
[0dd3a2f]73 UniqueId uniqueId;
[51b73452]74};
75
[17cd4eb]76class DeclarationWithType : public Declaration {
77 public:
[65cdc1e]78 // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2
79 std::string mangleName;
80 // need to remember the scope level at which the variable was declared, so that shadowed identifiers can be accessed
81 int scopeLevel = 0;
82
[e612146c]83 Expression *asmName;
[65cdc1e]84 std::list< Attribute * > attributes;
[3ed994e]85 bool isDeleted = false;
[65cdc1e]86
[ddfd945]87 DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs );
[0dd3a2f]88 DeclarationWithType( const DeclarationWithType &other );
[68f9c43]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
[e612146c]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
[e149f77]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:
[65cdc1e]120 Type *type;
121 Initializer *init;
122 Expression *bitfieldWidth;
123
[68fe077a]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() );
[0dd3a2f]126 ObjectDecl( const ObjectDecl &other );
127
[e149f77]128 virtual Type * get_type() const override { return type; }
129 virtual void set_type(Type *newType) override { type = newType; }
[0dd3a2f]130
131 Initializer *get_init() const { return init; }
132 void set_init( Initializer *newValue ) { init = newValue; }
[58dd019]133
[0dd3a2f]134 Expression *get_bitfieldWidth() const { return bitfieldWidth; }
135 void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; }
136
[96f9ef5]137 static ObjectDecl * newObject( const std::string & name, Type * type, Initializer * init );
138
[e149f77]139 virtual ObjectDecl *clone() const override { return new ObjectDecl( *this ); }
140 virtual void accept( Visitor &v ) override { v.visit( this ); }
141 virtual DeclarationWithType *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
[50377a4]142 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
143 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
[51b73452]144};
145
[17cd4eb]146class FunctionDecl : public DeclarationWithType {
[0dd3a2f]147 typedef DeclarationWithType Parent;
[17cd4eb]148 public:
[65cdc1e]149 FunctionType *type;
150 CompoundStmt *statements;
[574894d]151 std::list< Expression * > withExprs;
[65cdc1e]152
[68fe077a]153 FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements,
[ddfd945]154 const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
[0dd3a2f]155 FunctionDecl( const FunctionDecl &other );
[51b73452]156
[e149f77]157 virtual Type * get_type() const override { return type; }
158 virtual void set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); }
[51b73452]159
[9aaac6e9]160 FunctionType * get_functionType() const { return type; }
[0dd3a2f]161 void set_functionType( FunctionType *newValue ) { type = newValue; }
162 CompoundStmt *get_statements() const { return statements; }
163 void set_statements( CompoundStmt *newValue ) { statements = newValue; }
164
[75626a1]165 static FunctionDecl * newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements );
166
[e149f77]167 virtual FunctionDecl *clone() const override { return new FunctionDecl( *this ); }
168 virtual void accept( Visitor &v ) override { v.visit( this ); }
169 virtual DeclarationWithType *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
[50377a4]170 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
171 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
[51b73452]172};
173
[17cd4eb]174class NamedTypeDecl : public Declaration {
[0dd3a2f]175 typedef Declaration Parent;
[17cd4eb]176 public:
[65cdc1e]177 Type *base;
178 std::list< TypeDecl* > parameters;
179 std::list< DeclarationWithType* > assertions;
180
[68fe077a]181 NamedTypeDecl( const std::string &name, Type::StorageClasses scs, Type *type );
[46f6134]182 NamedTypeDecl( const NamedTypeDecl &other );
[0dd3a2f]183
184 Type *get_base() const { return base; }
185 void set_base( Type *newValue ) { base = newValue; }
186 std::list< TypeDecl* >& get_parameters() { return parameters; }
187 std::list< DeclarationWithType* >& get_assertions() { return assertions; }
188
[e39241b]189 virtual std::string typeString() const = 0;
190
[e149f77]191 virtual NamedTypeDecl *clone() const override = 0;
[50377a4]192 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
193 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
[51b73452]194};
195
[17cd4eb]196class TypeDecl : public NamedTypeDecl {
[0dd3a2f]197 typedef NamedTypeDecl Parent;
[17cd4eb]198 public:
[f0ecf9b]199 enum Kind { Dtype, Ftype, Ttype };
[65cdc1e]200
201 Type * init;
202 bool sized;
203
[2c57025]204 /// Data extracted from a type decl
205 struct Data {
206 TypeDecl::Kind kind;
207 bool isComplete;
208 Data() : kind( (TypeDecl::Kind)-1 ), isComplete( false ) {}
209 Data( TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {}
210 Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {}
211 bool operator==(const Data & other) const { return kind == other.kind && isComplete == other.isComplete; }
212 bool operator!=(const Data & other) const { return !(*this == other);}
213 };
[51b73452]214
[f0ecf9b]215 TypeDecl( const std::string &name, Type::StorageClasses scs, Type *type, Kind kind, bool sized, Type * init = nullptr );
[0dd3a2f]216 TypeDecl( const TypeDecl &other );
[17cd4eb]217
[0dd3a2f]218 Kind get_kind() const { return kind; }
[51b73452]219
[67cf18c]220 Type * get_init() const { return init; }
221 TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
222
[f0ecf9b]223 bool isComplete() const { return sized; }
[2c57025]224 bool get_sized() const { return sized; }
225 TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
226
[e149f77]227 virtual std::string typeString() const override;
[bdd0755]228 virtual std::string genTypeString() const;
[e39241b]229
[e149f77]230 virtual TypeDecl *clone() const override { return new TypeDecl( *this ); }
231 virtual void accept( Visitor &v ) override { v.visit( this ); }
232 virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
[50377a4]233 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
[67cf18c]234
[17cd4eb]235 private:
[0dd3a2f]236 Kind kind;
[51b73452]237};
238
[17cd4eb]239class TypedefDecl : public NamedTypeDecl {
[0dd3a2f]240 typedef NamedTypeDecl Parent;
[17cd4eb]241 public:
[0b0f1dd]242 TypedefDecl( const std::string &name, CodeLocation location, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall )
243 : Parent( name, scs, type ) { set_linkage( spec ); this->location = location; }
244
[0dd3a2f]245 TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
[17cd4eb]246
[e149f77]247 virtual std::string typeString() const override;
[e39241b]248
[e149f77]249 virtual TypedefDecl *clone() const override { return new TypedefDecl( *this ); }
250 virtual void accept( Visitor &v ) override { v.visit( this ); }
251 virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
[17cd4eb]252 private:
[51b73452]253};
254
[17cd4eb]255class AggregateDecl : public Declaration {
[0dd3a2f]256 typedef Declaration Parent;
[17cd4eb]257 public:
[65cdc1e]258 std::list<Declaration*> members;
259 std::list<TypeDecl*> parameters;
260 bool body;
261 std::list< Attribute * > attributes;
262
[fa4805f]263 AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
[0dd3a2f]264 AggregateDecl( const AggregateDecl &other );
[68f9c43]265
[0dd3a2f]266 std::list<Declaration*>& get_members() { return members; }
267 std::list<TypeDecl*>& get_parameters() { return parameters; }
[17cd4eb]268
[c0aa336]269 std::list< Attribute * >& get_attributes() { return attributes; }
270 const std::list< Attribute * >& get_attributes() const { return attributes; }
271
[5d125e4]272 bool has_body() const { return body; }
273 AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
274
[50377a4]275 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
276 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
[17cd4eb]277 protected:
[0dd3a2f]278 virtual std::string typeString() const = 0;
[51b73452]279};
280
[17cd4eb]281class StructDecl : public AggregateDecl {
[0dd3a2f]282 typedef AggregateDecl Parent;
[17cd4eb]283 public:
[f196351]284 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 ) {}
285 StructDecl( const StructDecl &other ) : Parent( other ), kind( other.kind ) {}
[17cd4eb]286
[409433da]287 bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
288 bool is_monitor() { return kind == DeclarationNode::Monitor; }
289 bool is_thread() { return kind == DeclarationNode::Thread; }
290
[e149f77]291 virtual StructDecl *clone() const override { return new StructDecl( *this ); }
292 virtual void accept( Visitor &v ) override { v.visit( this ); }
293 virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
[17cd4eb]294 private:
[409433da]295 DeclarationNode::Aggregate kind;
[e149f77]296 virtual std::string typeString() const override;
[51b73452]297};
298
[17cd4eb]299class UnionDecl : public AggregateDecl {
[0dd3a2f]300 typedef AggregateDecl Parent;
[17cd4eb]301 public:
[fa4805f]302 UnionDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
[0dd3a2f]303 UnionDecl( const UnionDecl &other ) : Parent( other ) {}
[17cd4eb]304
[e149f77]305 virtual UnionDecl *clone() const override { return new UnionDecl( *this ); }
306 virtual void accept( Visitor &v ) override { v.visit( this ); }
307 virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
[17cd4eb]308 private:
[e149f77]309 virtual std::string typeString() const override;
[51b73452]310};
311
[17cd4eb]312class EnumDecl : public AggregateDecl {
[0dd3a2f]313 typedef AggregateDecl Parent;
[17cd4eb]314 public:
[fa4805f]315 EnumDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
[0dd3a2f]316 EnumDecl( const EnumDecl &other ) : Parent( other ) {}
[17cd4eb]317
[fc9153d]318 bool valueOf( Declaration * enumerator, long long int & value );
319
[e149f77]320 virtual EnumDecl *clone() const override { return new EnumDecl( *this ); }
321 virtual void accept( Visitor &v ) override { v.visit( this ); }
322 virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
[17cd4eb]323 private:
[fc9153d]324 std::map< std::string, long long int > enumValues;
[e149f77]325 virtual std::string typeString() const override;
[51b73452]326};
327
[4040425]328class TraitDecl : public AggregateDecl {
[0dd3a2f]329 typedef AggregateDecl Parent;
[17cd4eb]330 public:
[bd46af4]331 TraitDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, attributes, linkage ) {
[c0aa336]332 assertf( attributes.empty(), "attribute unsupported for traits" );
333 }
[4040425]334 TraitDecl( const TraitDecl &other ) : Parent( other ) {}
[17cd4eb]335
[e149f77]336 virtual TraitDecl *clone() const override { return new TraitDecl( *this ); }
337 virtual void accept( Visitor &v ) override { v.visit( this ); }
338 virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
[17cd4eb]339 private:
[e149f77]340 virtual std::string typeString() const override;
[51b73452]341};
342
[e994912]343class AsmDecl : public Declaration {
344 public:
[65cdc1e]345 AsmStmt *stmt;
346
[e994912]347 AsmDecl( AsmStmt *stmt );
348 AsmDecl( const AsmDecl &other );
349
350 AsmStmt *get_stmt() { return stmt; }
351 void set_stmt( AsmStmt *newValue ) { stmt = newValue; }
352
[e149f77]353 virtual AsmDecl *clone() const override { return new AsmDecl( *this ); }
354 virtual void accept( Visitor &v ) override { v.visit( this ); }
355 virtual AsmDecl *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
[50377a4]356 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
357 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
[e994912]358};
359
[f6e3e34]360class StaticAssertDecl : public Declaration {
361public:
362 Expression * condition;
363 ConstantExpr * message; // string literal
364
365 StaticAssertDecl( Expression * condition, ConstantExpr * message );
366 StaticAssertDecl( const StaticAssertDecl & other );
367
368 virtual StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
369 virtual void accept( Visitor &v ) override { v.visit( this ); }
370 virtual StaticAssertDecl * acceptMutator( Mutator &m ) override { return m.mutate( this ); }
371 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
372 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
373};
374
[2c57025]375std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
[baf7fee]376
[0dd3a2f]377// Local Variables: //
378// tab-width: 4 //
379// mode: c++ //
380// compile-command: "make install" //
381// End: //
Note: See TracBrowser for help on using the repository browser.