source: src/SynTree/Declaration.h @ 0270824

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 0270824 was 8f60f0b, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

add support for ttype in the parser

  • Property mode set to 100644
File size: 11.5 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 : Tue Dec 13 13:37:33 2016
13// Update Count     : 49
14//
15
16#ifndef DECLARATION_H
17#define DECLARATION_H
18
19#include "SynTree.h"
20#include "Visitor.h"
21#include "Mutator.h"
22#include "Parser/LinkageSpec.h"
23#include "Parser/ParseNode.h"
24#include <string>
25
26class Declaration {
27  public:
28        Declaration( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Spec linkage );
29        Declaration( const Declaration &other );
30        virtual ~Declaration();
31
32        const std::string &get_name() const { return name; }
33        void set_name( std::string newValue ) { name = newValue; }
34        DeclarationNode::StorageClass get_storageClass() const { return storageClass; }
35        void set_storageClass( DeclarationNode::StorageClass newValue ) { storageClass = newValue; }
36        LinkageSpec::Spec get_linkage() const { return linkage; }
37        void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; }
38        bool get_isInline() const { return isInline; }
39        void set_isInline( bool newValue ) { isInline = newValue; }
40        bool get_isNoreturn() const { return isNoreturn; }
41        void set_isNoreturn( bool newValue ) { isNoreturn = newValue; }
42        UniqueId get_uniqueId() const { return uniqueId; }
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        DeclarationNode::StorageClass storageClass;
58        LinkageSpec::Spec linkage;
59        bool isInline, isNoreturn;
60        UniqueId uniqueId;
61        bool extension = false;
62};
63
64class DeclarationWithType : public Declaration {
65  public:
66        DeclarationWithType( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes );
67        DeclarationWithType( const DeclarationWithType &other );
68        virtual ~DeclarationWithType();
69
70        std::string get_mangleName() const { return mangleName; }
71        DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
72
73        std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
74
75        int get_scopeLevel() const { return scopeLevel; }
76        DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; }
77
78        ConstantExpr *get_asmName() const { return asmName; }
79        DeclarationWithType * set_asmName( ConstantExpr *newValue ) { asmName = newValue; return this; }
80
81        std::list< Attribute * >& get_attributes() { return attributes; }
82        const std::list< Attribute * >& get_attributes() const { return attributes; }
83
84        virtual DeclarationWithType *clone() const = 0;
85        virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0;
86
87        virtual Type *get_type() const = 0;
88        virtual void set_type(Type *) = 0;
89  private:
90        // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2
91        std::string mangleName;
92        // need to remember the scope level at which the variable was declared, so that shadowed identifiers can be accessed
93        int scopeLevel = 0;
94
95        ConstantExpr *asmName;
96        std::list< Attribute * > attributes;
97};
98
99class ObjectDecl : public DeclarationWithType {
100        typedef DeclarationWithType Parent;
101  public:
102        ObjectDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Spec linkage, Expression *bitfieldWidth, Type *type, Initializer *init, const std::list< Attribute * > attributes = std::list< Attribute * >(), bool isInline = false, bool isNoreturn = false );
103        ObjectDecl( const ObjectDecl &other );
104        virtual ~ObjectDecl();
105
106        virtual Type *get_type() const { return type; }
107        virtual void set_type(Type *newType) { type = newType; }
108
109        Initializer *get_init() const { return init; }
110        void set_init( Initializer *newValue ) { init = newValue; }
111
112        Expression *get_bitfieldWidth() const { return bitfieldWidth; }
113        void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; }
114
115        virtual ObjectDecl *clone() const { return new ObjectDecl( *this ); }
116        virtual void accept( Visitor &v ) { v.visit( this ); }
117        virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); }
118        virtual void print( std::ostream &os, int indent = 0 ) const;
119        virtual void printShort( std::ostream &os, int indent = 0 ) const;
120  private:
121        Type *type;
122        Initializer *init;
123        Expression *bitfieldWidth;
124};
125
126class FunctionDecl : public DeclarationWithType {
127        typedef DeclarationWithType Parent;
128  public:
129        FunctionDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements, bool isInline, bool isNoreturn, const std::list< Attribute * > attributes = std::list< Attribute * >() );
130        FunctionDecl( const FunctionDecl &other );
131        virtual ~FunctionDecl();
132
133        Type *get_type() const;
134        virtual void set_type(Type *);
135
136        FunctionType *get_functionType() const { return type; }
137        void set_functionType( FunctionType *newValue ) { type = newValue; }
138        CompoundStmt *get_statements() const { return statements; }
139        void set_statements( CompoundStmt *newValue ) { statements = newValue; }
140        std::list< std::string >& get_oldIdents() { return oldIdents; }
141        std::list< Declaration* >& get_oldDecls() { return oldDecls; }
142
143        virtual FunctionDecl *clone() const { return new FunctionDecl( *this ); }
144        virtual void accept( Visitor &v ) { v.visit( this ); }
145        virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); }
146        virtual void print( std::ostream &os, int indent = 0 ) const;
147        virtual void printShort( std::ostream &os, int indent = 0 ) const;
148  private:
149        FunctionType *type;
150        CompoundStmt *statements;
151        std::list< std::string > oldIdents;
152        std::list< Declaration* > oldDecls;
153};
154
155class NamedTypeDecl : public Declaration {
156        typedef Declaration Parent;
157  public:
158        NamedTypeDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type );
159        NamedTypeDecl( const NamedTypeDecl &other );
160        virtual ~NamedTypeDecl();
161
162        Type *get_base() const { return base; }
163        void set_base( Type *newValue ) { base = newValue; }
164        std::list< TypeDecl* >& get_parameters() { return parameters; }
165        std::list< DeclarationWithType* >& get_assertions() { return assertions; }
166
167        virtual NamedTypeDecl *clone() const = 0;
168        virtual void print( std::ostream &os, int indent = 0 ) const;
169        virtual void printShort( std::ostream &os, int indent = 0 ) const;
170  protected:
171        virtual std::string typeString() const = 0;
172  private:
173        Type *base;
174        std::list< TypeDecl* > parameters;
175        std::list< DeclarationWithType* > assertions;
176};
177
178class TypeDecl : public NamedTypeDecl {
179        typedef NamedTypeDecl Parent;
180  public:
181        enum Kind { Any, Dtype, Ftype, Ttype };
182        /// Data extracted from a type decl
183        struct Data {
184                TypeDecl::Kind kind;
185                bool isComplete;
186                Data() : kind( (TypeDecl::Kind)-1 ), isComplete( false ) {}
187                Data( TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {}
188                Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {}
189                bool operator==(const Data & other) const { return kind == other.kind && isComplete == other.isComplete; }
190                bool operator!=(const Data & other) const { return !(*this == other);}
191        };
192
193        TypeDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type, Kind kind );
194        TypeDecl( const TypeDecl &other );
195
196        Kind get_kind() const { return kind; }
197
198        bool isComplete() const { return kind == Any || sized; }
199        bool get_sized() const { return sized; }
200        TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
201
202        virtual TypeDecl *clone() const { return new TypeDecl( *this ); }
203        virtual void accept( Visitor &v ) { v.visit( this ); }
204        virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
205  private:
206        virtual std::string typeString() const;
207        Kind kind;
208        bool sized;
209};
210
211class TypedefDecl : public NamedTypeDecl {
212        typedef NamedTypeDecl Parent;
213  public:
214        TypedefDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type ) : Parent( name, sc, type ) {}
215        TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
216
217        virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); }
218        virtual void accept( Visitor &v ) { v.visit( this ); }
219        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
220  private:
221        virtual std::string typeString() const;
222};
223
224class AggregateDecl : public Declaration {
225        typedef Declaration Parent;
226  public:
227        AggregateDecl( const std::string &name );
228        AggregateDecl( const AggregateDecl &other );
229        virtual ~AggregateDecl();
230
231        std::list<Declaration*>& get_members() { return members; }
232        std::list<TypeDecl*>& get_parameters() { return parameters; }
233
234        bool has_body() const { return body; }
235        AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
236
237        virtual void print( std::ostream &os, int indent = 0 ) const;
238        virtual void printShort( std::ostream &os, int indent = 0 ) const;
239  protected:
240        virtual std::string typeString() const = 0;
241
242  private:
243        std::list<Declaration*> members;
244        std::list<TypeDecl*> parameters;
245        bool body;
246};
247
248class StructDecl : public AggregateDecl {
249        typedef AggregateDecl Parent;
250  public:
251        StructDecl( const std::string &name ) : Parent( name ) {}
252        StructDecl( const StructDecl &other ) : Parent( other ) {}
253
254        virtual StructDecl *clone() const { return new StructDecl( *this ); }
255        virtual void accept( Visitor &v ) { v.visit( this ); }
256        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
257  private:
258        virtual std::string typeString() const;
259};
260
261class UnionDecl : public AggregateDecl {
262        typedef AggregateDecl Parent;
263  public:
264        UnionDecl( const std::string &name ) : Parent( name ) {}
265        UnionDecl( const UnionDecl &other ) : Parent( other ) {}
266
267        virtual UnionDecl *clone() const { return new UnionDecl( *this ); }
268        virtual void accept( Visitor &v ) { v.visit( this ); }
269        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
270  private:
271        virtual std::string typeString() const;
272};
273
274class EnumDecl : public AggregateDecl {
275        typedef AggregateDecl Parent;
276  public:
277        EnumDecl( const std::string &name ) : Parent( name ) {}
278        EnumDecl( const EnumDecl &other ) : Parent( other ) {}
279
280        virtual EnumDecl *clone() const { return new EnumDecl( *this ); }
281        virtual void accept( Visitor &v ) { v.visit( this ); }
282        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
283  private:
284        virtual std::string typeString() const;
285};
286
287class TraitDecl : public AggregateDecl {
288        typedef AggregateDecl Parent;
289  public:
290        TraitDecl( const std::string &name ) : Parent( name ) {}
291        TraitDecl( const TraitDecl &other ) : Parent( other ) {}
292
293        virtual TraitDecl *clone() const { return new TraitDecl( *this ); }
294        virtual void accept( Visitor &v ) { v.visit( this ); }
295        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
296  private:
297        virtual std::string typeString() const;
298};
299
300std::ostream & operator<<( std::ostream & out, const Declaration * decl );
301std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
302
303#endif // DECLARATION_H
304
305// Local Variables: //
306// tab-width: 4 //
307// mode: c++ //
308// compile-command: "make install" //
309// End: //
Note: See TracBrowser for help on using the repository browser.