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