source: src/AST/Decl.hpp @ f6cc734e

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since f6cc734e was 99da267, checked in by Michael Brooks <mlbrooks@…>, 5 years ago

Running a deep-copy on FunctionType? at RenameVars? time. This manual action addresses the currently-problematic occurrence of 'the transitivity problem.' Resolution of bootloader (and thus builtins) is now completing. The change on RenameVars?.cc makes this happen. Changes on AST/*.hpp finish making the deep-copy framework compile.

  • 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// Decl.hpp --
8//
9// Author           : Aaron B. Moss
10// Created On       : Thu May 9 10:00:00 2019
11// Last Modified By : Aaron B. Moss
12// Last Modified On : Thu May 9 10:00:00 2019
13// Update Count     : 1
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 \
35    template<typename node_t> friend node_t * mutate(const node_t * node); \
36        template<typename node_t> friend node_t * shallowCopy(const node_t * node);
37
38namespace ast {
39
40/// Base declaration class
41class Decl : public ParseNode {
42public:
43        std::string name;
44        Storage::Classes storage;
45        Linkage::Spec linkage;
46        UniqueId uniqueId = 0;
47        bool extension = false;
48
49        Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
50                Linkage::Spec linkage )
51        : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {}
52
53        Decl* set_extension( bool ex ) { extension = ex; return this; }
54
55        /// Ensures this node has a unique ID
56        void fixUniqueId();
57        /// Get canonical declaration for unique ID
58        static readonly<Decl> fromId( UniqueId id );
59
60        const Decl * accept( Visitor & v ) const override = 0;
61private:
62        Decl * clone() const override = 0;
63        MUTATE_FRIEND
64};
65
66/// Typed declaration base class
67class DeclWithType : public Decl {
68public:
69        /// Represents the type with all types and typedefs expanded.
70        /// This field is generated by SymTab::Validate::Pass2
71        std::string mangleName;
72        /// Stores the scope level at which the variable was declared.
73        /// Used to access shadowed identifiers.
74        int scopeLevel = 0;
75
76        std::vector<ptr<Attribute>> attributes;
77        Function::Specs funcSpec;
78        ptr<Expr> asmName;
79        bool isDeleted = false;
80
81        DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
82                Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
83        : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
84                funcSpec(fs), asmName() {}
85
86        std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
87
88        /// Get type of this declaration. May be generated by subclass
89        virtual const Type * get_type() const = 0;
90        /// Set type of this declaration. May be verified by subclass
91        virtual void set_type( const Type * ) = 0;
92
93        const DeclWithType * accept( Visitor & v ) const override = 0;
94private:
95        DeclWithType * clone() const override = 0;
96        MUTATE_FRIEND
97};
98
99/// Object declaration `Foo foo = 42;`
100class ObjectDecl final : public DeclWithType {
101public:
102        ptr<Type> type;
103        ptr<Init> init;
104        ptr<Expr> bitfieldWidth;
105
106        ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type, 
107                const Init * init = nullptr, Storage::Classes storage = {}, 
108                Linkage::Spec linkage = Linkage::C, const Expr * bitWd = nullptr, 
109                std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
110        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
111          init( init ), bitfieldWidth( bitWd ) {}
112
113        const Type* get_type() const override { return type; }
114        void set_type( const Type * ty ) override { type = ty; }
115
116        const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
117private:
118        ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
119        MUTATE_FRIEND
120};
121
122/// Object declaration `int foo()`
123class FunctionDecl : public DeclWithType {
124public:
125        ptr<FunctionType> type;
126        ptr<CompoundStmt> stmts;
127        std::vector< ptr<Expr> > withExprs;
128
129        FunctionDecl( const CodeLocation & loc, const std::string &name, FunctionType * type,
130                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,
131                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {})
132        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
133          stmts( stmts ) {}
134
135        const Type * get_type() const override;
136        void set_type( const Type * t ) override;
137
138        bool has_body() const { return stmts; }
139
140        const DeclWithType * accept( Visitor &v ) const override { return v.visit( this ); }
141private:
142        FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
143        MUTATE_FRIEND
144};
145
146/// Base class for named type aliases
147class NamedTypeDecl : public Decl {
148public:
149        ptr<Type> base;
150        std::vector<ptr<TypeDecl>> params;
151        std::vector<ptr<DeclWithType>> assertions;
152
153        NamedTypeDecl( 
154                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
155                const Type * b, Linkage::Spec spec = Linkage::Cforall )
156        : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {}
157
158        /// Produces a name for the kind of alias
159        virtual std::string typeString() const = 0;
160
161private:
162        NamedTypeDecl* clone() const override = 0;
163        MUTATE_FRIEND
164};
165
166/// Cforall type variable: `dtype T`
167class TypeDecl final : public NamedTypeDecl {
168public:
169        TypeVar::Kind kind;
170        bool sized;
171        ptr<Type> init;
172
173        /// Data extracted from a type decl
174        struct Data {
175                TypeVar::Kind kind;
176                bool isComplete;
177
178                Data() : kind( (TypeVar::Kind)-1 ), isComplete( false ) {}
179                Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
180                Data( TypeVar::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 {
185                        return kind == o.kind && isComplete == o.isComplete;
186                }
187                bool operator!= ( const Data & o ) const { return !(*this == o); }
188        };
189
190        TypeDecl( 
191                const CodeLocation & loc, const std::string & name, Storage::Classes storage, 
192                const Type * b, TypeVar::Kind k, bool s, const Type * i = nullptr )
193        : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeVar::Ttype || s ),
194          init( i ) {}
195
196        std::string typeString() const override;
197        /// Produces a name for generated code
198        std::string genTypeString() const;
199
200        /// convenience accessor to match Type::isComplete()
201        bool isComplete() { return sized; }
202
203        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
204private:
205        TypeDecl * clone() const override { return new TypeDecl{ *this }; }
206        MUTATE_FRIEND
207};
208
209std::ostream & operator<< ( std::ostream &, const TypeDecl::Data & );
210
211/// C-style typedef `typedef Foo Bar`
212class TypedefDecl final : public NamedTypeDecl {
213public:
214        TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
215                Type* b, Linkage::Spec spec = Linkage::Cforall )
216        : NamedTypeDecl( loc, name, storage, b, spec ) {}
217
218        std::string typeString() const override { return "typedef"; }
219
220        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
221private:
222        TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
223        MUTATE_FRIEND
224};
225
226/// Aggregate type declaration base class
227class AggregateDecl : public Decl {
228public:
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 std::string 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        DeclarationNode::Aggregate kind;
254
255        StructDecl( const CodeLocation& loc, const std::string& name,
256                DeclarationNode::Aggregate kind = DeclarationNode::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 == DeclarationNode::Coroutine; }
261        bool is_monitor() { return kind == DeclarationNode::Monitor; }
262        bool is_thread() { return kind == DeclarationNode::Thread; }
263
264        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
265
266        std::string typeString() const override { return "struct"; }
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        std::string typeString() const override { return "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        std::string typeString() const override { return "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        std::string typeString() const override { return "trait"; }
321
322private:
323        TraitDecl * clone() const override { return new TraitDecl{ *this }; }
324        MUTATE_FRIEND
325};
326
327class AsmDecl : public Decl {
328public:
329        ptr<AsmStmt> stmt;
330
331        AsmDecl( const CodeLocation & loc, AsmStmt *stmt )
332        : Decl( loc, "", {}, {} ), stmt(stmt) {}
333
334        const AsmDecl * accept( Visitor &v ) const override { return v.visit( this ); }
335private:
336        AsmDecl *clone() const override { return new AsmDecl( *this ); }
337        MUTATE_FRIEND
338};
339
340class StaticAssertDecl : public Decl {
341public:
342        ptr<Expr> cond;
343        ptr<ConstantExpr> msg;   // string literal
344
345        StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
346        : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
347
348        const StaticAssertDecl * accept( Visitor &v ) const override { return v.visit( this ); }
349private:
350        StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
351        MUTATE_FRIEND
352};
353
354}
355
356#undef MUTATE_FRIEND
357
358// Local Variables: //
359// tab-width: 4 //
360// mode: c++ //
361// compile-command: "make install" //
362// End: //
Note: See TracBrowser for help on using the repository browser.