source: src/SynTree/Declaration.h @ cbce272

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since cbce272 was cbce272, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Structure based exception handling.

  • Property mode set to 100644
File size: 13.8 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 : Tus Aug  8 13:25:00 2017
13// Update Count     : 125
14//
15
16#pragma once
17
18#include <string>
19
20#include "BaseSyntaxNode.h"
21#include "Mutator.h"
22#include "Visitor.h"
23#include "SynTree.h"
24#include "Parser/LinkageSpec.h"
25#include "Parser/ParseNode.h"
26
27class Declaration : public BaseSyntaxNode {
28  public:
29        Declaration( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage );
30        Declaration( const Declaration &other );
31        virtual ~Declaration();
32
33        const std::string &get_name() const { return name; }
34        void set_name( std::string newValue ) { name = newValue; }
35
36        Type::StorageClasses get_storageClasses() const { return storageClasses; }
37
38        LinkageSpec::Spec get_linkage() const { return linkage; }
39        void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; }
40
41        UniqueId get_uniqueId() const { return uniqueId; }
42
43        bool get_extension() const { return extension; }
44        Declaration *set_extension( bool exten ) { extension = exten; return this; }
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 );
55  private:
56        std::string name;
57        Type::StorageClasses storageClasses;
58        LinkageSpec::Spec linkage;
59        UniqueId uniqueId;
60        bool extension = false;
61};
62
63class DeclarationWithType : public Declaration {
64  public:
65        DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs );
66        DeclarationWithType( const DeclarationWithType &other );
67        virtual ~DeclarationWithType();
68
69        std::string get_mangleName() const { return mangleName; }
70        DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
71
72        std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
73
74        int get_scopeLevel() const { return scopeLevel; }
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; }
79
80        std::list< Attribute * >& get_attributes() { return attributes; }
81        const std::list< Attribute * >& get_attributes() const { return attributes; }
82
83        Type::FuncSpecifiers get_funcSpec() const { return fs; }
84        //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; }
85
86        virtual DeclarationWithType *clone() const = 0;
87        virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0;
88
89        virtual Type *get_type() const = 0;
90        virtual void set_type(Type *) = 0;
91  private:
92        // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2
93        std::string mangleName;
94        // need to remember the scope level at which the variable was declared, so that shadowed identifiers can be accessed
95        int scopeLevel = 0;
96
97        ConstantExpr *asmName;
98        std::list< Attribute * > attributes;
99        Type::FuncSpecifiers fs;
100};
101
102class ObjectDecl : public DeclarationWithType {
103        typedef DeclarationWithType Parent;
104  public:
105        ObjectDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression *bitfieldWidth, Type *type, Initializer *init,
106                                const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
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; }
115
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 ); }
121        virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); }
122        virtual void print( std::ostream &os, int indent = 0 ) const;
123        virtual void printShort( std::ostream &os, int indent = 0 ) const;
124  private:
125        Type *type;
126        Initializer *init;
127        Expression *bitfieldWidth;
128};
129
130class FunctionDecl : public DeclarationWithType {
131        typedef DeclarationWithType Parent;
132  public:
133        FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements,
134                                  const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
135        FunctionDecl( const FunctionDecl &other );
136        virtual ~FunctionDecl();
137
138        Type *get_type() const;
139        virtual void set_type(Type *);
140
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;
151  private:
152        FunctionType *type;
153        CompoundStmt *statements;
154};
155
156class NamedTypeDecl : public Declaration {
157        typedef Declaration Parent;
158  public:
159        NamedTypeDecl( const std::string &name, Type::StorageClasses scs, Type *type );
160        NamedTypeDecl( const NamedTypeDecl &other );
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
168        virtual std::string typeString() const = 0;
169
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;
173  protected:
174  private:
175        Type *base;
176        std::list< TypeDecl* > parameters;
177        std::list< DeclarationWithType* > assertions;
178};
179
180class TypeDecl : public NamedTypeDecl {
181        typedef NamedTypeDecl Parent;
182  public:
183        enum Kind { Any, Dtype, Ftype, Ttype };
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        };
194
195        TypeDecl( const std::string &name, Type::StorageClasses scs, Type *type, Kind kind, Type * init = nullptr );
196        TypeDecl( const TypeDecl &other );
197        virtual ~TypeDecl();
198
199        Kind get_kind() const { return kind; }
200
201        Type * get_init() const { return init; }
202        TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
203
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
208        virtual std::string typeString() const;
209        virtual std::string genTypeString() const;
210
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 ); }
214        virtual void print( std::ostream &os, int indent = 0 ) const;
215
216  private:
217        Kind kind;
218        Type * init;
219        bool sized;
220};
221
222class TypedefDecl : public NamedTypeDecl {
223        typedef NamedTypeDecl Parent;
224  public:
225        TypedefDecl( const std::string &name, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall ) : Parent( name, scs, type ) { set_linkage( spec ); }
226        TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
227
228        virtual std::string typeString() const;
229
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 ); }
233  private:
234};
235
236class AggregateDecl : public Declaration {
237        typedef Declaration Parent;
238  public:
239        AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
240        AggregateDecl( const AggregateDecl &other );
241        virtual ~AggregateDecl();
242
243        std::list<Declaration*>& get_members() { return members; }
244        std::list<TypeDecl*>& get_parameters() { return parameters; }
245
246        std::list< Attribute * >& get_attributes() { return attributes; }
247        const std::list< Attribute * >& get_attributes() const { return attributes; }
248
249        bool has_body() const { return body; }
250        AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
251
252        virtual void print( std::ostream &os, int indent = 0 ) const;
253        virtual void printShort( std::ostream &os, int indent = 0 ) const;
254  protected:
255        virtual std::string typeString() const = 0;
256
257  private:
258        std::list<Declaration*> members;
259        std::list<TypeDecl*> parameters;
260        bool body;
261        std::list< Attribute * > attributes;
262};
263
264class StructDecl : public AggregateDecl {
265        typedef AggregateDecl Parent;
266  public:
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 : "" ) {}
269        StructDecl( const StructDecl &other ) : Parent( other ) {}
270
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
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
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 ); }
284  private:
285        DeclarationNode::Aggregate kind;
286        virtual std::string typeString() const;
287
288        bool tagged;
289        std::string parent_name;
290};
291
292class UnionDecl : public AggregateDecl {
293        typedef AggregateDecl Parent;
294  public:
295        UnionDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
296        UnionDecl( const UnionDecl &other ) : Parent( other ) {}
297
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 ); }
301  private:
302        virtual std::string typeString() const;
303};
304
305class EnumDecl : public AggregateDecl {
306        typedef AggregateDecl Parent;
307  public:
308        EnumDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
309        EnumDecl( const EnumDecl &other ) : Parent( other ) {}
310
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 ); }
314  private:
315        virtual std::string typeString() const;
316};
317
318class TraitDecl : public AggregateDecl {
319        typedef AggregateDecl Parent;
320  public:
321        TraitDecl( const std::string &name, const std::list< Attribute * > & attributes ) : Parent( name ) {
322                assertf( attributes.empty(), "attribute unsupported for traits" );
323        }
324        TraitDecl( const TraitDecl &other ) : Parent( other ) {}
325
326        virtual TraitDecl *clone() const { return new TraitDecl( *this ); }
327        virtual void accept( Visitor &v ) { v.visit( this ); }
328        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
329  private:
330        virtual std::string typeString() const;
331};
332
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
351std::ostream & operator<<( std::ostream & out, const Declaration * decl );
352std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
353
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.