source: src/AST/Decl.hpp @ c36a419

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

Removed Decl::fromId as it was unused. There are a few places that use uniqueId directly.

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