source: src/AST/Decl.hpp @ c7f9f53

Last change on this file since c7f9f53 was c7f9f53, checked in by Andrew Beach <ajbeach@…>, 6 months ago

Moved include from Decl header. Removed some old ast code from the Parser.

  • Property mode set to 100644
File size: 14.9 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 : Andrew Beach
12// Last Modified On : Wed Apr  5 10:42:00 2023
13// Update Count     : 35
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
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
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        std::string mangleName;
69        /// Stores the scope level at which the variable was declared.
70        /// Used to access shadowed identifiers.
71        int scopeLevel = 0;
72
73        std::vector<ptr<Attribute>> attributes;
74        Function::Specs funcSpec;
75        ptr<Expr> asmName;
76        bool isDeleted = false;
77        bool isTypeFixed = 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( const 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::Cforall, 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( const 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/// Function variable arguments flag
121enum ArgumentFlag { FixedArgs, VariableArgs };
122
123/// Object declaration `int foo()`
124class FunctionDecl final : public DeclWithType {
125public:
126        std::vector<ptr<TypeDecl>> type_params;
127        std::vector<ptr<DeclWithType>> assertions;
128        std::vector<ptr<DeclWithType>> params;
129        std::vector<ptr<DeclWithType>> returns;
130        // declared type, derived from parameter declarations
131        ptr<FunctionType> type;
132        /// Null for the forward declaration of a function.
133        ptr<CompoundStmt> stmts;
134        std::vector< ptr<Expr> > withExprs;
135
136        /// Monomorphic Function Constructor:
137        FunctionDecl( const CodeLocation & locaction, const std::string & name,
138                std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
139                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
140                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
141
142        /// Polymorphic Function Constructor:
143        FunctionDecl( const CodeLocation & location, const std::string & name,
144                std::vector<ptr<TypeDecl>>&& forall, std::vector<ptr<DeclWithType>>&& assertions,
145                std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
146                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
147                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
148
149        const Type * get_type() const override;
150        void set_type( const Type * t ) override;
151
152        bool has_body() const { return stmts; }
153
154        const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
155private:
156        FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
157        MUTATE_FRIEND
158};
159
160/// Base class for named type aliases
161class NamedTypeDecl : public Decl {
162public:
163        ptr<Type> base;
164        std::vector<ptr<DeclWithType>> assertions;
165
166        NamedTypeDecl(
167                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
168                const Type * b, Linkage::Spec spec = Linkage::Cforall )
169        : Decl( loc, name, storage, spec ), base( b ), assertions() {}
170
171        /// Produces a name for the kind of alias
172        virtual const char * typeString() const = 0;
173
174private:
175        NamedTypeDecl* clone() const override = 0;
176        MUTATE_FRIEND
177};
178
179/// Cforall type variable: `dtype T`
180class TypeDecl final : public NamedTypeDecl {
181  public:
182        enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS };
183
184        Kind kind;
185        bool sized;
186        ptr<Type> init;
187
188        TypeDecl(
189                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
190                const Type * b, TypeDecl::Kind k, bool s, const Type * i = nullptr )
191        : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeDecl::Ttype || s ),
192          init( i ) {}
193
194        const char * typeString() const override;
195        /// Produces a name for generated code
196        const char * genTypeString() const;
197
198        /// convenience accessor to match Type::isComplete()
199        bool isComplete() const { return sized; }
200
201        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
202  private:
203        TypeDecl * clone() const override { return new TypeDecl{ *this }; }
204        MUTATE_FRIEND
205};
206
207/// Data extracted from a TypeDecl.
208struct TypeData {
209        TypeDecl::Kind kind;
210        bool isComplete;
211
212        TypeData() : kind( TypeDecl::NUMBER_OF_KINDS ), isComplete( false ) {}
213        TypeData( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
214        TypeData( TypeDecl::Kind k, bool c ) : kind( k ), isComplete( c ) {}
215        TypeData( const TypeData & d1, const TypeData & d2 )
216                : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
217
218        bool operator==( const TypeData & o ) const { return kind == o.kind && isComplete == o.isComplete; }
219        bool operator!=( const TypeData & o ) const { return !(*this == o); }
220};
221
222std::ostream & operator<< ( std::ostream &, const TypeData & );
223
224/// C-style typedef `typedef Foo Bar`
225class TypedefDecl final : public NamedTypeDecl {
226public:
227        TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
228                Type* b, Linkage::Spec spec = Linkage::Cforall )
229        : NamedTypeDecl( loc, name, storage, b, spec ) {}
230
231        const char * typeString() const override { return "typedef"; }
232
233        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
234private:
235        TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
236        MUTATE_FRIEND
237};
238
239/// Aggregate type declaration base class
240class AggregateDecl : public Decl {
241public:
242        enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
243        static const char * aggrString( Aggregate aggr );
244
245        std::vector<ptr<Decl>> members;
246        std::vector<ptr<TypeDecl>> params;
247        std::vector<ptr<Attribute>> attributes;
248        bool body = false;
249        readonly<AggregateDecl> parent = {};
250
251        AggregateDecl( const CodeLocation& loc, const std::string& name,
252                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
253        : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
254          attributes( std::move(attrs) ) {}
255
256        AggregateDecl* set_body( bool b ) { body = b; return this; }
257
258        /// Produces a name for the kind of aggregate
259        virtual const char * typeString() const = 0;
260
261private:
262        AggregateDecl * clone() const override = 0;
263        MUTATE_FRIEND
264};
265
266/// struct declaration `struct Foo { ... };`
267class StructDecl final : public AggregateDecl {
268public:
269        Aggregate kind;
270
271        StructDecl( const CodeLocation& loc, const std::string& name,
272                Aggregate kind = Struct,
273                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
274        : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
275
276        bool is_coroutine() const { return kind == Coroutine; }
277        bool is_generator() const { return kind == Generator; }
278        bool is_monitor  () const { return kind == Monitor  ; }
279        bool is_thread   () const { return kind == Thread   ; }
280
281        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
282
283        const char * typeString() const override { return aggrString( kind ); }
284
285private:
286        StructDecl * clone() const override { return new StructDecl{ *this }; }
287        MUTATE_FRIEND
288};
289
290/// union declaration `union Foo { ... };`
291class UnionDecl final : public AggregateDecl {
292public:
293        UnionDecl( const CodeLocation& loc, const std::string& name,
294                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
295        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
296
297        const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
298
299        const char * typeString() const override { return aggrString( Union ); }
300
301private:
302        UnionDecl * clone() const override { return new UnionDecl{ *this }; }
303        MUTATE_FRIEND
304};
305
306/// enum declaration `enum Foo { ... };`
307class EnumDecl final : public AggregateDecl {
308public:
309        // isTyped indicated if the enum has a declaration like:
310        // enum (type_optional) Name {...}
311        bool isTyped;
312        // if isTyped == true && base.get() == nullptr, it is a "void" type enum
313        ptr<Type> base;
314        enum class EnumHiding { Visible, Hide } hide;
315
316        EnumDecl( const CodeLocation& loc, const std::string& name, bool isTyped = false,
317                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall,
318                Type const * base = nullptr, EnumHiding hide = EnumHiding::Hide,
319                std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() )
320        : AggregateDecl( loc, name, std::move(attrs), linkage ), isTyped(isTyped), base(base), hide(hide), enumValues(enumValues) {}
321
322        /// gets the integer value for this enumerator, returning true iff value found
323        // Maybe it is not used in producing the enum value
324        bool valueOf( const Decl * enumerator, long long& value ) const;
325
326        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
327
328        const char * typeString() const override { return aggrString( Enum ); }
329
330
331private:
332        EnumDecl * clone() const override { return new EnumDecl{ *this }; }
333        MUTATE_FRIEND
334
335        /// Map from names to enumerator values; kept private for lazy initialization
336        mutable std::unordered_map< std::string, long long > enumValues;
337};
338
339/// trait declaration `trait Foo( ... ) { ... };`
340class TraitDecl final : public AggregateDecl {
341public:
342        TraitDecl( const CodeLocation& loc, const std::string& name,
343                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
344        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
345
346        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
347
348        const char * typeString() const override { return "trait"; }
349
350private:
351        TraitDecl * clone() const override { return new TraitDecl{ *this }; }
352        MUTATE_FRIEND
353};
354
355/// With statement `with (...) ...`
356/// This is a statement lexically, but a Decl is needed for the SymbolTable.
357class WithStmt final : public Decl {
358public:
359        std::vector<ptr<Expr>> exprs;
360        ptr<Stmt> stmt;
361
362        WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
363        : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
364
365        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
366private:
367        WithStmt * clone() const override { return new WithStmt{ *this }; }
368        MUTATE_FRIEND
369};
370
371/// Assembly declaration: `asm ... ( "..." : ... )`
372class AsmDecl final : public Decl {
373public:
374        ptr<AsmStmt> stmt;
375
376        AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
377        : Decl( loc, "", {}, Linkage::C ), stmt(stmt) {}
378
379        const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
380private:
381        AsmDecl * clone() const override { return new AsmDecl( *this ); }
382        MUTATE_FRIEND
383};
384
385/// C-preprocessor directive `#...`
386class DirectiveDecl final : public Decl {
387public:
388        ptr<DirectiveStmt> stmt;
389
390        DirectiveDecl( const CodeLocation & loc, DirectiveStmt * stmt )
391        : Decl( loc, "", {}, Linkage::C ), stmt(stmt) {}
392
393        const DirectiveDecl * accept( Visitor & v ) const override { return v.visit( this ); }
394private:
395        DirectiveDecl * clone() const override { return new DirectiveDecl( *this ); }
396        MUTATE_FRIEND
397};
398
399/// Static Assertion `_Static_assert( ... , ... );`
400class StaticAssertDecl final : public Decl {
401public:
402        ptr<Expr> cond;
403        ptr<ConstantExpr> msg;   // string literal
404
405        StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
406        : Decl( loc, "", {}, Linkage::C ), cond( condition ), msg( msg ) {}
407
408        const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
409private:
410        StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
411        MUTATE_FRIEND
412};
413
414/// Inline Member Declaration `inline TypeName;`
415class InlineMemberDecl final : public DeclWithType {
416public:
417        ptr<Type> type;
418
419        InlineMemberDecl( const CodeLocation & loc, const std::string & name, const Type * type,
420                Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
421                std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
422        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ) {}
423
424        const Type * get_type() const override { return type; }
425        void set_type( const Type * ty ) override { type = ty; }
426
427        const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
428private:
429        InlineMemberDecl * clone() const override { return new InlineMemberDecl{ *this }; }
430        MUTATE_FRIEND
431};
432
433}
434
435#undef MUTATE_FRIEND
436
437// Local Variables: //
438// tab-width: 4 //
439// mode: c++ //
440// compile-command: "make install" //
441// End: //
Note: See TracBrowser for help on using the repository browser.