| [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 | // DeclarationNode.cc -- | 
|---|
| [b87a5ed] | 8 | // | 
|---|
|  | 9 | // Author           : Rodolfo G. Esteves | 
|---|
|  | 10 | // Created On       : Sat May 16 12:34:05 2015 | 
|---|
| [201aeb9] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [c38ae92] | 12 | // Last Modified On : Mon Nov 20 09:21:52 2017 | 
|---|
|  | 13 | // Update Count     : 1031 | 
|---|
| [b87a5ed] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [e3e16bc] | 16 | #include <cassert>                 // for assert, assertf, strict_dynamic_cast | 
|---|
| [d180746] | 17 | #include <iterator>                // for back_insert_iterator | 
|---|
|  | 18 | #include <list>                    // for list | 
|---|
|  | 19 | #include <memory>                  // for unique_ptr | 
|---|
|  | 20 | #include <ostream>                 // for operator<<, ostream, basic_ostream | 
|---|
|  | 21 | #include <string>                  // for string, operator+, allocator, char... | 
|---|
| [51b73452] | 22 |  | 
|---|
| [d180746] | 23 | #include "Common/SemanticError.h"  // for SemanticError | 
|---|
|  | 24 | #include "Common/UniqueName.h"     // for UniqueName | 
|---|
|  | 25 | #include "Common/utility.h"        // for maybeClone, maybeBuild, CodeLocation | 
|---|
|  | 26 | #include "Parser/LinkageSpec.h"    // for Spec, linkageName, Cforall | 
|---|
|  | 27 | #include "Parser/ParseNode.h"      // for DeclarationNode, ExpressionNode | 
|---|
|  | 28 | #include "SynTree/Attribute.h"     // for Attribute | 
|---|
|  | 29 | #include "SynTree/Declaration.h"   // for TypeDecl, ObjectDecl, Declaration | 
|---|
|  | 30 | #include "SynTree/Expression.h"    // for Expression, ConstantExpr | 
|---|
|  | 31 | #include "SynTree/Statement.h"     // for AsmStmt | 
|---|
|  | 32 | #include "SynTree/Type.h"          // for Type, Type::StorageClasses, Type::... | 
|---|
|  | 33 | #include "TypeData.h"              // for TypeData, TypeData::Aggregate_t | 
|---|
|  | 34 | #include "TypedefTable.h"          // for TypedefTable, TypedefTable::kind_t... | 
|---|
| [bdd516a] | 35 |  | 
|---|
| [d180746] | 36 | class Initializer; | 
|---|
| [51b73452] | 37 |  | 
|---|
| [de62360d] | 38 | extern TypedefTable typedefTable; | 
|---|
|  | 39 |  | 
|---|
| [51b73452] | 40 | using namespace std; | 
|---|
|  | 41 |  | 
|---|
| [201aeb9] | 42 | // These must harmonize with the corresponding DeclarationNode enumerations. | 
|---|
|  | 43 | const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "int128", "float80", "float128", "NoBasicTypeNames" }; | 
|---|
| [dd020c0] | 44 | const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" }; | 
|---|
|  | 45 | const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" }; | 
|---|
|  | 46 | const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" }; | 
|---|
| [409433da] | 47 | const char * DeclarationNode::aggregateNames[] = { "struct", "union", "trait", "coroutine", "monitor", "thread", "NoAggregateNames" }; | 
|---|
| [dd020c0] | 48 | const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" }; | 
|---|
|  | 49 | const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "NoBuiltinTypeNames" }; | 
|---|
| [51b73452] | 50 |  | 
|---|
|  | 51 | UniqueName DeclarationNode::anonymous( "__anonymous" ); | 
|---|
|  | 52 |  | 
|---|
| [8b7ee09] | 53 | extern LinkageSpec::Spec linkage;                                               // defined in parser.yy | 
|---|
| [51b73452] | 54 |  | 
|---|
| [7d05e7e] | 55 | DeclarationNode::DeclarationNode() : | 
|---|
| [2298f728] | 56 | type( nullptr ), | 
|---|
| [58dd019] | 57 | bitfieldWidth( nullptr ), | 
|---|
| [7d05e7e] | 58 | hasEllipsis( false ), | 
|---|
|  | 59 | linkage( ::linkage ), | 
|---|
| [58dd019] | 60 | asmName( nullptr ), | 
|---|
|  | 61 | initializer( nullptr ), | 
|---|
| [e994912] | 62 | extension( false ), | 
|---|
|  | 63 | asmStmt( nullptr ) { | 
|---|
| [2298f728] | 64 |  | 
|---|
| [faddbd8] | 65 | //      variable.name = nullptr; | 
|---|
|  | 66 | variable.tyClass = NoTypeClass; | 
|---|
| [28307be] | 67 | variable.assertions = nullptr; | 
|---|
| [67cf18c] | 68 | variable.initializer = nullptr; | 
|---|
| [7d05e7e] | 69 |  | 
|---|
| [faddbd8] | 70 | //      attr.name = nullptr; | 
|---|
| [7d05e7e] | 71 | attr.expr = nullptr; | 
|---|
|  | 72 | attr.type = nullptr; | 
|---|
| [28307be] | 73 | } | 
|---|
|  | 74 |  | 
|---|
|  | 75 | DeclarationNode::~DeclarationNode() { | 
|---|
| [faddbd8] | 76 | //      delete attr.name; | 
|---|
| [28307be] | 77 | delete attr.expr; | 
|---|
|  | 78 | delete attr.type; | 
|---|
| [2298f728] | 79 |  | 
|---|
| [faddbd8] | 80 | //      delete variable.name; | 
|---|
| [2298f728] | 81 | delete variable.assertions; | 
|---|
| [67cf18c] | 82 | delete variable.initializer; | 
|---|
| [2298f728] | 83 |  | 
|---|
| [28307be] | 84 | delete type; | 
|---|
|  | 85 | delete bitfieldWidth; | 
|---|
| [e994912] | 86 |  | 
|---|
|  | 87 | delete asmStmt; | 
|---|
| [58dd019] | 88 | // asmName, no delete, passed to next stage | 
|---|
| [28307be] | 89 | delete initializer; | 
|---|
|  | 90 | } | 
|---|
|  | 91 |  | 
|---|
| [ba7aa2d] | 92 | DeclarationNode * DeclarationNode::clone() const { | 
|---|
|  | 93 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [c0aa336] | 94 | newnode->set_next( maybeClone( get_next() ) ); | 
|---|
| [2298f728] | 95 | newnode->name = name ? new string( *name ) : nullptr; | 
|---|
| [c0aa336] | 96 |  | 
|---|
|  | 97 | newnode->type = maybeClone( type ); | 
|---|
| [a7c90d4] | 98 | newnode->storageClasses = storageClasses; | 
|---|
|  | 99 | newnode->funcSpecs = funcSpecs; | 
|---|
| [08d5507b] | 100 | newnode->bitfieldWidth = maybeClone( bitfieldWidth ); | 
|---|
| [c0aa336] | 101 | newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) ); | 
|---|
| [b87a5ed] | 102 | newnode->hasEllipsis = hasEllipsis; | 
|---|
|  | 103 | newnode->linkage = linkage; | 
|---|
| [58dd019] | 104 | newnode->asmName = maybeClone( asmName ); | 
|---|
| [c0aa336] | 105 | cloneAll( attributes, newnode->attributes ); | 
|---|
|  | 106 | newnode->initializer = maybeClone( initializer ); | 
|---|
|  | 107 | newnode->extension = extension; | 
|---|
| [e994912] | 108 | newnode->asmStmt = maybeClone( asmStmt ); | 
|---|
| [c0aa336] | 109 | newnode->error = error; | 
|---|
| [3848e0e] | 110 |  | 
|---|
| [faddbd8] | 111 | //      newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr; | 
|---|
| [28307be] | 112 | newnode->variable.tyClass = variable.tyClass; | 
|---|
| [fb114fa1] | 113 | newnode->variable.assertions = maybeClone( variable.assertions ); | 
|---|
| [67cf18c] | 114 | newnode->variable.initializer = maybeClone( variable.initializer ); | 
|---|
| [3848e0e] | 115 |  | 
|---|
| [faddbd8] | 116 | //      newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr; | 
|---|
| [28307be] | 117 | newnode->attr.expr = maybeClone( attr.expr ); | 
|---|
|  | 118 | newnode->attr.type = maybeClone( attr.type ); | 
|---|
|  | 119 | return newnode; | 
|---|
|  | 120 | } // DeclarationNode::clone | 
|---|
| [3848e0e] | 121 |  | 
|---|
|  | 122 | bool DeclarationNode::get_hasEllipsis() const { | 
|---|
| [b87a5ed] | 123 | return hasEllipsis; | 
|---|
| [3848e0e] | 124 | } | 
|---|
|  | 125 |  | 
|---|
|  | 126 | void DeclarationNode::print( std::ostream &os, int indent ) const { | 
|---|
| [59db689] | 127 | os << string( indent, ' ' ); | 
|---|
| [2298f728] | 128 | if ( name ) { | 
|---|
|  | 129 | os << *name << ": "; | 
|---|
| [b87a5ed] | 130 | } else { | 
|---|
| [2298f728] | 131 | os << "unnamed: "; | 
|---|
| [68cd1ce] | 132 | } // if | 
|---|
| [51b73452] | 133 |  | 
|---|
| [b87a5ed] | 134 | if ( linkage != LinkageSpec::Cforall ) { | 
|---|
| [faddbd8] | 135 | os << LinkageSpec::linkageName( linkage ) << " "; | 
|---|
| [68cd1ce] | 136 | } // if | 
|---|
| [3848e0e] | 137 |  | 
|---|
| [6e8bd43] | 138 | storageClasses.print( os ); | 
|---|
|  | 139 | funcSpecs.print( os ); | 
|---|
| [dd020c0] | 140 |  | 
|---|
| [b87a5ed] | 141 | if ( type ) { | 
|---|
|  | 142 | type->print( os, indent ); | 
|---|
|  | 143 | } else { | 
|---|
|  | 144 | os << "untyped entity "; | 
|---|
| [68cd1ce] | 145 | } // if | 
|---|
| [3848e0e] | 146 |  | 
|---|
| [b87a5ed] | 147 | if ( bitfieldWidth ) { | 
|---|
| [59db689] | 148 | os << endl << string( indent + 2, ' ' ) << "with bitfield width "; | 
|---|
| [b87a5ed] | 149 | bitfieldWidth->printOneLine( os ); | 
|---|
| [68cd1ce] | 150 | } // if | 
|---|
| [3848e0e] | 151 |  | 
|---|
| [2298f728] | 152 | if ( initializer ) { | 
|---|
| [59db689] | 153 | os << endl << string( indent + 2, ' ' ) << "with initializer "; | 
|---|
| [b87a5ed] | 154 | initializer->printOneLine( os ); | 
|---|
| [974906e2] | 155 | os << " maybe constructed? " << initializer->get_maybeConstructed(); | 
|---|
|  | 156 |  | 
|---|
| [68cd1ce] | 157 | } // if | 
|---|
| [3848e0e] | 158 |  | 
|---|
| [b87a5ed] | 159 | os << endl; | 
|---|
| [51b73452] | 160 | } | 
|---|
|  | 161 |  | 
|---|
| [3848e0e] | 162 | void DeclarationNode::printList( std::ostream &os, int indent ) const { | 
|---|
| [b87a5ed] | 163 | ParseNode::printList( os, indent ); | 
|---|
|  | 164 | if ( hasEllipsis ) { | 
|---|
|  | 165 | os << string( indent, ' ' )  << "and a variable number of other arguments" << endl; | 
|---|
| [68cd1ce] | 166 | } // if | 
|---|
| [51b73452] | 167 | } | 
|---|
|  | 168 |  | 
|---|
| [fb114fa1] | 169 | DeclarationNode * DeclarationNode::newFunction( string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle ) { | 
|---|
| [ba7aa2d] | 170 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [2298f728] | 171 | newnode->name = name; | 
|---|
| [b87a5ed] | 172 | newnode->type = new TypeData( TypeData::Function ); | 
|---|
| [8f6f47d7] | 173 | newnode->type->function.params = param; | 
|---|
|  | 174 | newnode->type->function.newStyle = newStyle; | 
|---|
|  | 175 | newnode->type->function.body = body; | 
|---|
| [c0aa336] | 176 |  | 
|---|
| [2298f728] | 177 | // ignore unnamed routine declarations: void p( int (*)(int) ); | 
|---|
|  | 178 | if ( newnode->name ) { | 
|---|
|  | 179 | typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID ); | 
|---|
|  | 180 | } // if | 
|---|
| [3848e0e] | 181 |  | 
|---|
| [b87a5ed] | 182 | if ( ret ) { | 
|---|
|  | 183 | newnode->type->base = ret->type; | 
|---|
| [2298f728] | 184 | ret->type = nullptr; | 
|---|
| [b87a5ed] | 185 | delete ret; | 
|---|
| [68cd1ce] | 186 | } // if | 
|---|
| [51b73452] | 187 |  | 
|---|
| [b87a5ed] | 188 | return newnode; | 
|---|
| [984dce6] | 189 | } // DeclarationNode::newFunction | 
|---|
| [3848e0e] | 190 |  | 
|---|
| [dd020c0] | 191 |  | 
|---|
| [68fe077a] | 192 | DeclarationNode * DeclarationNode::newStorageClass( Type::StorageClasses sc ) { | 
|---|
| [ba7aa2d] | 193 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [08d5507b] | 194 | newnode->storageClasses = sc; | 
|---|
| [b87a5ed] | 195 | return newnode; | 
|---|
| [dd020c0] | 196 | } // DeclarationNode::newStorageClass | 
|---|
| [3848e0e] | 197 |  | 
|---|
| [ddfd945] | 198 | DeclarationNode * DeclarationNode::newFuncSpecifier( Type::FuncSpecifiers fs ) { | 
|---|
| [ba7aa2d] | 199 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [08d5507b] | 200 | newnode->funcSpecs = fs; | 
|---|
| [c1c1112] | 201 | return newnode; | 
|---|
| [dd020c0] | 202 | } // DeclarationNode::newFuncSpecifier | 
|---|
| [c1c1112] | 203 |  | 
|---|
| [738e304] | 204 | DeclarationNode * DeclarationNode::newTypeQualifier( Type::Qualifiers tq ) { | 
|---|
| [ba7aa2d] | 205 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [dd020c0] | 206 | newnode->type = new TypeData(); | 
|---|
| [738e304] | 207 | newnode->type->qualifiers = tq; | 
|---|
| [b87a5ed] | 208 | return newnode; | 
|---|
| [dd020c0] | 209 | } // DeclarationNode::newQualifier | 
|---|
| [3848e0e] | 210 |  | 
|---|
| [c1c1112] | 211 | DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) { | 
|---|
| [ba7aa2d] | 212 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 213 | newnode->type = new TypeData( TypeData::Basic ); | 
|---|
| [5b639ee] | 214 | newnode->type->basictype = bt; | 
|---|
| [b87a5ed] | 215 | return newnode; | 
|---|
| [984dce6] | 216 | } // DeclarationNode::newBasicType | 
|---|
| [3848e0e] | 217 |  | 
|---|
| [5b639ee] | 218 | DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) { | 
|---|
| [ba7aa2d] | 219 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 220 | newnode->type = new TypeData( TypeData::Basic ); | 
|---|
| [5b639ee] | 221 | newnode->type->complextype = ct; | 
|---|
| [b87a5ed] | 222 | return newnode; | 
|---|
| [5b639ee] | 223 | } // DeclarationNode::newComplexType | 
|---|
|  | 224 |  | 
|---|
|  | 225 | DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) { | 
|---|
| [ba7aa2d] | 226 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [5b639ee] | 227 | newnode->type = new TypeData( TypeData::Basic ); | 
|---|
|  | 228 | newnode->type->signedness = sn; | 
|---|
|  | 229 | return newnode; | 
|---|
|  | 230 | } // DeclarationNode::newSignedNess | 
|---|
|  | 231 |  | 
|---|
|  | 232 | DeclarationNode * DeclarationNode::newLength( Length lnth ) { | 
|---|
| [ba7aa2d] | 233 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [5b639ee] | 234 | newnode->type = new TypeData( TypeData::Basic ); | 
|---|
|  | 235 | newnode->type->length = lnth; | 
|---|
|  | 236 | return newnode; | 
|---|
|  | 237 | } // DeclarationNode::newLength | 
|---|
| [3848e0e] | 238 |  | 
|---|
| [dd020c0] | 239 | DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) { | 
|---|
|  | 240 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
|  | 241 | newnode->type = new TypeData( TypeData::Unknown ); | 
|---|
|  | 242 | newnode->type->forall = forall; | 
|---|
|  | 243 | return newnode; | 
|---|
|  | 244 | } // DeclarationNode::newForall | 
|---|
|  | 245 |  | 
|---|
| [fb114fa1] | 246 | DeclarationNode * DeclarationNode::newFromTypedef( string * name ) { | 
|---|
| [ba7aa2d] | 247 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 248 | newnode->type = new TypeData( TypeData::SymbolicInst ); | 
|---|
| [fb114fa1] | 249 | newnode->type->symbolic.name = name; | 
|---|
| [8f6f47d7] | 250 | newnode->type->symbolic.isTypedef = true; | 
|---|
| [2298f728] | 251 | newnode->type->symbolic.params = nullptr; | 
|---|
| [b87a5ed] | 252 | return newnode; | 
|---|
| [984dce6] | 253 | } // DeclarationNode::newFromTypedef | 
|---|
| [3848e0e] | 254 |  | 
|---|
| [fb114fa1] | 255 | DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) { | 
|---|
| [6f95000] | 256 | assert( name ); | 
|---|
| [ba7aa2d] | 257 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 258 | newnode->type = new TypeData( TypeData::Aggregate ); | 
|---|
| [8f6f47d7] | 259 | newnode->type->aggregate.kind = kind; | 
|---|
| [6f95000] | 260 | newnode->type->aggregate.name = name; | 
|---|
| [8f6f47d7] | 261 | newnode->type->aggregate.actuals = actuals; | 
|---|
|  | 262 | newnode->type->aggregate.fields = fields; | 
|---|
|  | 263 | newnode->type->aggregate.body = body; | 
|---|
| [6ea87486] | 264 | newnode->type->aggregate.tagged = false; | 
|---|
|  | 265 | newnode->type->aggregate.parent = nullptr; | 
|---|
| [b87a5ed] | 266 | return newnode; | 
|---|
| [984dce6] | 267 | } // DeclarationNode::newAggregate | 
|---|
| [3848e0e] | 268 |  | 
|---|
| [ca1a547] | 269 | DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants, bool body ) { | 
|---|
| [6f95000] | 270 | assert( name ); | 
|---|
| [ba7aa2d] | 271 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 272 | newnode->type = new TypeData( TypeData::Enum ); | 
|---|
| [6f95000] | 273 | newnode->type->enumeration.name = name; | 
|---|
| [8f6f47d7] | 274 | newnode->type->enumeration.constants = constants; | 
|---|
| [ca1a547] | 275 | newnode->type->enumeration.body = body; | 
|---|
| [b87a5ed] | 276 | return newnode; | 
|---|
| [984dce6] | 277 | } // DeclarationNode::newEnum | 
|---|
| [3848e0e] | 278 |  | 
|---|
| [fb114fa1] | 279 | DeclarationNode * DeclarationNode::newEnumConstant( string * name, ExpressionNode * constant ) { | 
|---|
| [ba7aa2d] | 280 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [2298f728] | 281 | newnode->name = name; | 
|---|
| [4f147cc] | 282 | newnode->enumeratorValue.reset( constant ); | 
|---|
| [2298f728] | 283 | typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID ); | 
|---|
| [b87a5ed] | 284 | return newnode; | 
|---|
| [984dce6] | 285 | } // DeclarationNode::newEnumConstant | 
|---|
| [3848e0e] | 286 |  | 
|---|
| [fb114fa1] | 287 | DeclarationNode * DeclarationNode::newName( string * name ) { | 
|---|
| [ba7aa2d] | 288 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [2298f728] | 289 | newnode->name = name; | 
|---|
| [b87a5ed] | 290 | return newnode; | 
|---|
| [984dce6] | 291 | } // DeclarationNode::newName | 
|---|
| [3848e0e] | 292 |  | 
|---|
| [fb114fa1] | 293 | DeclarationNode * DeclarationNode::newFromTypeGen( string * name, ExpressionNode * params ) { | 
|---|
| [ba7aa2d] | 294 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 295 | newnode->type = new TypeData( TypeData::SymbolicInst ); | 
|---|
| [fb114fa1] | 296 | newnode->type->symbolic.name = name; | 
|---|
| [8f6f47d7] | 297 | newnode->type->symbolic.isTypedef = false; | 
|---|
|  | 298 | newnode->type->symbolic.actuals = params; | 
|---|
| [b87a5ed] | 299 | return newnode; | 
|---|
| [984dce6] | 300 | } // DeclarationNode::newFromTypeGen | 
|---|
| [3848e0e] | 301 |  | 
|---|
| [fb114fa1] | 302 | DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, string * name ) { | 
|---|
| [ba7aa2d] | 303 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [2298f728] | 304 | newnode->type = nullptr; | 
|---|
| [148f7290] | 305 | assert( ! newnode->name ); | 
|---|
| [faddbd8] | 306 | //      newnode->variable.name = name; | 
|---|
|  | 307 | newnode->name = name; | 
|---|
| [28307be] | 308 | newnode->variable.tyClass = tc; | 
|---|
| [faddbd8] | 309 | newnode->variable.assertions = nullptr; | 
|---|
| [b87a5ed] | 310 | return newnode; | 
|---|
| [984dce6] | 311 | } // DeclarationNode::newTypeParam | 
|---|
| [3848e0e] | 312 |  | 
|---|
| [fb114fa1] | 313 | DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) { | 
|---|
| [ba7aa2d] | 314 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 315 | newnode->type = new TypeData( TypeData::Aggregate ); | 
|---|
| [2298f728] | 316 | newnode->type->aggregate.name = name; | 
|---|
| [8f6f47d7] | 317 | newnode->type->aggregate.kind = Trait; | 
|---|
|  | 318 | newnode->type->aggregate.params = params; | 
|---|
|  | 319 | newnode->type->aggregate.fields = asserts; | 
|---|
| [b87a5ed] | 320 | return newnode; | 
|---|
| [984dce6] | 321 | } // DeclarationNode::newTrait | 
|---|
| [3848e0e] | 322 |  | 
|---|
| [fb114fa1] | 323 | DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) { | 
|---|
| [ba7aa2d] | 324 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 325 | newnode->type = new TypeData( TypeData::AggregateInst ); | 
|---|
| [8f6f47d7] | 326 | newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate ); | 
|---|
|  | 327 | newnode->type->aggInst.aggregate->aggregate.kind = Trait; | 
|---|
| [2298f728] | 328 | newnode->type->aggInst.aggregate->aggregate.name = name; | 
|---|
| [8f6f47d7] | 329 | newnode->type->aggInst.params = params; | 
|---|
| [b87a5ed] | 330 | return newnode; | 
|---|
| [984dce6] | 331 | } // DeclarationNode::newTraitUse | 
|---|
| [3848e0e] | 332 |  | 
|---|
| [fb114fa1] | 333 | DeclarationNode * DeclarationNode::newTypeDecl( string * name, DeclarationNode * typeParams ) { | 
|---|
| [ba7aa2d] | 334 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [1395748] | 335 | newnode->name = name; | 
|---|
| [b87a5ed] | 336 | newnode->type = new TypeData( TypeData::Symbolic ); | 
|---|
| [8f6f47d7] | 337 | newnode->type->symbolic.isTypedef = false; | 
|---|
|  | 338 | newnode->type->symbolic.params = typeParams; | 
|---|
| [b87a5ed] | 339 | return newnode; | 
|---|
| [984dce6] | 340 | } // DeclarationNode::newTypeDecl | 
|---|
| [3848e0e] | 341 |  | 
|---|
| [ce8c12f] | 342 | DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) { | 
|---|
| [ba7aa2d] | 343 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [ce8c12f] | 344 | newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference ); | 
|---|
| [71d0eab] | 345 | if ( kind == OperKinds::And ) { | 
|---|
|  | 346 | // T && is parsed as 'And' operator rather than two references => add a second reference type | 
|---|
|  | 347 | TypeData * td = new TypeData( TypeData::Reference ); | 
|---|
|  | 348 | td->base = newnode->type; | 
|---|
|  | 349 | newnode->type = td; | 
|---|
|  | 350 | } | 
|---|
| [c3396e0] | 351 | if ( qualifiers ) { | 
|---|
|  | 352 | return newnode->addQualifiers( qualifiers ); | 
|---|
|  | 353 | } else { | 
|---|
|  | 354 | return newnode; | 
|---|
|  | 355 | } // if | 
|---|
| [984dce6] | 356 | } // DeclarationNode::newPointer | 
|---|
| [3848e0e] | 357 |  | 
|---|
| [ba7aa2d] | 358 | DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) { | 
|---|
|  | 359 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 360 | newnode->type = new TypeData( TypeData::Array ); | 
|---|
| [8f6f47d7] | 361 | newnode->type->array.dimension = size; | 
|---|
|  | 362 | newnode->type->array.isStatic = isStatic; | 
|---|
| [2298f728] | 363 | if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) { | 
|---|
| [8f6f47d7] | 364 | newnode->type->array.isVarLen = false; | 
|---|
| [71bd8c6] | 365 | } else { | 
|---|
| [8f6f47d7] | 366 | newnode->type->array.isVarLen = true; | 
|---|
| [71bd8c6] | 367 | } // if | 
|---|
| [b87a5ed] | 368 | return newnode->addQualifiers( qualifiers ); | 
|---|
| [984dce6] | 369 | } // DeclarationNode::newArray | 
|---|
| [3848e0e] | 370 |  | 
|---|
| [ba7aa2d] | 371 | DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) { | 
|---|
|  | 372 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 373 | newnode->type = new TypeData( TypeData::Array ); | 
|---|
| [2298f728] | 374 | newnode->type->array.dimension = nullptr; | 
|---|
| [8f6f47d7] | 375 | newnode->type->array.isStatic = false; | 
|---|
|  | 376 | newnode->type->array.isVarLen = true; | 
|---|
| [b87a5ed] | 377 | return newnode->addQualifiers( qualifiers ); | 
|---|
| [3848e0e] | 378 | } | 
|---|
|  | 379 |  | 
|---|
| [ba7aa2d] | 380 | DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) { | 
|---|
|  | 381 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 382 | newnode->bitfieldWidth = size; | 
|---|
|  | 383 | return newnode; | 
|---|
| [3848e0e] | 384 | } | 
|---|
|  | 385 |  | 
|---|
| [ba7aa2d] | 386 | DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) { | 
|---|
|  | 387 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 388 | newnode->type = new TypeData( TypeData::Tuple ); | 
|---|
| [8f6f47d7] | 389 | newnode->type->tuple = members; | 
|---|
| [b87a5ed] | 390 | return newnode; | 
|---|
| [3848e0e] | 391 | } | 
|---|
|  | 392 |  | 
|---|
| [ba7aa2d] | 393 | DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) { | 
|---|
|  | 394 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 395 | newnode->type = new TypeData( TypeData::Typeof ); | 
|---|
| [8f6f47d7] | 396 | newnode->type->typeexpr = expr; | 
|---|
| [b87a5ed] | 397 | return newnode; | 
|---|
| [3848e0e] | 398 | } | 
|---|
|  | 399 |  | 
|---|
| [8f6f47d7] | 400 | DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) { | 
|---|
| [ba7aa2d] | 401 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [8f6f47d7] | 402 | newnode->type = new TypeData( TypeData::Builtin ); | 
|---|
|  | 403 | newnode->builtin = bt; | 
|---|
| [148f7290] | 404 | newnode->type->builtintype = newnode->builtin; | 
|---|
| [8f6f47d7] | 405 | return newnode; | 
|---|
|  | 406 | } // DeclarationNode::newBuiltinType | 
|---|
|  | 407 |  | 
|---|
| [fb114fa1] | 408 | DeclarationNode * DeclarationNode::newAttr( string * name, ExpressionNode * expr ) { | 
|---|
| [ba7aa2d] | 409 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [2298f728] | 410 | newnode->type = nullptr; | 
|---|
| [faddbd8] | 411 | //      newnode->attr.name = name; | 
|---|
|  | 412 | newnode->name = name; | 
|---|
| [28307be] | 413 | newnode->attr.expr = expr; | 
|---|
| [b87a5ed] | 414 | return newnode; | 
|---|
| [3848e0e] | 415 | } | 
|---|
|  | 416 |  | 
|---|
| [fb114fa1] | 417 | DeclarationNode * DeclarationNode::newAttr( string * name, DeclarationNode * type ) { | 
|---|
| [ba7aa2d] | 418 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [2298f728] | 419 | newnode->type = nullptr; | 
|---|
| [faddbd8] | 420 | //      newnode->attr.name = name; | 
|---|
|  | 421 | newnode->name = name; | 
|---|
| [28307be] | 422 | newnode->attr.type = type; | 
|---|
| [b87a5ed] | 423 | return newnode; | 
|---|
| [3848e0e] | 424 | } | 
|---|
|  | 425 |  | 
|---|
| [44a81853] | 426 | DeclarationNode * DeclarationNode::newAttribute( string * name, ExpressionNode * expr ) { | 
|---|
|  | 427 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
|  | 428 | newnode->type = nullptr; | 
|---|
|  | 429 | std::list< Expression * > exprs; | 
|---|
|  | 430 | buildList( expr, exprs ); | 
|---|
|  | 431 | newnode->attributes.push_back( new Attribute( *name, exprs ) ); | 
|---|
|  | 432 | delete name; | 
|---|
|  | 433 | return newnode; | 
|---|
|  | 434 | } | 
|---|
|  | 435 |  | 
|---|
| [e994912] | 436 | DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) { | 
|---|
|  | 437 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
|  | 438 | newnode->asmStmt = stmt; | 
|---|
|  | 439 | return newnode; | 
|---|
|  | 440 | } | 
|---|
|  | 441 |  | 
|---|
| [5b639ee] | 442 | void appendError( string & dst, const string & src ) { | 
|---|
|  | 443 | if ( src.empty() ) return; | 
|---|
|  | 444 | if ( dst.empty() ) { dst = src; return; } | 
|---|
|  | 445 | dst += ", " + src; | 
|---|
|  | 446 | } // appendError | 
|---|
|  | 447 |  | 
|---|
| [ba7aa2d] | 448 | void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) { | 
|---|
| [738e304] | 449 | const Type::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization | 
|---|
| [c1c1112] | 450 |  | 
|---|
| [6f95000] | 451 | if ( (qsrc & qdst).any() ) {                                            // duplicates ? | 
|---|
| [738e304] | 452 | for ( unsigned int i = 0; i < Type::NumTypeQualifier; i += 1 ) { // find duplicates | 
|---|
| [5b639ee] | 453 | if ( qsrc[i] && qdst[i] ) { | 
|---|
| [615a096] | 454 | appendError( error, string( "duplicate " ) + Type::QualifiersNames[i] ); | 
|---|
| [c1c1112] | 455 | } // if | 
|---|
|  | 456 | } // for | 
|---|
| [a7c90d4] | 457 | } // for | 
|---|
| [c1c1112] | 458 | } // DeclarationNode::checkQualifiers | 
|---|
|  | 459 |  | 
|---|
| [a7c90d4] | 460 | void DeclarationNode::checkSpecifiers( DeclarationNode * src ) { | 
|---|
| [6f95000] | 461 | if ( (funcSpecs & src->funcSpecs).any() ) {                     // duplicates ? | 
|---|
| [ddfd945] | 462 | for ( unsigned int i = 0; i < Type::NumFuncSpecifier; i += 1 ) { // find duplicates | 
|---|
| [a7c90d4] | 463 | if ( funcSpecs[i] && src->funcSpecs[i] ) { | 
|---|
| [615a096] | 464 | appendError( error, string( "duplicate " ) + Type::FuncSpecifiersNames[i] ); | 
|---|
| [dd020c0] | 465 | } // if | 
|---|
|  | 466 | } // for | 
|---|
|  | 467 | } // if | 
|---|
|  | 468 |  | 
|---|
| [6e8bd43] | 469 | if ( storageClasses.any() && src->storageClasses.any() ) { // any reason to check ? | 
|---|
| [6f95000] | 470 | if ( (storageClasses & src->storageClasses ).any() ) { // duplicates ? | 
|---|
| [68fe077a] | 471 | for ( unsigned int i = 0; i < Type::NumStorageClass; i += 1 ) { // find duplicates | 
|---|
| [a7c90d4] | 472 | if ( storageClasses[i] && src->storageClasses[i] ) { | 
|---|
| [615a096] | 473 | appendError( error, string( "duplicate " ) + Type::StorageClassesNames[i] ); | 
|---|
| [a7c90d4] | 474 | } // if | 
|---|
|  | 475 | } // for | 
|---|
|  | 476 | // src is the new item being added and has a single bit | 
|---|
| [08d5507b] | 477 | } else if ( ! src->storageClasses.is_threadlocal ) { // conflict ? | 
|---|
| [615a096] | 478 | appendError( error, string( "conflicting " ) + Type::StorageClassesNames[storageClasses.ffs()] + | 
|---|
|  | 479 | " & " + Type::StorageClassesNames[src->storageClasses.ffs()] ); | 
|---|
| [6f95000] | 480 | src->storageClasses.reset();                            // FIX to preserve invariant of one basic storage specifier | 
|---|
| [b6424d9] | 481 | } // if | 
|---|
|  | 482 | } // if | 
|---|
| [dd020c0] | 483 |  | 
|---|
| [a7c90d4] | 484 | appendError( error, src->error ); | 
|---|
|  | 485 | } // DeclarationNode::checkSpecifiers | 
|---|
| [b6424d9] | 486 |  | 
|---|
| [a7c90d4] | 487 | DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) { | 
|---|
| [6f95000] | 488 | funcSpecs |= q->funcSpecs; | 
|---|
|  | 489 | storageClasses |= q->storageClasses; | 
|---|
| [c0aa336] | 490 |  | 
|---|
|  | 491 | for ( Attribute *attr: reverseIterate( q->attributes ) ) { | 
|---|
|  | 492 | attributes.push_front( attr->clone() ); | 
|---|
|  | 493 | } // for | 
|---|
| [b6424d9] | 494 | return this; | 
|---|
| [a7c90d4] | 495 | } // DeclarationNode::copySpecifiers | 
|---|
| [b6424d9] | 496 |  | 
|---|
| [ba7aa2d] | 497 | static void addQualifiersToType( TypeData *&src, TypeData * dst ) { | 
|---|
| [101e0bd] | 498 | if ( src->forall && dst->kind == TypeData::Function ) { | 
|---|
|  | 499 | if ( dst->forall ) { | 
|---|
|  | 500 | dst->forall->appendList( src->forall ); | 
|---|
|  | 501 | } else { | 
|---|
|  | 502 | dst->forall = src->forall; | 
|---|
|  | 503 | } // if | 
|---|
| [2298f728] | 504 | src->forall = nullptr; | 
|---|
| [101e0bd] | 505 | } // if | 
|---|
|  | 506 | if ( dst->base ) { | 
|---|
|  | 507 | addQualifiersToType( src, dst->base ); | 
|---|
|  | 508 | } else if ( dst->kind == TypeData::Function ) { | 
|---|
|  | 509 | dst->base = src; | 
|---|
| [2298f728] | 510 | src = nullptr; | 
|---|
| [101e0bd] | 511 | } else { | 
|---|
| [6f95000] | 512 | dst->qualifiers |= src->qualifiers; | 
|---|
| [101e0bd] | 513 | } // if | 
|---|
|  | 514 | } // addQualifiersToType | 
|---|
|  | 515 |  | 
|---|
| [ba7aa2d] | 516 | DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) { | 
|---|
| [c38ae92] | 517 | if ( ! q ) { delete q; return this; }                           // empty qualifier | 
|---|
| [101e0bd] | 518 |  | 
|---|
| [a7c90d4] | 519 | checkSpecifiers( q ); | 
|---|
|  | 520 | copySpecifiers( q ); | 
|---|
| [101e0bd] | 521 |  | 
|---|
| [c38ae92] | 522 | if ( ! q->type ) { delete q; return this; } | 
|---|
| [101e0bd] | 523 |  | 
|---|
|  | 524 | if ( ! type ) { | 
|---|
| [c38ae92] | 525 | type = q->type;                                                                 // reuse structure | 
|---|
| [1b772749] | 526 | q->type = nullptr; | 
|---|
|  | 527 | delete q; | 
|---|
| [101e0bd] | 528 | return this; | 
|---|
|  | 529 | } // if | 
|---|
|  | 530 |  | 
|---|
| [c38ae92] | 531 | if ( q->type->forall ) {                                                        // forall qualifier ? | 
|---|
|  | 532 | if ( type->forall ) {                                                   // polymorphic routine ? | 
|---|
|  | 533 | type->forall->appendList( q->type->forall ); // augment forall qualifier | 
|---|
| [101e0bd] | 534 | } else { | 
|---|
| [c38ae92] | 535 | if ( type->kind == TypeData::Aggregate ) {      // struct/union ? | 
|---|
|  | 536 | if ( type->aggregate.params ) {                 // polymorphic ? | 
|---|
|  | 537 | type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier | 
|---|
|  | 538 | } else {                                                                // not polymorphic | 
|---|
|  | 539 | type->aggregate.params = q->type->forall; // make polymorphic type | 
|---|
|  | 540 | // change implicit typedef from TYPEDEFname to TYPEGENname | 
|---|
|  | 541 | typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG ); | 
|---|
|  | 542 | } // if | 
|---|
|  | 543 | } else {                                                                        // not polymorphic | 
|---|
|  | 544 | type->forall = q->type->forall;                 // make polymorphic routine | 
|---|
| [68cd1ce] | 545 | } // if | 
|---|
|  | 546 | } // if | 
|---|
| [c38ae92] | 547 | q->type->forall = nullptr;                                              // forall qualifier moved | 
|---|
| [68cd1ce] | 548 | } // if | 
|---|
| [6ef2d81] | 549 |  | 
|---|
| [a7c90d4] | 550 | checkQualifiers( type, q->type ); | 
|---|
| [6ef2d81] | 551 | addQualifiersToType( q->type, type ); | 
|---|
|  | 552 |  | 
|---|
| [b87a5ed] | 553 | delete q; | 
|---|
|  | 554 | return this; | 
|---|
| [101e0bd] | 555 | } // addQualifiers | 
|---|
| [3848e0e] | 556 |  | 
|---|
|  | 557 | static void addTypeToType( TypeData *&src, TypeData *&dst ) { | 
|---|
| [101e0bd] | 558 | if ( src->forall && dst->kind == TypeData::Function ) { | 
|---|
|  | 559 | if ( dst->forall ) { | 
|---|
|  | 560 | dst->forall->appendList( src->forall ); | 
|---|
| [b87a5ed] | 561 | } else { | 
|---|
| [101e0bd] | 562 | dst->forall = src->forall; | 
|---|
|  | 563 | } // if | 
|---|
| [2298f728] | 564 | src->forall = nullptr; | 
|---|
| [101e0bd] | 565 | } // if | 
|---|
|  | 566 | if ( dst->base ) { | 
|---|
|  | 567 | addTypeToType( src, dst->base ); | 
|---|
|  | 568 | } else { | 
|---|
|  | 569 | switch ( dst->kind ) { | 
|---|
|  | 570 | case TypeData::Unknown: | 
|---|
| [6f95000] | 571 | src->qualifiers |= dst->qualifiers; | 
|---|
| [101e0bd] | 572 | dst = src; | 
|---|
| [2298f728] | 573 | src = nullptr; | 
|---|
| [101e0bd] | 574 | break; | 
|---|
|  | 575 | case TypeData::Basic: | 
|---|
| [6f95000] | 576 | dst->qualifiers |= src->qualifiers; | 
|---|
| [101e0bd] | 577 | if ( src->kind != TypeData::Unknown ) { | 
|---|
|  | 578 | assert( src->kind == TypeData::Basic ); | 
|---|
|  | 579 |  | 
|---|
|  | 580 | if ( dst->basictype == DeclarationNode::NoBasicType ) { | 
|---|
|  | 581 | dst->basictype = src->basictype; | 
|---|
|  | 582 | } else if ( src->basictype != DeclarationNode::NoBasicType ) | 
|---|
| [d55d7a6] | 583 | throw SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " ); | 
|---|
| [101e0bd] | 584 |  | 
|---|
|  | 585 | if ( dst->complextype == DeclarationNode::NoComplexType ) { | 
|---|
|  | 586 | dst->complextype = src->complextype; | 
|---|
|  | 587 | } else if ( src->complextype != DeclarationNode::NoComplexType ) | 
|---|
| [d55d7a6] | 588 | throw SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " ); | 
|---|
| [101e0bd] | 589 |  | 
|---|
|  | 590 | if ( dst->signedness == DeclarationNode::NoSignedness ) { | 
|---|
|  | 591 | dst->signedness = src->signedness; | 
|---|
|  | 592 | } else if ( src->signedness != DeclarationNode::NoSignedness ) | 
|---|
| [d55d7a6] | 593 | throw SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " ); | 
|---|
| [101e0bd] | 594 |  | 
|---|
|  | 595 | if ( dst->length == DeclarationNode::NoLength ) { | 
|---|
|  | 596 | dst->length = src->length; | 
|---|
|  | 597 | } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) { | 
|---|
|  | 598 | dst->length = DeclarationNode::LongLong; | 
|---|
|  | 599 | } else if ( src->length != DeclarationNode::NoLength ) | 
|---|
| [d55d7a6] | 600 | throw SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " ); | 
|---|
| [101e0bd] | 601 | } // if | 
|---|
|  | 602 | break; | 
|---|
|  | 603 | default: | 
|---|
|  | 604 | switch ( src->kind ) { | 
|---|
|  | 605 | case TypeData::Aggregate: | 
|---|
|  | 606 | case TypeData::Enum: | 
|---|
|  | 607 | dst->base = new TypeData( TypeData::AggregateInst ); | 
|---|
|  | 608 | dst->base->aggInst.aggregate = src; | 
|---|
|  | 609 | if ( src->kind == TypeData::Aggregate ) { | 
|---|
|  | 610 | dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); | 
|---|
| [68cd1ce] | 611 | } // if | 
|---|
| [6f95000] | 612 | dst->base->qualifiers |= src->qualifiers; | 
|---|
| [2298f728] | 613 | src = nullptr; | 
|---|
| [b87a5ed] | 614 | break; | 
|---|
|  | 615 | default: | 
|---|
| [101e0bd] | 616 | if ( dst->forall ) { | 
|---|
|  | 617 | dst->forall->appendList( src->forall ); | 
|---|
|  | 618 | } else { | 
|---|
|  | 619 | dst->forall = src->forall; | 
|---|
|  | 620 | } // if | 
|---|
| [2298f728] | 621 | src->forall = nullptr; | 
|---|
| [101e0bd] | 622 | dst->base = src; | 
|---|
| [2298f728] | 623 | src = nullptr; | 
|---|
| [68cd1ce] | 624 | } // switch | 
|---|
| [101e0bd] | 625 | } // switch | 
|---|
| [68cd1ce] | 626 | } // if | 
|---|
| [3848e0e] | 627 | } | 
|---|
|  | 628 |  | 
|---|
| [ba7aa2d] | 629 | DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) { | 
|---|
| [b87a5ed] | 630 | if ( o ) { | 
|---|
| [a7c90d4] | 631 | checkSpecifiers( o ); | 
|---|
|  | 632 | copySpecifiers( o ); | 
|---|
| [b87a5ed] | 633 | if ( o->type ) { | 
|---|
|  | 634 | if ( ! type ) { | 
|---|
|  | 635 | if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) { | 
|---|
|  | 636 | type = new TypeData( TypeData::AggregateInst ); | 
|---|
| [8f6f47d7] | 637 | type->aggInst.aggregate = o->type; | 
|---|
| [b87a5ed] | 638 | if ( o->type->kind == TypeData::Aggregate ) { | 
|---|
| [43c89a7] | 639 | type->aggInst.hoistType = o->type->aggregate.body; | 
|---|
| [8f6f47d7] | 640 | type->aggInst.params = maybeClone( o->type->aggregate.actuals ); | 
|---|
| [43c89a7] | 641 | } else { | 
|---|
|  | 642 | type->aggInst.hoistType = o->type->enumeration.body; | 
|---|
| [68cd1ce] | 643 | } // if | 
|---|
| [6f95000] | 644 | type->qualifiers |= o->type->qualifiers; | 
|---|
| [b87a5ed] | 645 | } else { | 
|---|
|  | 646 | type = o->type; | 
|---|
| [68cd1ce] | 647 | } // if | 
|---|
| [2298f728] | 648 | o->type = nullptr; | 
|---|
| [b87a5ed] | 649 | } else { | 
|---|
|  | 650 | addTypeToType( o->type, type ); | 
|---|
| [68cd1ce] | 651 | } // if | 
|---|
|  | 652 | } // if | 
|---|
| [b87a5ed] | 653 | if ( o->bitfieldWidth ) { | 
|---|
|  | 654 | bitfieldWidth = o->bitfieldWidth; | 
|---|
| [68cd1ce] | 655 | } // if | 
|---|
| [71bd8c6] | 656 |  | 
|---|
|  | 657 | // there may be typedefs chained onto the type | 
|---|
| [1d4580a] | 658 | if ( o->get_next() ) { | 
|---|
|  | 659 | set_last( o->get_next()->clone() ); | 
|---|
| [1db21619] | 660 | } // if | 
|---|
| [68cd1ce] | 661 | } // if | 
|---|
| [b87a5ed] | 662 | delete o; | 
|---|
|  | 663 | return this; | 
|---|
| [3848e0e] | 664 | } | 
|---|
|  | 665 |  | 
|---|
| [ba7aa2d] | 666 | DeclarationNode * DeclarationNode::addTypedef() { | 
|---|
|  | 667 | TypeData * newtype = new TypeData( TypeData::Symbolic ); | 
|---|
| [2298f728] | 668 | newtype->symbolic.params = nullptr; | 
|---|
| [8f6f47d7] | 669 | newtype->symbolic.isTypedef = true; | 
|---|
| [2298f728] | 670 | newtype->symbolic.name = name ? new string( *name ) : nullptr; | 
|---|
| [b87a5ed] | 671 | newtype->base = type; | 
|---|
|  | 672 | type = newtype; | 
|---|
|  | 673 | return this; | 
|---|
| [3848e0e] | 674 | } | 
|---|
|  | 675 |  | 
|---|
| [ba7aa2d] | 676 | DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) { | 
|---|
| [faddbd8] | 677 | if ( variable.tyClass != NoTypeClass ) { | 
|---|
| [2298f728] | 678 | if ( variable.assertions ) { | 
|---|
|  | 679 | variable.assertions->appendList( assertions ); | 
|---|
|  | 680 | } else { | 
|---|
|  | 681 | variable.assertions = assertions; | 
|---|
|  | 682 | } // if | 
|---|
|  | 683 | return this; | 
|---|
|  | 684 | } // if | 
|---|
|  | 685 |  | 
|---|
| [b87a5ed] | 686 | assert( type ); | 
|---|
|  | 687 | switch ( type->kind ) { | 
|---|
|  | 688 | case TypeData::Symbolic: | 
|---|
| [8f6f47d7] | 689 | if ( type->symbolic.assertions ) { | 
|---|
|  | 690 | type->symbolic.assertions->appendList( assertions ); | 
|---|
| [b87a5ed] | 691 | } else { | 
|---|
| [8f6f47d7] | 692 | type->symbolic.assertions = assertions; | 
|---|
| [68cd1ce] | 693 | } // if | 
|---|
| [b87a5ed] | 694 | break; | 
|---|
|  | 695 | default: | 
|---|
|  | 696 | assert( false ); | 
|---|
| [68cd1ce] | 697 | } // switch | 
|---|
| [974906e2] | 698 |  | 
|---|
| [b87a5ed] | 699 | return this; | 
|---|
| [51b73452] | 700 | } | 
|---|
|  | 701 |  | 
|---|
| [fb114fa1] | 702 | DeclarationNode * DeclarationNode::addName( string * newname ) { | 
|---|
| [2298f728] | 703 | assert( ! name ); | 
|---|
|  | 704 | name = newname; | 
|---|
| [b87a5ed] | 705 | return this; | 
|---|
| [51b73452] | 706 | } | 
|---|
|  | 707 |  | 
|---|
| [c0aa336] | 708 | DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) { | 
|---|
| [58dd019] | 709 | assert( ! asmName ); | 
|---|
| [c0aa336] | 710 | asmName = newname ? newname->asmName : nullptr; | 
|---|
|  | 711 | return this->addQualifiers( newname ); | 
|---|
| [58dd019] | 712 | } | 
|---|
|  | 713 |  | 
|---|
| [ba7aa2d] | 714 | DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) { | 
|---|
| [b87a5ed] | 715 | bitfieldWidth = size; | 
|---|
|  | 716 | return this; | 
|---|
| [51b73452] | 717 | } | 
|---|
|  | 718 |  | 
|---|
| [ba7aa2d] | 719 | DeclarationNode * DeclarationNode::addVarArgs() { | 
|---|
| [b87a5ed] | 720 | assert( type ); | 
|---|
|  | 721 | hasEllipsis = true; | 
|---|
|  | 722 | return this; | 
|---|
| [51b73452] | 723 | } | 
|---|
|  | 724 |  | 
|---|
| [c453ac4] | 725 | DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) { | 
|---|
| [b87a5ed] | 726 | assert( type ); | 
|---|
|  | 727 | assert( type->kind == TypeData::Function ); | 
|---|
| [2298f728] | 728 | assert( ! type->function.body ); | 
|---|
| [8f6f47d7] | 729 | type->function.body = body; | 
|---|
| [c453ac4] | 730 | type->function.withExprs = withExprs; | 
|---|
| [b87a5ed] | 731 | return this; | 
|---|
| [51b73452] | 732 | } | 
|---|
|  | 733 |  | 
|---|
| [ba7aa2d] | 734 | DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) { | 
|---|
| [b87a5ed] | 735 | assert( type ); | 
|---|
|  | 736 | assert( type->kind == TypeData::Function ); | 
|---|
| [2298f728] | 737 | assert( ! type->function.oldDeclList ); | 
|---|
| [8f6f47d7] | 738 | type->function.oldDeclList = list; | 
|---|
| [b87a5ed] | 739 | return this; | 
|---|
| [51b73452] | 740 | } | 
|---|
|  | 741 |  | 
|---|
| [c0aa336] | 742 | DeclarationNode * DeclarationNode::setBase( TypeData * newType ) { | 
|---|
| [b87a5ed] | 743 | if ( type ) { | 
|---|
| [ba7aa2d] | 744 | TypeData * prevBase = type; | 
|---|
|  | 745 | TypeData * curBase = type->base; | 
|---|
| [2298f728] | 746 | while ( curBase != nullptr ) { | 
|---|
| [b87a5ed] | 747 | prevBase = curBase; | 
|---|
|  | 748 | curBase = curBase->base; | 
|---|
| [68cd1ce] | 749 | } // while | 
|---|
| [b87a5ed] | 750 | prevBase->base = newType; | 
|---|
|  | 751 | } else { | 
|---|
|  | 752 | type = newType; | 
|---|
| [68cd1ce] | 753 | } // if | 
|---|
| [c0aa336] | 754 | return this; | 
|---|
| [3848e0e] | 755 | } | 
|---|
|  | 756 |  | 
|---|
| [c0aa336] | 757 | DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) { | 
|---|
|  | 758 | if ( a ) { | 
|---|
|  | 759 | for ( Attribute *attr: reverseIterate( a->attributes ) ) { | 
|---|
|  | 760 | attributes.push_front( attr ); | 
|---|
|  | 761 | } // for | 
|---|
|  | 762 | a->attributes.clear(); | 
|---|
|  | 763 | } // if | 
|---|
|  | 764 | return this; | 
|---|
|  | 765 | } // copyAttribute | 
|---|
|  | 766 |  | 
|---|
| [ba7aa2d] | 767 | DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) { | 
|---|
| [b87a5ed] | 768 | if ( p ) { | 
|---|
| [ce8c12f] | 769 | assert( p->type->kind == TypeData::Pointer || TypeData::Reference ); | 
|---|
| [c0aa336] | 770 | setBase( p->type ); | 
|---|
| [2298f728] | 771 | p->type = nullptr; | 
|---|
| [c0aa336] | 772 | copyAttribute( p ); | 
|---|
| [b87a5ed] | 773 | delete p; | 
|---|
| [68cd1ce] | 774 | } // if | 
|---|
| [b87a5ed] | 775 | return this; | 
|---|
| [3848e0e] | 776 | } | 
|---|
|  | 777 |  | 
|---|
| [ba7aa2d] | 778 | DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) { | 
|---|
| [b87a5ed] | 779 | if ( a ) { | 
|---|
|  | 780 | assert( a->type->kind == TypeData::Array ); | 
|---|
| [c0aa336] | 781 | setBase( a->type ); | 
|---|
| [2298f728] | 782 | a->type = nullptr; | 
|---|
| [c0aa336] | 783 | copyAttribute( a ); | 
|---|
| [b87a5ed] | 784 | delete a; | 
|---|
| [68cd1ce] | 785 | } // if | 
|---|
| [b87a5ed] | 786 | return this; | 
|---|
| [51b73452] | 787 | } | 
|---|
|  | 788 |  | 
|---|
| [ba7aa2d] | 789 | DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) { | 
|---|
| [b87a5ed] | 790 | if ( p ) { | 
|---|
| [e6cee92] | 791 | assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference ); | 
|---|
| [b87a5ed] | 792 | if ( type ) { | 
|---|
|  | 793 | switch ( type->kind ) { | 
|---|
|  | 794 | case TypeData::Aggregate: | 
|---|
|  | 795 | case TypeData::Enum: | 
|---|
|  | 796 | p->type->base = new TypeData( TypeData::AggregateInst ); | 
|---|
| [8f6f47d7] | 797 | p->type->base->aggInst.aggregate = type; | 
|---|
| [b87a5ed] | 798 | if ( type->kind == TypeData::Aggregate ) { | 
|---|
| [8f6f47d7] | 799 | p->type->base->aggInst.params = maybeClone( type->aggregate.actuals ); | 
|---|
| [68cd1ce] | 800 | } // if | 
|---|
| [6f95000] | 801 | p->type->base->qualifiers |= type->qualifiers; | 
|---|
| [b87a5ed] | 802 | break; | 
|---|
|  | 803 |  | 
|---|
|  | 804 | default: | 
|---|
|  | 805 | p->type->base = type; | 
|---|
| [68cd1ce] | 806 | } // switch | 
|---|
| [2298f728] | 807 | type = nullptr; | 
|---|
| [68cd1ce] | 808 | } // if | 
|---|
| [b87a5ed] | 809 | delete this; | 
|---|
|  | 810 | return p; | 
|---|
|  | 811 | } else { | 
|---|
|  | 812 | return this; | 
|---|
| [68cd1ce] | 813 | } // if | 
|---|
| [51b73452] | 814 | } | 
|---|
|  | 815 |  | 
|---|
| [ba7aa2d] | 816 | static TypeData * findLast( TypeData * a ) { | 
|---|
| [b87a5ed] | 817 | assert( a ); | 
|---|
| [ba7aa2d] | 818 | TypeData * cur = a; | 
|---|
| [a32b204] | 819 | while ( cur->base ) { | 
|---|
| [b87a5ed] | 820 | cur = cur->base; | 
|---|
| [68cd1ce] | 821 | } // while | 
|---|
| [b87a5ed] | 822 | return cur; | 
|---|
| [3848e0e] | 823 | } | 
|---|
|  | 824 |  | 
|---|
| [ba7aa2d] | 825 | DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) { | 
|---|
| [738e304] | 826 | if ( ! a ) return this; | 
|---|
|  | 827 | assert( a->type->kind == TypeData::Array ); | 
|---|
|  | 828 | TypeData * lastArray = findLast( a->type ); | 
|---|
|  | 829 | if ( type ) { | 
|---|
|  | 830 | switch ( type->kind ) { | 
|---|
|  | 831 | case TypeData::Aggregate: | 
|---|
|  | 832 | case TypeData::Enum: | 
|---|
|  | 833 | lastArray->base = new TypeData( TypeData::AggregateInst ); | 
|---|
|  | 834 | lastArray->base->aggInst.aggregate = type; | 
|---|
|  | 835 | if ( type->kind == TypeData::Aggregate ) { | 
|---|
|  | 836 | lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals ); | 
|---|
|  | 837 | } // if | 
|---|
| [6f95000] | 838 | lastArray->base->qualifiers |= type->qualifiers; | 
|---|
| [738e304] | 839 | break; | 
|---|
|  | 840 | default: | 
|---|
|  | 841 | lastArray->base = type; | 
|---|
|  | 842 | } // switch | 
|---|
|  | 843 | type = nullptr; | 
|---|
| [68cd1ce] | 844 | } // if | 
|---|
| [738e304] | 845 | delete this; | 
|---|
|  | 846 | return a; | 
|---|
| [51b73452] | 847 | } | 
|---|
| [3848e0e] | 848 |  | 
|---|
| [ba7aa2d] | 849 | DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) { | 
|---|
|  | 850 | TypeData * ftype = new TypeData( TypeData::Function ); | 
|---|
| [8f6f47d7] | 851 | ftype->function.params = params; | 
|---|
| [c0aa336] | 852 | setBase( ftype ); | 
|---|
| [b87a5ed] | 853 | return this; | 
|---|
| [3848e0e] | 854 | } | 
|---|
|  | 855 |  | 
|---|
| [ba7aa2d] | 856 | static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) { | 
|---|
| [b87a5ed] | 857 | if ( type ) { | 
|---|
|  | 858 | if ( type->kind != TypeData::Function ) { | 
|---|
|  | 859 | type->base = addIdListToType( type->base, ids ); | 
|---|
|  | 860 | } else { | 
|---|
| [8f6f47d7] | 861 | type->function.idList = ids; | 
|---|
| [68cd1ce] | 862 | } // if | 
|---|
| [b87a5ed] | 863 | return type; | 
|---|
| [3848e0e] | 864 | } else { | 
|---|
| [ba7aa2d] | 865 | TypeData * newtype = new TypeData( TypeData::Function ); | 
|---|
| [8f6f47d7] | 866 | newtype->function.idList = ids; | 
|---|
| [b87a5ed] | 867 | return newtype; | 
|---|
| [68cd1ce] | 868 | } // if | 
|---|
| [2298f728] | 869 | } // addIdListToType | 
|---|
| [974906e2] | 870 |  | 
|---|
| [ba7aa2d] | 871 | DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) { | 
|---|
| [b87a5ed] | 872 | type = addIdListToType( type, ids ); | 
|---|
|  | 873 | return this; | 
|---|
| [3848e0e] | 874 | } | 
|---|
|  | 875 |  | 
|---|
| [ba7aa2d] | 876 | DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) { | 
|---|
| [b87a5ed] | 877 | initializer = init; | 
|---|
|  | 878 | return this; | 
|---|
| [3848e0e] | 879 | } | 
|---|
|  | 880 |  | 
|---|
| [67cf18c] | 881 | DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) { | 
|---|
|  | 882 | assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." ); | 
|---|
|  | 883 | variable.initializer = init; | 
|---|
|  | 884 | return this; | 
|---|
|  | 885 | } | 
|---|
|  | 886 |  | 
|---|
| [ba7aa2d] | 887 | DeclarationNode * DeclarationNode::cloneType( string * newName ) { | 
|---|
|  | 888 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 889 | newnode->type = maybeClone( type ); | 
|---|
| [a7c90d4] | 890 | newnode->copySpecifiers( this ); | 
|---|
| [ba7aa2d] | 891 | assert( newName ); | 
|---|
| [2298f728] | 892 | newnode->name = newName; | 
|---|
| [b87a5ed] | 893 | return newnode; | 
|---|
| [3848e0e] | 894 | } | 
|---|
|  | 895 |  | 
|---|
| [2298f728] | 896 | DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) { | 
|---|
|  | 897 | if ( ! o ) return nullptr; | 
|---|
|  | 898 |  | 
|---|
| [a7c90d4] | 899 | o->copySpecifiers( this ); | 
|---|
| [2298f728] | 900 | if ( type ) { | 
|---|
|  | 901 | TypeData * srcType = type; | 
|---|
|  | 902 |  | 
|---|
| [c0aa336] | 903 | // search for the base type by scanning off pointers and array designators | 
|---|
| [2298f728] | 904 | while ( srcType->base ) { | 
|---|
|  | 905 | srcType = srcType->base; | 
|---|
|  | 906 | } // while | 
|---|
|  | 907 |  | 
|---|
|  | 908 | TypeData * newType = srcType->clone(); | 
|---|
|  | 909 | if ( newType->kind == TypeData::AggregateInst ) { | 
|---|
|  | 910 | // don't duplicate members | 
|---|
|  | 911 | if ( newType->aggInst.aggregate->kind == TypeData::Enum ) { | 
|---|
|  | 912 | delete newType->aggInst.aggregate->enumeration.constants; | 
|---|
|  | 913 | newType->aggInst.aggregate->enumeration.constants = nullptr; | 
|---|
| [b87a5ed] | 914 | } else { | 
|---|
| [2298f728] | 915 | assert( newType->aggInst.aggregate->kind == TypeData::Aggregate ); | 
|---|
|  | 916 | delete newType->aggInst.aggregate->aggregate.fields; | 
|---|
|  | 917 | newType->aggInst.aggregate->aggregate.fields = nullptr; | 
|---|
| [68cd1ce] | 918 | } // if | 
|---|
| [43c89a7] | 919 | // don't hoist twice | 
|---|
|  | 920 | newType->aggInst.hoistType = false; | 
|---|
| [68cd1ce] | 921 | } // if | 
|---|
| [2298f728] | 922 |  | 
|---|
|  | 923 | newType->forall = maybeClone( type->forall ); | 
|---|
|  | 924 | if ( ! o->type ) { | 
|---|
|  | 925 | o->type = newType; | 
|---|
|  | 926 | } else { | 
|---|
|  | 927 | addTypeToType( newType, o->type ); | 
|---|
|  | 928 | delete newType; | 
|---|
|  | 929 | } // if | 
|---|
| [68cd1ce] | 930 | } // if | 
|---|
| [b87a5ed] | 931 | return o; | 
|---|
| [51b73452] | 932 | } | 
|---|
|  | 933 |  | 
|---|
| [ba7aa2d] | 934 | DeclarationNode * DeclarationNode::extractAggregate() const { | 
|---|
| [b87a5ed] | 935 | if ( type ) { | 
|---|
| [ba7aa2d] | 936 | TypeData * ret = typeextractAggregate( type ); | 
|---|
| [b87a5ed] | 937 | if ( ret ) { | 
|---|
| [ba7aa2d] | 938 | DeclarationNode * newnode = new DeclarationNode; | 
|---|
| [b87a5ed] | 939 | newnode->type = ret; | 
|---|
|  | 940 | return newnode; | 
|---|
| [843054c2] | 941 | } // if | 
|---|
|  | 942 | } // if | 
|---|
| [2298f728] | 943 | return nullptr; | 
|---|
| [3848e0e] | 944 | } | 
|---|
|  | 945 |  | 
|---|
| [ba7aa2d] | 946 | void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) { | 
|---|
| [b87a5ed] | 947 | SemanticError errors; | 
|---|
| [7880579] | 948 | std::back_insert_iterator< std::list< Declaration * > > out( outputList ); | 
|---|
| [2298f728] | 949 |  | 
|---|
| [3a5131ed] | 950 | for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { | 
|---|
| [b87a5ed] | 951 | try { | 
|---|
| [ba7aa2d] | 952 | if ( DeclarationNode * extr = cur->extractAggregate() ) { | 
|---|
| [b87a5ed] | 953 | // handle the case where a structure declaration is contained within an object or type declaration | 
|---|
| [ba7aa2d] | 954 | Declaration * decl = extr->build(); | 
|---|
| [b87a5ed] | 955 | if ( decl ) { | 
|---|
| [294647b] | 956 | decl->location = cur->location; | 
|---|
| [ba7aa2d] | 957 | * out++ = decl; | 
|---|
| [843054c2] | 958 | } // if | 
|---|
| [f39096c] | 959 | delete extr; | 
|---|
| [843054c2] | 960 | } // if | 
|---|
| [2298f728] | 961 |  | 
|---|
| [ba7aa2d] | 962 | Declaration * decl = cur->build(); | 
|---|
| [b87a5ed] | 963 | if ( decl ) { | 
|---|
| [294647b] | 964 | decl->location = cur->location; | 
|---|
| [ba7aa2d] | 965 | * out++ = decl; | 
|---|
| [843054c2] | 966 | } // if | 
|---|
| [b87a5ed] | 967 | } catch( SemanticError &e ) { | 
|---|
|  | 968 | errors.append( e ); | 
|---|
| [843054c2] | 969 | } // try | 
|---|
|  | 970 | } // while | 
|---|
| [2298f728] | 971 |  | 
|---|
| [b87a5ed] | 972 | if ( ! errors.isEmpty() ) { | 
|---|
|  | 973 | throw errors; | 
|---|
| [843054c2] | 974 | } // if | 
|---|
| [2298f728] | 975 | } // buildList | 
|---|
| [3848e0e] | 976 |  | 
|---|
| [ba7aa2d] | 977 | void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) { | 
|---|
| [b87a5ed] | 978 | SemanticError errors; | 
|---|
| [7880579] | 979 | std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); | 
|---|
| [43c89a7] | 980 |  | 
|---|
| [3a5131ed] | 981 | for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { | 
|---|
| [b87a5ed] | 982 | try { | 
|---|
| [ba7aa2d] | 983 | Declaration * decl = cur->build(); | 
|---|
| [b87a5ed] | 984 | if ( decl ) { | 
|---|
| [ba7aa2d] | 985 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { | 
|---|
| [294647b] | 986 | dwt->location = cur->location; | 
|---|
| [ba7aa2d] | 987 | * out++ = dwt; | 
|---|
|  | 988 | } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) { | 
|---|
|  | 989 | StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); | 
|---|
| [68fe077a] | 990 | auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); | 
|---|
| [294647b] | 991 | obj->location = cur->location; | 
|---|
| [43c89a7] | 992 | * out++ = obj; | 
|---|
| [b87a5ed] | 993 | delete agg; | 
|---|
| [ba7aa2d] | 994 | } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { | 
|---|
|  | 995 | UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); | 
|---|
| [68fe077a] | 996 | auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); | 
|---|
| [294647b] | 997 | obj->location = cur->location; | 
|---|
|  | 998 | * out++ = obj; | 
|---|
| [843054c2] | 999 | } // if | 
|---|
|  | 1000 | } // if | 
|---|
| [b87a5ed] | 1001 | } catch( SemanticError &e ) { | 
|---|
|  | 1002 | errors.append( e ); | 
|---|
| [843054c2] | 1003 | } // try | 
|---|
| [3a5131ed] | 1004 | } // for | 
|---|
|  | 1005 |  | 
|---|
| [b87a5ed] | 1006 | if ( ! errors.isEmpty() ) { | 
|---|
|  | 1007 | throw errors; | 
|---|
| [843054c2] | 1008 | } // if | 
|---|
| [2298f728] | 1009 | } // buildList | 
|---|
| [3848e0e] | 1010 |  | 
|---|
| [ba7aa2d] | 1011 | void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) { | 
|---|
| [b87a5ed] | 1012 | SemanticError errors; | 
|---|
| [7880579] | 1013 | std::back_insert_iterator< std::list< Type * > > out( outputList ); | 
|---|
| [ba7aa2d] | 1014 | const DeclarationNode * cur = firstNode; | 
|---|
| [2298f728] | 1015 |  | 
|---|
| [a32b204] | 1016 | while ( cur ) { | 
|---|
| [b87a5ed] | 1017 | try { | 
|---|
| [ba7aa2d] | 1018 | * out++ = cur->buildType(); | 
|---|
| [b87a5ed] | 1019 | } catch( SemanticError &e ) { | 
|---|
|  | 1020 | errors.append( e ); | 
|---|
| [843054c2] | 1021 | } // try | 
|---|
| [7880579] | 1022 | cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); | 
|---|
| [843054c2] | 1023 | } // while | 
|---|
| [2298f728] | 1024 |  | 
|---|
| [b87a5ed] | 1025 | if ( ! errors.isEmpty() ) { | 
|---|
|  | 1026 | throw errors; | 
|---|
| [843054c2] | 1027 | } // if | 
|---|
| [2298f728] | 1028 | } // buildTypeList | 
|---|
| [51b73452] | 1029 |  | 
|---|
| [ba7aa2d] | 1030 | Declaration * DeclarationNode::build() const { | 
|---|
| [d55d7a6] | 1031 | if ( ! error.empty() ) throw SemanticError( this, error + " in declaration of " ); | 
|---|
| [2298f728] | 1032 |  | 
|---|
| [e994912] | 1033 | if ( asmStmt ) { | 
|---|
| [e3e16bc] | 1034 | return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) ); | 
|---|
| [e994912] | 1035 | } // if | 
|---|
|  | 1036 |  | 
|---|
| [faddbd8] | 1037 | if ( variable.tyClass != NoTypeClass ) { | 
|---|
| [f0ecf9b] | 1038 | // otype is internally converted to dtype + otype parameters | 
|---|
|  | 1039 | static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype }; | 
|---|
|  | 1040 | assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." ); | 
|---|
| [8f60f0b] | 1041 | assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." ); | 
|---|
| [f0ecf9b] | 1042 | TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr ); | 
|---|
| [2298f728] | 1043 | buildList( variable.assertions, ret->get_assertions() ); | 
|---|
|  | 1044 | return ret; | 
|---|
|  | 1045 | } // if | 
|---|
|  | 1046 |  | 
|---|
| [843054c2] | 1047 | if ( type ) { | 
|---|
| [dd020c0] | 1048 | // Function specifiers can only appear on a function definition/declaration. | 
|---|
|  | 1049 | // | 
|---|
|  | 1050 | //    inline _Noreturn int f();                 // allowed | 
|---|
|  | 1051 | //    inline _Noreturn int g( int i );  // allowed | 
|---|
|  | 1052 | //    inline _Noreturn int i;                   // disallowed | 
|---|
| [fb04321] | 1053 | if ( type->kind != TypeData::Function && funcSpecs.any() ) { | 
|---|
| [d55d7a6] | 1054 | throw SemanticError( this, "invalid function specifier for " ); | 
|---|
| [dd020c0] | 1055 | } // if | 
|---|
| [a7c90d4] | 1056 | return buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension ); | 
|---|
| [843054c2] | 1057 | } // if | 
|---|
| [2298f728] | 1058 |  | 
|---|
| [dd020c0] | 1059 | // SUE's cannot have function specifiers, either | 
|---|
|  | 1060 | // | 
|---|
|  | 1061 | //    inlne _Noreturn struct S { ... };         // disallowed | 
|---|
|  | 1062 | //    inlne _Noreturn enum   E { ... };         // disallowed | 
|---|
| [fb04321] | 1063 | if ( funcSpecs.any() ) { | 
|---|
| [d55d7a6] | 1064 | throw SemanticError( this, "invalid function specifier for " ); | 
|---|
| [843054c2] | 1065 | } // if | 
|---|
| [dd020c0] | 1066 | assertf( name, "ObjectDecl must a have name\n" ); | 
|---|
| [a7c90d4] | 1067 | return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension ); | 
|---|
| [51b73452] | 1068 | } | 
|---|
|  | 1069 |  | 
|---|
| [ba7aa2d] | 1070 | Type * DeclarationNode::buildType() const { | 
|---|
| [b87a5ed] | 1071 | assert( type ); | 
|---|
| [974906e2] | 1072 |  | 
|---|
| [faddbd8] | 1073 | if ( attr.expr ) { | 
|---|
| [c0aa336] | 1074 | return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes ); | 
|---|
| [faddbd8] | 1075 | } else if ( attr.type ) { | 
|---|
| [c0aa336] | 1076 | return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes ); | 
|---|
| [2298f728] | 1077 | } // if | 
|---|
|  | 1078 |  | 
|---|
| [b87a5ed] | 1079 | switch ( type->kind ) { | 
|---|
| [43c89a7] | 1080 | case TypeData::Enum: | 
|---|
| [b87a5ed] | 1081 | case TypeData::Aggregate: { | 
|---|
| [fa4805f] | 1082 | ReferenceToType * ret = buildComAggInst( type, attributes, linkage ); | 
|---|
| [8f6f47d7] | 1083 | buildList( type->aggregate.actuals, ret->get_parameters() ); | 
|---|
| [b87a5ed] | 1084 | return ret; | 
|---|
|  | 1085 | } | 
|---|
|  | 1086 | case TypeData::Symbolic: { | 
|---|
| [c0aa336] | 1087 | TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes ); | 
|---|
| [8f6f47d7] | 1088 | buildList( type->symbolic.actuals, ret->get_parameters() ); | 
|---|
| [b87a5ed] | 1089 | return ret; | 
|---|
|  | 1090 | } | 
|---|
|  | 1091 | default: | 
|---|
| [c0aa336] | 1092 | Type * simpletypes = typebuild( type ); | 
|---|
|  | 1093 | simpletypes->get_attributes() = attributes;             // copy because member is const | 
|---|
|  | 1094 | return simpletypes; | 
|---|
| [b87a5ed] | 1095 | } // switch | 
|---|
| [3848e0e] | 1096 | } | 
|---|
|  | 1097 |  | 
|---|
| [b87a5ed] | 1098 | // Local Variables: // | 
|---|
|  | 1099 | // tab-width: 4 // | 
|---|
|  | 1100 | // mode: c++ // | 
|---|
|  | 1101 | // compile-command: "make install" // | 
|---|
|  | 1102 | // End: // | 
|---|