[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
|
---|
[0d0931d] | 11 | // Last Modified By : Andrew Beach
|
---|
[bb7422a] | 12 | // Last Modified On : Tue Apr 4 10:28:00 2023
|
---|
| 13 | // Update Count : 1392
|
---|
[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 |
|
---|
[bb7422a] | 23 | #include "AST/Attribute.hpp" // for Attribute
|
---|
| 24 | #include "AST/Copy.hpp" // for shallowCopy
|
---|
| 25 | #include "AST/Decl.hpp" // for Decl
|
---|
| 26 | #include "AST/Expr.hpp" // for Expr
|
---|
| 27 | #include "AST/Print.hpp" // for print
|
---|
| 28 | #include "AST/Stmt.hpp" // for AsmStmt, DirectiveStmt
|
---|
| 29 | #include "AST/StorageClasses.hpp" // for Storage::Class
|
---|
| 30 | #include "AST/Type.hpp" // for Type
|
---|
| 31 | #include "Common/CodeLocation.h" // for CodeLocation
|
---|
| 32 | #include "Common/Iterate.hpp" // for reverseIterate
|
---|
[d180746] | 33 | #include "Common/SemanticError.h" // for SemanticError
|
---|
| 34 | #include "Common/UniqueName.h" // for UniqueName
|
---|
[bb7422a] | 35 | #include "Common/utility.h" // for maybeClone
|
---|
[d180746] | 36 | #include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode
|
---|
| 37 | #include "TypeData.h" // for TypeData, TypeData::Aggregate_t
|
---|
[2f0a0678] | 38 | #include "TypedefTable.h" // for TypedefTable
|
---|
[bdd516a] | 39 |
|
---|
[d180746] | 40 | class Initializer;
|
---|
[51b73452] | 41 |
|
---|
[de62360d] | 42 | extern TypedefTable typedefTable;
|
---|
| 43 |
|
---|
[51b73452] | 44 | using namespace std;
|
---|
| 45 |
|
---|
[201aeb9] | 46 | // These must harmonize with the corresponding DeclarationNode enumerations.
|
---|
[702e826] | 47 | const char * DeclarationNode::basicTypeNames[] = {
|
---|
| 48 | "void", "_Bool", "char", "int", "int128",
|
---|
| 49 | "float", "double", "long double", "float80", "float128",
|
---|
| 50 | "_float16", "_float32", "_float32x", "_float64", "_float64x", "_float128", "_float128x", "NoBasicTypeNames"
|
---|
| 51 | };
|
---|
| 52 | const char * DeclarationNode::complexTypeNames[] = {
|
---|
| 53 | "_Complex", "NoComplexTypeNames", "_Imaginary"
|
---|
| 54 | }; // Imaginary unsupported => parse, but make invisible and print error message
|
---|
| 55 | const char * DeclarationNode::signednessNames[] = {
|
---|
| 56 | "signed", "unsigned", "NoSignednessNames"
|
---|
| 57 | };
|
---|
| 58 | const char * DeclarationNode::lengthNames[] = {
|
---|
| 59 | "short", "long", "long long", "NoLengthNames"
|
---|
| 60 | };
|
---|
| 61 | const char * DeclarationNode::builtinTypeNames[] = {
|
---|
| 62 | "__builtin_va_list", "__auto_type", "zero_t", "one_t", "NoBuiltinTypeNames"
|
---|
| 63 | };
|
---|
[51b73452] | 64 |
|
---|
| 65 | UniqueName DeclarationNode::anonymous( "__anonymous" );
|
---|
| 66 |
|
---|
[bb7422a] | 67 | extern ast::Linkage::Spec linkage; // defined in parser.yy
|
---|
[51b73452] | 68 |
|
---|
[7d05e7e] | 69 | DeclarationNode::DeclarationNode() :
|
---|
[e07caa2] | 70 | linkage( ::linkage ) {
|
---|
[2298f728] | 71 |
|
---|
[faddbd8] | 72 | // variable.name = nullptr;
|
---|
[bb7422a] | 73 | variable.tyClass = ast::TypeDecl::NUMBER_OF_KINDS;
|
---|
[28307be] | 74 | variable.assertions = nullptr;
|
---|
[67cf18c] | 75 | variable.initializer = nullptr;
|
---|
[7d05e7e] | 76 |
|
---|
[f6e3e34] | 77 | assert.condition = nullptr;
|
---|
| 78 | assert.message = nullptr;
|
---|
[28307be] | 79 | }
|
---|
| 80 |
|
---|
| 81 | DeclarationNode::~DeclarationNode() {
|
---|
[faddbd8] | 82 | // delete variable.name;
|
---|
[2298f728] | 83 | delete variable.assertions;
|
---|
[67cf18c] | 84 | delete variable.initializer;
|
---|
[2298f728] | 85 |
|
---|
[702e826] | 86 | // delete type;
|
---|
[28307be] | 87 | delete bitfieldWidth;
|
---|
[e994912] | 88 |
|
---|
| 89 | delete asmStmt;
|
---|
[58dd019] | 90 | // asmName, no delete, passed to next stage
|
---|
[28307be] | 91 | delete initializer;
|
---|
[f6e3e34] | 92 |
|
---|
| 93 | delete assert.condition;
|
---|
| 94 | delete assert.message;
|
---|
[28307be] | 95 | }
|
---|
| 96 |
|
---|
[ba7aa2d] | 97 | DeclarationNode * DeclarationNode::clone() const {
|
---|
| 98 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[c0aa336] | 99 | newnode->set_next( maybeClone( get_next() ) );
|
---|
[2298f728] | 100 | newnode->name = name ? new string( *name ) : nullptr;
|
---|
[c0aa336] | 101 |
|
---|
[2e5fa345] | 102 | newnode->builtin = NoBuiltinType;
|
---|
[c0aa336] | 103 | newnode->type = maybeClone( type );
|
---|
[679e644] | 104 | newnode->inLine = inLine;
|
---|
[a7c90d4] | 105 | newnode->storageClasses = storageClasses;
|
---|
| 106 | newnode->funcSpecs = funcSpecs;
|
---|
[08d5507b] | 107 | newnode->bitfieldWidth = maybeClone( bitfieldWidth );
|
---|
[c0aa336] | 108 | newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) );
|
---|
[b87a5ed] | 109 | newnode->hasEllipsis = hasEllipsis;
|
---|
| 110 | newnode->linkage = linkage;
|
---|
[bb7422a] | 111 | newnode->asmName = maybeCopy( asmName );
|
---|
| 112 | newnode->attributes = attributes;
|
---|
[c0aa336] | 113 | newnode->initializer = maybeClone( initializer );
|
---|
| 114 | newnode->extension = extension;
|
---|
[e994912] | 115 | newnode->asmStmt = maybeClone( asmStmt );
|
---|
[c0aa336] | 116 | newnode->error = error;
|
---|
[3848e0e] | 117 |
|
---|
[faddbd8] | 118 | // newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
|
---|
[28307be] | 119 | newnode->variable.tyClass = variable.tyClass;
|
---|
[fb114fa1] | 120 | newnode->variable.assertions = maybeClone( variable.assertions );
|
---|
[67cf18c] | 121 | newnode->variable.initializer = maybeClone( variable.initializer );
|
---|
[3848e0e] | 122 |
|
---|
[f6e3e34] | 123 | newnode->assert.condition = maybeClone( assert.condition );
|
---|
[bb7422a] | 124 | newnode->assert.message = maybeCopy( assert.message );
|
---|
[28307be] | 125 | return newnode;
|
---|
| 126 | } // DeclarationNode::clone
|
---|
[3848e0e] | 127 |
|
---|
[f2f512ba] | 128 | void DeclarationNode::print( std::ostream & os, int indent ) const {
|
---|
[59db689] | 129 | os << string( indent, ' ' );
|
---|
[2298f728] | 130 | if ( name ) {
|
---|
| 131 | os << *name << ": ";
|
---|
[68cd1ce] | 132 | } // if
|
---|
[51b73452] | 133 |
|
---|
[bb7422a] | 134 | if ( linkage != ast::Linkage::Cforall ) {
|
---|
| 135 | os << ast::Linkage::name( linkage ) << " ";
|
---|
[68cd1ce] | 136 | } // if
|
---|
[3848e0e] | 137 |
|
---|
[bb7422a] | 138 | ast::print( os, storageClasses );
|
---|
| 139 | ast::print( os, funcSpecs );
|
---|
[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();
|
---|
[68cd1ce] | 156 | } // if
|
---|
[3848e0e] | 157 |
|
---|
[692c1cc] | 158 | if ( ! attributes.empty() ) {
|
---|
| 159 | os << string( indent + 2, ' ' ) << "with attributes " << endl;
|
---|
[bb7422a] | 160 | for ( ast::ptr<ast::Attribute> const & attr : reverseIterate( attributes ) ) {
|
---|
[692c1cc] | 161 | os << string( indent + 4, ' ' ) << attr->name.c_str() << endl;
|
---|
| 162 | } // for
|
---|
| 163 | } // if
|
---|
[66406f3] | 164 |
|
---|
[b87a5ed] | 165 | os << endl;
|
---|
[51b73452] | 166 | }
|
---|
| 167 |
|
---|
[f2f512ba] | 168 | void DeclarationNode::printList( std::ostream & os, int indent ) const {
|
---|
[b87a5ed] | 169 | ParseNode::printList( os, indent );
|
---|
| 170 | if ( hasEllipsis ) {
|
---|
| 171 | os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
|
---|
[68cd1ce] | 172 | } // if
|
---|
[51b73452] | 173 | }
|
---|
| 174 |
|
---|
[bb7422a] | 175 | DeclarationNode * DeclarationNode::newStorageClass( ast::Storage::Classes sc ) {
|
---|
[ba7aa2d] | 176 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[08d5507b] | 177 | newnode->storageClasses = sc;
|
---|
[b87a5ed] | 178 | return newnode;
|
---|
[dd020c0] | 179 | } // DeclarationNode::newStorageClass
|
---|
[3848e0e] | 180 |
|
---|
[bb7422a] | 181 | DeclarationNode * DeclarationNode::newFuncSpecifier( ast::Function::Specs fs ) {
|
---|
[ba7aa2d] | 182 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[08d5507b] | 183 | newnode->funcSpecs = fs;
|
---|
[c1c1112] | 184 | return newnode;
|
---|
[dd020c0] | 185 | } // DeclarationNode::newFuncSpecifier
|
---|
[c1c1112] | 186 |
|
---|
[bb7422a] | 187 | DeclarationNode * DeclarationNode::newTypeQualifier( ast::CV::Qualifiers tq ) {
|
---|
[ba7aa2d] | 188 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[dd020c0] | 189 | newnode->type = new TypeData();
|
---|
[738e304] | 190 | newnode->type->qualifiers = tq;
|
---|
[b87a5ed] | 191 | return newnode;
|
---|
[dd020c0] | 192 | } // DeclarationNode::newQualifier
|
---|
[3848e0e] | 193 |
|
---|
[c1c1112] | 194 | DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
|
---|
[ba7aa2d] | 195 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[b87a5ed] | 196 | newnode->type = new TypeData( TypeData::Basic );
|
---|
[5b639ee] | 197 | newnode->type->basictype = bt;
|
---|
[b87a5ed] | 198 | return newnode;
|
---|
[984dce6] | 199 | } // DeclarationNode::newBasicType
|
---|
[3848e0e] | 200 |
|
---|
[5b639ee] | 201 | DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
|
---|
[ba7aa2d] | 202 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[b87a5ed] | 203 | newnode->type = new TypeData( TypeData::Basic );
|
---|
[5b639ee] | 204 | newnode->type->complextype = ct;
|
---|
[b87a5ed] | 205 | return newnode;
|
---|
[5b639ee] | 206 | } // DeclarationNode::newComplexType
|
---|
| 207 |
|
---|
| 208 | DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
|
---|
[ba7aa2d] | 209 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[5b639ee] | 210 | newnode->type = new TypeData( TypeData::Basic );
|
---|
| 211 | newnode->type->signedness = sn;
|
---|
| 212 | return newnode;
|
---|
| 213 | } // DeclarationNode::newSignedNess
|
---|
| 214 |
|
---|
| 215 | DeclarationNode * DeclarationNode::newLength( Length lnth ) {
|
---|
[ba7aa2d] | 216 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[5b639ee] | 217 | newnode->type = new TypeData( TypeData::Basic );
|
---|
| 218 | newnode->type->length = lnth;
|
---|
| 219 | return newnode;
|
---|
| 220 | } // DeclarationNode::newLength
|
---|
[3848e0e] | 221 |
|
---|
[dd020c0] | 222 | DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
|
---|
| 223 | DeclarationNode * newnode = new DeclarationNode;
|
---|
| 224 | newnode->type = new TypeData( TypeData::Unknown );
|
---|
| 225 | newnode->type->forall = forall;
|
---|
| 226 | return newnode;
|
---|
| 227 | } // DeclarationNode::newForall
|
---|
| 228 |
|
---|
[47498bd] | 229 | DeclarationNode * DeclarationNode::newFromGlobalScope() {
|
---|
| 230 | DeclarationNode * newnode = new DeclarationNode;
|
---|
| 231 | newnode->type = new TypeData( TypeData::GlobalScope );
|
---|
| 232 | return newnode;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[c5d7701] | 235 | DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) {
|
---|
[c194661] | 236 | DeclarationNode * newnode = new DeclarationNode;
|
---|
| 237 | newnode->type = new TypeData( TypeData::Qualified );
|
---|
| 238 | newnode->type->qualified.parent = parent->type;
|
---|
| 239 | newnode->type->qualified.child = child->type;
|
---|
| 240 | parent->type = nullptr;
|
---|
| 241 | child->type = nullptr;
|
---|
| 242 | delete parent;
|
---|
| 243 | delete child;
|
---|
| 244 | return newnode;
|
---|
[c5d7701] | 245 | }
|
---|
| 246 |
|
---|
[bb7422a] | 247 | DeclarationNode * DeclarationNode::newAggregate( ast::AggregateDecl::Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
|
---|
[ba7aa2d] | 248 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[b87a5ed] | 249 | newnode->type = new TypeData( TypeData::Aggregate );
|
---|
[8f6f47d7] | 250 | newnode->type->aggregate.kind = kind;
|
---|
[692c1cc] | 251 | newnode->type->aggregate.anon = name == nullptr;
|
---|
| 252 | newnode->type->aggregate.name = newnode->type->aggregate.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
|
---|
[8f6f47d7] | 253 | newnode->type->aggregate.actuals = actuals;
|
---|
| 254 | newnode->type->aggregate.fields = fields;
|
---|
| 255 | newnode->type->aggregate.body = body;
|
---|
[6ea87486] | 256 | newnode->type->aggregate.tagged = false;
|
---|
| 257 | newnode->type->aggregate.parent = nullptr;
|
---|
[b87a5ed] | 258 | return newnode;
|
---|
[984dce6] | 259 | } // DeclarationNode::newAggregate
|
---|
[3848e0e] | 260 |
|
---|
[e4d7c1c] | 261 | DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, bool typed, DeclarationNode * base, EnumHiding hiding ) {
|
---|
[ba7aa2d] | 262 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[b87a5ed] | 263 | newnode->type = new TypeData( TypeData::Enum );
|
---|
[692c1cc] | 264 | newnode->type->enumeration.anon = name == nullptr;
|
---|
| 265 | newnode->type->enumeration.name = newnode->type->enumeration.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
|
---|
[8f6f47d7] | 266 | newnode->type->enumeration.constants = constants;
|
---|
[ca1a547] | 267 | newnode->type->enumeration.body = body;
|
---|
[b0d9ff7] | 268 | newnode->type->enumeration.typed = typed;
|
---|
[e4d7c1c] | 269 | newnode->type->enumeration.hiding = hiding;
|
---|
[78e2fca] | 270 | if ( base && base->type ) {
|
---|
[ed9a1ae] | 271 | newnode->type->base = base->type;
|
---|
[9e7236f4] | 272 | } // if
|
---|
| 273 |
|
---|
[b87a5ed] | 274 | return newnode;
|
---|
[984dce6] | 275 | } // DeclarationNode::newEnum
|
---|
[3848e0e] | 276 |
|
---|
[a46b69c] | 277 | DeclarationNode * DeclarationNode::newName( const string * name ) {
|
---|
[ba7aa2d] | 278 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[a46b69c] | 279 | assert( ! newnode->name );
|
---|
[2298f728] | 280 | newnode->name = name;
|
---|
[a46b69c] | 281 | return newnode;
|
---|
| 282 | } // DeclarationNode::newName
|
---|
| 283 |
|
---|
[374cb117] | 284 | DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) {
|
---|
[a46b69c] | 285 | DeclarationNode * newnode = newName( name );
|
---|
[4f147cc] | 286 | newnode->enumeratorValue.reset( constant );
|
---|
[b87a5ed] | 287 | return newnode;
|
---|
[984dce6] | 288 | } // DeclarationNode::newEnumConstant
|
---|
[3848e0e] | 289 |
|
---|
[374cb117] | 290 | DeclarationNode * DeclarationNode::newEnumValueGeneric( const string * name, InitializerNode * init ) {
|
---|
[b0d9ff7] | 291 | if ( init ) {
|
---|
| 292 | if ( init->get_expression() ) {
|
---|
[374cb117] | 293 | return newEnumConstant( name, init->get_expression() );
|
---|
[b0d9ff7] | 294 | } else {
|
---|
[374cb117] | 295 | DeclarationNode * newnode = newName( name );
|
---|
| 296 | newnode->initializer = init;
|
---|
| 297 | return newnode;
|
---|
| 298 | } // if
|
---|
| 299 | } else {
|
---|
[b0d9ff7] | 300 | return newName( name );
|
---|
[374cb117] | 301 | } // if
|
---|
[9e7236f4] | 302 | } // DeclarationNode::newEnumValueGeneric
|
---|
[374cb117] | 303 |
|
---|
[1e30df7] | 304 | DeclarationNode * DeclarationNode::newEnumInLine( const string name ) {
|
---|
| 305 | DeclarationNode * newnode = newName( new std::string(name) );
|
---|
| 306 | newnode->enumInLine = true;
|
---|
| 307 | return newnode;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[a46b69c] | 310 | DeclarationNode * DeclarationNode::newFromTypedef( const string * name ) {
|
---|
[ba7aa2d] | 311 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[a46b69c] | 312 | newnode->type = new TypeData( TypeData::SymbolicInst );
|
---|
| 313 | newnode->type->symbolic.name = name;
|
---|
| 314 | newnode->type->symbolic.isTypedef = true;
|
---|
| 315 | newnode->type->symbolic.params = nullptr;
|
---|
[b87a5ed] | 316 | return newnode;
|
---|
[a46b69c] | 317 | } // DeclarationNode::newFromTypedef
|
---|
[3848e0e] | 318 |
|
---|
[25bca42] | 319 | DeclarationNode * DeclarationNode::newFromTypeGen( const string * name, ExpressionNode * params ) {
|
---|
[ba7aa2d] | 320 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[b87a5ed] | 321 | newnode->type = new TypeData( TypeData::SymbolicInst );
|
---|
[fb114fa1] | 322 | newnode->type->symbolic.name = name;
|
---|
[8f6f47d7] | 323 | newnode->type->symbolic.isTypedef = false;
|
---|
| 324 | newnode->type->symbolic.actuals = params;
|
---|
[b87a5ed] | 325 | return newnode;
|
---|
[984dce6] | 326 | } // DeclarationNode::newFromTypeGen
|
---|
[3848e0e] | 327 |
|
---|
[bb7422a] | 328 | DeclarationNode * DeclarationNode::newTypeParam( ast::TypeDecl::Kind tc, const string * name ) {
|
---|
[a46b69c] | 329 | DeclarationNode * newnode = newName( name );
|
---|
[2298f728] | 330 | newnode->type = nullptr;
|
---|
[28307be] | 331 | newnode->variable.tyClass = tc;
|
---|
[faddbd8] | 332 | newnode->variable.assertions = nullptr;
|
---|
[b87a5ed] | 333 | return newnode;
|
---|
[984dce6] | 334 | } // DeclarationNode::newTypeParam
|
---|
[3848e0e] | 335 |
|
---|
[fb114fa1] | 336 | DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
|
---|
[ba7aa2d] | 337 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[b87a5ed] | 338 | newnode->type = new TypeData( TypeData::Aggregate );
|
---|
[2298f728] | 339 | newnode->type->aggregate.name = name;
|
---|
[bb7422a] | 340 | newnode->type->aggregate.kind = ast::AggregateDecl::Trait;
|
---|
[8f6f47d7] | 341 | newnode->type->aggregate.params = params;
|
---|
| 342 | newnode->type->aggregate.fields = asserts;
|
---|
[b87a5ed] | 343 | return newnode;
|
---|
[984dce6] | 344 | } // DeclarationNode::newTrait
|
---|
[3848e0e] | 345 |
|
---|
[fb114fa1] | 346 | DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
|
---|
[ba7aa2d] | 347 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[b87a5ed] | 348 | newnode->type = new TypeData( TypeData::AggregateInst );
|
---|
[8f6f47d7] | 349 | newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
|
---|
[bb7422a] | 350 | newnode->type->aggInst.aggregate->aggregate.kind = ast::AggregateDecl::Trait;
|
---|
[2298f728] | 351 | newnode->type->aggInst.aggregate->aggregate.name = name;
|
---|
[8f6f47d7] | 352 | newnode->type->aggInst.params = params;
|
---|
[b87a5ed] | 353 | return newnode;
|
---|
[984dce6] | 354 | } // DeclarationNode::newTraitUse
|
---|
[3848e0e] | 355 |
|
---|
[25bca42] | 356 | DeclarationNode * DeclarationNode::newTypeDecl( const string * name, DeclarationNode * typeParams ) {
|
---|
[a46b69c] | 357 | DeclarationNode * newnode = newName( name );
|
---|
[b87a5ed] | 358 | newnode->type = new TypeData( TypeData::Symbolic );
|
---|
[8f6f47d7] | 359 | newnode->type->symbolic.isTypedef = false;
|
---|
| 360 | newnode->type->symbolic.params = typeParams;
|
---|
[b87a5ed] | 361 | return newnode;
|
---|
[984dce6] | 362 | } // DeclarationNode::newTypeDecl
|
---|
[3848e0e] | 363 |
|
---|
[ce8c12f] | 364 | DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) {
|
---|
[ba7aa2d] | 365 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[ce8c12f] | 366 | newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference );
|
---|
[71d0eab] | 367 | if ( kind == OperKinds::And ) {
|
---|
| 368 | // T && is parsed as 'And' operator rather than two references => add a second reference type
|
---|
| 369 | TypeData * td = new TypeData( TypeData::Reference );
|
---|
| 370 | td->base = newnode->type;
|
---|
| 371 | newnode->type = td;
|
---|
| 372 | }
|
---|
[c3396e0] | 373 | if ( qualifiers ) {
|
---|
| 374 | return newnode->addQualifiers( qualifiers );
|
---|
| 375 | } else {
|
---|
| 376 | return newnode;
|
---|
| 377 | } // if
|
---|
[984dce6] | 378 | } // DeclarationNode::newPointer
|
---|
[3848e0e] | 379 |
|
---|
[ba7aa2d] | 380 | DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
|
---|
| 381 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[b87a5ed] | 382 | newnode->type = new TypeData( TypeData::Array );
|
---|
[8f6f47d7] | 383 | newnode->type->array.dimension = size;
|
---|
| 384 | newnode->type->array.isStatic = isStatic;
|
---|
[bb7422a] | 385 | if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ast::ConstantExpr *>() ) {
|
---|
[8f6f47d7] | 386 | newnode->type->array.isVarLen = false;
|
---|
[71bd8c6] | 387 | } else {
|
---|
[8f6f47d7] | 388 | newnode->type->array.isVarLen = true;
|
---|
[71bd8c6] | 389 | } // if
|
---|
[b87a5ed] | 390 | return newnode->addQualifiers( qualifiers );
|
---|
[984dce6] | 391 | } // DeclarationNode::newArray
|
---|
[3848e0e] | 392 |
|
---|
[ba7aa2d] | 393 | DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
|
---|
| 394 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[b87a5ed] | 395 | newnode->type = new TypeData( TypeData::Array );
|
---|
[2298f728] | 396 | newnode->type->array.dimension = nullptr;
|
---|
[8f6f47d7] | 397 | newnode->type->array.isStatic = false;
|
---|
| 398 | newnode->type->array.isVarLen = true;
|
---|
[b87a5ed] | 399 | return newnode->addQualifiers( qualifiers );
|
---|
[3848e0e] | 400 | }
|
---|
| 401 |
|
---|
[ba7aa2d] | 402 | DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
|
---|
| 403 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[b87a5ed] | 404 | newnode->bitfieldWidth = size;
|
---|
| 405 | return newnode;
|
---|
[3848e0e] | 406 | }
|
---|
| 407 |
|
---|
[ba7aa2d] | 408 | DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
|
---|
| 409 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[b87a5ed] | 410 | newnode->type = new TypeData( TypeData::Tuple );
|
---|
[8f6f47d7] | 411 | newnode->type->tuple = members;
|
---|
[b87a5ed] | 412 | return newnode;
|
---|
[3848e0e] | 413 | }
|
---|
| 414 |
|
---|
[f855545] | 415 | DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr, bool basetypeof ) {
|
---|
[ba7aa2d] | 416 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[f855545] | 417 | newnode->type = new TypeData( basetypeof ? TypeData::Basetypeof : TypeData::Typeof );
|
---|
[8f6f47d7] | 418 | newnode->type->typeexpr = expr;
|
---|
[b87a5ed] | 419 | return newnode;
|
---|
[3848e0e] | 420 | }
|
---|
| 421 |
|
---|
[93bbbc4] | 422 | DeclarationNode * DeclarationNode::newVtableType( DeclarationNode * decl ) {
|
---|
| 423 | DeclarationNode * newnode = new DeclarationNode;
|
---|
| 424 | newnode->type = new TypeData( TypeData::Vtable );
|
---|
| 425 | newnode->setBase( decl->type );
|
---|
| 426 | return newnode;
|
---|
| 427 | }
|
---|
| 428 |
|
---|
[8f6f47d7] | 429 | DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
|
---|
[ba7aa2d] | 430 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[8f6f47d7] | 431 | newnode->type = new TypeData( TypeData::Builtin );
|
---|
| 432 | newnode->builtin = bt;
|
---|
[148f7290] | 433 | newnode->type->builtintype = newnode->builtin;
|
---|
[8f6f47d7] | 434 | return newnode;
|
---|
| 435 | } // DeclarationNode::newBuiltinType
|
---|
| 436 |
|
---|
[a46b69c] | 437 | DeclarationNode * DeclarationNode::newFunction( const string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ) {
|
---|
| 438 | DeclarationNode * newnode = newName( name );
|
---|
| 439 | newnode->type = new TypeData( TypeData::Function );
|
---|
| 440 | newnode->type->function.params = param;
|
---|
| 441 | newnode->type->function.body = body;
|
---|
| 442 |
|
---|
| 443 | if ( ret ) {
|
---|
| 444 | newnode->type->base = ret->type;
|
---|
| 445 | ret->type = nullptr;
|
---|
| 446 | delete ret;
|
---|
| 447 | } // if
|
---|
| 448 |
|
---|
| 449 | return newnode;
|
---|
| 450 | } // DeclarationNode::newFunction
|
---|
| 451 |
|
---|
[25bca42] | 452 | DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
|
---|
[44a81853] | 453 | DeclarationNode * newnode = new DeclarationNode;
|
---|
| 454 | newnode->type = nullptr;
|
---|
[bb7422a] | 455 | std::vector<ast::ptr<ast::Expr>> exprs;
|
---|
[44a81853] | 456 | buildList( expr, exprs );
|
---|
[bb7422a] | 457 | newnode->attributes.push_back(
|
---|
| 458 | new ast::Attribute( *name, std::move( exprs ) ) );
|
---|
[44a81853] | 459 | delete name;
|
---|
| 460 | return newnode;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
[2d019af] | 463 | DeclarationNode * DeclarationNode::newDirectiveStmt( StatementNode * stmt ) {
|
---|
| 464 | DeclarationNode * newnode = new DeclarationNode;
|
---|
| 465 | newnode->directiveStmt = stmt;
|
---|
| 466 | return newnode;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
[e994912] | 469 | DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
|
---|
| 470 | DeclarationNode * newnode = new DeclarationNode;
|
---|
| 471 | newnode->asmStmt = stmt;
|
---|
| 472 | return newnode;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[bb7422a] | 475 | DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, ast::Expr * message ) {
|
---|
[f6e3e34] | 476 | DeclarationNode * newnode = new DeclarationNode;
|
---|
| 477 | newnode->assert.condition = condition;
|
---|
| 478 | newnode->assert.message = message;
|
---|
| 479 | return newnode;
|
---|
| 480 | }
|
---|
| 481 |
|
---|
[bb7422a] | 482 | static void appendError( string & dst, const string & src ) {
|
---|
[5b639ee] | 483 | if ( src.empty() ) return;
|
---|
| 484 | if ( dst.empty() ) { dst = src; return; }
|
---|
| 485 | dst += ", " + src;
|
---|
| 486 | } // appendError
|
---|
| 487 |
|
---|
[ba7aa2d] | 488 | void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
|
---|
[bb7422a] | 489 | const ast::CV::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
|
---|
| 490 | const ast::CV::Qualifiers duplicates = qsrc & qdst;
|
---|
| 491 |
|
---|
| 492 | if ( duplicates.any() ) {
|
---|
| 493 | std::stringstream str;
|
---|
| 494 | str << "duplicate ";
|
---|
| 495 | ast::print( str, duplicates );
|
---|
| 496 | str << "qualifier(s)";
|
---|
| 497 | appendError( error, str.str() );
|
---|
[a7c90d4] | 498 | } // for
|
---|
[c1c1112] | 499 | } // DeclarationNode::checkQualifiers
|
---|
| 500 |
|
---|
[a7c90d4] | 501 | void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
|
---|
[bb7422a] | 502 | ast::Function::Specs fsDups = funcSpecs & src->funcSpecs;
|
---|
| 503 | if ( fsDups.any() ) {
|
---|
| 504 | std::stringstream str;
|
---|
| 505 | str << "duplicate ";
|
---|
| 506 | ast::print( str, fsDups );
|
---|
| 507 | str << "function specifier(s)";
|
---|
| 508 | appendError( error, str.str() );
|
---|
[dd020c0] | 509 | } // if
|
---|
| 510 |
|
---|
[bb7422a] | 511 | // Skip if everything is unset.
|
---|
| 512 | if ( storageClasses.any() && src->storageClasses.any() ) {
|
---|
| 513 | ast::Storage::Classes dups = storageClasses & src->storageClasses;
|
---|
| 514 | // Check for duplicates.
|
---|
| 515 | if ( dups.any() ) {
|
---|
| 516 | std::stringstream str;
|
---|
| 517 | str << "duplicate ";
|
---|
| 518 | ast::print( str, dups );
|
---|
| 519 | str << "storage class(es)";
|
---|
| 520 | appendError( error, str.str() );
|
---|
| 521 | // Check for conflicts.
|
---|
| 522 | } else if ( !src->storageClasses.is_threadlocal_any() ) {
|
---|
| 523 | std::stringstream str;
|
---|
| 524 | str << "conflicting ";
|
---|
| 525 | ast::print( str, ast::Storage::Classes( 1 << storageClasses.ffs() ) );
|
---|
| 526 | str << "& ";
|
---|
| 527 | ast::print( str, ast::Storage::Classes( 1 << src->storageClasses.ffs() ) );
|
---|
| 528 | str << "storage classes";
|
---|
| 529 | appendError( error, str.str() );
|
---|
| 530 | // FIX to preserve invariant of one basic storage specifier
|
---|
| 531 | src->storageClasses.reset();
|
---|
| 532 | }
|
---|
[b6424d9] | 533 | } // if
|
---|
[dd020c0] | 534 |
|
---|
[a7c90d4] | 535 | appendError( error, src->error );
|
---|
| 536 | } // DeclarationNode::checkSpecifiers
|
---|
[b6424d9] | 537 |
|
---|
[a7c90d4] | 538 | DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) {
|
---|
[6f95000] | 539 | funcSpecs |= q->funcSpecs;
|
---|
| 540 | storageClasses |= q->storageClasses;
|
---|
[c0aa336] | 541 |
|
---|
[bb7422a] | 542 | std::vector<ast::ptr<ast::Attribute>> tmp;
|
---|
| 543 | tmp.reserve( q->attributes.size() );
|
---|
| 544 | for ( auto const & attr : q->attributes ) {
|
---|
| 545 | tmp.emplace_back( ast::shallowCopy( attr.get() ) );
|
---|
| 546 | }
|
---|
| 547 | spliceBegin( attributes, tmp );
|
---|
| 548 |
|
---|
[b6424d9] | 549 | return this;
|
---|
[a7c90d4] | 550 | } // DeclarationNode::copySpecifiers
|
---|
[b6424d9] | 551 |
|
---|
[f2f512ba] | 552 | static void addQualifiersToType( TypeData *& src, TypeData * dst ) {
|
---|
[101e0bd] | 553 | if ( dst->base ) {
|
---|
| 554 | addQualifiersToType( src, dst->base );
|
---|
| 555 | } else if ( dst->kind == TypeData::Function ) {
|
---|
| 556 | dst->base = src;
|
---|
[2298f728] | 557 | src = nullptr;
|
---|
[101e0bd] | 558 | } else {
|
---|
[6f95000] | 559 | dst->qualifiers |= src->qualifiers;
|
---|
[101e0bd] | 560 | } // if
|
---|
| 561 | } // addQualifiersToType
|
---|
| 562 |
|
---|
[ba7aa2d] | 563 | DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
|
---|
[af9da5f] | 564 | if ( ! q ) { return this; } // empty qualifier
|
---|
[101e0bd] | 565 |
|
---|
[a7c90d4] | 566 | checkSpecifiers( q );
|
---|
| 567 | copySpecifiers( q );
|
---|
[101e0bd] | 568 |
|
---|
[c38ae92] | 569 | if ( ! q->type ) { delete q; return this; }
|
---|
[101e0bd] | 570 |
|
---|
| 571 | if ( ! type ) {
|
---|
[c38ae92] | 572 | type = q->type; // reuse structure
|
---|
[1b772749] | 573 | q->type = nullptr;
|
---|
| 574 | delete q;
|
---|
[101e0bd] | 575 | return this;
|
---|
| 576 | } // if
|
---|
| 577 |
|
---|
[c38ae92] | 578 | if ( q->type->forall ) { // forall qualifier ?
|
---|
| 579 | if ( type->forall ) { // polymorphic routine ?
|
---|
| 580 | type->forall->appendList( q->type->forall ); // augment forall qualifier
|
---|
[101e0bd] | 581 | } else {
|
---|
[c38ae92] | 582 | if ( type->kind == TypeData::Aggregate ) { // struct/union ?
|
---|
| 583 | if ( type->aggregate.params ) { // polymorphic ?
|
---|
| 584 | type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
|
---|
| 585 | } else { // not polymorphic
|
---|
[a1c9ddd] | 586 | type->aggregate.params = q->type->forall; // set forall qualifier
|
---|
[c38ae92] | 587 | } // if
|
---|
| 588 | } else { // not polymorphic
|
---|
| 589 | type->forall = q->type->forall; // make polymorphic routine
|
---|
[68cd1ce] | 590 | } // if
|
---|
| 591 | } // if
|
---|
[c38ae92] | 592 | q->type->forall = nullptr; // forall qualifier moved
|
---|
[68cd1ce] | 593 | } // if
|
---|
[6ef2d81] | 594 |
|
---|
[a7c90d4] | 595 | checkQualifiers( type, q->type );
|
---|
[78e2fca] | 596 | if ( (builtin == Zero || builtin == One) && q->type->qualifiers.any() && error.length() == 0 ) {
|
---|
[be00a2d] | 597 | SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, builtinTypeNames[builtin] );
|
---|
[9dc31c10] | 598 | } // if
|
---|
[6ef2d81] | 599 | addQualifiersToType( q->type, type );
|
---|
| 600 |
|
---|
[b87a5ed] | 601 | delete q;
|
---|
| 602 | return this;
|
---|
[101e0bd] | 603 | } // addQualifiers
|
---|
[3848e0e] | 604 |
|
---|
[f2f512ba] | 605 | static void addTypeToType( TypeData *& src, TypeData *& dst ) {
|
---|
[101e0bd] | 606 | if ( src->forall && dst->kind == TypeData::Function ) {
|
---|
| 607 | if ( dst->forall ) {
|
---|
| 608 | dst->forall->appendList( src->forall );
|
---|
[b87a5ed] | 609 | } else {
|
---|
[101e0bd] | 610 | dst->forall = src->forall;
|
---|
| 611 | } // if
|
---|
[2298f728] | 612 | src->forall = nullptr;
|
---|
[101e0bd] | 613 | } // if
|
---|
| 614 | if ( dst->base ) {
|
---|
| 615 | addTypeToType( src, dst->base );
|
---|
| 616 | } else {
|
---|
| 617 | switch ( dst->kind ) {
|
---|
[0d0931d] | 618 | case TypeData::Unknown:
|
---|
[6f95000] | 619 | src->qualifiers |= dst->qualifiers;
|
---|
[101e0bd] | 620 | dst = src;
|
---|
[2298f728] | 621 | src = nullptr;
|
---|
[101e0bd] | 622 | break;
|
---|
[0d0931d] | 623 | case TypeData::Basic:
|
---|
[6f95000] | 624 | dst->qualifiers |= src->qualifiers;
|
---|
[101e0bd] | 625 | if ( src->kind != TypeData::Unknown ) {
|
---|
| 626 | assert( src->kind == TypeData::Basic );
|
---|
| 627 |
|
---|
| 628 | if ( dst->basictype == DeclarationNode::NoBasicType ) {
|
---|
| 629 | dst->basictype = src->basictype;
|
---|
| 630 | } else if ( src->basictype != DeclarationNode::NoBasicType )
|
---|
[a16764a6] | 631 | SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " );
|
---|
[101e0bd] | 632 |
|
---|
| 633 | if ( dst->complextype == DeclarationNode::NoComplexType ) {
|
---|
| 634 | dst->complextype = src->complextype;
|
---|
| 635 | } else if ( src->complextype != DeclarationNode::NoComplexType )
|
---|
[a16764a6] | 636 | SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " );
|
---|
[101e0bd] | 637 |
|
---|
| 638 | if ( dst->signedness == DeclarationNode::NoSignedness ) {
|
---|
| 639 | dst->signedness = src->signedness;
|
---|
| 640 | } else if ( src->signedness != DeclarationNode::NoSignedness )
|
---|
[a16764a6] | 641 | SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " );
|
---|
[101e0bd] | 642 |
|
---|
| 643 | if ( dst->length == DeclarationNode::NoLength ) {
|
---|
| 644 | dst->length = src->length;
|
---|
| 645 | } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
|
---|
| 646 | dst->length = DeclarationNode::LongLong;
|
---|
| 647 | } else if ( src->length != DeclarationNode::NoLength )
|
---|
[a16764a6] | 648 | SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " );
|
---|
[101e0bd] | 649 | } // if
|
---|
| 650 | break;
|
---|
[0d0931d] | 651 | default:
|
---|
[101e0bd] | 652 | switch ( src->kind ) {
|
---|
[0d0931d] | 653 | case TypeData::Aggregate:
|
---|
| 654 | case TypeData::Enum:
|
---|
[101e0bd] | 655 | dst->base = new TypeData( TypeData::AggregateInst );
|
---|
| 656 | dst->base->aggInst.aggregate = src;
|
---|
| 657 | if ( src->kind == TypeData::Aggregate ) {
|
---|
| 658 | dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
|
---|
[68cd1ce] | 659 | } // if
|
---|
[6f95000] | 660 | dst->base->qualifiers |= src->qualifiers;
|
---|
[2298f728] | 661 | src = nullptr;
|
---|
[b87a5ed] | 662 | break;
|
---|
[0d0931d] | 663 | default:
|
---|
[101e0bd] | 664 | if ( dst->forall ) {
|
---|
| 665 | dst->forall->appendList( src->forall );
|
---|
| 666 | } else {
|
---|
| 667 | dst->forall = src->forall;
|
---|
| 668 | } // if
|
---|
[2298f728] | 669 | src->forall = nullptr;
|
---|
[101e0bd] | 670 | dst->base = src;
|
---|
[2298f728] | 671 | src = nullptr;
|
---|
[68cd1ce] | 672 | } // switch
|
---|
[101e0bd] | 673 | } // switch
|
---|
[68cd1ce] | 674 | } // if
|
---|
[3848e0e] | 675 | }
|
---|
| 676 |
|
---|
[ba7aa2d] | 677 | DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
|
---|
[b87a5ed] | 678 | if ( o ) {
|
---|
[a7c90d4] | 679 | checkSpecifiers( o );
|
---|
| 680 | copySpecifiers( o );
|
---|
[b87a5ed] | 681 | if ( o->type ) {
|
---|
| 682 | if ( ! type ) {
|
---|
| 683 | if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
|
---|
| 684 | type = new TypeData( TypeData::AggregateInst );
|
---|
[8f6f47d7] | 685 | type->aggInst.aggregate = o->type;
|
---|
[b87a5ed] | 686 | if ( o->type->kind == TypeData::Aggregate ) {
|
---|
[43c89a7] | 687 | type->aggInst.hoistType = o->type->aggregate.body;
|
---|
[8f6f47d7] | 688 | type->aggInst.params = maybeClone( o->type->aggregate.actuals );
|
---|
[43c89a7] | 689 | } else {
|
---|
| 690 | type->aggInst.hoistType = o->type->enumeration.body;
|
---|
[68cd1ce] | 691 | } // if
|
---|
[6f95000] | 692 | type->qualifiers |= o->type->qualifiers;
|
---|
[b87a5ed] | 693 | } else {
|
---|
| 694 | type = o->type;
|
---|
[68cd1ce] | 695 | } // if
|
---|
[2298f728] | 696 | o->type = nullptr;
|
---|
[b87a5ed] | 697 | } else {
|
---|
| 698 | addTypeToType( o->type, type );
|
---|
[68cd1ce] | 699 | } // if
|
---|
| 700 | } // if
|
---|
[b87a5ed] | 701 | if ( o->bitfieldWidth ) {
|
---|
| 702 | bitfieldWidth = o->bitfieldWidth;
|
---|
[68cd1ce] | 703 | } // if
|
---|
[71bd8c6] | 704 |
|
---|
| 705 | // there may be typedefs chained onto the type
|
---|
[1d4580a] | 706 | if ( o->get_next() ) {
|
---|
| 707 | set_last( o->get_next()->clone() );
|
---|
[1db21619] | 708 | } // if
|
---|
[68cd1ce] | 709 | } // if
|
---|
[b87a5ed] | 710 | delete o;
|
---|
[66406f3] | 711 |
|
---|
[b87a5ed] | 712 | return this;
|
---|
[3848e0e] | 713 | }
|
---|
| 714 |
|
---|
[f135b50] | 715 | DeclarationNode * DeclarationNode::addEnumBase( DeclarationNode * o ) {
|
---|
| 716 | if ( o && o -> type) {
|
---|
| 717 | type->base= o->type;
|
---|
| 718 | }
|
---|
| 719 | delete o;
|
---|
| 720 | return this;
|
---|
| 721 | }
|
---|
| 722 |
|
---|
[ba7aa2d] | 723 | DeclarationNode * DeclarationNode::addTypedef() {
|
---|
| 724 | TypeData * newtype = new TypeData( TypeData::Symbolic );
|
---|
[2298f728] | 725 | newtype->symbolic.params = nullptr;
|
---|
[8f6f47d7] | 726 | newtype->symbolic.isTypedef = true;
|
---|
[2298f728] | 727 | newtype->symbolic.name = name ? new string( *name ) : nullptr;
|
---|
[b87a5ed] | 728 | newtype->base = type;
|
---|
| 729 | type = newtype;
|
---|
| 730 | return this;
|
---|
[3848e0e] | 731 | }
|
---|
| 732 |
|
---|
[ba7aa2d] | 733 | DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
|
---|
[bb7422a] | 734 | if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
|
---|
[702e826] | 735 | if ( variable.assertions ) {
|
---|
| 736 | variable.assertions->appendList( assertions );
|
---|
| 737 | } else {
|
---|
| 738 | variable.assertions = assertions;
|
---|
| 739 | } // if
|
---|
| 740 | return this;
|
---|
[2298f728] | 741 | } // if
|
---|
| 742 |
|
---|
[b87a5ed] | 743 | assert( type );
|
---|
| 744 | switch ( type->kind ) {
|
---|
[0d0931d] | 745 | case TypeData::Symbolic:
|
---|
[8f6f47d7] | 746 | if ( type->symbolic.assertions ) {
|
---|
| 747 | type->symbolic.assertions->appendList( assertions );
|
---|
[b87a5ed] | 748 | } else {
|
---|
[8f6f47d7] | 749 | type->symbolic.assertions = assertions;
|
---|
[68cd1ce] | 750 | } // if
|
---|
[b87a5ed] | 751 | break;
|
---|
[0d0931d] | 752 | default:
|
---|
[b87a5ed] | 753 | assert( false );
|
---|
[68cd1ce] | 754 | } // switch
|
---|
[974906e2] | 755 |
|
---|
[b87a5ed] | 756 | return this;
|
---|
[51b73452] | 757 | }
|
---|
| 758 |
|
---|
[fb114fa1] | 759 | DeclarationNode * DeclarationNode::addName( string * newname ) {
|
---|
[2298f728] | 760 | assert( ! name );
|
---|
| 761 | name = newname;
|
---|
[b87a5ed] | 762 | return this;
|
---|
[51b73452] | 763 | }
|
---|
| 764 |
|
---|
[c0aa336] | 765 | DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
|
---|
[58dd019] | 766 | assert( ! asmName );
|
---|
[c0aa336] | 767 | asmName = newname ? newname->asmName : nullptr;
|
---|
| 768 | return this->addQualifiers( newname );
|
---|
[58dd019] | 769 | }
|
---|
| 770 |
|
---|
[ba7aa2d] | 771 | DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
|
---|
[b87a5ed] | 772 | bitfieldWidth = size;
|
---|
| 773 | return this;
|
---|
[51b73452] | 774 | }
|
---|
| 775 |
|
---|
[ba7aa2d] | 776 | DeclarationNode * DeclarationNode::addVarArgs() {
|
---|
[b87a5ed] | 777 | assert( type );
|
---|
| 778 | hasEllipsis = true;
|
---|
| 779 | return this;
|
---|
[51b73452] | 780 | }
|
---|
| 781 |
|
---|
[c453ac4] | 782 | DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
|
---|
[b87a5ed] | 783 | assert( type );
|
---|
| 784 | assert( type->kind == TypeData::Function );
|
---|
[2298f728] | 785 | assert( ! type->function.body );
|
---|
[8f6f47d7] | 786 | type->function.body = body;
|
---|
[c453ac4] | 787 | type->function.withExprs = withExprs;
|
---|
[b87a5ed] | 788 | return this;
|
---|
[51b73452] | 789 | }
|
---|
| 790 |
|
---|
[ba7aa2d] | 791 | DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
|
---|
[b87a5ed] | 792 | assert( type );
|
---|
| 793 | assert( type->kind == TypeData::Function );
|
---|
[2298f728] | 794 | assert( ! type->function.oldDeclList );
|
---|
[8f6f47d7] | 795 | type->function.oldDeclList = list;
|
---|
[b87a5ed] | 796 | return this;
|
---|
[51b73452] | 797 | }
|
---|
| 798 |
|
---|
[c0aa336] | 799 | DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
|
---|
[b87a5ed] | 800 | if ( type ) {
|
---|
[ba7aa2d] | 801 | TypeData * prevBase = type;
|
---|
| 802 | TypeData * curBase = type->base;
|
---|
[2298f728] | 803 | while ( curBase != nullptr ) {
|
---|
[b87a5ed] | 804 | prevBase = curBase;
|
---|
| 805 | curBase = curBase->base;
|
---|
[68cd1ce] | 806 | } // while
|
---|
[b87a5ed] | 807 | prevBase->base = newType;
|
---|
| 808 | } else {
|
---|
| 809 | type = newType;
|
---|
[68cd1ce] | 810 | } // if
|
---|
[c0aa336] | 811 | return this;
|
---|
[3848e0e] | 812 | }
|
---|
| 813 |
|
---|
[c0aa336] | 814 | DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
|
---|
| 815 | if ( a ) {
|
---|
[bb7422a] | 816 | spliceBegin( attributes, a->attributes );
|
---|
[c0aa336] | 817 | a->attributes.clear();
|
---|
| 818 | } // if
|
---|
| 819 | return this;
|
---|
| 820 | } // copyAttribute
|
---|
| 821 |
|
---|
[ba7aa2d] | 822 | DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
|
---|
[b87a5ed] | 823 | if ( p ) {
|
---|
[6926a6d] | 824 | assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
|
---|
[c0aa336] | 825 | setBase( p->type );
|
---|
[2298f728] | 826 | p->type = nullptr;
|
---|
[c0aa336] | 827 | copyAttribute( p );
|
---|
[b87a5ed] | 828 | delete p;
|
---|
[68cd1ce] | 829 | } // if
|
---|
[b87a5ed] | 830 | return this;
|
---|
[3848e0e] | 831 | }
|
---|
| 832 |
|
---|
[ba7aa2d] | 833 | DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
|
---|
[b87a5ed] | 834 | if ( a ) {
|
---|
| 835 | assert( a->type->kind == TypeData::Array );
|
---|
[c0aa336] | 836 | setBase( a->type );
|
---|
[2298f728] | 837 | a->type = nullptr;
|
---|
[c0aa336] | 838 | copyAttribute( a );
|
---|
[b87a5ed] | 839 | delete a;
|
---|
[68cd1ce] | 840 | } // if
|
---|
[b87a5ed] | 841 | return this;
|
---|
[51b73452] | 842 | }
|
---|
| 843 |
|
---|
[ba7aa2d] | 844 | DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
|
---|
[b87a5ed] | 845 | if ( p ) {
|
---|
[e6cee92] | 846 | assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
|
---|
[b87a5ed] | 847 | if ( type ) {
|
---|
| 848 | switch ( type->kind ) {
|
---|
[0d0931d] | 849 | case TypeData::Aggregate:
|
---|
| 850 | case TypeData::Enum:
|
---|
[b87a5ed] | 851 | p->type->base = new TypeData( TypeData::AggregateInst );
|
---|
[8f6f47d7] | 852 | p->type->base->aggInst.aggregate = type;
|
---|
[b87a5ed] | 853 | if ( type->kind == TypeData::Aggregate ) {
|
---|
[8f6f47d7] | 854 | p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
|
---|
[68cd1ce] | 855 | } // if
|
---|
[6f95000] | 856 | p->type->base->qualifiers |= type->qualifiers;
|
---|
[b87a5ed] | 857 | break;
|
---|
| 858 |
|
---|
[0d0931d] | 859 | default:
|
---|
[b87a5ed] | 860 | p->type->base = type;
|
---|
[68cd1ce] | 861 | } // switch
|
---|
[2298f728] | 862 | type = nullptr;
|
---|
[68cd1ce] | 863 | } // if
|
---|
[b87a5ed] | 864 | delete this;
|
---|
| 865 | return p;
|
---|
| 866 | } else {
|
---|
| 867 | return this;
|
---|
[68cd1ce] | 868 | } // if
|
---|
[51b73452] | 869 | }
|
---|
| 870 |
|
---|
[ba7aa2d] | 871 | static TypeData * findLast( TypeData * a ) {
|
---|
[b87a5ed] | 872 | assert( a );
|
---|
[ba7aa2d] | 873 | TypeData * cur = a;
|
---|
[a32b204] | 874 | while ( cur->base ) {
|
---|
[b87a5ed] | 875 | cur = cur->base;
|
---|
[68cd1ce] | 876 | } // while
|
---|
[b87a5ed] | 877 | return cur;
|
---|
[3848e0e] | 878 | }
|
---|
| 879 |
|
---|
[ba7aa2d] | 880 | DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
|
---|
[0d0931d] | 881 | if ( ! a ) return this;
|
---|
[738e304] | 882 | assert( a->type->kind == TypeData::Array );
|
---|
| 883 | TypeData * lastArray = findLast( a->type );
|
---|
| 884 | if ( type ) {
|
---|
| 885 | switch ( type->kind ) {
|
---|
[0d0931d] | 886 | case TypeData::Aggregate:
|
---|
| 887 | case TypeData::Enum:
|
---|
[738e304] | 888 | lastArray->base = new TypeData( TypeData::AggregateInst );
|
---|
| 889 | lastArray->base->aggInst.aggregate = type;
|
---|
| 890 | if ( type->kind == TypeData::Aggregate ) {
|
---|
| 891 | lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
|
---|
| 892 | } // if
|
---|
[6f95000] | 893 | lastArray->base->qualifiers |= type->qualifiers;
|
---|
[738e304] | 894 | break;
|
---|
[0d0931d] | 895 | default:
|
---|
[738e304] | 896 | lastArray->base = type;
|
---|
| 897 | } // switch
|
---|
| 898 | type = nullptr;
|
---|
[68cd1ce] | 899 | } // if
|
---|
[738e304] | 900 | delete this;
|
---|
| 901 | return a;
|
---|
[51b73452] | 902 | }
|
---|
[3848e0e] | 903 |
|
---|
[ba7aa2d] | 904 | DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
|
---|
| 905 | TypeData * ftype = new TypeData( TypeData::Function );
|
---|
[8f6f47d7] | 906 | ftype->function.params = params;
|
---|
[c0aa336] | 907 | setBase( ftype );
|
---|
[b87a5ed] | 908 | return this;
|
---|
[3848e0e] | 909 | }
|
---|
| 910 |
|
---|
[ba7aa2d] | 911 | static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
|
---|
[b87a5ed] | 912 | if ( type ) {
|
---|
| 913 | if ( type->kind != TypeData::Function ) {
|
---|
| 914 | type->base = addIdListToType( type->base, ids );
|
---|
| 915 | } else {
|
---|
[8f6f47d7] | 916 | type->function.idList = ids;
|
---|
[68cd1ce] | 917 | } // if
|
---|
[b87a5ed] | 918 | return type;
|
---|
[3848e0e] | 919 | } else {
|
---|
[ba7aa2d] | 920 | TypeData * newtype = new TypeData( TypeData::Function );
|
---|
[8f6f47d7] | 921 | newtype->function.idList = ids;
|
---|
[b87a5ed] | 922 | return newtype;
|
---|
[68cd1ce] | 923 | } // if
|
---|
[2298f728] | 924 | } // addIdListToType
|
---|
[974906e2] | 925 |
|
---|
[ba7aa2d] | 926 | DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
|
---|
[b87a5ed] | 927 | type = addIdListToType( type, ids );
|
---|
| 928 | return this;
|
---|
[3848e0e] | 929 | }
|
---|
| 930 |
|
---|
[ba7aa2d] | 931 | DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
|
---|
[b87a5ed] | 932 | initializer = init;
|
---|
| 933 | return this;
|
---|
[3848e0e] | 934 | }
|
---|
| 935 |
|
---|
[67cf18c] | 936 | DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
|
---|
[bb7422a] | 937 | assertf( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS, "Called addTypeInitializer on something that isn't a type variable." );
|
---|
[67cf18c] | 938 | variable.initializer = init;
|
---|
| 939 | return this;
|
---|
| 940 | }
|
---|
| 941 |
|
---|
[a46b69c] | 942 | DeclarationNode * DeclarationNode::cloneType( string * name ) {
|
---|
| 943 | DeclarationNode * newnode = newName( name );
|
---|
[b87a5ed] | 944 | newnode->type = maybeClone( type );
|
---|
[a7c90d4] | 945 | newnode->copySpecifiers( this );
|
---|
[b87a5ed] | 946 | return newnode;
|
---|
[3848e0e] | 947 | }
|
---|
| 948 |
|
---|
[2298f728] | 949 | DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
|
---|
| 950 | if ( ! o ) return nullptr;
|
---|
| 951 |
|
---|
[a7c90d4] | 952 | o->copySpecifiers( this );
|
---|
[2298f728] | 953 | if ( type ) {
|
---|
| 954 | TypeData * srcType = type;
|
---|
| 955 |
|
---|
[c0aa336] | 956 | // search for the base type by scanning off pointers and array designators
|
---|
[2298f728] | 957 | while ( srcType->base ) {
|
---|
| 958 | srcType = srcType->base;
|
---|
| 959 | } // while
|
---|
| 960 |
|
---|
| 961 | TypeData * newType = srcType->clone();
|
---|
| 962 | if ( newType->kind == TypeData::AggregateInst ) {
|
---|
| 963 | // don't duplicate members
|
---|
| 964 | if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
|
---|
| 965 | delete newType->aggInst.aggregate->enumeration.constants;
|
---|
| 966 | newType->aggInst.aggregate->enumeration.constants = nullptr;
|
---|
[b2da0574] | 967 | newType->aggInst.aggregate->enumeration.body = false;
|
---|
[b87a5ed] | 968 | } else {
|
---|
[2298f728] | 969 | assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
|
---|
| 970 | delete newType->aggInst.aggregate->aggregate.fields;
|
---|
| 971 | newType->aggInst.aggregate->aggregate.fields = nullptr;
|
---|
[b2da0574] | 972 | newType->aggInst.aggregate->aggregate.body = false;
|
---|
[68cd1ce] | 973 | } // if
|
---|
[43c89a7] | 974 | // don't hoist twice
|
---|
| 975 | newType->aggInst.hoistType = false;
|
---|
[68cd1ce] | 976 | } // if
|
---|
[2298f728] | 977 |
|
---|
| 978 | newType->forall = maybeClone( type->forall );
|
---|
| 979 | if ( ! o->type ) {
|
---|
| 980 | o->type = newType;
|
---|
| 981 | } else {
|
---|
| 982 | addTypeToType( newType, o->type );
|
---|
| 983 | delete newType;
|
---|
| 984 | } // if
|
---|
[68cd1ce] | 985 | } // if
|
---|
[b87a5ed] | 986 | return o;
|
---|
[51b73452] | 987 | }
|
---|
| 988 |
|
---|
[ba7aa2d] | 989 | DeclarationNode * DeclarationNode::extractAggregate() const {
|
---|
[b87a5ed] | 990 | if ( type ) {
|
---|
[ba7aa2d] | 991 | TypeData * ret = typeextractAggregate( type );
|
---|
[b87a5ed] | 992 | if ( ret ) {
|
---|
[ba7aa2d] | 993 | DeclarationNode * newnode = new DeclarationNode;
|
---|
[b87a5ed] | 994 | newnode->type = ret;
|
---|
| 995 | return newnode;
|
---|
[843054c2] | 996 | } // if
|
---|
| 997 | } // if
|
---|
[2298f728] | 998 | return nullptr;
|
---|
[3848e0e] | 999 | }
|
---|
| 1000 |
|
---|
[bb7422a] | 1001 | void buildList( const DeclarationNode * firstNode,
|
---|
| 1002 | std::vector<ast::ptr<ast::Decl>> & outputList ) {
|
---|
[a16764a6] | 1003 | SemanticErrorException errors;
|
---|
[bb7422a] | 1004 | std::back_insert_iterator<std::vector<ast::ptr<ast::Decl>>> out( outputList );
|
---|
[2298f728] | 1005 |
|
---|
[3a5131ed] | 1006 | for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
|
---|
[b87a5ed] | 1007 | try {
|
---|
[692c1cc] | 1008 | bool extracted = false, anon = false;
|
---|
[bb7422a] | 1009 | ast::AggregateDecl * unionDecl = nullptr;
|
---|
[692c1cc] | 1010 |
|
---|
[ba7aa2d] | 1011 | if ( DeclarationNode * extr = cur->extractAggregate() ) {
|
---|
[78e2fca] | 1012 | // Handle the case where a SUE declaration is contained within an object or type declaration.
|
---|
| 1013 |
|
---|
| 1014 | assert( cur->type );
|
---|
| 1015 | // Replace anonymous SUE name with typedef name to prevent anonymous naming problems across translation units.
|
---|
| 1016 | if ( cur->type->kind == TypeData::Symbolic && cur->type->symbolic.isTypedef ) {
|
---|
| 1017 | assert( extr->type );
|
---|
| 1018 | // Handle anonymous aggregates: typedef struct { int i; } foo
|
---|
| 1019 | extr->type->qualifiers.reset(); // clear any CVs associated with the aggregate
|
---|
| 1020 | if ( extr->type->kind == TypeData::Aggregate && extr->type->aggregate.anon ) {
|
---|
| 1021 | delete extr->type->aggregate.name;
|
---|
| 1022 | extr->type->aggregate.name = new string( "__anonymous_" + *cur->name );
|
---|
| 1023 | extr->type->aggregate.anon = false;
|
---|
| 1024 | assert( cur->type->base );
|
---|
| 1025 | if ( cur->type->base ) {
|
---|
| 1026 | delete cur->type->base->aggInst.aggregate->aggregate.name;
|
---|
| 1027 | cur->type->base->aggInst.aggregate->aggregate.name = new string( "__anonymous_" + *cur->name );
|
---|
| 1028 | cur->type->base->aggInst.aggregate->aggregate.anon = false;
|
---|
| 1029 | cur->type->base->aggInst.aggregate->qualifiers.reset();
|
---|
| 1030 | } // if
|
---|
| 1031 | } // if
|
---|
| 1032 | // Handle anonymous enumeration: typedef enum { A, B, C } foo
|
---|
| 1033 | if ( extr->type->kind == TypeData::Enum && extr->type->enumeration.anon ) {
|
---|
| 1034 | delete extr->type->enumeration.name;
|
---|
| 1035 | extr->type->enumeration.name = new string( "__anonymous_" + *cur->name );
|
---|
| 1036 | extr->type->enumeration.anon = false;
|
---|
| 1037 | assert( cur->type->base );
|
---|
| 1038 | if ( cur->type->base ) {
|
---|
| 1039 | delete cur->type->base->aggInst.aggregate->enumeration.name;
|
---|
| 1040 | cur->type->base->aggInst.aggregate->enumeration.name = new string( "__anonymous_" + *cur->name );
|
---|
| 1041 | cur->type->base->aggInst.aggregate->enumeration.anon = false;
|
---|
| 1042 | } // if
|
---|
| 1043 | } // if
|
---|
| 1044 | } // if
|
---|
[692c1cc] | 1045 |
|
---|
[bb7422a] | 1046 | ast::Decl * decl = extr->build();
|
---|
[b87a5ed] | 1047 | if ( decl ) {
|
---|
[692c1cc] | 1048 | // Remember the declaration if it is a union aggregate ?
|
---|
[bb7422a] | 1049 | unionDecl = dynamic_cast<ast::UnionDecl *>( decl );
|
---|
[692c1cc] | 1050 |
|
---|
[294647b] | 1051 | decl->location = cur->location;
|
---|
[692c1cc] | 1052 | *out++ = decl;
|
---|
[3d7e53b] | 1053 |
|
---|
| 1054 | // need to remember the cases where a declaration contains an anonymous aggregate definition
|
---|
| 1055 | extracted = true;
|
---|
| 1056 | assert( extr->type );
|
---|
| 1057 | if ( extr->type->kind == TypeData::Aggregate ) {
|
---|
[692c1cc] | 1058 | // typedef struct { int A } B is the only case?
|
---|
[3d7e53b] | 1059 | anon = extr->type->aggregate.anon;
|
---|
| 1060 | } else if ( extr->type->kind == TypeData::Enum ) {
|
---|
[692c1cc] | 1061 | // typedef enum { A } B is the only case?
|
---|
[3d7e53b] | 1062 | anon = extr->type->enumeration.anon;
|
---|
| 1063 | }
|
---|
[843054c2] | 1064 | } // if
|
---|
[f39096c] | 1065 | delete extr;
|
---|
[843054c2] | 1066 | } // if
|
---|
[2298f728] | 1067 |
|
---|
[bb7422a] | 1068 | ast::Decl * decl = cur->build();
|
---|
[b87a5ed] | 1069 | if ( decl ) {
|
---|
[bb7422a] | 1070 | if ( auto typedefDecl = dynamic_cast<ast::TypedefDecl *>( decl ) ) {
|
---|
[692c1cc] | 1071 | if ( unionDecl ) { // is the typedef alias a union aggregate ?
|
---|
| 1072 | // This code handles a special issue with the attribute transparent_union.
|
---|
| 1073 | //
|
---|
| 1074 | // typedef union U { int i; } typedef_name __attribute__(( aligned(16) )) __attribute__(( transparent_union ))
|
---|
| 1075 | //
|
---|
| 1076 | // Here the attribute aligned goes with the typedef_name, so variables declared of this type are
|
---|
| 1077 | // aligned. However, the attribute transparent_union must be moved from the typedef_name to
|
---|
| 1078 | // alias union U. Currently, this is the only know attribute that must be moved from typedef to
|
---|
| 1079 | // alias.
|
---|
| 1080 |
|
---|
| 1081 | // If typedef is an alias for a union, then its alias type was hoisted above and remembered.
|
---|
[bb7422a] | 1082 | if ( auto unionInstType = typedefDecl->base.as<ast::UnionInstType>() ) {
|
---|
| 1083 | auto instType = ast::mutate( unionInstType );
|
---|
[692c1cc] | 1084 | // Remove all transparent_union attributes from typedef and move to alias union.
|
---|
[bb7422a] | 1085 | for ( auto attr = instType->attributes.begin() ; attr != instType->attributes.end() ; ) { // forward order
|
---|
| 1086 | assert( *attr );
|
---|
[692c1cc] | 1087 | if ( (*attr)->name == "transparent_union" || (*attr)->name == "__transparent_union__" ) {
|
---|
[bb7422a] | 1088 | unionDecl->attributes.emplace_back( attr->release() );
|
---|
| 1089 | attr = instType->attributes.erase( attr );
|
---|
[692c1cc] | 1090 | } else {
|
---|
[bb7422a] | 1091 | attr++;
|
---|
[692c1cc] | 1092 | } // if
|
---|
| 1093 | } // for
|
---|
[bb7422a] | 1094 | typedefDecl->base = instType;
|
---|
[692c1cc] | 1095 | } // if
|
---|
| 1096 | } // if
|
---|
| 1097 | } // if
|
---|
| 1098 |
|
---|
[3d7e53b] | 1099 | // don't include anonymous declaration for named aggregates, but do include them for anonymous aggregates, e.g.:
|
---|
| 1100 | // struct S {
|
---|
| 1101 | // struct T { int x; }; // no anonymous member
|
---|
| 1102 | // struct { int y; }; // anonymous member
|
---|
| 1103 | // struct T; // anonymous member
|
---|
| 1104 | // };
|
---|
[e07caa2] | 1105 | if ( ! (extracted && decl->name == "" && ! anon && ! cur->get_inLine()) ) {
|
---|
| 1106 | if ( decl->name == "" ) {
|
---|
[bb7422a] | 1107 | if ( auto dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) {
|
---|
| 1108 | if ( auto aggr = dynamic_cast<ast::BaseInstType const *>( dwt->get_type() ) ) {
|
---|
[2e02851] | 1109 | if ( aggr->name.find("anonymous") == std::string::npos ) {
|
---|
[e07caa2] | 1110 | if ( ! cur->get_inLine() ) {
|
---|
| 1111 | // temporary: warn about anonymous member declarations of named types, since
|
---|
| 1112 | // this conflicts with the syntax for the forward declaration of an anonymous type
|
---|
[2e02851] | 1113 | SemanticWarning( cur->location, Warning::AggrForwardDecl, aggr->name.c_str() );
|
---|
[e07caa2] | 1114 | } // if
|
---|
| 1115 | } // if
|
---|
| 1116 | } // if
|
---|
| 1117 | } // if
|
---|
| 1118 | } // if
|
---|
[3d7e53b] | 1119 | decl->location = cur->location;
|
---|
[e07caa2] | 1120 | *out++ = decl;
|
---|
| 1121 | } // if
|
---|
[843054c2] | 1122 | } // if
|
---|
[f2f512ba] | 1123 | } catch( SemanticErrorException & e ) {
|
---|
[b87a5ed] | 1124 | errors.append( e );
|
---|
[843054c2] | 1125 | } // try
|
---|
[e07caa2] | 1126 | } // for
|
---|
[2298f728] | 1127 |
|
---|
[b87a5ed] | 1128 | if ( ! errors.isEmpty() ) {
|
---|
| 1129 | throw errors;
|
---|
[843054c2] | 1130 | } // if
|
---|
[2298f728] | 1131 | } // buildList
|
---|
[3848e0e] | 1132 |
|
---|
[3d7e53b] | 1133 | // currently only builds assertions, function parameters, and return values
|
---|
[bb7422a] | 1134 | void buildList( const DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList ) {
|
---|
[a16764a6] | 1135 | SemanticErrorException errors;
|
---|
[bb7422a] | 1136 | std::back_insert_iterator<std::vector<ast::ptr<ast::DeclWithType>>> out( outputList );
|
---|
[43c89a7] | 1137 |
|
---|
[3a5131ed] | 1138 | for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
|
---|
[b87a5ed] | 1139 | try {
|
---|
[bb7422a] | 1140 | ast::Decl * decl = cur->build();
|
---|
[47498bd] | 1141 | assert( decl );
|
---|
[bb7422a] | 1142 | if ( ast::DeclWithType * dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) {
|
---|
[47498bd] | 1143 | dwt->location = cur->location;
|
---|
[3ca7ef3] | 1144 | *out++ = dwt;
|
---|
[bb7422a] | 1145 | } else if ( ast::StructDecl * agg = dynamic_cast<ast::StructDecl *>( decl ) ) {
|
---|
[3d7e53b] | 1146 | // e.g., int foo(struct S) {}
|
---|
[bb7422a] | 1147 | auto inst = new ast::StructInstType( agg->name );
|
---|
| 1148 | auto obj = new ast::ObjectDecl( cur->location, "", inst );
|
---|
| 1149 | obj->linkage = linkage;
|
---|
[3ca7ef3] | 1150 | *out++ = obj;
|
---|
[47498bd] | 1151 | delete agg;
|
---|
[bb7422a] | 1152 | } else if ( ast::UnionDecl * agg = dynamic_cast<ast::UnionDecl *>( decl ) ) {
|
---|
[3d7e53b] | 1153 | // e.g., int foo(union U) {}
|
---|
[bb7422a] | 1154 | auto inst = new ast::UnionInstType( agg->name );
|
---|
| 1155 | auto obj = new ast::ObjectDecl( cur->location,
|
---|
| 1156 | "", inst, nullptr, ast::Storage::Classes(),
|
---|
| 1157 | linkage );
|
---|
[3ca7ef3] | 1158 | *out++ = obj;
|
---|
[bb7422a] | 1159 | } else if ( ast::EnumDecl * agg = dynamic_cast<ast::EnumDecl *>( decl ) ) {
|
---|
[3d7e53b] | 1160 | // e.g., int foo(enum E) {}
|
---|
[bb7422a] | 1161 | auto inst = new ast::EnumInstType( agg->name );
|
---|
| 1162 | auto obj = new ast::ObjectDecl( cur->location,
|
---|
| 1163 | "",
|
---|
| 1164 | inst,
|
---|
| 1165 | nullptr,
|
---|
| 1166 | ast::Storage::Classes(),
|
---|
| 1167 | linkage
|
---|
| 1168 | );
|
---|
[3ca7ef3] | 1169 | *out++ = obj;
|
---|
[843054c2] | 1170 | } // if
|
---|
[f2f512ba] | 1171 | } catch( SemanticErrorException & e ) {
|
---|
[b87a5ed] | 1172 | errors.append( e );
|
---|
[843054c2] | 1173 | } // try
|
---|
[3a5131ed] | 1174 | } // for
|
---|
| 1175 |
|
---|
[b87a5ed] | 1176 | if ( ! errors.isEmpty() ) {
|
---|
| 1177 | throw errors;
|
---|
[843054c2] | 1178 | } // if
|
---|
[2298f728] | 1179 | } // buildList
|
---|
[3848e0e] | 1180 |
|
---|
[bb7422a] | 1181 | void buildTypeList( const DeclarationNode * firstNode,
|
---|
| 1182 | std::vector<ast::ptr<ast::Type>> & outputList ) {
|
---|
[a16764a6] | 1183 | SemanticErrorException errors;
|
---|
[bb7422a] | 1184 | std::back_insert_iterator<std::vector<ast::ptr<ast::Type>>> out( outputList );
|
---|
[ba7aa2d] | 1185 | const DeclarationNode * cur = firstNode;
|
---|
[2298f728] | 1186 |
|
---|
[a32b204] | 1187 | while ( cur ) {
|
---|
[b87a5ed] | 1188 | try {
|
---|
[ba7aa2d] | 1189 | * out++ = cur->buildType();
|
---|
[f2f512ba] | 1190 | } catch( SemanticErrorException & e ) {
|
---|
[b87a5ed] | 1191 | errors.append( e );
|
---|
[843054c2] | 1192 | } // try
|
---|
[7880579] | 1193 | cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
|
---|
[843054c2] | 1194 | } // while
|
---|
[2298f728] | 1195 |
|
---|
[b87a5ed] | 1196 | if ( ! errors.isEmpty() ) {
|
---|
| 1197 | throw errors;
|
---|
[843054c2] | 1198 | } // if
|
---|
[2298f728] | 1199 | } // buildTypeList
|
---|
[51b73452] | 1200 |
|
---|
[bb7422a] | 1201 | ast::Decl * DeclarationNode::build() const {
|
---|
[a16764a6] | 1202 | if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
|
---|
[2298f728] | 1203 |
|
---|
[e994912] | 1204 | if ( asmStmt ) {
|
---|
[bb7422a] | 1205 | auto stmt = strict_dynamic_cast<ast::AsmStmt *>( asmStmt->build() );
|
---|
| 1206 | return new ast::AsmDecl( stmt->location, stmt );
|
---|
[e994912] | 1207 | } // if
|
---|
[2d019af] | 1208 | if ( directiveStmt ) {
|
---|
[bb7422a] | 1209 | auto stmt = strict_dynamic_cast<ast::DirectiveStmt *>( directiveStmt->build() );
|
---|
| 1210 | return new ast::DirectiveDecl( stmt->location, stmt );
|
---|
[2d019af] | 1211 | } // if
|
---|
[e994912] | 1212 |
|
---|
[bb7422a] | 1213 | if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
|
---|
[f0ecf9b] | 1214 | // otype is internally converted to dtype + otype parameters
|
---|
[bb7422a] | 1215 | static const ast::TypeDecl::Kind kindMap[] = { ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Ftype, ast::TypeDecl::Ttype, ast::TypeDecl::Dimension };
|
---|
| 1216 | static_assert( sizeof(kindMap) / sizeof(kindMap[0]) == ast::TypeDecl::NUMBER_OF_KINDS, "DeclarationNode::build: kindMap is out of sync." );
|
---|
[8f60f0b] | 1217 | assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
|
---|
[bb7422a] | 1218 | ast::TypeDecl * ret = new ast::TypeDecl( location,
|
---|
| 1219 | *name,
|
---|
| 1220 | ast::Storage::Classes(),
|
---|
| 1221 | (ast::Type *)nullptr,
|
---|
| 1222 | kindMap[ variable.tyClass ],
|
---|
| 1223 | variable.tyClass == ast::TypeDecl::Otype || variable.tyClass == ast::TypeDecl::DStype,
|
---|
| 1224 | variable.initializer ? variable.initializer->buildType() : nullptr
|
---|
| 1225 | );
|
---|
| 1226 | buildList( variable.assertions, ret->assertions );
|
---|
[2298f728] | 1227 | return ret;
|
---|
| 1228 | } // if
|
---|
| 1229 |
|
---|
[843054c2] | 1230 | if ( type ) {
|
---|
[dd020c0] | 1231 | // Function specifiers can only appear on a function definition/declaration.
|
---|
| 1232 | //
|
---|
| 1233 | // inline _Noreturn int f(); // allowed
|
---|
| 1234 | // inline _Noreturn int g( int i ); // allowed
|
---|
| 1235 | // inline _Noreturn int i; // disallowed
|
---|
[fb04321] | 1236 | if ( type->kind != TypeData::Function && funcSpecs.any() ) {
|
---|
[a16764a6] | 1237 | SemanticError( this, "invalid function specifier for " );
|
---|
[dd020c0] | 1238 | } // if
|
---|
[284da8c] | 1239 | // Forall qualifier can only appear on a function/aggregate definition/declaration.
|
---|
| 1240 | //
|
---|
| 1241 | // forall int f(); // allowed
|
---|
| 1242 | // forall int g( int i ); // allowed
|
---|
| 1243 | // forall int i; // disallowed
|
---|
| 1244 | if ( type->kind != TypeData::Function && type->forall ) {
|
---|
| 1245 | SemanticError( this, "invalid type qualifier for " );
|
---|
| 1246 | } // if
|
---|
[3ed994e] | 1247 | bool isDelete = initializer && initializer->get_isDelete();
|
---|
[bb7422a] | 1248 | ast::Decl * decl = buildDecl(
|
---|
| 1249 | type,
|
---|
| 1250 | name ? *name : string( "" ),
|
---|
| 1251 | storageClasses,
|
---|
| 1252 | maybeBuild( bitfieldWidth ),
|
---|
| 1253 | funcSpecs,
|
---|
| 1254 | linkage,
|
---|
| 1255 | asmName,
|
---|
| 1256 | isDelete ? nullptr : maybeBuild( initializer ),
|
---|
| 1257 | copy( attributes )
|
---|
| 1258 | )->set_extension( extension );
|
---|
[3ed994e] | 1259 | if ( isDelete ) {
|
---|
[bb7422a] | 1260 | auto dwt = strict_dynamic_cast<ast::DeclWithType *>( decl );
|
---|
[3ed994e] | 1261 | dwt->isDeleted = true;
|
---|
| 1262 | }
|
---|
| 1263 | return decl;
|
---|
[843054c2] | 1264 | } // if
|
---|
[2298f728] | 1265 |
|
---|
[f6e3e34] | 1266 | if ( assert.condition ) {
|
---|
[bb7422a] | 1267 | auto cond = maybeBuild( assert.condition );
|
---|
| 1268 | auto msg = strict_dynamic_cast<ast::ConstantExpr *>( maybeCopy( assert.message ) );
|
---|
| 1269 | return new ast::StaticAssertDecl( location, cond, msg );
|
---|
[f6e3e34] | 1270 | }
|
---|
| 1271 |
|
---|
[dd020c0] | 1272 | // SUE's cannot have function specifiers, either
|
---|
| 1273 | //
|
---|
[79aae15] | 1274 | // inline _Noreturn struct S { ... }; // disallowed
|
---|
| 1275 | // inline _Noreturn enum E { ... }; // disallowed
|
---|
[fb04321] | 1276 | if ( funcSpecs.any() ) {
|
---|
[a16764a6] | 1277 | SemanticError( this, "invalid function specifier for " );
|
---|
[843054c2] | 1278 | } // if
|
---|
[e874605] | 1279 | if ( enumInLine ) {
|
---|
[bb7422a] | 1280 | return new ast::InlineMemberDecl( location,
|
---|
| 1281 | *name, (ast::Type*)nullptr, storageClasses, linkage );
|
---|
[e874605] | 1282 | } // if
|
---|
[dd020c0] | 1283 | assertf( name, "ObjectDecl must a have name\n" );
|
---|
[bb7422a] | 1284 | auto ret = new ast::ObjectDecl( location,
|
---|
| 1285 | *name,
|
---|
| 1286 | (ast::Type*)nullptr,
|
---|
| 1287 | maybeBuild( initializer ),
|
---|
| 1288 | storageClasses,
|
---|
| 1289 | linkage,
|
---|
| 1290 | maybeBuild( bitfieldWidth )
|
---|
| 1291 | );
|
---|
| 1292 | ret->asmName = asmName;
|
---|
| 1293 | ret->extension = extension;
|
---|
| 1294 | return ret;
|
---|
[51b73452] | 1295 | }
|
---|
| 1296 |
|
---|
[bb7422a] | 1297 | ast::Type * DeclarationNode::buildType() const {
|
---|
[b87a5ed] | 1298 | assert( type );
|
---|
[974906e2] | 1299 |
|
---|
[b87a5ed] | 1300 | switch ( type->kind ) {
|
---|
[0d0931d] | 1301 | case TypeData::Enum:
|
---|
| 1302 | case TypeData::Aggregate: {
|
---|
[bb7422a] | 1303 | ast::BaseInstType * ret =
|
---|
| 1304 | buildComAggInst( type, copy( attributes ), linkage );
|
---|
| 1305 | buildList( type->aggregate.actuals, ret->params );
|
---|
[0d0931d] | 1306 | return ret;
|
---|
| 1307 | }
|
---|
| 1308 | case TypeData::Symbolic: {
|
---|
[bb7422a] | 1309 | ast::TypeInstType * ret = new ast::TypeInstType(
|
---|
| 1310 | *type->symbolic.name,
|
---|
| 1311 | // This is just a default, the true value is not known yet.
|
---|
| 1312 | ast::TypeDecl::Dtype,
|
---|
| 1313 | buildQualifiers( type ),
|
---|
| 1314 | copy( attributes ) );
|
---|
| 1315 | buildList( type->symbolic.actuals, ret->params );
|
---|
[0d0931d] | 1316 | return ret;
|
---|
| 1317 | }
|
---|
| 1318 | default:
|
---|
[bb7422a] | 1319 | ast::Type * simpletypes = typebuild( type );
|
---|
| 1320 | // copy because member is const
|
---|
| 1321 | simpletypes->attributes = attributes;
|
---|
[c0aa336] | 1322 | return simpletypes;
|
---|
[b87a5ed] | 1323 | } // switch
|
---|
[3848e0e] | 1324 | }
|
---|
| 1325 |
|
---|
[b87a5ed] | 1326 | // Local Variables: //
|
---|
| 1327 | // tab-width: 4 //
|
---|
| 1328 | // mode: c++ //
|
---|
| 1329 | // compile-command: "make install" //
|
---|
| 1330 | // End: //
|
---|