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.h --
|
---|
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.h"
|
---|
19 |
|
---|
20 | struct TypeData;
|
---|
21 | struct InitializerNode;
|
---|
22 |
|
---|
23 | struct DeclarationNode final : public ParseList<DeclarationNode> {
|
---|
24 | // These enumerations must harmonize with their names in DeclarationNode.cc.
|
---|
25 | enum BasicType {
|
---|
26 | Void, Bool, Char, Int, Int128,
|
---|
27 | Float, Double, LongDouble, uuFloat80, uuFloat128,
|
---|
28 | uFloat16, uFloat32, uFloat32x, uFloat64, uFloat64x, uFloat128, uFloat128x,
|
---|
29 | NoBasicType
|
---|
30 | };
|
---|
31 | static const char * basicTypeNames[];
|
---|
32 | enum ComplexType { Complex, NoComplexType, Imaginary };
|
---|
33 | // Imaginary unsupported => parse, but make invisible and print error message
|
---|
34 | static const char * complexTypeNames[];
|
---|
35 | enum Signedness { Signed, Unsigned, NoSignedness };
|
---|
36 | static const char * signednessNames[];
|
---|
37 | enum Length { Short, Long, LongLong, NoLength };
|
---|
38 | static const char * lengthNames[];
|
---|
39 | enum BuiltinType { Valist, AutoType, Zero, One, NoBuiltinType };
|
---|
40 | static const char * builtinTypeNames[];
|
---|
41 |
|
---|
42 | static DeclarationNode * newFromTypeData( TypeData * );
|
---|
43 | static DeclarationNode * newStorageClass( ast::Storage::Classes );
|
---|
44 | static DeclarationNode * newFuncSpecifier( ast::Function::Specs );
|
---|
45 | static DeclarationNode * newFunction( const std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body );
|
---|
46 | static DeclarationNode * newAggregate( ast::AggregateDecl::Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body );
|
---|
47 | static DeclarationNode * newEnum( const std::string * name, DeclarationNode * constants, bool body, bool typed, DeclarationNode * base = nullptr, EnumHiding hiding = EnumHiding::Visible );
|
---|
48 | static DeclarationNode * newEnumConstant( const std::string * name, ExpressionNode * constant );
|
---|
49 | static DeclarationNode * newEnumValueGeneric( const std::string * name, InitializerNode * init );
|
---|
50 | static DeclarationNode * newEnumInLine( const std::string name );
|
---|
51 | static DeclarationNode * newName( const std::string * );
|
---|
52 | static DeclarationNode * newTypeParam( ast::TypeDecl::Kind, const std::string * );
|
---|
53 | static DeclarationNode * newTrait( const std::string * name, DeclarationNode * params, DeclarationNode * asserts );
|
---|
54 | static DeclarationNode * newTraitUse( const std::string * name, ExpressionNode * params );
|
---|
55 | static DeclarationNode * newTypeDecl( const std::string * name, DeclarationNode * typeParams );
|
---|
56 | static DeclarationNode * newPointer( DeclarationNode * qualifiers, OperKinds kind );
|
---|
57 | static DeclarationNode * newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic );
|
---|
58 | static DeclarationNode * newVarArray( DeclarationNode * qualifiers );
|
---|
59 | static DeclarationNode * newBitfield( ExpressionNode * size );
|
---|
60 | static DeclarationNode * newTuple( DeclarationNode * members );
|
---|
61 | static DeclarationNode * newTypeof( ExpressionNode * expr, bool basetypeof = false );
|
---|
62 | static DeclarationNode * newAttribute( const std::string *, ExpressionNode * expr = nullptr ); // gcc attributes
|
---|
63 | static DeclarationNode * newDirectiveStmt( StatementNode * stmt ); // gcc external directive statement
|
---|
64 | static DeclarationNode * newAsmStmt( StatementNode * stmt ); // gcc external asm statement
|
---|
65 | static DeclarationNode * newStaticAssert( ExpressionNode * condition, ast::Expr * message );
|
---|
66 |
|
---|
67 | DeclarationNode();
|
---|
68 | ~DeclarationNode();
|
---|
69 | DeclarationNode * clone() const override;
|
---|
70 |
|
---|
71 | DeclarationNode * addQualifiers( DeclarationNode * );
|
---|
72 | void checkQualifiers( const TypeData *, const TypeData * );
|
---|
73 | void checkSpecifiers( DeclarationNode * );
|
---|
74 | DeclarationNode * copySpecifiers( DeclarationNode *, bool = true );
|
---|
75 | DeclarationNode * addType( DeclarationNode *, bool = true );
|
---|
76 | DeclarationNode * addTypedef();
|
---|
77 | DeclarationNode * addEnumBase( DeclarationNode * );
|
---|
78 | DeclarationNode * addAssertions( DeclarationNode * );
|
---|
79 | DeclarationNode * addName( std::string * );
|
---|
80 | DeclarationNode * addAsmName( DeclarationNode * );
|
---|
81 | DeclarationNode * addBitfield( ExpressionNode * size );
|
---|
82 | DeclarationNode * addVarArgs();
|
---|
83 | DeclarationNode * addFunctionBody( StatementNode * body, ExpressionNode * with = nullptr );
|
---|
84 | DeclarationNode * addOldDeclList( DeclarationNode * list );
|
---|
85 | DeclarationNode * setBase( TypeData * newType );
|
---|
86 | DeclarationNode * copyAttribute( DeclarationNode * attr );
|
---|
87 | DeclarationNode * addPointer( DeclarationNode * qualifiers );
|
---|
88 | DeclarationNode * addArray( DeclarationNode * array );
|
---|
89 | DeclarationNode * addNewPointer( DeclarationNode * pointer );
|
---|
90 | DeclarationNode * addNewArray( DeclarationNode * array );
|
---|
91 | DeclarationNode * addParamList( DeclarationNode * list );
|
---|
92 | DeclarationNode * addIdList( DeclarationNode * list ); // old-style functions
|
---|
93 | DeclarationNode * addInitializer( InitializerNode * init );
|
---|
94 | DeclarationNode * addTypeInitializer( DeclarationNode * init );
|
---|
95 |
|
---|
96 | DeclarationNode * cloneType( std::string * newName );
|
---|
97 | DeclarationNode * cloneBaseType( DeclarationNode * newdecl, bool = true );
|
---|
98 |
|
---|
99 | virtual void print( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const override;
|
---|
100 | virtual void printList( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const override;
|
---|
101 |
|
---|
102 | ast::Decl * build() const;
|
---|
103 | ast::Type * buildType() const;
|
---|
104 |
|
---|
105 | ast::Linkage::Spec get_linkage() const { return linkage; }
|
---|
106 | DeclarationNode * extractAggregate() const;
|
---|
107 | bool has_enumeratorValue() const { return (bool)enumeratorValue; }
|
---|
108 | ExpressionNode * consume_enumeratorValue() const { return const_cast<DeclarationNode *>(this)->enumeratorValue.release(); }
|
---|
109 |
|
---|
110 | bool get_extension() const { return extension; }
|
---|
111 | DeclarationNode * set_extension( bool exten ) { extension = exten; return this; }
|
---|
112 |
|
---|
113 | bool get_inLine() const { return inLine; }
|
---|
114 | DeclarationNode * set_inLine( bool inL ) { inLine = inL; return this; }
|
---|
115 |
|
---|
116 | const std::string * name = nullptr;
|
---|
117 |
|
---|
118 | struct Variable_t {
|
---|
119 | // const std::string * name;
|
---|
120 | ast::TypeDecl::Kind tyClass;
|
---|
121 | DeclarationNode * assertions;
|
---|
122 | DeclarationNode * initializer;
|
---|
123 | };
|
---|
124 | Variable_t variable;
|
---|
125 |
|
---|
126 | struct StaticAssert_t {
|
---|
127 | ExpressionNode * condition;
|
---|
128 | ast::Expr * message;
|
---|
129 | };
|
---|
130 | StaticAssert_t assert;
|
---|
131 |
|
---|
132 | TypeData * type = nullptr;
|
---|
133 |
|
---|
134 | bool inLine = false;
|
---|
135 | bool enumInLine = false;
|
---|
136 | ast::Function::Specs funcSpecs;
|
---|
137 | ast::Storage::Classes storageClasses;
|
---|
138 |
|
---|
139 | ExpressionNode * bitfieldWidth = nullptr;
|
---|
140 | std::unique_ptr<ExpressionNode> enumeratorValue;
|
---|
141 | bool hasEllipsis = false;
|
---|
142 | ast::Linkage::Spec linkage;
|
---|
143 | ast::Expr * asmName = nullptr;
|
---|
144 | std::vector<ast::ptr<ast::Attribute>> attributes;
|
---|
145 | InitializerNode * initializer = nullptr;
|
---|
146 | bool extension = false;
|
---|
147 | std::string error;
|
---|
148 | StatementNode * asmStmt = nullptr;
|
---|
149 | StatementNode * directiveStmt = nullptr;
|
---|
150 |
|
---|
151 | static UniqueName anonymous;
|
---|
152 | }; // DeclarationNode
|
---|
153 |
|
---|
154 | static inline ast::Type * maybeMoveBuildType( const DeclarationNode * orig ) {
|
---|
155 | ast::Type * ret = orig ? orig->buildType() : nullptr;
|
---|
156 | delete orig;
|
---|
157 | return ret;
|
---|
158 | }
|
---|
159 |
|
---|
160 | // This generic buildList is here along side its overloads.
|
---|
161 | template<typename AstType, typename NodeType,
|
---|
162 | template<typename, typename...> class Container, typename... Args>
|
---|
163 | void buildList( NodeType * firstNode,
|
---|
164 | Container<ast::ptr<AstType>, Args...> & output ) {
|
---|
165 | SemanticErrorException errors;
|
---|
166 | std::back_insert_iterator<Container<ast::ptr<AstType>, Args...>> out( output );
|
---|
167 |
|
---|
168 | for ( NodeType * cur = firstNode ; cur ; cur = cur->next ) {
|
---|
169 | try {
|
---|
170 | AstType * node = dynamic_cast<AstType *>( maybeBuild( cur ) );
|
---|
171 | assertf( node, "buildList: Did not build node of correct type." );
|
---|
172 | *out++ = node;
|
---|
173 | } catch ( SemanticErrorException & e ) {
|
---|
174 | errors.append( e );
|
---|
175 | } // try
|
---|
176 | } // for
|
---|
177 | if ( ! errors.isEmpty() ) {
|
---|
178 | throw errors;
|
---|
179 | } // if
|
---|
180 | }
|
---|
181 |
|
---|
182 | void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::Decl>> & outputList );
|
---|
183 | void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList );
|
---|
184 | void buildTypeList( const DeclarationNode * firstNode, std::vector<ast::ptr<ast::Type>> & outputList );
|
---|
185 |
|
---|
186 | template<typename AstType, typename NodeType,
|
---|
187 | template<typename, typename...> class Container, typename... Args>
|
---|
188 | void buildMoveList( NodeType * firstNode,
|
---|
189 | Container<ast::ptr<AstType>, Args...> & output ) {
|
---|
190 | buildList<AstType, NodeType, Container, Args...>( firstNode, output );
|
---|
191 | delete firstNode;
|
---|
192 | }
|
---|