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