source: src/AST/Decl.hpp @ 1fb7bfd

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 1fb7bfd was 24afc53, checked in by Aaron Moss <a3moss@…>, 5 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

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