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