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 | // DeclarationNode.hpp -- |
---|
8 | // |
---|
9 | // Author : Andrew Beach |
---|
10 | // Created On : Wed Apr 5 11:38:00 2023 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Sat Feb 17 09:24:12 2024 |
---|
13 | // Update Count : 4 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include "ParseNode.hpp" |
---|
19 | |
---|
20 | struct TypeData; |
---|
21 | struct InitializerNode; |
---|
22 | |
---|
23 | struct DeclarationNode final : public ParseList<DeclarationNode> { |
---|
24 | static DeclarationNode * newFromTypeData( TypeData * ); |
---|
25 | static DeclarationNode * newStorageClass( ast::Storage::Classes ); |
---|
26 | static DeclarationNode * newFuncSpecifier( ast::Function::Specs ); |
---|
27 | static DeclarationNode * newFunction( const std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ); |
---|
28 | static DeclarationNode * newAggregate( ast::AggregateDecl::Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ); |
---|
29 | static DeclarationNode * newEnum( const std::string * name, DeclarationNode * constants, bool body, bool isCfa = false, DeclarationNode * base = nullptr, EnumHiding hiding = EnumHiding::Visible ); |
---|
30 | static DeclarationNode * newEnumConstant( const std::string * name, ExpressionNode * constant ); |
---|
31 | static DeclarationNode * newEnumValueGeneric( const std::string * name, InitializerNode * init ); |
---|
32 | static DeclarationNode * newEnumInLine( const std::string * name ); |
---|
33 | static DeclarationNode * newName( const std::string * ); |
---|
34 | static DeclarationNode * newTypeParam( ast::TypeDecl::Kind, const std::string * ); |
---|
35 | static DeclarationNode * newTrait( const std::string * name, DeclarationNode * params, DeclarationNode * asserts ); |
---|
36 | static DeclarationNode * newTraitUse( const std::string * name, ExpressionNode * params ); |
---|
37 | static DeclarationNode * newTypeDecl( const std::string * name, DeclarationNode * typeParams ); |
---|
38 | static DeclarationNode * newPointer( DeclarationNode * qualifiers, OperKinds kind ); |
---|
39 | static DeclarationNode * newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ); |
---|
40 | static DeclarationNode * newVarArray( DeclarationNode * qualifiers ); |
---|
41 | static DeclarationNode * newBitfield( ExpressionNode * size ); |
---|
42 | static DeclarationNode * newTuple( DeclarationNode * members ); |
---|
43 | static DeclarationNode * newTypeof( ExpressionNode * expr, bool basetypeof = false ); |
---|
44 | static DeclarationNode * newAttribute( const std::string *, ExpressionNode * expr = nullptr ); // gcc attributes |
---|
45 | static DeclarationNode * newDirectiveStmt( StatementNode * stmt ); // gcc external directive statement |
---|
46 | static DeclarationNode * newAsmStmt( StatementNode * stmt ); // gcc external asm statement |
---|
47 | static DeclarationNode * newStaticAssert( ExpressionNode * condition, ast::Expr * message ); |
---|
48 | |
---|
49 | DeclarationNode(); |
---|
50 | ~DeclarationNode(); |
---|
51 | DeclarationNode * clone() const override; |
---|
52 | |
---|
53 | DeclarationNode * addQualifiers( DeclarationNode * ); |
---|
54 | void checkQualifiers( const TypeData *, const TypeData * ); |
---|
55 | void checkSpecifiers( DeclarationNode * ); |
---|
56 | DeclarationNode * copySpecifiers( DeclarationNode *, bool = true ); |
---|
57 | DeclarationNode * addType( DeclarationNode *, bool = true ); |
---|
58 | DeclarationNode * addTypedef(); |
---|
59 | DeclarationNode * addEnumBase( DeclarationNode * ); |
---|
60 | DeclarationNode * addAssertions( DeclarationNode * ); |
---|
61 | DeclarationNode * addName( std::string * ); |
---|
62 | DeclarationNode * addAsmName( DeclarationNode * ); |
---|
63 | DeclarationNode * addBitfield( ExpressionNode * size ); |
---|
64 | DeclarationNode * addVarArgs(); |
---|
65 | DeclarationNode * addFunctionBody( StatementNode * body, ExpressionNode * with = nullptr ); |
---|
66 | DeclarationNode * addOldDeclList( DeclarationNode * list ); |
---|
67 | DeclarationNode * setBase( TypeData * newType ); |
---|
68 | DeclarationNode * copyAttribute( DeclarationNode * attr ); |
---|
69 | DeclarationNode * addPointer( DeclarationNode * qualifiers ); |
---|
70 | DeclarationNode * addArray( DeclarationNode * array ); |
---|
71 | DeclarationNode * addNewPointer( DeclarationNode * pointer ); |
---|
72 | DeclarationNode * addNewArray( DeclarationNode * array ); |
---|
73 | DeclarationNode * addParamList( DeclarationNode * list ); |
---|
74 | DeclarationNode * addIdList( DeclarationNode * list ); // old-style functions |
---|
75 | DeclarationNode * addInitializer( InitializerNode * init ); |
---|
76 | DeclarationNode * addTypeInitializer( DeclarationNode * init ); |
---|
77 | |
---|
78 | DeclarationNode * cloneType( std::string * newName ); |
---|
79 | DeclarationNode * cloneBaseType( DeclarationNode * newdecl, bool = true ); |
---|
80 | |
---|
81 | virtual void print( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const override; |
---|
82 | virtual void printList( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const override; |
---|
83 | |
---|
84 | ast::Decl * build() const; |
---|
85 | ast::Type * buildType() const; |
---|
86 | |
---|
87 | ast::Linkage::Spec get_linkage() const { return linkage; } |
---|
88 | DeclarationNode * extractAggregate() const; |
---|
89 | bool has_enumeratorValue() const { return (bool)enumeratorValue; } |
---|
90 | ExpressionNode * consume_enumeratorValue() const { return const_cast<DeclarationNode *>(this)->enumeratorValue.release(); } |
---|
91 | |
---|
92 | bool get_extension() const { return extension; } |
---|
93 | DeclarationNode * set_extension( bool exten ) { extension = exten; return this; } |
---|
94 | |
---|
95 | bool get_inLine() const { return inLine; } |
---|
96 | DeclarationNode * set_inLine( bool inL ) { inLine = inL; return this; } |
---|
97 | |
---|
98 | const std::string * name = nullptr; |
---|
99 | |
---|
100 | struct Variable_t { |
---|
101 | ast::TypeDecl::Kind tyClass; |
---|
102 | DeclarationNode * assertions; |
---|
103 | DeclarationNode * initializer; |
---|
104 | }; |
---|
105 | Variable_t variable; |
---|
106 | |
---|
107 | struct StaticAssert_t { |
---|
108 | ExpressionNode * condition; |
---|
109 | ast::Expr * message; |
---|
110 | }; |
---|
111 | StaticAssert_t assert; |
---|
112 | |
---|
113 | TypeData * type = nullptr; |
---|
114 | |
---|
115 | bool inLine = false; |
---|
116 | bool enumInLine = false; |
---|
117 | ast::Function::Specs funcSpecs; |
---|
118 | ast::Storage::Classes storageClasses; |
---|
119 | |
---|
120 | ExpressionNode * bitfieldWidth = nullptr; |
---|
121 | std::unique_ptr<ExpressionNode> enumeratorValue; |
---|
122 | bool hasEllipsis = false; |
---|
123 | ast::Linkage::Spec linkage; |
---|
124 | ast::Expr * asmName = nullptr; |
---|
125 | std::vector<ast::ptr<ast::Attribute>> attributes; |
---|
126 | InitializerNode * initializer = nullptr; |
---|
127 | bool extension = false; |
---|
128 | std::string error; |
---|
129 | StatementNode * asmStmt = nullptr; |
---|
130 | StatementNode * directiveStmt = nullptr; |
---|
131 | |
---|
132 | static UniqueName anonymous; |
---|
133 | }; // DeclarationNode |
---|
134 | |
---|
135 | static inline ast::Type * maybeMoveBuildType( const DeclarationNode * orig ) { |
---|
136 | ast::Type * ret = orig ? orig->buildType() : nullptr; |
---|
137 | delete orig; |
---|
138 | return ret; |
---|
139 | } |
---|
140 | |
---|
141 | // This generic buildList is here along side its overloads. |
---|
142 | template<typename AstType, typename NodeType, |
---|
143 | template<typename, typename...> class Container, typename... Args> |
---|
144 | void buildList( NodeType * firstNode, |
---|
145 | Container<ast::ptr<AstType>, Args...> & output ) { |
---|
146 | SemanticErrorException errors; |
---|
147 | std::back_insert_iterator<Container<ast::ptr<AstType>, Args...>> out( output ); |
---|
148 | |
---|
149 | for ( NodeType * cur = firstNode ; cur ; cur = cur->next ) { |
---|
150 | try { |
---|
151 | AstType * node = dynamic_cast<AstType *>( maybeBuild( cur ) ); |
---|
152 | assertf( node, "buildList: Did not build node of correct type." ); |
---|
153 | *out++ = node; |
---|
154 | } catch ( SemanticErrorException & e ) { |
---|
155 | errors.append( e ); |
---|
156 | } // try |
---|
157 | } // for |
---|
158 | if ( ! errors.isEmpty() ) { |
---|
159 | throw errors; |
---|
160 | } // if |
---|
161 | } |
---|
162 | |
---|
163 | void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::Decl>> & outputList ); |
---|
164 | void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList ); |
---|
165 | void buildTypeList( const DeclarationNode * firstNode, std::vector<ast::ptr<ast::Type>> & outputList ); |
---|
166 | |
---|
167 | template<typename AstType, typename NodeType, |
---|
168 | template<typename, typename...> class Container, typename... Args> |
---|
169 | void buildMoveList( NodeType * firstNode, |
---|
170 | Container<ast::ptr<AstType>, Args...> & output ) { |
---|
171 | buildList<AstType, NodeType, Container, Args...>( firstNode, output ); |
---|
172 | delete firstNode; |
---|
173 | } |
---|