source: src/SynTree/Declaration.h @ 14f6bb39

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 14f6bb39 was 3a5131ed, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

handle KR function declarations

  • Property mode set to 100644
File size: 12.6 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 : Thu Feb 16 14:53:35 2017
13// Update Count     : 57
14//
15
16#ifndef DECLARATION_H
17#define DECLARATION_H
18
19#include <string>
20
21#include "BaseSyntaxNode.h"
22#include "Mutator.h"
23#include "Visitor.h"
24#include "SynTree.h"
25#include "Parser/LinkageSpec.h"
26#include "Parser/ParseNode.h"
27
28class Declaration : public BaseSyntaxNode {
29  public:
30        Declaration( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Spec linkage );
31        Declaration( const Declaration &other );
32        virtual ~Declaration();
33
34        const std::string &get_name() const { return name; }
35        void set_name( std::string newValue ) { name = newValue; }
36        DeclarationNode::StorageClass get_storageClass() const { return storageClass; }
37        void set_storageClass( DeclarationNode::StorageClass newValue ) { storageClass = newValue; }
38        LinkageSpec::Spec get_linkage() const { return linkage; }
39        void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; }
40        bool get_isInline() const { return isInline; }
41        void set_isInline( bool newValue ) { isInline = newValue; }
42        bool get_isNoreturn() const { return isNoreturn; }
43        void set_isNoreturn( bool newValue ) { isNoreturn = newValue; }
44        UniqueId get_uniqueId() const { return uniqueId; }
45        bool get_extension() const { return extension; }
46        Declaration *set_extension( bool exten ) { extension = exten; return this; }
47
48        void fixUniqueId( void );
49        virtual Declaration *clone() const = 0;
50        virtual void accept( Visitor &v ) = 0;
51        virtual Declaration *acceptMutator( Mutator &m ) = 0;
52        virtual void print( std::ostream &os, int indent = 0 ) const = 0;
53        virtual void printShort( std::ostream &os, int indent = 0 ) const = 0;
54
55        static void dumpIds( std::ostream &os );
56        static Declaration *declFromId( UniqueId id );
57  private:
58        std::string name;
59        DeclarationNode::StorageClass storageClass;
60        LinkageSpec::Spec linkage;
61        bool isInline, isNoreturn;
62        UniqueId uniqueId;
63        bool extension = false;
64};
65
66class DeclarationWithType : public Declaration {
67  public:
68        DeclarationWithType( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes );
69        DeclarationWithType( const DeclarationWithType &other );
70        virtual ~DeclarationWithType();
71
72        std::string get_mangleName() const { return mangleName; }
73        DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
74
75        std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
76
77        int get_scopeLevel() const { return scopeLevel; }
78        DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; }
79
80        ConstantExpr *get_asmName() const { return asmName; }
81        DeclarationWithType * set_asmName( ConstantExpr *newValue ) { asmName = newValue; return this; }
82
83        std::list< Attribute * >& get_attributes() { return attributes; }
84        const std::list< Attribute * >& get_attributes() const { return attributes; }
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};
100
101class ObjectDecl : public DeclarationWithType {
102        typedef DeclarationWithType Parent;
103  public:
104        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 );
105        ObjectDecl( const ObjectDecl &other );
106        virtual ~ObjectDecl();
107
108        virtual Type *get_type() const { return type; }
109        virtual void set_type(Type *newType) { type = newType; }
110
111        Initializer *get_init() const { return init; }
112        void set_init( Initializer *newValue ) { init = newValue; }
113
114        Expression *get_bitfieldWidth() const { return bitfieldWidth; }
115        void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; }
116
117        virtual ObjectDecl *clone() const { return new ObjectDecl( *this ); }
118        virtual void accept( Visitor &v ) { v.visit( this ); }
119        virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); }
120        virtual void print( std::ostream &os, int indent = 0 ) const;
121        virtual void printShort( std::ostream &os, int indent = 0 ) const;
122  private:
123        Type *type;
124        Initializer *init;
125        Expression *bitfieldWidth;
126};
127
128class FunctionDecl : public DeclarationWithType {
129        typedef DeclarationWithType Parent;
130  public:
131        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 * >() );
132        FunctionDecl( const FunctionDecl &other );
133        virtual ~FunctionDecl();
134
135        Type *get_type() const;
136        virtual void set_type(Type *);
137
138        FunctionType *get_functionType() const { return type; }
139        void set_functionType( FunctionType *newValue ) { type = newValue; }
140        CompoundStmt *get_statements() const { return statements; }
141        void set_statements( CompoundStmt *newValue ) { statements = newValue; }
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};
152
153class NamedTypeDecl : public Declaration {
154        typedef Declaration Parent;
155  public:
156        NamedTypeDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type );
157        NamedTypeDecl( const NamedTypeDecl &other );
158        virtual ~NamedTypeDecl();
159
160        Type *get_base() const { return base; }
161        void set_base( Type *newValue ) { base = newValue; }
162        std::list< TypeDecl* >& get_parameters() { return parameters; }
163        std::list< DeclarationWithType* >& get_assertions() { return assertions; }
164
165        virtual NamedTypeDecl *clone() const = 0;
166        virtual void print( std::ostream &os, int indent = 0 ) const;
167        virtual void printShort( std::ostream &os, int indent = 0 ) const;
168  protected:
169        virtual std::string typeString() const = 0;
170  private:
171        Type *base;
172        std::list< TypeDecl* > parameters;
173        std::list< DeclarationWithType* > assertions;
174};
175
176class TypeDecl : public NamedTypeDecl {
177        typedef NamedTypeDecl Parent;
178  public:
179        enum Kind { Any, Dtype, Ftype, Ttype };
180        /// Data extracted from a type decl
181        struct Data {
182                TypeDecl::Kind kind;
183                bool isComplete;
184                Data() : kind( (TypeDecl::Kind)-1 ), isComplete( false ) {}
185                Data( TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {}
186                Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {}
187                bool operator==(const Data & other) const { return kind == other.kind && isComplete == other.isComplete; }
188                bool operator!=(const Data & other) const { return !(*this == other);}
189        };
190
191        TypeDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type, Kind kind );
192        TypeDecl( const TypeDecl &other );
193
194        Kind get_kind() const { return kind; }
195
196        bool isComplete() const { return kind == Any || sized; }
197        bool get_sized() const { return sized; }
198        TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
199
200        virtual TypeDecl *clone() const { return new TypeDecl( *this ); }
201        virtual void accept( Visitor &v ) { v.visit( this ); }
202        virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
203  private:
204        virtual std::string typeString() const;
205        Kind kind;
206        bool sized;
207};
208
209class TypedefDecl : public NamedTypeDecl {
210        typedef NamedTypeDecl Parent;
211  public:
212        TypedefDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type ) : Parent( name, sc, type ) {}
213        TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
214
215        virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); }
216        virtual void accept( Visitor &v ) { v.visit( this ); }
217        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
218  private:
219        virtual std::string typeString() const;
220};
221
222class AggregateDecl : public Declaration {
223        typedef Declaration Parent;
224  public:
225        AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >() );
226        AggregateDecl( const AggregateDecl &other );
227        virtual ~AggregateDecl();
228
229        std::list<Declaration*>& get_members() { return members; }
230        std::list<TypeDecl*>& get_parameters() { return parameters; }
231
232        std::list< Attribute * >& get_attributes() { return attributes; }
233        const std::list< Attribute * >& get_attributes() const { return attributes; }
234
235        bool has_body() const { return body; }
236        AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
237
238        virtual void print( std::ostream &os, int indent = 0 ) const;
239        virtual void printShort( std::ostream &os, int indent = 0 ) const;
240  protected:
241        virtual std::string typeString() const = 0;
242
243  private:
244        std::list<Declaration*> members;
245        std::list<TypeDecl*> parameters;
246        bool body;
247        std::list< Attribute * > attributes;
248};
249
250class StructDecl : public AggregateDecl {
251        typedef AggregateDecl Parent;
252  public:
253        StructDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >() ) : Parent( name, attributes ) {}
254        StructDecl( const StructDecl &other ) : Parent( other ) {}
255
256        virtual StructDecl *clone() const { return new StructDecl( *this ); }
257        virtual void accept( Visitor &v ) { v.visit( this ); }
258        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
259  private:
260        virtual std::string typeString() const;
261};
262
263class UnionDecl : public AggregateDecl {
264        typedef AggregateDecl Parent;
265  public:
266        UnionDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >() ) : Parent( name, attributes ) {}
267        UnionDecl( const UnionDecl &other ) : Parent( other ) {}
268
269        virtual UnionDecl *clone() const { return new UnionDecl( *this ); }
270        virtual void accept( Visitor &v ) { v.visit( this ); }
271        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
272  private:
273        virtual std::string typeString() const;
274};
275
276class EnumDecl : public AggregateDecl {
277        typedef AggregateDecl Parent;
278  public:
279        EnumDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >() ) : Parent( name, attributes ) {}
280        EnumDecl( const EnumDecl &other ) : Parent( other ) {}
281
282        virtual EnumDecl *clone() const { return new EnumDecl( *this ); }
283        virtual void accept( Visitor &v ) { v.visit( this ); }
284        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
285  private:
286        virtual std::string typeString() const;
287};
288
289class TraitDecl : public AggregateDecl {
290        typedef AggregateDecl Parent;
291  public:
292        TraitDecl( const std::string &name, const std::list< Attribute * > & attributes ) : Parent( name ) {
293                assertf( attributes.empty(), "attribute unsupported for traits" );
294        }
295        TraitDecl( const TraitDecl &other ) : Parent( other ) {}
296
297        virtual TraitDecl *clone() const { return new TraitDecl( *this ); }
298        virtual void accept( Visitor &v ) { v.visit( this ); }
299        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
300  private:
301        virtual std::string typeString() const;
302};
303
304class AsmDecl : public Declaration {
305  public:
306        AsmDecl( AsmStmt *stmt );
307        AsmDecl( const AsmDecl &other );
308        virtual ~AsmDecl();
309
310        AsmStmt *get_stmt() { return stmt; }
311        void set_stmt( AsmStmt *newValue ) { stmt = newValue; }
312
313        virtual AsmDecl *clone() const { return new AsmDecl( *this ); }
314        virtual void accept( Visitor &v ) { v.visit( this ); }
315        virtual AsmDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
316        virtual void print( std::ostream &os, int indent = 0 ) const;
317        virtual void printShort( std::ostream &os, int indent = 0 ) const;
318  private:
319        AsmStmt *stmt;
320};
321
322std::ostream & operator<<( std::ostream & out, const Declaration * decl );
323std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
324
325#endif // DECLARATION_H
326
327// Local Variables: //
328// tab-width: 4 //
329// mode: c++ //
330// compile-command: "make install" //
331// End: //
Note: See TracBrowser for help on using the repository browser.