source: src/AST/Decl.hpp @ 37cdd97

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 37cdd97 was 07de76b, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

remove file TypeVar?.h* and put TypeVar::Kind into TypeDecl?, move LinkageSpec?.* from directory Parse to SynTree?

  • Property mode set to 100644
File size: 12.0 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// Decl.hpp --
8//
9// Author           : Aaron B. Moss
10// Created On       : Thu May 9 10:00:00 2019
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Dec 13 17:38:33 2019
13// Update Count     : 29
14//
15
16#pragma once
17
18#include <iosfwd>
19#include <string>              // for string, to_string
20#include <unordered_map>
21#include <vector>
22#include <algorithm>
23
24#include "FunctionSpec.hpp"
25#include "Fwd.hpp"             // for UniqueId
26#include "LinkageSpec.hpp"
27#include "Node.hpp"            // for ptr, readonly
28#include "ParseNode.hpp"
29#include "StorageClasses.hpp"
30#include "Visitor.hpp"
31#include "Common/utility.h"
32#include "Common/SemanticError.h"                                               // error_str
33
34// Must be included in *all* AST classes; should be #undef'd at the end of the file
35#define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node);
36
37namespace ast {
38
39/// Base declaration class
40class Decl : public ParseNode {
41public:
42        std::string name;
43        Storage::Classes storage;
44        Linkage::Spec linkage;
45        UniqueId uniqueId = 0;
46        bool extension = false;
47
48        Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
49                Linkage::Spec linkage )
50        : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {}
51
52        Decl* set_extension( bool ex ) { extension = ex; return this; }
53
54        /// Ensures this node has a unique ID
55        void fixUniqueId();
56        /// Get canonical declaration for unique ID
57        static readonly<Decl> fromId( UniqueId id );
58
59        const Decl * accept( Visitor & v ) const override = 0;
60private:
61        Decl * clone() const override = 0;
62        MUTATE_FRIEND
63};
64
65/// Typed declaration base class
66class DeclWithType : public Decl {
67public:
68        /// Represents the type with all types and typedefs expanded.
69        /// This field is generated by SymTab::Validate::Pass2
70        std::string mangleName;
71        /// Stores the scope level at which the variable was declared.
72        /// Used to access shadowed identifiers.
73        int scopeLevel = 0;
74
75        std::vector<ptr<Attribute>> attributes;
76        Function::Specs funcSpec;
77        ptr<Expr> asmName;
78        bool isDeleted = false;
79
80        DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
81                Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
82        : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
83                funcSpec(fs), asmName() {}
84
85        std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
86
87        /// Get type of this declaration. May be generated by subclass
88        virtual const Type * get_type() const = 0;
89        /// Set type of this declaration. May be verified by subclass
90        virtual void set_type(Type *) = 0;
91
92        const DeclWithType * accept( Visitor & v ) const override = 0;
93private:
94        DeclWithType * clone() const override = 0;
95        MUTATE_FRIEND
96};
97
98/// Object declaration `Foo foo = 42;`
99class ObjectDecl final : public DeclWithType {
100public:
101        ptr<Type> type;
102        ptr<Init> init;
103        ptr<Expr> bitfieldWidth;
104
105        ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type,
106                const Init * init = nullptr, Storage::Classes storage = {},
107                Linkage::Spec linkage = Linkage::C, const Expr * bitWd = nullptr,
108                std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
109        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
110          init( init ), bitfieldWidth( bitWd ) {}
111
112        const Type* get_type() const override { return type; }
113        void set_type( Type * ty ) override { type = ty; }
114
115        const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
116private:
117        ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
118        MUTATE_FRIEND
119};
120
121/// Object declaration `int foo()`
122class FunctionDecl : public DeclWithType {
123public:
124        ptr<FunctionType> type;
125        ptr<CompoundStmt> stmts;
126        std::vector< ptr<Expr> > withExprs;
127
128        FunctionDecl( const CodeLocation & loc, const std::string & name, FunctionType * type,
129                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,
130                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {})
131        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
132          stmts( stmts ) {}
133
134        const Type * get_type() const override;
135        void set_type(Type * t) override;
136
137        bool has_body() const { return stmts; }
138
139        const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
140private:
141        FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
142        MUTATE_FRIEND
143};
144
145/// Base class for named type aliases
146class NamedTypeDecl : public Decl {
147public:
148        ptr<Type> base;
149        std::vector<ptr<TypeDecl>> params;
150        std::vector<ptr<DeclWithType>> assertions;
151
152        NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
153                Type* b, Linkage::Spec spec = Linkage::Cforall )
154        : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {}
155
156        /// Produces a name for the kind of alias
157        virtual const char * typeString() const = 0;
158
159private:
160        NamedTypeDecl* clone() const override = 0;
161        MUTATE_FRIEND
162};
163
164/// Cforall type variable: `dtype T`
165class TypeDecl final : public NamedTypeDecl {
166  public:
167        enum Kind { Dtype, Otype, Ftype, Ttype, NUMBER_OF_KINDS };
168
169        Kind kind;
170        bool sized;
171        ptr<Type> init;
172
173        /// Data extracted from a type decl
174        struct Data {
175                Kind kind;
176                bool isComplete;
177
178                Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
179                Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
180                Data( Kind k, bool c ) : kind( k ), isComplete( c ) {}
181                Data( const Data & d1, const Data & d2 )
182                        : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
183
184                bool operator==( const Data & o ) const { return kind == o.kind && isComplete == o.isComplete; }
185                bool operator!=( const Data & o ) const { return !(*this == o); }
186        };
187
188        TypeDecl( const CodeLocation & loc, const std::string & name, Storage::Classes storage, Type * b,
189                          Kind k, bool s, Type * i = nullptr )
190                : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == Ttype || s ),
191                init( i ) {}
192
193        const char * typeString() const override;
194        /// Produces a name for generated code
195        const char * genTypeString() const;
196
197        /// convenience accessor to match Type::isComplete()
198        bool isComplete() { return sized; }
199
200        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
201  private:
202        TypeDecl * clone() const override { return new TypeDecl{ *this }; }
203        MUTATE_FRIEND
204};
205
206std::ostream & operator<< ( std::ostream &, const TypeDecl::Data & );
207
208/// C-style typedef `typedef Foo Bar`
209class TypedefDecl final : public NamedTypeDecl {
210public:
211        TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
212                Type* b, Linkage::Spec spec = Linkage::Cforall )
213        : NamedTypeDecl( loc, name, storage, b, spec ) {}
214
215        const char * typeString() const override { return "typedef"; }
216
217        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
218private:
219        TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
220        MUTATE_FRIEND
221};
222
223/// Aggregate type declaration base class
224class AggregateDecl : public Decl {
225public:
226        enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
227        static const char * aggrString( Aggregate aggr );
228
229        std::vector<ptr<Decl>> members;
230        std::vector<ptr<TypeDecl>> params;
231        std::vector<ptr<Attribute>> attributes;
232        bool body = false;
233        readonly<AggregateDecl> parent = {};
234
235        AggregateDecl( const CodeLocation& loc, const std::string& name,
236                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
237        : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
238          attributes( std::move(attrs) ) {}
239
240        AggregateDecl* set_body( bool b ) { body = b; return this; }
241
242        /// Produces a name for the kind of aggregate
243        virtual const char * typeString() const = 0;
244
245private:
246        AggregateDecl * clone() const override = 0;
247        MUTATE_FRIEND
248};
249
250/// struct declaration `struct Foo { ... };`
251class StructDecl final : public AggregateDecl {
252public:
253        Aggregate kind;
254
255        StructDecl( const CodeLocation& loc, const std::string& name,
256                Aggregate kind = Struct,
257                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
258        : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
259
260        bool is_coroutine() { return kind == Coroutine; }
261        bool is_monitor() { return kind == Monitor; }
262        bool is_thread() { return kind == Thread; }
263
264        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
265
266        const char * typeString() const override { return aggrString( kind ); }
267
268private:
269        StructDecl * clone() const override { return new StructDecl{ *this }; }
270        MUTATE_FRIEND
271};
272
273/// union declaration `union Foo { ... };`
274class UnionDecl final : public AggregateDecl {
275public:
276        UnionDecl( const CodeLocation& loc, const std::string& name,
277                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
278        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
279
280        const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
281
282        const char * typeString() const override { return aggrString( Union ); }
283
284private:
285        UnionDecl * clone() const override { return new UnionDecl{ *this }; }
286        MUTATE_FRIEND
287};
288
289/// enum declaration `enum Foo { ... };`
290class EnumDecl final : public AggregateDecl {
291public:
292        EnumDecl( const CodeLocation& loc, const std::string& name,
293                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
294        : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
295
296        /// gets the integer value for this enumerator, returning true iff value found
297        bool valueOf( const Decl * enumerator, long long& value ) const;
298
299        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
300
301        const char * typeString() const override { return aggrString( Enum ); }
302
303private:
304        EnumDecl * clone() const override { return new EnumDecl{ *this }; }
305        MUTATE_FRIEND
306
307        /// Map from names to enumerator values; kept private for lazy initialization
308        mutable std::unordered_map< std::string, long long > enumValues;
309};
310
311/// trait declaration `trait Foo( ... ) { ... };`
312class TraitDecl final : public AggregateDecl {
313public:
314        TraitDecl( const CodeLocation& loc, const std::string& name,
315                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
316        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
317
318        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
319
320        const char * typeString() const override { return "trait"; }
321
322private:
323        TraitDecl * clone() const override { return new TraitDecl{ *this }; }
324        MUTATE_FRIEND
325};
326
327/// With statement `with (...) ...`
328class WithStmt final : public Decl {
329public:
330        std::vector<ptr<Expr>> exprs;
331        ptr<Stmt> stmt;
332
333        WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
334        : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
335
336        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
337private:
338        WithStmt * clone() const override { return new WithStmt{ *this }; }
339        MUTATE_FRIEND
340};
341
342class AsmDecl : public Decl {
343public:
344        ptr<AsmStmt> stmt;
345
346        AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
347        : Decl( loc, "", {}, {} ), stmt(stmt) {}
348
349        const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
350private:
351        AsmDecl * clone() const override { return new AsmDecl( *this ); }
352        MUTATE_FRIEND
353};
354
355class StaticAssertDecl : public Decl {
356public:
357        ptr<Expr> cond;
358        ptr<ConstantExpr> msg;   // string literal
359
360        StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
361        : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
362
363        const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
364private:
365        StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
366        MUTATE_FRIEND
367};
368
369}
370
371#undef MUTATE_FRIEND
372
373// Local Variables: //
374// tab-width: 4 //
375// mode: c++ //
376// compile-command: "make install" //
377// End: //
Note: See TracBrowser for help on using the repository browser.