[b87a5ed] | 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 | // |
---|
[974906e2] | 7 | // ParseNode.h -- |
---|
[b87a5ed] | 8 | // |
---|
| 9 | // Author : Rodolfo G. Esteves |
---|
| 10 | // Created On : Sat May 16 13:28:16 2015 |
---|
[76c62b2] | 11 | // Last Modified By : Peter A. Buhr |
---|
[9fd9d015] | 12 | // Last Modified On : Wed Mar 29 08:40:27 2023 |
---|
| 13 | // Update Count : 948 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[d180746] | 18 | #include <algorithm> // for move |
---|
| 19 | #include <cassert> // for assert, assertf |
---|
| 20 | #include <iosfwd> // for ostream |
---|
| 21 | #include <iterator> // for back_insert_iterator |
---|
| 22 | #include <list> // for list |
---|
| 23 | #include <memory> // for unique_ptr, pointer_traits |
---|
| 24 | #include <string> // for string |
---|
[51b7345] | 25 | |
---|
[21f0aa8] | 26 | #include "Common/CodeLocation.h" // for CodeLocation |
---|
[d180746] | 27 | #include "Common/SemanticError.h" // for SemanticError |
---|
[be9288a] | 28 | #include "Common/UniqueName.h" // for UniqueName |
---|
[4b60b28] | 29 | #include "Common/utility.h" // for maybeClone |
---|
| 30 | #include "Parser/parserutility.h" // for maybeBuild |
---|
[07de76b] | 31 | #include "SynTree/LinkageSpec.h" // for Spec |
---|
[312029a] | 32 | #include "SynTree/Declaration.h" // for Aggregate |
---|
[d180746] | 33 | #include "SynTree/Expression.h" // for Expression, ConstantExpr (ptr only) |
---|
| 34 | #include "SynTree/Label.h" // for Label |
---|
| 35 | #include "SynTree/Statement.h" // for Statement, BranchStmt, BranchStmt:... |
---|
| 36 | #include "SynTree/Type.h" // for Type, Type::FuncSpecifiers, Type::... |
---|
| 37 | |
---|
| 38 | class Attribute; |
---|
| 39 | class Declaration; |
---|
[b2e0df3] | 40 | struct DeclarationNode; |
---|
[d180746] | 41 | class DeclarationWithType; |
---|
| 42 | class Initializer; |
---|
[a6e5091] | 43 | class ExpressionNode; |
---|
[b2e0df3] | 44 | struct StatementNode; |
---|
[51b7345] | 45 | |
---|
[7880579] | 46 | //############################################################################## |
---|
| 47 | |
---|
[d48e529] | 48 | typedef CodeLocation YYLTYPE; |
---|
| 49 | #define YYLTYPE_IS_DECLARED 1 /* alert the parser that we have our own definition */ |
---|
| 50 | |
---|
| 51 | extern YYLTYPE yylloc; |
---|
[294647b] | 52 | |
---|
[51b7345] | 53 | class ParseNode { |
---|
[bdd516a] | 54 | public: |
---|
[99cad3aa] | 55 | ParseNode() {}; |
---|
[2298f728] | 56 | virtual ~ParseNode() { delete next; delete name; }; |
---|
[b6424d9] | 57 | virtual ParseNode * clone() const = 0; |
---|
[51b7345] | 58 | |
---|
[b6424d9] | 59 | ParseNode * get_next() const { return next; } |
---|
| 60 | ParseNode * set_next( ParseNode * newlink ) { next = newlink; return this; } |
---|
[1b77274] | 61 | |
---|
[b6424d9] | 62 | ParseNode * get_last() { |
---|
| 63 | ParseNode * current; |
---|
[2298f728] | 64 | for ( current = this; current->get_next() != nullptr; current = current->get_next() ); |
---|
[99cad3aa] | 65 | return current; |
---|
| 66 | } |
---|
[b6424d9] | 67 | ParseNode * set_last( ParseNode * newlast ) { |
---|
[2298f728] | 68 | if ( newlast != nullptr ) get_last()->set_next( newlast ); |
---|
[99cad3aa] | 69 | return this; |
---|
| 70 | } |
---|
[51b7345] | 71 | |
---|
[f2f512ba] | 72 | virtual void print( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const {} |
---|
| 73 | virtual void printList( std::ostream & os, int indent = 0 ) const { |
---|
[e4bc986] | 74 | print( os, indent ); |
---|
| 75 | if ( next ) next->print( os, indent ); |
---|
| 76 | } |
---|
[1b77274] | 77 | |
---|
[b87a5ed] | 78 | static int indent_by; |
---|
[7880579] | 79 | |
---|
[b6424d9] | 80 | ParseNode * next = nullptr; |
---|
[25bca42] | 81 | const std::string * name = nullptr; |
---|
[d48e529] | 82 | CodeLocation location = yylloc; |
---|
[7880579] | 83 | }; // ParseNode |
---|
[51b7345] | 84 | |
---|
[7bf7fb9] | 85 | //############################################################################## |
---|
| 86 | |
---|
[d1625f8] | 87 | class InitializerNode : public ParseNode { |
---|
| 88 | public: |
---|
[82bbaf4] | 89 | InitializerNode( ExpressionNode *, bool aggrp = false, ExpressionNode * des = nullptr ); |
---|
[2298f728] | 90 | InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode * des = nullptr ); |
---|
[3ed994e] | 91 | InitializerNode( bool isDelete ); |
---|
[d1625f8] | 92 | ~InitializerNode(); |
---|
[a7741435] | 93 | virtual InitializerNode * clone() const { assert( false ); return nullptr; } |
---|
[d1625f8] | 94 | |
---|
[b6424d9] | 95 | ExpressionNode * get_expression() const { return expr; } |
---|
[d1625f8] | 96 | |
---|
[b6424d9] | 97 | InitializerNode * set_designators( ExpressionNode * des ) { designator = des; return this; } |
---|
| 98 | ExpressionNode * get_designators() const { return designator; } |
---|
[d1625f8] | 99 | |
---|
[b6424d9] | 100 | InitializerNode * set_maybeConstructed( bool value ) { maybeConstructed = value; return this; } |
---|
[d1625f8] | 101 | bool get_maybeConstructed() const { return maybeConstructed; } |
---|
| 102 | |
---|
[3ed994e] | 103 | bool get_isDelete() const { return isDelete; } |
---|
| 104 | |
---|
[b6424d9] | 105 | InitializerNode * next_init() const { return kids; } |
---|
[d1625f8] | 106 | |
---|
[f2f512ba] | 107 | void print( std::ostream & os, int indent = 0 ) const; |
---|
[d1625f8] | 108 | void printOneLine( std::ostream & ) const; |
---|
| 109 | |
---|
[b6424d9] | 110 | virtual Initializer * build() const; |
---|
[d1625f8] | 111 | private: |
---|
[b6424d9] | 112 | ExpressionNode * expr; |
---|
[d1625f8] | 113 | bool aggregate; |
---|
[b6424d9] | 114 | ExpressionNode * designator; // may be list |
---|
| 115 | InitializerNode * kids; |
---|
[d1625f8] | 116 | bool maybeConstructed; |
---|
[3ed994e] | 117 | bool isDelete; |
---|
[c1c1112] | 118 | }; // InitializerNode |
---|
[d1625f8] | 119 | |
---|
| 120 | //############################################################################## |
---|
| 121 | |
---|
[ac71a86] | 122 | class ExpressionNode final : public ParseNode { |
---|
[bdd516a] | 123 | public: |
---|
[d1625f8] | 124 | ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {} |
---|
| 125 | virtual ~ExpressionNode() {} |
---|
[6eb4398] | 126 | virtual ExpressionNode * clone() const override { return expr ? static_cast<ExpressionNode*>((new ExpressionNode( expr->clone() ))->set_next( maybeClone( get_next() ) )) : nullptr; } |
---|
[51b7345] | 127 | |
---|
[e04ef3a] | 128 | bool get_extension() const { return extension; } |
---|
[b6424d9] | 129 | ExpressionNode * set_extension( bool exten ) { extension = exten; return this; } |
---|
[51b7345] | 130 | |
---|
[f2f512ba] | 131 | virtual void print( std::ostream & os, __attribute__((unused)) int indent = 0 ) const override { |
---|
| 132 | os << expr.get(); |
---|
[e4bc986] | 133 | } |
---|
[f2f512ba] | 134 | void printOneLine( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const {} |
---|
[ac71a86] | 135 | |
---|
| 136 | template<typename T> |
---|
[513e165] | 137 | bool isExpressionType() const { return nullptr != dynamic_cast<T>(expr.get()); } |
---|
[51b7345] | 138 | |
---|
[702e826] | 139 | Expression * build() const { |
---|
| 140 | Expression * node = const_cast<ExpressionNode *>(this)->expr.release(); |
---|
| 141 | node->set_extension( this->get_extension() ); |
---|
| 142 | node->location = this->location; |
---|
| 143 | return node; |
---|
| 144 | } |
---|
[67d4e37] | 145 | |
---|
| 146 | std::unique_ptr<Expression> expr; // public because of lifetime implications |
---|
[bdd516a] | 147 | private: |
---|
[e04ef3a] | 148 | bool extension = false; |
---|
[c1c1112] | 149 | }; // ExpressionNode |
---|
[e04ef3a] | 150 | |
---|
[e5f2a67] | 151 | // Must harmonize with OperName. |
---|
[d9e2280] | 152 | enum class OperKinds { |
---|
| 153 | // diadic |
---|
[e5f2a67] | 154 | SizeOf, AlignOf, OffsetOf, Plus, Minus, Exp, Mul, Div, Mod, Or, And, |
---|
[d9e2280] | 155 | BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq, |
---|
[e5f2a67] | 156 | Assign, AtAssn, ExpAssn, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn, |
---|
[d9e2280] | 157 | Index, Range, |
---|
| 158 | // monadic |
---|
[5809461] | 159 | UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, |
---|
[d9e2280] | 160 | Ctor, Dtor, |
---|
[c1c1112] | 161 | }; // OperKinds |
---|
[51b7345] | 162 | |
---|
[7cf8006] | 163 | enum class EnumHiding { Visible, Hide }; |
---|
| 164 | |
---|
[e82aa9df] | 165 | struct LabelNode { |
---|
| 166 | std::list< Label > labels; |
---|
| 167 | }; |
---|
| 168 | |
---|
[25bca42] | 169 | Expression * build_constantInteger( std::string & str ); // these 4 routines modify the string |
---|
| 170 | Expression * build_constantFloat( std::string & str ); |
---|
| 171 | Expression * build_constantChar( std::string & str ); |
---|
| 172 | Expression * build_constantStr( std::string & str ); |
---|
[930f69e] | 173 | Expression * build_field_name_FLOATING_FRACTIONconstant( const std::string & str ); |
---|
| 174 | Expression * build_field_name_FLOATING_DECIMALconstant( const std::string & str ); |
---|
[8780e30] | 175 | Expression * build_field_name_FLOATINGconstant( const std::string & str ); |
---|
| 176 | Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ); |
---|
[b6424d9] | 177 | |
---|
[d7dc824] | 178 | NameExpr * build_varref( const std::string * name ); |
---|
[4e2befe3] | 179 | QualifiedNameExpr * build_qualified_expr( const DeclarationNode * decl_node, const NameExpr * name ); |
---|
[b0d9ff7] | 180 | QualifiedNameExpr * build_qualified_expr( const EnumDecl * decl, const NameExpr * name ); |
---|
[6e50a6b] | 181 | DimensionExpr * build_dimensionref( const std::string * name ); |
---|
[b6424d9] | 182 | |
---|
| 183 | Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ); |
---|
[312029a] | 184 | Expression * build_keyword_cast( AggregateDecl::Aggregate target, ExpressionNode * expr_node ); |
---|
[a5f0529] | 185 | Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ); |
---|
[fd782b2] | 186 | Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ); |
---|
| 187 | Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ); |
---|
[b6424d9] | 188 | Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ); |
---|
| 189 | Expression * build_and( ExpressionNode * expr_node1, ExpressionNode * expr_node2 ); |
---|
| 190 | Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ); |
---|
| 191 | Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ); |
---|
| 192 | Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ); |
---|
| 193 | Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ); |
---|
| 194 | Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ); |
---|
[2298f728] | 195 | Expression * build_tuple( ExpressionNode * expr_node = nullptr ); |
---|
[b6424d9] | 196 | Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ); |
---|
| 197 | Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ); |
---|
[51b7345] | 198 | |
---|
[7bf7fb9] | 199 | //############################################################################## |
---|
| 200 | |
---|
[62e5546] | 201 | struct TypeData; |
---|
[51b7345] | 202 | |
---|
[a025ea8] | 203 | struct DeclarationNode : public ParseNode { |
---|
[ba01b14] | 204 | // These enumerations must harmonize with their names in DeclarationNode.cc. |
---|
[702e826] | 205 | enum BasicType { |
---|
| 206 | Void, Bool, Char, Int, Int128, |
---|
| 207 | Float, Double, LongDouble, uuFloat80, uuFloat128, |
---|
| 208 | uFloat16, uFloat32, uFloat32x, uFloat64, uFloat64x, uFloat128, uFloat128x, |
---|
| 209 | NoBasicType |
---|
| 210 | }; |
---|
[dd020c0] | 211 | static const char * basicTypeNames[]; |
---|
[ba01b14] | 212 | enum ComplexType { Complex, NoComplexType, Imaginary }; // Imaginary unsupported => parse, but make invisible and print error message |
---|
[dd020c0] | 213 | static const char * complexTypeNames[]; |
---|
[201aeb9] | 214 | enum Signedness { Signed, Unsigned, NoSignedness }; |
---|
[dd020c0] | 215 | static const char * signednessNames[]; |
---|
[201aeb9] | 216 | enum Length { Short, Long, LongLong, NoLength }; |
---|
[dd020c0] | 217 | static const char * lengthNames[]; |
---|
[f673c13c] | 218 | enum BuiltinType { Valist, AutoType, Zero, One, NoBuiltinType }; |
---|
[dd020c0] | 219 | static const char * builtinTypeNames[]; |
---|
[b6424d9] | 220 | |
---|
[68fe077a] | 221 | static DeclarationNode * newStorageClass( Type::StorageClasses ); |
---|
[ddfd945] | 222 | static DeclarationNode * newFuncSpecifier( Type::FuncSpecifiers ); |
---|
[738e304] | 223 | static DeclarationNode * newTypeQualifier( Type::Qualifiers ); |
---|
[b6424d9] | 224 | static DeclarationNode * newBasicType( BasicType ); |
---|
[5b639ee] | 225 | static DeclarationNode * newComplexType( ComplexType ); |
---|
[dd020c0] | 226 | static DeclarationNode * newSignedNess( Signedness ); |
---|
| 227 | static DeclarationNode * newLength( Length ); |
---|
[b6424d9] | 228 | static DeclarationNode * newBuiltinType( BuiltinType ); |
---|
[dd020c0] | 229 | static DeclarationNode * newForall( DeclarationNode * ); |
---|
[25bca42] | 230 | static DeclarationNode * newFromTypedef( const std::string * ); |
---|
[47498bd] | 231 | static DeclarationNode * newFromGlobalScope(); |
---|
[c5d7701] | 232 | static DeclarationNode * newQualifiedType( DeclarationNode *, DeclarationNode * ); |
---|
[25bca42] | 233 | static DeclarationNode * newFunction( const std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ); |
---|
[312029a] | 234 | static DeclarationNode * newAggregate( AggregateDecl::Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ); |
---|
[e4d7c1c] | 235 | static DeclarationNode * newEnum( const std::string * name, DeclarationNode * constants, bool body, bool typed, DeclarationNode * base = nullptr, EnumHiding hiding = EnumHiding::Visible ); |
---|
[25bca42] | 236 | static DeclarationNode * newEnumConstant( const std::string * name, ExpressionNode * constant ); |
---|
[374cb117] | 237 | static DeclarationNode * newEnumValueGeneric( const std::string * name, InitializerNode * init ); |
---|
[1e30df7] | 238 | static DeclarationNode * newEnumInLine( const std::string name ); |
---|
[25bca42] | 239 | static DeclarationNode * newName( const std::string * ); |
---|
| 240 | static DeclarationNode * newFromTypeGen( const std::string *, ExpressionNode * params ); |
---|
[07de76b] | 241 | static DeclarationNode * newTypeParam( TypeDecl::Kind, const std::string * ); |
---|
[2298f728] | 242 | static DeclarationNode * newTrait( const std::string * name, DeclarationNode * params, DeclarationNode * asserts ); |
---|
| 243 | static DeclarationNode * newTraitUse( const std::string * name, ExpressionNode * params ); |
---|
[25bca42] | 244 | static DeclarationNode * newTypeDecl( const std::string * name, DeclarationNode * typeParams ); |
---|
[ce8c12f] | 245 | static DeclarationNode * newPointer( DeclarationNode * qualifiers, OperKinds kind ); |
---|
[b6424d9] | 246 | static DeclarationNode * newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ); |
---|
| 247 | static DeclarationNode * newVarArray( DeclarationNode * qualifiers ); |
---|
| 248 | static DeclarationNode * newBitfield( ExpressionNode * size ); |
---|
| 249 | static DeclarationNode * newTuple( DeclarationNode * members ); |
---|
[f855545] | 250 | static DeclarationNode * newTypeof( ExpressionNode * expr, bool basetypeof = false ); |
---|
[93bbbc4] | 251 | static DeclarationNode * newVtableType( DeclarationNode * expr ); |
---|
[25bca42] | 252 | static DeclarationNode * newAttribute( const std::string *, ExpressionNode * expr = nullptr ); // gcc attributes |
---|
[2d019af] | 253 | static DeclarationNode * newDirectiveStmt( StatementNode * stmt ); // gcc external directive statement |
---|
[e994912] | 254 | static DeclarationNode * newAsmStmt( StatementNode * stmt ); // gcc external asm statement |
---|
[f6e3e34] | 255 | static DeclarationNode * newStaticAssert( ExpressionNode * condition, Expression * message ); |
---|
[b87a5ed] | 256 | |
---|
[7880579] | 257 | DeclarationNode(); |
---|
| 258 | ~DeclarationNode(); |
---|
[6a0d4d61] | 259 | DeclarationNode * clone() const override; |
---|
[7880579] | 260 | |
---|
[2298f728] | 261 | DeclarationNode * addQualifiers( DeclarationNode * ); |
---|
[413ad05] | 262 | void checkQualifiers( const TypeData *, const TypeData * ); |
---|
[a7c90d4] | 263 | void checkSpecifiers( DeclarationNode * ); |
---|
| 264 | DeclarationNode * copySpecifiers( DeclarationNode * ); |
---|
[2298f728] | 265 | DeclarationNode * addType( DeclarationNode * ); |
---|
[b6424d9] | 266 | DeclarationNode * addTypedef(); |
---|
[f135b50] | 267 | DeclarationNode * addEnumBase( DeclarationNode * ); |
---|
[2298f728] | 268 | DeclarationNode * addAssertions( DeclarationNode * ); |
---|
| 269 | DeclarationNode * addName( std::string * ); |
---|
[c0aa336] | 270 | DeclarationNode * addAsmName( DeclarationNode * ); |
---|
[b6424d9] | 271 | DeclarationNode * addBitfield( ExpressionNode * size ); |
---|
| 272 | DeclarationNode * addVarArgs(); |
---|
[c453ac4] | 273 | DeclarationNode * addFunctionBody( StatementNode * body, ExpressionNode * with = nullptr ); |
---|
[b6424d9] | 274 | DeclarationNode * addOldDeclList( DeclarationNode * list ); |
---|
[c0aa336] | 275 | DeclarationNode * setBase( TypeData * newType ); |
---|
| 276 | DeclarationNode * copyAttribute( DeclarationNode * attr ); |
---|
[b6424d9] | 277 | DeclarationNode * addPointer( DeclarationNode * qualifiers ); |
---|
| 278 | DeclarationNode * addArray( DeclarationNode * array ); |
---|
| 279 | DeclarationNode * addNewPointer( DeclarationNode * pointer ); |
---|
| 280 | DeclarationNode * addNewArray( DeclarationNode * array ); |
---|
| 281 | DeclarationNode * addParamList( DeclarationNode * list ); |
---|
| 282 | DeclarationNode * addIdList( DeclarationNode * list ); // old-style functions |
---|
| 283 | DeclarationNode * addInitializer( InitializerNode * init ); |
---|
[67cf18c] | 284 | DeclarationNode * addTypeInitializer( DeclarationNode * init ); |
---|
[b6424d9] | 285 | |
---|
| 286 | DeclarationNode * cloneType( std::string * newName ); |
---|
| 287 | DeclarationNode * cloneBaseType( DeclarationNode * newdecl ); |
---|
| 288 | |
---|
| 289 | DeclarationNode * appendList( DeclarationNode * node ) { |
---|
[99cad3aa] | 290 | return (DeclarationNode *)set_last( node ); |
---|
| 291 | } |
---|
[b87a5ed] | 292 | |
---|
[f2f512ba] | 293 | virtual void print( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const override; |
---|
| 294 | virtual void printList( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const override; |
---|
[b87a5ed] | 295 | |
---|
[b6424d9] | 296 | Declaration * build() const; |
---|
[a7c90d4] | 297 | Type * buildType() const; |
---|
[b87a5ed] | 298 | |
---|
[8b7ee09] | 299 | LinkageSpec::Spec get_linkage() const { return linkage; } |
---|
[b6424d9] | 300 | DeclarationNode * extractAggregate() const; |
---|
[4f147cc] | 301 | bool has_enumeratorValue() const { return (bool)enumeratorValue; } |
---|
[a7c90d4] | 302 | ExpressionNode * consume_enumeratorValue() const { return const_cast<DeclarationNode *>(this)->enumeratorValue.release(); } |
---|
[b87a5ed] | 303 | |
---|
[7305915] | 304 | bool get_extension() const { return extension; } |
---|
[b6424d9] | 305 | DeclarationNode * set_extension( bool exten ) { extension = exten; return this; } |
---|
[e07caa2] | 306 | |
---|
| 307 | bool get_inLine() const { return inLine; } |
---|
| 308 | DeclarationNode * set_inLine( bool inL ) { inLine = inL; return this; } |
---|
[a025ea8] | 309 | |
---|
[481115f] | 310 | DeclarationNode * get_last() { return (DeclarationNode *)ParseNode::get_last(); } |
---|
| 311 | |
---|
[28307be] | 312 | struct Variable_t { |
---|
[faddbd8] | 313 | // const std::string * name; |
---|
[07de76b] | 314 | TypeDecl::Kind tyClass; |
---|
[28307be] | 315 | DeclarationNode * assertions; |
---|
[67cf18c] | 316 | DeclarationNode * initializer; |
---|
[28307be] | 317 | }; |
---|
| 318 | Variable_t variable; |
---|
| 319 | |
---|
[f6e3e34] | 320 | struct StaticAssert_t { |
---|
| 321 | ExpressionNode * condition; |
---|
| 322 | Expression * message; |
---|
| 323 | }; |
---|
| 324 | StaticAssert_t assert; |
---|
| 325 | |
---|
[e07caa2] | 326 | BuiltinType builtin = NoBuiltinType; |
---|
[8f6f47d7] | 327 | |
---|
[e07caa2] | 328 | TypeData * type = nullptr; |
---|
[dd020c0] | 329 | |
---|
[e07caa2] | 330 | bool inLine = false; |
---|
[44a0ca2] | 331 | bool enumInLine = false; |
---|
[ddfd945] | 332 | Type::FuncSpecifiers funcSpecs; |
---|
[68fe077a] | 333 | Type::StorageClasses storageClasses; |
---|
[dd020c0] | 334 | |
---|
[e07caa2] | 335 | ExpressionNode * bitfieldWidth = nullptr; |
---|
[4f147cc] | 336 | std::unique_ptr<ExpressionNode> enumeratorValue; |
---|
[e07caa2] | 337 | bool hasEllipsis = false; |
---|
[8b7ee09] | 338 | LinkageSpec::Spec linkage; |
---|
[e07caa2] | 339 | Expression * asmName = nullptr; |
---|
[44a81853] | 340 | std::list< Attribute * > attributes; |
---|
[e07caa2] | 341 | InitializerNode * initializer = nullptr; |
---|
[7305915] | 342 | bool extension = false; |
---|
[13e3b50] | 343 | std::string error; |
---|
[e07caa2] | 344 | StatementNode * asmStmt = nullptr; |
---|
[2d019af] | 345 | StatementNode * directiveStmt = nullptr; |
---|
[b87a5ed] | 346 | |
---|
| 347 | static UniqueName anonymous; |
---|
[1db21619] | 348 | }; // DeclarationNode |
---|
[51b7345] | 349 | |
---|
[b6424d9] | 350 | Type * buildType( TypeData * type ); |
---|
[d1625f8] | 351 | |
---|
[b6424d9] | 352 | static inline Type * maybeMoveBuildType( const DeclarationNode * orig ) { |
---|
[a7c90d4] | 353 | Type * ret = orig ? orig->buildType() : nullptr; |
---|
[4f147cc] | 354 | delete orig; |
---|
| 355 | return ret; |
---|
| 356 | } |
---|
| 357 | |
---|
[7bf7fb9] | 358 | //############################################################################## |
---|
| 359 | |
---|
[a025ea8] | 360 | struct StatementNode final : public ParseNode { |
---|
[e82aa9df] | 361 | StatementNode() { stmt = nullptr; } |
---|
[b6424d9] | 362 | StatementNode( Statement * stmt ) : stmt( stmt ) {} |
---|
| 363 | StatementNode( DeclarationNode * decl ); |
---|
[e82aa9df] | 364 | virtual ~StatementNode() {} |
---|
[51b7345] | 365 | |
---|
[b6424d9] | 366 | virtual StatementNode * clone() const final { assert( false ); return nullptr; } |
---|
[a7c90d4] | 367 | Statement * build() const { return const_cast<StatementNode *>(this)->stmt.release(); } |
---|
[2f22cc4] | 368 | |
---|
[44a81853] | 369 | virtual StatementNode * add_label( const std::string * name, DeclarationNode * attr = nullptr ) { |
---|
| 370 | stmt->get_labels().emplace_back( * name, nullptr, attr ? std::move( attr->attributes ) : std::list< Attribute * > {} ); |
---|
| 371 | delete attr; |
---|
[ac71a86] | 372 | delete name; |
---|
[2f22cc4] | 373 | return this; |
---|
| 374 | } |
---|
| 375 | |
---|
[b6424d9] | 376 | virtual StatementNode * append_last_case( StatementNode * ); |
---|
[1d4580a] | 377 | |
---|
[f2f512ba] | 378 | virtual void print( std::ostream & os, __attribute__((unused)) int indent = 0 ) const override { |
---|
[e4bc986] | 379 | os << stmt.get() << std::endl; |
---|
| 380 | } |
---|
[a025ea8] | 381 | |
---|
[ac71a86] | 382 | std::unique_ptr<Statement> stmt; |
---|
[2f22cc4] | 383 | }; // StatementNode |
---|
| 384 | |
---|
[b6424d9] | 385 | Statement * build_expr( ExpressionNode * ctl ); |
---|
[1d4580a] | 386 | |
---|
[473d1da0] | 387 | struct CondCtl { |
---|
| 388 | CondCtl( DeclarationNode * decl, ExpressionNode * condition ) : |
---|
[936e9f4] | 389 | init( decl ? new StatementNode( decl ) : nullptr ), condition( condition ) {} |
---|
| 390 | |
---|
| 391 | StatementNode * init; |
---|
| 392 | ExpressionNode * condition; |
---|
| 393 | }; |
---|
| 394 | |
---|
[f271bdd] | 395 | struct ForCtrl { |
---|
[1cdc052] | 396 | ForCtrl( StatementNode * stmt, ExpressionNode * condition, ExpressionNode * change ) : |
---|
| 397 | init( stmt ), condition( condition ), change( change ) {} |
---|
[2f22cc4] | 398 | |
---|
[b6424d9] | 399 | StatementNode * init; |
---|
| 400 | ExpressionNode * condition; |
---|
| 401 | ExpressionNode * change; |
---|
[2f22cc4] | 402 | }; |
---|
| 403 | |
---|
[473d1da0] | 404 | Expression * build_if_control( CondCtl * ctl, std::list< Statement * > & init ); |
---|
[436bbe5] | 405 | Statement * build_if( CondCtl * ctl, StatementNode * then, StatementNode * else_ ); |
---|
[6a276a0] | 406 | Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ); |
---|
[b6424d9] | 407 | Statement * build_case( ExpressionNode * ctl ); |
---|
| 408 | Statement * build_default(); |
---|
[436bbe5] | 409 | Statement * build_while( CondCtl * ctl, StatementNode * stmt, StatementNode * else_ = nullptr ); |
---|
| 410 | Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt, StatementNode * else_ = nullptr ); |
---|
| 411 | Statement * build_for( ForCtrl * forctl, StatementNode * stmt, StatementNode * else_ = nullptr ); |
---|
[b6424d9] | 412 | Statement * build_branch( BranchStmt::Type kind ); |
---|
| 413 | Statement * build_branch( std::string * identifier, BranchStmt::Type kind ); |
---|
| 414 | Statement * build_computedgoto( ExpressionNode * ctl ); |
---|
| 415 | Statement * build_return( ExpressionNode * ctl ); |
---|
| 416 | Statement * build_throw( ExpressionNode * ctl ); |
---|
[daf1af8] | 417 | Statement * build_resume( ExpressionNode * ctl ); |
---|
| 418 | Statement * build_resume_at( ExpressionNode * ctl , ExpressionNode * target ); |
---|
[436bbe5] | 419 | Statement * build_try( StatementNode * try_, StatementNode * catch_, StatementNode * finally_ ); |
---|
| 420 | Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ); |
---|
[b6424d9] | 421 | Statement * build_finally( StatementNode * stmt ); |
---|
| 422 | Statement * build_compound( StatementNode * first ); |
---|
[a025ea8] | 423 | StatementNode * maybe_build_compound( StatementNode * first ); |
---|
[6d539f83] | 424 | Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output = nullptr, ExpressionNode * input = nullptr, ExpressionNode * clobber = nullptr, LabelNode * gotolabels = nullptr ); |
---|
| 425 | Statement * build_directive( std::string * directive ); |
---|
[427854b] | 426 | SuspendStmt * build_suspend( StatementNode *, SuspendStmt::Type = SuspendStmt::None); |
---|
[9fd9d015] | 427 | WaitForStmt * build_waitfor( WaitForStmt * existing, ExpressionNode * when, ExpressionNode * targetExpr, StatementNode * stmt ); |
---|
| 428 | WaitForStmt * build_waitfor_else( WaitForStmt * existing, ExpressionNode * when, StatementNode * stmt ); |
---|
| 429 | WaitForStmt * build_waitfor_timeout( WaitForStmt * existing, ExpressionNode * when, ExpressionNode * timeout, StatementNode * stmt ); |
---|
[e67991f] | 430 | Statement * build_with( ExpressionNode * exprs, StatementNode * stmt ); |
---|
[6cebfef] | 431 | Statement * build_mutex( ExpressionNode * exprs, StatementNode * stmt ); |
---|
[7f5566b] | 432 | |
---|
[7bf7fb9] | 433 | //############################################################################## |
---|
| 434 | |
---|
[aefcc3b] | 435 | template< typename SynTreeType, typename NodeType, template< typename, typename...> class Container, typename... Args > |
---|
[f2f512ba] | 436 | void buildList( const NodeType * firstNode, Container< SynTreeType *, Args... > & outputList ) { |
---|
[a16764a6] | 437 | SemanticErrorException errors; |
---|
[aefcc3b] | 438 | std::back_insert_iterator< Container< SynTreeType *, Args... > > out( outputList ); |
---|
[b6424d9] | 439 | const NodeType * cur = firstNode; |
---|
[b87a5ed] | 440 | |
---|
| 441 | while ( cur ) { |
---|
| 442 | try { |
---|
[702e826] | 443 | SynTreeType * result = dynamic_cast< SynTreeType * >( maybeBuild( cur ) ); |
---|
[b87a5ed] | 444 | if ( result ) { |
---|
[294647b] | 445 | result->location = cur->location; |
---|
[b6424d9] | 446 | * out++ = result; |
---|
[046e04a] | 447 | } else { |
---|
[3e274ab] | 448 | SemanticError( cur->location, "type specifier declaration in forall clause is currently unimplemented." ); |
---|
[b87a5ed] | 449 | } // if |
---|
[f2f512ba] | 450 | } catch( SemanticErrorException & e ) { |
---|
[b87a5ed] | 451 | errors.append( e ); |
---|
| 452 | } // try |
---|
[4678c1ec] | 453 | const ParseNode * temp = (cur->get_next()); |
---|
| 454 | cur = dynamic_cast< const NodeType * >( temp ); // should not return nullptr |
---|
| 455 | if ( ! cur && temp ) { // non-homogeneous nodes ? |
---|
[82ff4ed1] | 456 | SemanticError( temp->location, "internal error, non-homogeneous nodes founds in buildList processing." ); |
---|
[4678c1ec] | 457 | } // if |
---|
[b87a5ed] | 458 | } // while |
---|
[a32b204] | 459 | if ( ! errors.isEmpty() ) { |
---|
[b87a5ed] | 460 | throw errors; |
---|
| 461 | } // if |
---|
[51b7345] | 462 | } |
---|
| 463 | |
---|
| 464 | // in DeclarationNode.cc |
---|
[f2f512ba] | 465 | void buildList( const DeclarationNode * firstNode, std::list< Declaration * > & outputList ); |
---|
| 466 | void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > & outputList ); |
---|
| 467 | void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > & outputList ); |
---|
[51b7345] | 468 | |
---|
[7ecbb7e] | 469 | template< typename SynTreeType, typename NodeType > |
---|
[f2f512ba] | 470 | void buildMoveList( const NodeType * firstNode, std::list< SynTreeType * > & outputList ) { |
---|
[3a5131ed] | 471 | buildList( firstNode, outputList ); |
---|
[7ecbb7e] | 472 | delete firstNode; |
---|
| 473 | } |
---|
| 474 | |
---|
[c8dfcd3] | 475 | // in ParseNode.cc |
---|
| 476 | std::ostream & operator<<( std::ostream & out, const ParseNode * node ); |
---|
[7ecbb7e] | 477 | |
---|
[51b7345] | 478 | // Local Variables: // |
---|
[b87a5ed] | 479 | // tab-width: 4 // |
---|
| 480 | // mode: c++ // |
---|
| 481 | // compile-command: "make install" // |
---|
[51b7345] | 482 | // End: // |
---|