[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 |
---|
[e04ef3a] | 11 | // Last Modified By : Peter A. Buhr |
---|
[faddbd8] | 12 | // Last Modified On : Mon Oct 3 18:03:08 2016 |
---|
| 13 | // Update Count : 636 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #ifndef PARSENODE_H |
---|
| 17 | #define PARSENODE_H |
---|
| 18 | |
---|
| 19 | #include <string> |
---|
| 20 | #include <list> |
---|
| 21 | #include <iterator> |
---|
[e04ef3a] | 22 | #include <memory> |
---|
[51b7345] | 23 | |
---|
[68cd1ce] | 24 | #include "Parser/LinkageSpec.h" |
---|
[59db689] | 25 | #include "SynTree/Type.h" |
---|
[e04ef3a] | 26 | #include "SynTree/Expression.h" |
---|
[2f22cc4] | 27 | #include "SynTree/Statement.h" |
---|
[0f8e4ac] | 28 | #include "SynTree/Label.h" |
---|
[7880579] | 29 | #include "Common/utility.h" |
---|
| 30 | #include "Common/UniqueName.h" |
---|
[51b7345] | 31 | |
---|
| 32 | class StatementNode; |
---|
| 33 | class CompoundStmtNode; |
---|
| 34 | class DeclarationNode; |
---|
[d1625f8] | 35 | class ExpressionNode; |
---|
[51b7345] | 36 | class InitializerNode; |
---|
| 37 | |
---|
[7880579] | 38 | //############################################################################## |
---|
| 39 | |
---|
[51b7345] | 40 | class ParseNode { |
---|
[bdd516a] | 41 | public: |
---|
[99cad3aa] | 42 | ParseNode() {}; |
---|
[2298f728] | 43 | virtual ~ParseNode() { delete next; delete name; }; |
---|
[b6424d9] | 44 | virtual ParseNode * clone() const = 0; |
---|
[51b7345] | 45 | |
---|
[b6424d9] | 46 | ParseNode * get_next() const { return next; } |
---|
| 47 | ParseNode * set_next( ParseNode * newlink ) { next = newlink; return this; } |
---|
[1b772749] | 48 | |
---|
[b6424d9] | 49 | ParseNode * get_last() { |
---|
| 50 | ParseNode * current; |
---|
[2298f728] | 51 | for ( current = this; current->get_next() != nullptr; current = current->get_next() ); |
---|
[99cad3aa] | 52 | return current; |
---|
| 53 | } |
---|
[b6424d9] | 54 | ParseNode * set_last( ParseNode * newlast ) { |
---|
[2298f728] | 55 | if ( newlast != nullptr ) get_last()->set_next( newlast ); |
---|
[99cad3aa] | 56 | return this; |
---|
| 57 | } |
---|
[51b7345] | 58 | |
---|
[7880579] | 59 | virtual void print( std::ostream &os, int indent = 0 ) const {} |
---|
[0da3e2c] | 60 | virtual void printList( std::ostream &os, int indent = 0 ) const {} |
---|
[1b772749] | 61 | |
---|
[b87a5ed] | 62 | static int indent_by; |
---|
[7880579] | 63 | |
---|
[b6424d9] | 64 | ParseNode * next = nullptr; |
---|
[2298f728] | 65 | std::string * name = nullptr; |
---|
[7880579] | 66 | }; // ParseNode |
---|
[51b7345] | 67 | |
---|
[7bf7fb9] | 68 | //############################################################################## |
---|
| 69 | |
---|
[d1625f8] | 70 | class InitializerNode : public ParseNode { |
---|
| 71 | public: |
---|
[2298f728] | 72 | InitializerNode( ExpressionNode *, bool aggrp = false, ExpressionNode * des = nullptr ); |
---|
| 73 | InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode * des = nullptr ); |
---|
[d1625f8] | 74 | ~InitializerNode(); |
---|
[b6424d9] | 75 | virtual InitializerNode * clone() const { assert( false ); return nullptr; } |
---|
[d1625f8] | 76 | |
---|
[b6424d9] | 77 | ExpressionNode * get_expression() const { return expr; } |
---|
[d1625f8] | 78 | |
---|
[b6424d9] | 79 | InitializerNode * set_designators( ExpressionNode * des ) { designator = des; return this; } |
---|
| 80 | ExpressionNode * get_designators() const { return designator; } |
---|
[d1625f8] | 81 | |
---|
[b6424d9] | 82 | InitializerNode * set_maybeConstructed( bool value ) { maybeConstructed = value; return this; } |
---|
[d1625f8] | 83 | bool get_maybeConstructed() const { return maybeConstructed; } |
---|
| 84 | |
---|
[b6424d9] | 85 | InitializerNode * next_init() const { return kids; } |
---|
[d1625f8] | 86 | |
---|
| 87 | void print( std::ostream &os, int indent = 0 ) const; |
---|
| 88 | void printOneLine( std::ostream & ) const; |
---|
| 89 | |
---|
[b6424d9] | 90 | virtual Initializer * build() const; |
---|
[d1625f8] | 91 | private: |
---|
[b6424d9] | 92 | ExpressionNode * expr; |
---|
[d1625f8] | 93 | bool aggregate; |
---|
[b6424d9] | 94 | ExpressionNode * designator; // may be list |
---|
| 95 | InitializerNode * kids; |
---|
[d1625f8] | 96 | bool maybeConstructed; |
---|
[c1c1112] | 97 | }; // InitializerNode |
---|
[d1625f8] | 98 | |
---|
| 99 | //############################################################################## |
---|
| 100 | |
---|
[ac71a86] | 101 | class ExpressionNode final : public ParseNode { |
---|
[bdd516a] | 102 | public: |
---|
[d1625f8] | 103 | ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {} |
---|
[b87a5ed] | 104 | ExpressionNode( const ExpressionNode &other ); |
---|
[d1625f8] | 105 | virtual ~ExpressionNode() {} |
---|
[b6424d9] | 106 | virtual ExpressionNode * clone() const { return expr ? new ExpressionNode( expr->clone() ) : nullptr; } |
---|
[51b7345] | 107 | |
---|
[e04ef3a] | 108 | bool get_extension() const { return extension; } |
---|
[b6424d9] | 109 | ExpressionNode * set_extension( bool exten ) { extension = exten; return this; } |
---|
[51b7345] | 110 | |
---|
[62e5546] | 111 | virtual void print( std::ostream &os, int indent = 0 ) const override {} |
---|
[ac71a86] | 112 | void printOneLine( std::ostream &os, int indent = 0 ) const {} |
---|
| 113 | |
---|
| 114 | template<typename T> |
---|
| 115 | bool isExpressionType() const { |
---|
| 116 | return nullptr != dynamic_cast<T>(expr.get()); |
---|
| 117 | } |
---|
[51b7345] | 118 | |
---|
[b6424d9] | 119 | Expression * build() const { return const_cast<ExpressionNode*>(this)->expr.release(); } |
---|
[bdd516a] | 120 | private: |
---|
[e04ef3a] | 121 | bool extension = false; |
---|
[ac71a86] | 122 | std::unique_ptr<Expression> expr; |
---|
[c1c1112] | 123 | }; // ExpressionNode |
---|
[e04ef3a] | 124 | |
---|
| 125 | template< typename T > |
---|
[7880579] | 126 | struct maybeBuild_t< Expression, T > { |
---|
[b6424d9] | 127 | static inline Expression * doit( const T * orig ) { |
---|
[e04ef3a] | 128 | if ( orig ) { |
---|
[b6424d9] | 129 | Expression * p = orig->build(); |
---|
[e04ef3a] | 130 | p->set_extension( orig->get_extension() ); |
---|
| 131 | return p; |
---|
| 132 | } else { |
---|
[7880579] | 133 | return nullptr; |
---|
[e04ef3a] | 134 | } // if |
---|
| 135 | } |
---|
[51b7345] | 136 | }; |
---|
| 137 | |
---|
[d9e2280] | 138 | enum class OperKinds { |
---|
| 139 | // diadic |
---|
[7bf7fb9] | 140 | SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And, |
---|
[d9e2280] | 141 | BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq, |
---|
[a839867] | 142 | Assign, AtAssn, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn, |
---|
[d9e2280] | 143 | Index, Range, |
---|
| 144 | // monadic |
---|
| 145 | UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress, |
---|
| 146 | Ctor, Dtor, |
---|
[c1c1112] | 147 | }; // OperKinds |
---|
[51b7345] | 148 | |
---|
[e82aa9df] | 149 | struct LabelNode { |
---|
| 150 | std::list< Label > labels; |
---|
| 151 | }; |
---|
| 152 | |
---|
[b6424d9] | 153 | Expression * build_constantInteger( const std::string &str ); |
---|
| 154 | Expression * build_constantFloat( const std::string &str ); |
---|
| 155 | Expression * build_constantChar( const std::string &str ); |
---|
| 156 | ConstantExpr * build_constantStr( const std::string &str ); |
---|
[8780e30] | 157 | Expression * build_field_name_FLOATINGconstant( const std::string & str ); |
---|
| 158 | Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ); |
---|
| 159 | Expression * build_field_name_REALFRACTIONconstant( const std::string & str ); |
---|
| 160 | Expression * build_field_name_REALDECIMALconstant( const std::string & str ); |
---|
[b6424d9] | 161 | |
---|
| 162 | NameExpr * build_varref( const std::string * name, bool labelp = false ); |
---|
| 163 | Expression * build_typevalue( DeclarationNode * decl ); |
---|
| 164 | |
---|
| 165 | Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ); |
---|
[fd782b2] | 166 | Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ); |
---|
| 167 | Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ); |
---|
[b6424d9] | 168 | Expression * build_addressOf( ExpressionNode * expr_node ); |
---|
| 169 | Expression * build_sizeOfexpr( ExpressionNode * expr_node ); |
---|
| 170 | Expression * build_sizeOftype( DeclarationNode * decl_node ); |
---|
| 171 | Expression * build_alignOfexpr( ExpressionNode * expr_node ); |
---|
| 172 | Expression * build_alignOftype( DeclarationNode * decl_node ); |
---|
| 173 | Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ); |
---|
| 174 | Expression * build_and( ExpressionNode * expr_node1, ExpressionNode * expr_node2 ); |
---|
| 175 | Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ); |
---|
| 176 | Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ); |
---|
| 177 | Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ); |
---|
| 178 | Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ); |
---|
| 179 | Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ); |
---|
| 180 | Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ); |
---|
| 181 | Expression * build_comma( ExpressionNode * expr_node1, ExpressionNode * expr_node2 ); |
---|
| 182 | Expression * build_attrexpr( NameExpr * var, ExpressionNode * expr_node ); |
---|
| 183 | Expression * build_attrtype( NameExpr * var, DeclarationNode * decl_node ); |
---|
[2298f728] | 184 | Expression * build_tuple( ExpressionNode * expr_node = nullptr ); |
---|
[b6424d9] | 185 | Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ); |
---|
| 186 | Expression * build_range( ExpressionNode * low, ExpressionNode * high ); |
---|
| 187 | Expression * build_asmexpr( ExpressionNode * inout, ConstantExpr * constraint, ExpressionNode * operand ); |
---|
| 188 | Expression * build_valexpr( StatementNode * s ); |
---|
| 189 | Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ); |
---|
[51b7345] | 190 | |
---|
[7bf7fb9] | 191 | //############################################################################## |
---|
| 192 | |
---|
[62e5546] | 193 | struct TypeData; |
---|
[51b7345] | 194 | |
---|
[bdd516a] | 195 | class DeclarationNode : public ParseNode { |
---|
| 196 | public: |
---|
[5b639ee] | 197 | // These must remain in the same order as the corresponding DeclarationNode names. |
---|
[68cd1ce] | 198 | enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, }; |
---|
[5b639ee] | 199 | enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic, NoQualifier }; |
---|
| 200 | enum BasicType { Void, Bool, Char, Int, Float, Double, LongDouble, NoBasicType }; |
---|
| 201 | enum ComplexType { Complex, Imaginary, NoComplexType }; |
---|
| 202 | enum Signedness { Signed, Unsigned, NoSignedness }; |
---|
| 203 | enum Length { Short, Long, LongLong, NoLength }; |
---|
[faddbd8] | 204 | enum Aggregate { Struct, Union, Trait, NoAggregate }; |
---|
| 205 | enum TypeClass { Otype, Dtype, Ftype, NoTypeClass }; |
---|
[90c3b1c] | 206 | enum BuiltinType { Valist }; |
---|
[b87a5ed] | 207 | |
---|
[b6424d9] | 208 | static const char * storageName[]; |
---|
[5b639ee] | 209 | static const char * qualifierName[]; |
---|
[b6424d9] | 210 | static const char * basicTypeName[]; |
---|
[5b639ee] | 211 | static const char * complexTypeName[]; |
---|
| 212 | static const char * signednessName[]; |
---|
| 213 | static const char * lengthName[]; |
---|
[b6424d9] | 214 | static const char * aggregateName[]; |
---|
| 215 | static const char * typeClassName[]; |
---|
| 216 | static const char * builtinTypeName[]; |
---|
| 217 | |
---|
| 218 | static DeclarationNode * newFunction( std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle = false ); |
---|
| 219 | static DeclarationNode * newQualifier( Qualifier ); |
---|
[2298f728] | 220 | static DeclarationNode * newForall( DeclarationNode * ); |
---|
[b6424d9] | 221 | static DeclarationNode * newStorageClass( StorageClass ); |
---|
| 222 | static DeclarationNode * newBasicType( BasicType ); |
---|
[5b639ee] | 223 | static DeclarationNode * newComplexType( ComplexType ); |
---|
| 224 | static DeclarationNode * newSignedNess( Signedness sn ); |
---|
| 225 | static DeclarationNode * newLength( Length lnth ); |
---|
[b6424d9] | 226 | static DeclarationNode * newBuiltinType( BuiltinType ); |
---|
[2298f728] | 227 | static DeclarationNode * newFromTypedef( std::string * ); |
---|
[b6424d9] | 228 | static DeclarationNode * newAggregate( Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ); |
---|
| 229 | static DeclarationNode * newEnum( std::string * name, DeclarationNode * constants ); |
---|
| 230 | static DeclarationNode * newEnumConstant( std::string * name, ExpressionNode * constant ); |
---|
[2298f728] | 231 | static DeclarationNode * newName( std::string * ); |
---|
[b6424d9] | 232 | static DeclarationNode * newFromTypeGen( std::string *, ExpressionNode * params ); |
---|
[2298f728] | 233 | static DeclarationNode * newTypeParam( TypeClass, std::string * ); |
---|
| 234 | static DeclarationNode * newTrait( const std::string * name, DeclarationNode * params, DeclarationNode * asserts ); |
---|
| 235 | static DeclarationNode * newTraitUse( const std::string * name, ExpressionNode * params ); |
---|
[b6424d9] | 236 | static DeclarationNode * newTypeDecl( std::string * name, DeclarationNode * typeParams ); |
---|
| 237 | static DeclarationNode * newPointer( DeclarationNode * qualifiers ); |
---|
| 238 | static DeclarationNode * newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ); |
---|
| 239 | static DeclarationNode * newVarArray( DeclarationNode * qualifiers ); |
---|
| 240 | static DeclarationNode * newBitfield( ExpressionNode * size ); |
---|
| 241 | static DeclarationNode * newTuple( DeclarationNode * members ); |
---|
| 242 | static DeclarationNode * newTypeof( ExpressionNode * expr ); |
---|
| 243 | static DeclarationNode * newAttr( std::string *, ExpressionNode * expr ); |
---|
| 244 | static DeclarationNode * newAttr( std::string *, DeclarationNode * type ); |
---|
[b87a5ed] | 245 | |
---|
[7880579] | 246 | DeclarationNode(); |
---|
| 247 | ~DeclarationNode(); |
---|
[b6424d9] | 248 | DeclarationNode * clone() const; |
---|
[7880579] | 249 | |
---|
[2298f728] | 250 | DeclarationNode * addQualifiers( DeclarationNode * ); |
---|
[413ad05] | 251 | void checkQualifiers( const TypeData *, const TypeData * ); |
---|
[2298f728] | 252 | void checkStorageClasses( DeclarationNode * ); |
---|
| 253 | DeclarationNode * copyStorageClasses( DeclarationNode * ); |
---|
| 254 | DeclarationNode * addType( DeclarationNode * ); |
---|
[b6424d9] | 255 | DeclarationNode * addTypedef(); |
---|
[2298f728] | 256 | DeclarationNode * addAssertions( DeclarationNode * ); |
---|
| 257 | DeclarationNode * addName( std::string * ); |
---|
[b6424d9] | 258 | DeclarationNode * addBitfield( ExpressionNode * size ); |
---|
| 259 | DeclarationNode * addVarArgs(); |
---|
| 260 | DeclarationNode * addFunctionBody( StatementNode * body ); |
---|
| 261 | DeclarationNode * addOldDeclList( DeclarationNode * list ); |
---|
| 262 | DeclarationNode * addPointer( DeclarationNode * qualifiers ); |
---|
| 263 | DeclarationNode * addArray( DeclarationNode * array ); |
---|
| 264 | DeclarationNode * addNewPointer( DeclarationNode * pointer ); |
---|
| 265 | DeclarationNode * addNewArray( DeclarationNode * array ); |
---|
| 266 | DeclarationNode * addParamList( DeclarationNode * list ); |
---|
| 267 | DeclarationNode * addIdList( DeclarationNode * list ); // old-style functions |
---|
| 268 | DeclarationNode * addInitializer( InitializerNode * init ); |
---|
| 269 | |
---|
| 270 | DeclarationNode * cloneType( std::string * newName ); |
---|
| 271 | DeclarationNode * cloneBaseType( DeclarationNode * newdecl ); |
---|
| 272 | |
---|
| 273 | DeclarationNode * appendList( DeclarationNode * node ) { |
---|
[99cad3aa] | 274 | return (DeclarationNode *)set_last( node ); |
---|
| 275 | } |
---|
[b87a5ed] | 276 | |
---|
[62e5546] | 277 | virtual void print( std::ostream &os, int indent = 0 ) const override; |
---|
| 278 | virtual void printList( std::ostream &os, int indent = 0 ) const override; |
---|
[b87a5ed] | 279 | |
---|
[b6424d9] | 280 | Declaration * build() const; |
---|
| 281 | ::Type * buildType() const; |
---|
[b87a5ed] | 282 | |
---|
| 283 | bool get_hasEllipsis() const; |
---|
[8b7ee09] | 284 | LinkageSpec::Spec get_linkage() const { return linkage; } |
---|
[b6424d9] | 285 | DeclarationNode * extractAggregate() const; |
---|
[4f147cc] | 286 | bool has_enumeratorValue() const { return (bool)enumeratorValue; } |
---|
[b6424d9] | 287 | ExpressionNode * consume_enumeratorValue() const { return const_cast<DeclarationNode*>(this)->enumeratorValue.release(); } |
---|
[b87a5ed] | 288 | |
---|
[7305915] | 289 | bool get_extension() const { return extension; } |
---|
[b6424d9] | 290 | DeclarationNode * set_extension( bool exten ) { extension = exten; return this; } |
---|
[c1c1112] | 291 | public: |
---|
[28307be] | 292 | struct Variable_t { |
---|
[faddbd8] | 293 | // const std::string * name; |
---|
[28307be] | 294 | DeclarationNode::TypeClass tyClass; |
---|
| 295 | DeclarationNode * assertions; |
---|
| 296 | }; |
---|
| 297 | Variable_t variable; |
---|
| 298 | |
---|
| 299 | struct Attr_t { |
---|
[faddbd8] | 300 | // const std::string * name; |
---|
[28307be] | 301 | ExpressionNode * expr; |
---|
| 302 | DeclarationNode * type; |
---|
| 303 | }; |
---|
| 304 | Attr_t attr; |
---|
| 305 | |
---|
[8f6f47d7] | 306 | BuiltinType builtin; |
---|
| 307 | |
---|
[b6424d9] | 308 | TypeData * type; |
---|
[13e3b50] | 309 | StorageClass storageClass; |
---|
| 310 | bool isInline, isNoreturn; |
---|
[1db21619] | 311 | std::list< std::string > attributes; |
---|
[b6424d9] | 312 | ExpressionNode * bitfieldWidth; |
---|
[4f147cc] | 313 | std::unique_ptr<ExpressionNode> enumeratorValue; |
---|
[b6424d9] | 314 | InitializerNode * initializer; |
---|
[b87a5ed] | 315 | bool hasEllipsis; |
---|
[8b7ee09] | 316 | LinkageSpec::Spec linkage; |
---|
[7305915] | 317 | bool extension = false; |
---|
[13e3b50] | 318 | std::string error; |
---|
[b87a5ed] | 319 | |
---|
| 320 | static UniqueName anonymous; |
---|
[1db21619] | 321 | }; // DeclarationNode |
---|
[51b7345] | 322 | |
---|
[b6424d9] | 323 | Type * buildType( TypeData * type ); |
---|
[d1625f8] | 324 | |
---|
[b6424d9] | 325 | static inline Type * maybeMoveBuildType( const DeclarationNode * orig ) { |
---|
[4f147cc] | 326 | Type* ret = orig ? orig->buildType() : nullptr; |
---|
| 327 | delete orig; |
---|
| 328 | return ret; |
---|
| 329 | } |
---|
| 330 | |
---|
[7bf7fb9] | 331 | //############################################################################## |
---|
| 332 | |
---|
[ac71a86] | 333 | class StatementNode final : public ParseNode { |
---|
[bdd516a] | 334 | public: |
---|
[e82aa9df] | 335 | StatementNode() { stmt = nullptr; } |
---|
[b6424d9] | 336 | StatementNode( Statement * stmt ) : stmt( stmt ) {} |
---|
| 337 | StatementNode( DeclarationNode * decl ); |
---|
[e82aa9df] | 338 | virtual ~StatementNode() {} |
---|
[51b7345] | 339 | |
---|
[b6424d9] | 340 | virtual StatementNode * clone() const final { assert( false ); return nullptr; } |
---|
| 341 | Statement * build() const { return const_cast<StatementNode*>(this)->stmt.release(); } |
---|
[2f22cc4] | 342 | |
---|
[b6424d9] | 343 | virtual StatementNode * add_label( const std::string * name ) { |
---|
| 344 | stmt->get_labels().emplace_back( * name ); |
---|
[ac71a86] | 345 | delete name; |
---|
[2f22cc4] | 346 | return this; |
---|
| 347 | } |
---|
| 348 | |
---|
[b6424d9] | 349 | virtual StatementNode * append_last_case( StatementNode * ); |
---|
[1d4580a] | 350 | |
---|
[62e5546] | 351 | virtual void print( std::ostream &os, int indent = 0 ) const override {} |
---|
| 352 | virtual void printList( std::ostream &os, int indent = 0 ) const override {} |
---|
[2f22cc4] | 353 | private: |
---|
[ac71a86] | 354 | std::unique_ptr<Statement> stmt; |
---|
[2f22cc4] | 355 | }; // StatementNode |
---|
| 356 | |
---|
[b6424d9] | 357 | Statement * build_expr( ExpressionNode * ctl ); |
---|
[1d4580a] | 358 | |
---|
[2f22cc4] | 359 | struct ForCtl { |
---|
[b6424d9] | 360 | ForCtl( ExpressionNode * expr, ExpressionNode * condition, ExpressionNode * change ) : |
---|
[e82aa9df] | 361 | init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {} |
---|
[b6424d9] | 362 | ForCtl( DeclarationNode * decl, ExpressionNode * condition, ExpressionNode * change ) : |
---|
[e82aa9df] | 363 | init( new StatementNode( decl ) ), condition( condition ), change( change ) {} |
---|
[2f22cc4] | 364 | |
---|
[b6424d9] | 365 | StatementNode * init; |
---|
| 366 | ExpressionNode * condition; |
---|
| 367 | ExpressionNode * change; |
---|
[2f22cc4] | 368 | }; |
---|
| 369 | |
---|
[b6424d9] | 370 | Statement * build_if( ExpressionNode * ctl, StatementNode * then_stmt, StatementNode * else_stmt ); |
---|
| 371 | Statement * build_switch( ExpressionNode * ctl, StatementNode * stmt ); |
---|
| 372 | Statement * build_case( ExpressionNode * ctl ); |
---|
| 373 | Statement * build_default(); |
---|
| 374 | Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind = false ); |
---|
| 375 | Statement * build_for( ForCtl * forctl, StatementNode * stmt ); |
---|
| 376 | Statement * build_branch( BranchStmt::Type kind ); |
---|
| 377 | Statement * build_branch( std::string * identifier, BranchStmt::Type kind ); |
---|
| 378 | Statement * build_computedgoto( ExpressionNode * ctl ); |
---|
| 379 | Statement * build_return( ExpressionNode * ctl ); |
---|
| 380 | Statement * build_throw( ExpressionNode * ctl ); |
---|
| 381 | Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ); |
---|
| 382 | Statement * build_catch( DeclarationNode * decl, StatementNode * stmt, bool catchAny = false ); |
---|
| 383 | Statement * build_finally( StatementNode * stmt ); |
---|
| 384 | Statement * build_compound( StatementNode * first ); |
---|
[2298f728] | 385 | Statement * build_asmstmt( bool voltile, ConstantExpr * instruction, ExpressionNode * output = nullptr, ExpressionNode * input = nullptr, ExpressionNode * clobber = nullptr, LabelNode * gotolabels = nullptr ); |
---|
[7f5566b] | 386 | |
---|
[7bf7fb9] | 387 | //############################################################################## |
---|
| 388 | |
---|
[aefcc3b] | 389 | template< typename SynTreeType, typename NodeType, template< typename, typename...> class Container, typename... Args > |
---|
| 390 | void buildList( const NodeType * firstNode, Container< SynTreeType *, Args... > &outputList ) { |
---|
[b87a5ed] | 391 | SemanticError errors; |
---|
[aefcc3b] | 392 | std::back_insert_iterator< Container< SynTreeType *, Args... > > out( outputList ); |
---|
[b6424d9] | 393 | const NodeType * cur = firstNode; |
---|
[b87a5ed] | 394 | |
---|
| 395 | while ( cur ) { |
---|
| 396 | try { |
---|
[b6424d9] | 397 | // SynTreeType * result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::result_of< decltype(&NodeType::build)(NodeType)>::type >( cur ) ); |
---|
| 398 | SynTreeType * result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::pointer_traits< decltype(cur->build())>::element_type >( cur ) ); |
---|
[b87a5ed] | 399 | if ( result ) { |
---|
[b6424d9] | 400 | * out++ = result; |
---|
[b87a5ed] | 401 | } // if |
---|
| 402 | } catch( SemanticError &e ) { |
---|
| 403 | errors.append( e ); |
---|
| 404 | } // try |
---|
[7880579] | 405 | cur = dynamic_cast< NodeType * >( cur->get_next() ); |
---|
[b87a5ed] | 406 | } // while |
---|
[a32b204] | 407 | if ( ! errors.isEmpty() ) { |
---|
[b87a5ed] | 408 | throw errors; |
---|
| 409 | } // if |
---|
[51b7345] | 410 | } |
---|
| 411 | |
---|
| 412 | // in DeclarationNode.cc |
---|
[b6424d9] | 413 | void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ); |
---|
| 414 | void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ); |
---|
| 415 | void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ); |
---|
[51b7345] | 416 | |
---|
[7ecbb7e] | 417 | template< typename SynTreeType, typename NodeType > |
---|
[b6424d9] | 418 | void buildMoveList( const NodeType * firstNode, std::list< SynTreeType * > &outputList ) { |
---|
[7ecbb7e] | 419 | buildList(firstNode, outputList); |
---|
| 420 | delete firstNode; |
---|
| 421 | } |
---|
| 422 | |
---|
[c8dfcd3] | 423 | // in ParseNode.cc |
---|
| 424 | std::ostream & operator<<( std::ostream & out, const ParseNode * node ); |
---|
[7ecbb7e] | 425 | |
---|
[bdd516a] | 426 | #endif // PARSENODE_H |
---|
[51b7345] | 427 | |
---|
| 428 | // Local Variables: // |
---|
[b87a5ed] | 429 | // tab-width: 4 // |
---|
| 430 | // mode: c++ // |
---|
| 431 | // compile-command: "make install" // |
---|
[51b7345] | 432 | // End: // |
---|