source: src/SynTree/Declaration.h @ e67991f

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since e67991f was e67991f, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

WithStmt? is now a Declaration

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