source: src/SynTree/Declaration.h@ 184557e

new-env
Last change on this file since 184557e 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
Line 
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//
7// Declaration.h --
8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Sep 3 19:24:06 2017
13// Update Count : 131
14//
15
16#pragma once
17
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;
38
39class Declaration : public BaseSyntaxNode {
40 public:
41 std::string name;
42 LinkageSpec::Spec linkage;
43 bool extension = false;
44
45 Declaration( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage );
46 Declaration( const Declaration &other );
47
48 const std::string &get_name() const { return name; }
49 void set_name( std::string newValue ) { name = newValue; }
50
51 Type::StorageClasses get_storageClasses() const { return storageClasses; }
52
53 LinkageSpec::Spec get_linkage() const { return linkage; }
54 void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; }
55
56 UniqueId get_uniqueId() const { return uniqueId; }
57
58 bool get_extension() const { return extension; }
59 Declaration *set_extension( bool exten ) { extension = exten; return this; }
60
61 void fixUniqueId( void );
62 virtual Declaration *clone() const override = 0;
63 virtual void accept( Visitor &v ) override = 0;
64 virtual Declaration *acceptMutator( Mutator &m ) override = 0;
65 virtual void print( std::ostream &os, Indenter indent = {} ) const override = 0;
66 virtual void printShort( std::ostream &os, Indenter indent = {} ) const = 0;
67
68 static void dumpIds( std::ostream &os );
69 static Declaration *declFromId( UniqueId id );
70
71 private:
72 Type::StorageClasses storageClasses;
73 UniqueId uniqueId;
74};
75
76class DeclarationWithType : public Declaration {
77 public:
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
83 Expression *asmName;
84 std::list< Attribute * > attributes;
85 bool isDeleted = false;
86
87 DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs );
88 DeclarationWithType( const DeclarationWithType &other );
89
90 std::string get_mangleName() const { return mangleName; }
91 DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
92
93 std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
94
95 int get_scopeLevel() const { return scopeLevel; }
96 DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; }
97
98 Expression *get_asmName() const { return asmName; }
99 DeclarationWithType * set_asmName( Expression *newValue ) { asmName = newValue; return this; }
100
101 std::list< Attribute * >& get_attributes() { return attributes; }
102 const std::list< Attribute * >& get_attributes() const { return attributes; }
103
104 Type::FuncSpecifiers get_funcSpec() const { return fs; }
105 //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; }
106
107 virtual DeclarationWithType *clone() const override = 0;
108 virtual DeclarationWithType *acceptMutator( Mutator &m ) override = 0;
109
110 virtual Type * get_type() const = 0;
111 virtual void set_type(Type *) = 0;
112
113 private:
114 Type::FuncSpecifiers fs;
115};
116
117class ObjectDecl : public DeclarationWithType {
118 typedef DeclarationWithType Parent;
119 public:
120 Type *type;
121 Initializer *init;
122 Expression *bitfieldWidth;
123
124 ObjectDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression *bitfieldWidth, Type *type, Initializer *init,
125 const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
126 ObjectDecl( const ObjectDecl &other );
127
128 virtual Type * get_type() const override { return type; }
129 virtual void set_type(Type *newType) override { type = newType; }
130
131 Initializer *get_init() const { return init; }
132 void set_init( Initializer *newValue ) { init = newValue; }
133
134 Expression *get_bitfieldWidth() const { return bitfieldWidth; }
135 void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; }
136
137 static ObjectDecl * newObject( const std::string & name, Type * type, Initializer * init );
138
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 ); }
142 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
143 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
144};
145
146class FunctionDecl : public DeclarationWithType {
147 typedef DeclarationWithType Parent;
148 public:
149 FunctionType *type;
150 CompoundStmt *statements;
151 std::list< Expression * > withExprs;
152
153 FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements,
154 const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
155 FunctionDecl( const FunctionDecl &other );
156
157 virtual Type * get_type() const override { return type; }
158 virtual void set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); }
159
160 FunctionType * get_functionType() const { return type; }
161 void set_functionType( FunctionType *newValue ) { type = newValue; }
162 CompoundStmt *get_statements() const { return statements; }
163 void set_statements( CompoundStmt *newValue ) { statements = newValue; }
164
165 static FunctionDecl * newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements );
166
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 ); }
170 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
171 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
172};
173
174class NamedTypeDecl : public Declaration {
175 typedef Declaration Parent;
176 public:
177 Type *base;
178 std::list< TypeDecl* > parameters;
179 std::list< DeclarationWithType* > assertions;
180
181 NamedTypeDecl( const std::string &name, Type::StorageClasses scs, Type *type );
182 NamedTypeDecl( const NamedTypeDecl &other );
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
189 virtual std::string typeString() const = 0;
190
191 virtual NamedTypeDecl *clone() const override = 0;
192 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
193 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
194};
195
196class TypeDecl : public NamedTypeDecl {
197 typedef NamedTypeDecl Parent;
198 public:
199 enum Kind { Dtype, Ftype, Ttype };
200
201 Type * init;
202 bool sized;
203
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 };
214
215 TypeDecl( const std::string &name, Type::StorageClasses scs, Type *type, Kind kind, bool sized, Type * init = nullptr );
216 TypeDecl( const TypeDecl &other );
217
218 Kind get_kind() const { return kind; }
219
220 Type * get_init() const { return init; }
221 TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
222
223 bool isComplete() const { return sized; }
224 bool get_sized() const { return sized; }
225 TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
226
227 virtual std::string typeString() const override;
228 virtual std::string genTypeString() const;
229
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 ); }
233 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
234
235 private:
236 Kind kind;
237};
238
239class TypedefDecl : public NamedTypeDecl {
240 typedef NamedTypeDecl Parent;
241 public:
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
245 TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
246
247 virtual std::string typeString() const override;
248
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 ); }
252 private:
253};
254
255class AggregateDecl : public Declaration {
256 typedef Declaration Parent;
257 public:
258 std::list<Declaration*> members;
259 std::list<TypeDecl*> parameters;
260 bool body;
261 std::list< Attribute * > attributes;
262
263 AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
264 AggregateDecl( const AggregateDecl &other );
265
266 std::list<Declaration*>& get_members() { return members; }
267 std::list<TypeDecl*>& get_parameters() { return parameters; }
268
269 std::list< Attribute * >& get_attributes() { return attributes; }
270 const std::list< Attribute * >& get_attributes() const { return attributes; }
271
272 bool has_body() const { return body; }
273 AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
274
275 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
276 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
277 protected:
278 virtual std::string typeString() const = 0;
279};
280
281class StructDecl : public AggregateDecl {
282 typedef AggregateDecl Parent;
283 public:
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 ) {}
286
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
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 ); }
294 private:
295 DeclarationNode::Aggregate kind;
296 virtual std::string typeString() const override;
297};
298
299class UnionDecl : public AggregateDecl {
300 typedef AggregateDecl Parent;
301 public:
302 UnionDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
303 UnionDecl( const UnionDecl &other ) : Parent( other ) {}
304
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 ); }
308 private:
309 virtual std::string typeString() const override;
310};
311
312class EnumDecl : public AggregateDecl {
313 typedef AggregateDecl Parent;
314 public:
315 EnumDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
316 EnumDecl( const EnumDecl &other ) : Parent( other ) {}
317
318 bool valueOf( Declaration * enumerator, long long int & value );
319
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 ); }
323 private:
324 std::map< std::string, long long int > enumValues;
325 virtual std::string typeString() const override;
326};
327
328class TraitDecl : public AggregateDecl {
329 typedef AggregateDecl Parent;
330 public:
331 TraitDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, attributes, linkage ) {
332 assertf( attributes.empty(), "attribute unsupported for traits" );
333 }
334 TraitDecl( const TraitDecl &other ) : Parent( other ) {}
335
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 ); }
339 private:
340 virtual std::string typeString() const override;
341};
342
343class AsmDecl : public Declaration {
344 public:
345 AsmStmt *stmt;
346
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
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 ); }
356 virtual void print( std::ostream &os, Indenter indent = {} ) const override;
357 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
358};
359
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
375std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
376
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.