source: src/SynTree/Declaration.h @ 5802a4f

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 5802a4f was 2c57025, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

add support for built-in sized trait which decouples size/alignment information from otype parameters, add test for sized trait

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