source: src/AST/Decl.hpp @ ab5c0008

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

move enum Aggregate from DeclarationNode? to AggregateDecl?, add control-keyword field-dereference to replace control-keyword cast

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