| 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 | // TypeData.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Rodolfo G. Esteves
|
|---|
| 10 | // Created On : Sat May 16 15:12:51 2015
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Mon Dec 16 07:56:46 2019
|
|---|
| 13 | // Update Count : 662
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <cassert> // for assert
|
|---|
| 17 | #include <ostream> // for operator<<, ostream, basic_ostream
|
|---|
| 18 |
|
|---|
| 19 | #include "Common/SemanticError.h" // for SemanticError
|
|---|
| 20 | #include "Common/utility.h" // for maybeClone, maybeBuild, maybeMoveB...
|
|---|
| 21 | #include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode
|
|---|
| 22 | #include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, FunctionDecl
|
|---|
| 23 | #include "SynTree/Expression.h" // for Expression, ConstantExpr (ptr only)
|
|---|
| 24 | #include "SynTree/Initializer.h" // for SingleInit, Initializer (ptr only)
|
|---|
| 25 | #include "SynTree/Statement.h" // for CompoundStmt, Statement
|
|---|
| 26 | #include "SynTree/Type.h" // for BasicType, Type, Type::ForallList
|
|---|
| 27 | #include "TypeData.h"
|
|---|
| 28 |
|
|---|
| 29 | class Attribute;
|
|---|
| 30 |
|
|---|
| 31 | using namespace std;
|
|---|
| 32 |
|
|---|
| 33 | TypeData::TypeData( Kind k ) : location( yylloc ), kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ {
|
|---|
| 34 | switch ( kind ) {
|
|---|
| 35 | case Unknown:
|
|---|
| 36 | case Pointer:
|
|---|
| 37 | case Reference:
|
|---|
| 38 | case EnumConstant:
|
|---|
| 39 | case GlobalScope:
|
|---|
| 40 | // nothing else to initialize
|
|---|
| 41 | break;
|
|---|
| 42 | case Basic:
|
|---|
| 43 | // basic = new Basic_t;
|
|---|
| 44 | break;
|
|---|
| 45 | case Array:
|
|---|
| 46 | // array = new Array_t;
|
|---|
| 47 | array.dimension = nullptr;
|
|---|
| 48 | array.isVarLen = false;
|
|---|
| 49 | array.isStatic = false;
|
|---|
| 50 | break;
|
|---|
| 51 | case Function:
|
|---|
| 52 | // function = new Function_t;
|
|---|
| 53 | function.params = nullptr;
|
|---|
| 54 | function.idList = nullptr;
|
|---|
| 55 | function.oldDeclList = nullptr;
|
|---|
| 56 | function.body = nullptr;
|
|---|
| 57 | function.withExprs = nullptr;
|
|---|
| 58 | break;
|
|---|
| 59 | // Enum is an Aggregate, so both structures are initialized together.
|
|---|
| 60 | case Enum:
|
|---|
| 61 | // enumeration = new Enumeration_t;
|
|---|
| 62 | enumeration.name = nullptr;
|
|---|
| 63 | enumeration.constants = nullptr;
|
|---|
| 64 | enumeration.body = false;
|
|---|
| 65 | enumeration.anon = false;
|
|---|
| 66 | break;
|
|---|
| 67 | case Aggregate:
|
|---|
| 68 | // aggregate = new Aggregate_t;
|
|---|
| 69 | aggregate.kind = AggregateDecl::NoAggregate;
|
|---|
| 70 | aggregate.name = nullptr;
|
|---|
| 71 | aggregate.params = nullptr;
|
|---|
| 72 | aggregate.actuals = nullptr;
|
|---|
| 73 | aggregate.fields = nullptr;
|
|---|
| 74 | aggregate.body = false;
|
|---|
| 75 | aggregate.tagged = false;
|
|---|
| 76 | aggregate.parent = nullptr;
|
|---|
| 77 | aggregate.anon = false;
|
|---|
| 78 | break;
|
|---|
| 79 | case AggregateInst:
|
|---|
| 80 | // aggInst = new AggInst_t;
|
|---|
| 81 | aggInst.aggregate = nullptr;
|
|---|
| 82 | aggInst.params = nullptr;
|
|---|
| 83 | aggInst.hoistType = false;
|
|---|
| 84 | break;
|
|---|
| 85 | case Symbolic:
|
|---|
| 86 | case SymbolicInst:
|
|---|
| 87 | // symbolic = new Symbolic_t;
|
|---|
| 88 | symbolic.name = nullptr;
|
|---|
| 89 | symbolic.params = nullptr;
|
|---|
| 90 | symbolic.actuals = nullptr;
|
|---|
| 91 | symbolic.assertions = nullptr;
|
|---|
| 92 | break;
|
|---|
| 93 | case Tuple:
|
|---|
| 94 | // tuple = new Tuple_t;
|
|---|
| 95 | tuple = nullptr;
|
|---|
| 96 | break;
|
|---|
| 97 | case Typeof:
|
|---|
| 98 | case Basetypeof:
|
|---|
| 99 | // typeexpr = new Typeof_t;
|
|---|
| 100 | typeexpr = nullptr;
|
|---|
| 101 | break;
|
|---|
| 102 | case Builtin:
|
|---|
| 103 | // builtin = new Builtin_t;
|
|---|
| 104 | case Qualified:
|
|---|
| 105 | qualified.parent = nullptr;
|
|---|
| 106 | qualified.child = nullptr;
|
|---|
| 107 | break;
|
|---|
| 108 | } // switch
|
|---|
| 109 | } // TypeData::TypeData
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | TypeData::~TypeData() {
|
|---|
| 113 | delete base;
|
|---|
| 114 | delete forall;
|
|---|
| 115 |
|
|---|
| 116 | switch ( kind ) {
|
|---|
| 117 | case Unknown:
|
|---|
| 118 | case Pointer:
|
|---|
| 119 | case Reference:
|
|---|
| 120 | case EnumConstant:
|
|---|
| 121 | case GlobalScope:
|
|---|
| 122 | // nothing to destroy
|
|---|
| 123 | break;
|
|---|
| 124 | case Basic:
|
|---|
| 125 | // delete basic;
|
|---|
| 126 | break;
|
|---|
| 127 | case Array:
|
|---|
| 128 | delete array.dimension;
|
|---|
| 129 | // delete array;
|
|---|
| 130 | break;
|
|---|
| 131 | case Function:
|
|---|
| 132 | delete function.params;
|
|---|
| 133 | delete function.idList;
|
|---|
| 134 | delete function.oldDeclList;
|
|---|
| 135 | delete function.body;
|
|---|
| 136 | delete function.withExprs;
|
|---|
| 137 | // delete function;
|
|---|
| 138 | break;
|
|---|
| 139 | case Aggregate:
|
|---|
| 140 | delete aggregate.name;
|
|---|
| 141 | delete aggregate.params;
|
|---|
| 142 | delete aggregate.actuals;
|
|---|
| 143 | delete aggregate.fields;
|
|---|
| 144 | // delete aggregate;
|
|---|
| 145 | break;
|
|---|
| 146 | case AggregateInst:
|
|---|
| 147 | delete aggInst.aggregate;
|
|---|
| 148 | delete aggInst.params;
|
|---|
| 149 | // delete aggInst;
|
|---|
| 150 | break;
|
|---|
| 151 | case Enum:
|
|---|
| 152 | delete enumeration.name;
|
|---|
| 153 | delete enumeration.constants;
|
|---|
| 154 | // delete enumeration;
|
|---|
| 155 | break;
|
|---|
| 156 | case Symbolic:
|
|---|
| 157 | case SymbolicInst:
|
|---|
| 158 | delete symbolic.name;
|
|---|
| 159 | delete symbolic.params;
|
|---|
| 160 | delete symbolic.actuals;
|
|---|
| 161 | delete symbolic.assertions;
|
|---|
| 162 | // delete symbolic;
|
|---|
| 163 | break;
|
|---|
| 164 | case Tuple:
|
|---|
| 165 | // delete tuple->members;
|
|---|
| 166 | delete tuple;
|
|---|
| 167 | break;
|
|---|
| 168 | case Typeof:
|
|---|
| 169 | case Basetypeof:
|
|---|
| 170 | // delete typeexpr->expr;
|
|---|
| 171 | delete typeexpr;
|
|---|
| 172 | break;
|
|---|
| 173 | case Builtin:
|
|---|
| 174 | // delete builtin;
|
|---|
| 175 | break;
|
|---|
| 176 | case Qualified:
|
|---|
| 177 | delete qualified.parent;
|
|---|
| 178 | delete qualified.child;
|
|---|
| 179 | } // switch
|
|---|
| 180 | } // TypeData::~TypeData
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 | TypeData * TypeData::clone() const {
|
|---|
| 184 | TypeData * newtype = new TypeData( kind );
|
|---|
| 185 | newtype->qualifiers = qualifiers;
|
|---|
| 186 | newtype->base = maybeClone( base );
|
|---|
| 187 | newtype->forall = maybeClone( forall );
|
|---|
| 188 |
|
|---|
| 189 | switch ( kind ) {
|
|---|
| 190 | case Unknown:
|
|---|
| 191 | case EnumConstant:
|
|---|
| 192 | case Pointer:
|
|---|
| 193 | case Reference:
|
|---|
| 194 | case GlobalScope:
|
|---|
| 195 | // nothing else to copy
|
|---|
| 196 | break;
|
|---|
| 197 | case Basic:
|
|---|
| 198 | newtype->basictype = basictype;
|
|---|
| 199 | newtype->complextype = complextype;
|
|---|
| 200 | newtype->signedness = signedness;
|
|---|
| 201 | newtype->length = length;
|
|---|
| 202 | break;
|
|---|
| 203 | case Array:
|
|---|
| 204 | newtype->array.dimension = maybeClone( array.dimension );
|
|---|
| 205 | newtype->array.isVarLen = array.isVarLen;
|
|---|
| 206 | newtype->array.isStatic = array.isStatic;
|
|---|
| 207 | break;
|
|---|
| 208 | case Function:
|
|---|
| 209 | newtype->function.params = maybeClone( function.params );
|
|---|
| 210 | newtype->function.idList = maybeClone( function.idList );
|
|---|
| 211 | newtype->function.oldDeclList = maybeClone( function.oldDeclList );
|
|---|
| 212 | newtype->function.body = maybeClone( function.body );
|
|---|
| 213 | newtype->function.withExprs = maybeClone( function.withExprs );
|
|---|
| 214 | break;
|
|---|
| 215 | case Aggregate:
|
|---|
| 216 | newtype->aggregate.kind = aggregate.kind;
|
|---|
| 217 | newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
|
|---|
| 218 | newtype->aggregate.params = maybeClone( aggregate.params );
|
|---|
| 219 | newtype->aggregate.actuals = maybeClone( aggregate.actuals );
|
|---|
| 220 | newtype->aggregate.fields = maybeClone( aggregate.fields );
|
|---|
| 221 | newtype->aggregate.body = aggregate.body;
|
|---|
| 222 | newtype->aggregate.anon = aggregate.anon;
|
|---|
| 223 | newtype->aggregate.tagged = aggregate.tagged;
|
|---|
| 224 | newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
|
|---|
| 225 | break;
|
|---|
| 226 | case AggregateInst:
|
|---|
| 227 | newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
|
|---|
| 228 | newtype->aggInst.params = maybeClone( aggInst.params );
|
|---|
| 229 | newtype->aggInst.hoistType = aggInst.hoistType;
|
|---|
| 230 | break;
|
|---|
| 231 | case Enum:
|
|---|
| 232 | newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
|
|---|
| 233 | newtype->enumeration.constants = maybeClone( enumeration.constants );
|
|---|
| 234 | newtype->enumeration.body = enumeration.body;
|
|---|
| 235 | newtype->enumeration.anon = enumeration.anon;
|
|---|
| 236 | break;
|
|---|
| 237 | case Symbolic:
|
|---|
| 238 | case SymbolicInst:
|
|---|
| 239 | newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
|
|---|
| 240 | newtype->symbolic.params = maybeClone( symbolic.params );
|
|---|
| 241 | newtype->symbolic.actuals = maybeClone( symbolic.actuals );
|
|---|
| 242 | newtype->symbolic.assertions = maybeClone( symbolic.assertions );
|
|---|
| 243 | newtype->symbolic.isTypedef = symbolic.isTypedef;
|
|---|
| 244 | break;
|
|---|
| 245 | case Tuple:
|
|---|
| 246 | newtype->tuple = maybeClone( tuple );
|
|---|
| 247 | break;
|
|---|
| 248 | case Typeof:
|
|---|
| 249 | case Basetypeof:
|
|---|
| 250 | newtype->typeexpr = maybeClone( typeexpr );
|
|---|
| 251 | break;
|
|---|
| 252 | case Builtin:
|
|---|
| 253 | assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
|
|---|
| 254 | newtype->builtintype = builtintype;
|
|---|
| 255 | break;
|
|---|
| 256 | case Qualified:
|
|---|
| 257 | newtype->qualified.parent = maybeClone( qualified.parent );
|
|---|
| 258 | newtype->qualified.child = maybeClone( qualified.child );
|
|---|
| 259 | break;
|
|---|
| 260 | } // switch
|
|---|
| 261 | return newtype;
|
|---|
| 262 | } // TypeData::clone
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 | void TypeData::print( ostream &os, int indent ) const {
|
|---|
| 266 | for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
|
|---|
| 267 | if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
|
|---|
| 268 | } // for
|
|---|
| 269 |
|
|---|
| 270 | if ( forall ) {
|
|---|
| 271 | os << "forall " << endl;
|
|---|
| 272 | forall->printList( os, indent + 4 );
|
|---|
| 273 | } // if
|
|---|
| 274 |
|
|---|
| 275 | switch ( kind ) {
|
|---|
| 276 | case Basic:
|
|---|
| 277 | if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
|
|---|
| 278 | if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
|
|---|
| 279 | if ( complextype == DeclarationNode::NoComplexType ) { // basic type
|
|---|
| 280 | assert( basictype != DeclarationNode::NoBasicType );
|
|---|
| 281 | os << DeclarationNode::basicTypeNames[ basictype ] << " ";
|
|---|
| 282 | } else { // complex type
|
|---|
| 283 | // handle double _Complex
|
|---|
| 284 | if ( basictype != DeclarationNode::NoBasicType ) os << DeclarationNode::basicTypeNames[ basictype ] << " ";
|
|---|
| 285 | os << DeclarationNode::complexTypeNames[ complextype ] << " ";
|
|---|
| 286 | } // if
|
|---|
| 287 | break;
|
|---|
| 288 | case Pointer:
|
|---|
| 289 | os << "pointer ";
|
|---|
| 290 | if ( base ) {
|
|---|
| 291 | os << "to ";
|
|---|
| 292 | base->print( os, indent );
|
|---|
| 293 | } // if
|
|---|
| 294 | break;
|
|---|
| 295 | case Reference:
|
|---|
| 296 | os << "reference ";
|
|---|
| 297 | if ( base ) {
|
|---|
| 298 | os << "to ";
|
|---|
| 299 | base->print( os, indent );
|
|---|
| 300 | } // if
|
|---|
| 301 | break;
|
|---|
| 302 | case Array:
|
|---|
| 303 | if ( array.isStatic ) {
|
|---|
| 304 | os << "static ";
|
|---|
| 305 | } // if
|
|---|
| 306 | if ( array.dimension ) {
|
|---|
| 307 | os << "array of ";
|
|---|
| 308 | array.dimension->printOneLine( os, indent );
|
|---|
| 309 | } else if ( array.isVarLen ) {
|
|---|
| 310 | os << "variable-length array of ";
|
|---|
| 311 | } else {
|
|---|
| 312 | os << "open array of ";
|
|---|
| 313 | } // if
|
|---|
| 314 | if ( base ) {
|
|---|
| 315 | base->print( os, indent );
|
|---|
| 316 | } // if
|
|---|
| 317 | break;
|
|---|
| 318 | case Function:
|
|---|
| 319 | os << "function" << endl;
|
|---|
| 320 | if ( function.params ) {
|
|---|
| 321 | os << string( indent + 2, ' ' ) << "with parameters " << endl;
|
|---|
| 322 | function.params->printList( os, indent + 4 );
|
|---|
| 323 | } else {
|
|---|
| 324 | os << string( indent + 2, ' ' ) << "with no parameters" << endl;
|
|---|
| 325 | } // if
|
|---|
| 326 | if ( function.idList ) {
|
|---|
| 327 | os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
|
|---|
| 328 | function.idList->printList( os, indent + 4 );
|
|---|
| 329 | } // if
|
|---|
| 330 | if ( function.oldDeclList ) {
|
|---|
| 331 | os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
|
|---|
| 332 | function.oldDeclList->printList( os, indent + 4 );
|
|---|
| 333 | } // if
|
|---|
| 334 | os << string( indent + 2, ' ' ) << "returning ";
|
|---|
| 335 | if ( base ) {
|
|---|
| 336 | base->print( os, indent + 4 );
|
|---|
| 337 | } else {
|
|---|
| 338 | os << "nothing ";
|
|---|
| 339 | } // if
|
|---|
| 340 | os << endl;
|
|---|
| 341 | if ( function.body ) {
|
|---|
| 342 | os << string( indent + 2, ' ' ) << "with body " << endl;
|
|---|
| 343 | function.body->printList( os, indent + 2 );
|
|---|
| 344 | } // if
|
|---|
| 345 | break;
|
|---|
| 346 | case Aggregate:
|
|---|
| 347 | os << AggregateDecl::aggrString( aggregate.kind ) << ' ' << *aggregate.name << endl;
|
|---|
| 348 | if ( aggregate.params ) {
|
|---|
| 349 | os << string( indent + 2, ' ' ) << "with type parameters" << endl;
|
|---|
| 350 | aggregate.params->printList( os, indent + 4 );
|
|---|
| 351 | } // if
|
|---|
| 352 | if ( aggregate.actuals ) {
|
|---|
| 353 | os << string( indent + 2, ' ' ) << "instantiated with actual parameters" << endl;
|
|---|
| 354 | aggregate.actuals->printList( os, indent + 4 );
|
|---|
| 355 | } // if
|
|---|
| 356 | if ( aggregate.fields ) {
|
|---|
| 357 | os << string( indent + 2, ' ' ) << "with members" << endl;
|
|---|
| 358 | aggregate.fields->printList( os, indent + 4 );
|
|---|
| 359 | } // if
|
|---|
| 360 | if ( aggregate.body ) {
|
|---|
| 361 | os << string( indent + 2, ' ' ) << " with body" << endl;
|
|---|
| 362 | } // if
|
|---|
| 363 | break;
|
|---|
| 364 | case AggregateInst:
|
|---|
| 365 | if ( aggInst.aggregate ) {
|
|---|
| 366 | os << "instance of " ;
|
|---|
| 367 | aggInst.aggregate->print( os, indent );
|
|---|
| 368 | } else {
|
|---|
| 369 | os << "instance of an unspecified aggregate ";
|
|---|
| 370 | } // if
|
|---|
| 371 | if ( aggInst.params ) {
|
|---|
| 372 | os << string( indent + 2, ' ' ) << "with parameters" << endl;
|
|---|
| 373 | aggInst.params->printList( os, indent + 2 );
|
|---|
| 374 | } // if
|
|---|
| 375 | break;
|
|---|
| 376 | case Enum:
|
|---|
| 377 | os << "enumeration ";
|
|---|
| 378 | if ( enumeration.constants ) {
|
|---|
| 379 | os << "with constants" << endl;
|
|---|
| 380 | enumeration.constants->printList( os, indent + 2 );
|
|---|
| 381 | } // if
|
|---|
| 382 | if ( enumeration.body ) {
|
|---|
| 383 | os << string( indent + 2, ' ' ) << " with body" << endl;
|
|---|
| 384 | } // if
|
|---|
| 385 | break;
|
|---|
| 386 | case EnumConstant:
|
|---|
| 387 | os << "enumeration constant ";
|
|---|
| 388 | break;
|
|---|
| 389 | case Symbolic:
|
|---|
| 390 | if ( symbolic.isTypedef ) {
|
|---|
| 391 | os << "typedef definition ";
|
|---|
| 392 | } else {
|
|---|
| 393 | os << "type definition ";
|
|---|
| 394 | } // if
|
|---|
| 395 | if ( symbolic.params ) {
|
|---|
| 396 | os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
|
|---|
| 397 | symbolic.params->printList( os, indent + 2 );
|
|---|
| 398 | } // if
|
|---|
| 399 | if ( symbolic.assertions ) {
|
|---|
| 400 | os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
|
|---|
| 401 | symbolic.assertions->printList( os, indent + 4 );
|
|---|
| 402 | os << string( indent + 2, ' ' );
|
|---|
| 403 | } // if
|
|---|
| 404 | if ( base ) {
|
|---|
| 405 | os << "for ";
|
|---|
| 406 | base->print( os, indent + 2 );
|
|---|
| 407 | } // if
|
|---|
| 408 | break;
|
|---|
| 409 | case SymbolicInst:
|
|---|
| 410 | os << *symbolic.name;
|
|---|
| 411 | if ( symbolic.actuals ) {
|
|---|
| 412 | os << "(";
|
|---|
| 413 | symbolic.actuals->printList( os, indent + 2 );
|
|---|
| 414 | os << ")";
|
|---|
| 415 | } // if
|
|---|
| 416 | break;
|
|---|
| 417 | case Tuple:
|
|---|
| 418 | os << "tuple ";
|
|---|
| 419 | if ( tuple ) {
|
|---|
| 420 | os << "with members" << endl;
|
|---|
| 421 | tuple->printList( os, indent + 2 );
|
|---|
| 422 | } // if
|
|---|
| 423 | break;
|
|---|
| 424 | case Basetypeof:
|
|---|
| 425 | os << "base-";
|
|---|
| 426 | #if defined(__GNUC__) && __GNUC__ >= 7
|
|---|
| 427 | __attribute__((fallthrough));
|
|---|
| 428 | #endif
|
|---|
| 429 | case Typeof:
|
|---|
| 430 | os << "type-of expression ";
|
|---|
| 431 | if ( typeexpr ) {
|
|---|
| 432 | typeexpr->print( os, indent + 2 );
|
|---|
| 433 | } // if
|
|---|
| 434 | break;
|
|---|
| 435 | case Builtin:
|
|---|
| 436 | os << DeclarationNode::builtinTypeNames[builtintype];
|
|---|
| 437 | break;
|
|---|
| 438 | case GlobalScope:
|
|---|
| 439 | break;
|
|---|
| 440 | case Qualified:
|
|---|
| 441 | qualified.parent->print( os );
|
|---|
| 442 | os << ".";
|
|---|
| 443 | qualified.child->print( os );
|
|---|
| 444 | break;
|
|---|
| 445 | case Unknown:
|
|---|
| 446 | os << "entity of unknown type ";
|
|---|
| 447 | break;
|
|---|
| 448 | default:
|
|---|
| 449 | os << "internal error: TypeData::print " << kind << endl;
|
|---|
| 450 | assert( false );
|
|---|
| 451 | } // switch
|
|---|
| 452 | } // TypeData::print
|
|---|
| 453 |
|
|---|
| 454 | const std::string * TypeData::leafName() const {
|
|---|
| 455 | switch ( kind ) {
|
|---|
| 456 | case Unknown:
|
|---|
| 457 | case Pointer:
|
|---|
| 458 | case Reference:
|
|---|
| 459 | case EnumConstant:
|
|---|
| 460 | case GlobalScope:
|
|---|
| 461 | case Array:
|
|---|
| 462 | case Basic:
|
|---|
| 463 | case Function:
|
|---|
| 464 | case AggregateInst:
|
|---|
| 465 | case Tuple:
|
|---|
| 466 | case Typeof:
|
|---|
| 467 | case Basetypeof:
|
|---|
| 468 | case Builtin:
|
|---|
| 469 | assertf(false, "Tried to get leaf name from kind without a name: %d", kind);
|
|---|
| 470 | break;
|
|---|
| 471 | case Aggregate:
|
|---|
| 472 | return aggregate.name;
|
|---|
| 473 | case Enum:
|
|---|
| 474 | return enumeration.name;
|
|---|
| 475 | case Symbolic:
|
|---|
| 476 | case SymbolicInst:
|
|---|
| 477 | return symbolic.name;
|
|---|
| 478 | case Qualified:
|
|---|
| 479 | return qualified.child->leafName();
|
|---|
| 480 | } // switch
|
|---|
| 481 | assert(false);
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 | template< typename ForallList >
|
|---|
| 486 | void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
|
|---|
| 487 | buildList( firstNode, outputList );
|
|---|
| 488 | auto n = firstNode;
|
|---|
| 489 | for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i, n = (DeclarationNode*)n->get_next() ) {
|
|---|
| 490 | TypeDecl * td = static_cast<TypeDecl *>(*i);
|
|---|
| 491 | if ( n->variable.tyClass == TypeDecl::Otype ) {
|
|---|
| 492 | // add assertion parameters to `type' tyvars in reverse order
|
|---|
| 493 | // add dtor: void ^?{}(T *)
|
|---|
| 494 | FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
|
|---|
| 495 | dtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
|
|---|
| 496 | td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
|
|---|
| 497 |
|
|---|
| 498 | // add copy ctor: void ?{}(T *, T)
|
|---|
| 499 | FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
|
|---|
| 500 | copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
|
|---|
| 501 | copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
|
|---|
| 502 | td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
|
|---|
| 503 |
|
|---|
| 504 | // add default ctor: void ?{}(T *)
|
|---|
| 505 | FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
|
|---|
| 506 | ctorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
|
|---|
| 507 | td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
|
|---|
| 508 |
|
|---|
| 509 | // add assignment operator: T * ?=?(T *, T)
|
|---|
| 510 | FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
|
|---|
| 511 | assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
|
|---|
| 512 | assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
|
|---|
| 513 | assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
|
|---|
| 514 | td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
|
|---|
| 515 | } // if
|
|---|
| 516 | } // for
|
|---|
| 517 | } // buildForall
|
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 | Type * typebuild( const TypeData * td ) {
|
|---|
| 521 | assert( td );
|
|---|
| 522 | switch ( td->kind ) {
|
|---|
| 523 | case TypeData::Unknown:
|
|---|
| 524 | // fill in implicit int
|
|---|
| 525 | return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
|
|---|
| 526 | case TypeData::Basic:
|
|---|
| 527 | return buildBasicType( td );
|
|---|
| 528 | case TypeData::Pointer:
|
|---|
| 529 | return buildPointer( td );
|
|---|
| 530 | case TypeData::Array:
|
|---|
| 531 | return buildArray( td );
|
|---|
| 532 | case TypeData::Reference:
|
|---|
| 533 | return buildReference( td );
|
|---|
| 534 | case TypeData::Function:
|
|---|
| 535 | return buildFunction( td );
|
|---|
| 536 | case TypeData::AggregateInst:
|
|---|
| 537 | return buildAggInst( td );
|
|---|
| 538 | case TypeData::EnumConstant:
|
|---|
| 539 | // the name gets filled in later -- by SymTab::Validate
|
|---|
| 540 | return new EnumInstType( buildQualifiers( td ), "" );
|
|---|
| 541 | case TypeData::SymbolicInst:
|
|---|
| 542 | return buildSymbolicInst( td );
|
|---|
| 543 | case TypeData::Tuple:
|
|---|
| 544 | return buildTuple( td );
|
|---|
| 545 | case TypeData::Typeof:
|
|---|
| 546 | case TypeData::Basetypeof:
|
|---|
| 547 | return buildTypeof( td );
|
|---|
| 548 | case TypeData::Builtin:
|
|---|
| 549 | switch ( td->builtintype ) {
|
|---|
| 550 | case DeclarationNode::Zero:
|
|---|
| 551 | return new ZeroType( noQualifiers );
|
|---|
| 552 | case DeclarationNode::One:
|
|---|
| 553 | return new OneType( noQualifiers );
|
|---|
| 554 | default:
|
|---|
| 555 | return new VarArgsType( buildQualifiers( td ) );
|
|---|
| 556 | } // switch
|
|---|
| 557 | case TypeData::GlobalScope:
|
|---|
| 558 | return new GlobalScopeType();
|
|---|
| 559 | case TypeData::Qualified:
|
|---|
| 560 | return new QualifiedType( buildQualifiers( td ), typebuild( td->qualified.parent ), typebuild( td->qualified.child ) );
|
|---|
| 561 | case TypeData::Symbolic:
|
|---|
| 562 | case TypeData::Enum:
|
|---|
| 563 | case TypeData::Aggregate:
|
|---|
| 564 | assert( false );
|
|---|
| 565 | } // switch
|
|---|
| 566 |
|
|---|
| 567 | return nullptr;
|
|---|
| 568 | } // typebuild
|
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 | TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
|
|---|
| 572 | TypeData * ret = nullptr;
|
|---|
| 573 |
|
|---|
| 574 | switch ( td->kind ) {
|
|---|
| 575 | case TypeData::Aggregate:
|
|---|
| 576 | if ( ! toplevel && td->aggregate.body ) {
|
|---|
| 577 | ret = td->clone();
|
|---|
| 578 | } // if
|
|---|
| 579 | break;
|
|---|
| 580 | case TypeData::Enum:
|
|---|
| 581 | if ( ! toplevel && td->enumeration.body ) {
|
|---|
| 582 | ret = td->clone();
|
|---|
| 583 | } // if
|
|---|
| 584 | break;
|
|---|
| 585 | case TypeData::AggregateInst:
|
|---|
| 586 | if ( td->aggInst.aggregate ) {
|
|---|
| 587 | ret = typeextractAggregate( td->aggInst.aggregate, false );
|
|---|
| 588 | } // if
|
|---|
| 589 | break;
|
|---|
| 590 | default:
|
|---|
| 591 | if ( td->base ) {
|
|---|
| 592 | ret = typeextractAggregate( td->base, false );
|
|---|
| 593 | } // if
|
|---|
| 594 | } // switch
|
|---|
| 595 | return ret;
|
|---|
| 596 | } // typeextractAggregate
|
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 | Type::Qualifiers buildQualifiers( const TypeData * td ) {
|
|---|
| 600 | return td->qualifiers;
|
|---|
| 601 | } // buildQualifiers
|
|---|
| 602 |
|
|---|
| 603 |
|
|---|
| 604 | static string genTSError( string msg, DeclarationNode::BasicType basictype ) {
|
|---|
| 605 | SemanticError( yylloc, string( "invalid type specifier \"" ) + msg + "\" for type \"" + DeclarationNode::basicTypeNames[basictype] + "\"." );
|
|---|
| 606 | } // genTSError
|
|---|
| 607 |
|
|---|
| 608 | Type * buildBasicType( const TypeData * td ) {
|
|---|
| 609 | BasicType::Kind ret;
|
|---|
| 610 |
|
|---|
| 611 | switch ( td->basictype ) {
|
|---|
| 612 | case DeclarationNode::Void:
|
|---|
| 613 | if ( td->signedness != DeclarationNode::NoSignedness ) {
|
|---|
| 614 | genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
|
|---|
| 615 | } // if
|
|---|
| 616 | if ( td->length != DeclarationNode::NoLength ) {
|
|---|
| 617 | genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
|
|---|
| 618 | } // if
|
|---|
| 619 | return new VoidType( buildQualifiers( td ) );
|
|---|
| 620 | break;
|
|---|
| 621 |
|
|---|
| 622 | case DeclarationNode::Bool:
|
|---|
| 623 | if ( td->signedness != DeclarationNode::NoSignedness ) {
|
|---|
| 624 | genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
|
|---|
| 625 | } // if
|
|---|
| 626 | if ( td->length != DeclarationNode::NoLength ) {
|
|---|
| 627 | genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
|
|---|
| 628 | } // if
|
|---|
| 629 |
|
|---|
| 630 | ret = BasicType::Bool;
|
|---|
| 631 | break;
|
|---|
| 632 |
|
|---|
| 633 | case DeclarationNode::Char:
|
|---|
| 634 | // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
|
|---|
| 635 | // character types. The implementation shall define char to have the same range, representation, and behavior as
|
|---|
| 636 | // either signed char or unsigned char.
|
|---|
| 637 | static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
|
|---|
| 638 |
|
|---|
| 639 | if ( td->length != DeclarationNode::NoLength ) {
|
|---|
| 640 | genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
|
|---|
| 641 | } // if
|
|---|
| 642 |
|
|---|
| 643 | ret = chartype[ td->signedness ];
|
|---|
| 644 | break;
|
|---|
| 645 |
|
|---|
| 646 | case DeclarationNode::Int:
|
|---|
| 647 | static BasicType::Kind inttype[2][4] = {
|
|---|
| 648 | { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
|
|---|
| 649 | { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
|
|---|
| 650 | };
|
|---|
| 651 |
|
|---|
| 652 | Integral: ;
|
|---|
| 653 | if ( td->signedness == DeclarationNode::NoSignedness ) {
|
|---|
| 654 | const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
|
|---|
| 655 | } // if
|
|---|
| 656 | ret = inttype[ td->signedness ][ td->length ];
|
|---|
| 657 | break;
|
|---|
| 658 |
|
|---|
| 659 | case DeclarationNode::Int128:
|
|---|
| 660 | ret = td->signedness == DeclarationNode::Unsigned ? BasicType::UnsignedInt128 : BasicType::SignedInt128;
|
|---|
| 661 | if ( td->length != DeclarationNode::NoLength ) {
|
|---|
| 662 | genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
|
|---|
| 663 | } // if
|
|---|
| 664 | break;
|
|---|
| 665 |
|
|---|
| 666 | case DeclarationNode::Float:
|
|---|
| 667 | case DeclarationNode::Double:
|
|---|
| 668 | case DeclarationNode::LongDouble: // not set until below
|
|---|
| 669 | case DeclarationNode::uuFloat80:
|
|---|
| 670 | case DeclarationNode::uuFloat128:
|
|---|
| 671 | case DeclarationNode::uFloat16:
|
|---|
| 672 | case DeclarationNode::uFloat32:
|
|---|
| 673 | case DeclarationNode::uFloat32x:
|
|---|
| 674 | case DeclarationNode::uFloat64:
|
|---|
| 675 | case DeclarationNode::uFloat64x:
|
|---|
| 676 | case DeclarationNode::uFloat128:
|
|---|
| 677 | case DeclarationNode::uFloat128x:
|
|---|
| 678 | static BasicType::Kind floattype[2][12] = {
|
|---|
| 679 | { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, (BasicType::Kind)-1, (BasicType::Kind)-1, BasicType::uFloat16Complex, BasicType::uFloat32Complex, BasicType::uFloat32xComplex, BasicType::uFloat64Complex, BasicType::uFloat64xComplex, BasicType::uFloat128Complex, BasicType::uFloat128xComplex, },
|
|---|
| 680 | { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x, },
|
|---|
| 681 | };
|
|---|
| 682 |
|
|---|
| 683 | FloatingPoint: ;
|
|---|
| 684 | if ( td->signedness != DeclarationNode::NoSignedness ) {
|
|---|
| 685 | genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
|
|---|
| 686 | } // if
|
|---|
| 687 | if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
|
|---|
| 688 | genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
|
|---|
| 689 | } // if
|
|---|
| 690 | if ( td->basictype != DeclarationNode::Double && td->length == DeclarationNode::Long ) {
|
|---|
| 691 | genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
|
|---|
| 692 | } // if
|
|---|
| 693 | if ( td->complextype == DeclarationNode::Imaginary ) {
|
|---|
| 694 | genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
|
|---|
| 695 | } // if
|
|---|
| 696 | if ( (td->basictype == DeclarationNode::uuFloat80 || td->basictype == DeclarationNode::uuFloat128) && td->complextype == DeclarationNode::Complex ) { // gcc unsupported
|
|---|
| 697 | genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
|
|---|
| 698 | } // if
|
|---|
| 699 | if ( td->length == DeclarationNode::Long ) {
|
|---|
| 700 | const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
|
|---|
| 701 | } // if
|
|---|
| 702 |
|
|---|
| 703 | ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
|
|---|
| 704 | //printf( "XXXX %d %d %d %d\n", td->complextype, td->basictype, DeclarationNode::Float, ret );
|
|---|
| 705 | break;
|
|---|
| 706 |
|
|---|
| 707 | case DeclarationNode::NoBasicType:
|
|---|
| 708 | // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
|
|---|
| 709 | if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
|
|---|
| 710 | const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
|
|---|
| 711 | goto FloatingPoint;
|
|---|
| 712 | } // if
|
|---|
| 713 |
|
|---|
| 714 | const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
|
|---|
| 715 | goto Integral;
|
|---|
| 716 | default:
|
|---|
| 717 | assertf( false, "unknown basic type" );
|
|---|
| 718 | return nullptr;
|
|---|
| 719 | } // switch
|
|---|
| 720 |
|
|---|
| 721 | BasicType * bt = new BasicType( buildQualifiers( td ), ret );
|
|---|
| 722 | buildForall( td->forall, bt->get_forall() );
|
|---|
| 723 | return bt;
|
|---|
| 724 | } // buildBasicType
|
|---|
| 725 |
|
|---|
| 726 |
|
|---|
| 727 | PointerType * buildPointer( const TypeData * td ) {
|
|---|
| 728 | PointerType * pt;
|
|---|
| 729 | if ( td->base ) {
|
|---|
| 730 | pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
|
|---|
| 731 | } else {
|
|---|
| 732 | pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
|
|---|
| 733 | } // if
|
|---|
| 734 | buildForall( td->forall, pt->get_forall() );
|
|---|
| 735 | return pt;
|
|---|
| 736 | } // buildPointer
|
|---|
| 737 |
|
|---|
| 738 |
|
|---|
| 739 | ArrayType * buildArray( const TypeData * td ) {
|
|---|
| 740 | ArrayType * at;
|
|---|
| 741 | if ( td->base ) {
|
|---|
| 742 | at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
|
|---|
| 743 | td->array.isVarLen, td->array.isStatic );
|
|---|
| 744 | } else {
|
|---|
| 745 | at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
|
|---|
| 746 | maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
|
|---|
| 747 | } // if
|
|---|
| 748 | buildForall( td->forall, at->get_forall() );
|
|---|
| 749 | return at;
|
|---|
| 750 | } // buildArray
|
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 | ReferenceType * buildReference( const TypeData * td ) {
|
|---|
| 754 | ReferenceType * rt;
|
|---|
| 755 | if ( td->base ) {
|
|---|
| 756 | rt = new ReferenceType( buildQualifiers( td ), typebuild( td->base ) );
|
|---|
| 757 | } else {
|
|---|
| 758 | rt = new ReferenceType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
|
|---|
| 759 | } // if
|
|---|
| 760 | buildForall( td->forall, rt->get_forall() );
|
|---|
| 761 | return rt;
|
|---|
| 762 | } // buildReference
|
|---|
| 763 |
|
|---|
| 764 |
|
|---|
| 765 | AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
|
|---|
| 766 | assert( td->kind == TypeData::Aggregate );
|
|---|
| 767 | AggregateDecl * at;
|
|---|
| 768 | switch ( td->aggregate.kind ) {
|
|---|
| 769 | case AggregateDecl::Struct:
|
|---|
| 770 | case AggregateDecl::Coroutine:
|
|---|
| 771 | case AggregateDecl::Generator:
|
|---|
| 772 | case AggregateDecl::Monitor:
|
|---|
| 773 | case AggregateDecl::Thread:
|
|---|
| 774 | at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
|
|---|
| 775 | buildForall( td->aggregate.params, at->get_parameters() );
|
|---|
| 776 | break;
|
|---|
| 777 | case AggregateDecl::Union:
|
|---|
| 778 | at = new UnionDecl( *td->aggregate.name, attributes, linkage );
|
|---|
| 779 | buildForall( td->aggregate.params, at->get_parameters() );
|
|---|
| 780 | break;
|
|---|
| 781 | case AggregateDecl::Trait:
|
|---|
| 782 | at = new TraitDecl( *td->aggregate.name, attributes, linkage );
|
|---|
| 783 | buildList( td->aggregate.params, at->get_parameters() );
|
|---|
| 784 | break;
|
|---|
| 785 | default:
|
|---|
| 786 | assert( false );
|
|---|
| 787 | } // switch
|
|---|
| 788 |
|
|---|
| 789 | buildList( td->aggregate.fields, at->get_members() );
|
|---|
| 790 | at->set_body( td->aggregate.body );
|
|---|
| 791 |
|
|---|
| 792 | return at;
|
|---|
| 793 | } // buildAggregate
|
|---|
| 794 |
|
|---|
| 795 |
|
|---|
| 796 | ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
|
|---|
| 797 | switch ( type->kind ) {
|
|---|
| 798 | case TypeData::Enum: {
|
|---|
| 799 | if ( type->enumeration.body ) {
|
|---|
| 800 | EnumDecl * typedecl = buildEnum( type, attributes, linkage );
|
|---|
| 801 | return new EnumInstType( buildQualifiers( type ), typedecl );
|
|---|
| 802 | } else {
|
|---|
| 803 | return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
|
|---|
| 804 | } // if
|
|---|
| 805 | }
|
|---|
| 806 | case TypeData::Aggregate: {
|
|---|
| 807 | ReferenceToType * ret;
|
|---|
| 808 | if ( type->aggregate.body ) {
|
|---|
| 809 | AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
|
|---|
| 810 | switch ( type->aggregate.kind ) {
|
|---|
| 811 | case AggregateDecl::Struct:
|
|---|
| 812 | case AggregateDecl::Coroutine:
|
|---|
| 813 | case AggregateDecl::Monitor:
|
|---|
| 814 | case AggregateDecl::Thread:
|
|---|
| 815 | ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
|
|---|
| 816 | break;
|
|---|
| 817 | case AggregateDecl::Union:
|
|---|
| 818 | ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
|
|---|
| 819 | break;
|
|---|
| 820 | case AggregateDecl::Trait:
|
|---|
| 821 | assert( false );
|
|---|
| 822 | //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
|
|---|
| 823 | break;
|
|---|
| 824 | default:
|
|---|
| 825 | assert( false );
|
|---|
| 826 | } // switch
|
|---|
| 827 | } else {
|
|---|
| 828 | switch ( type->aggregate.kind ) {
|
|---|
| 829 | case AggregateDecl::Struct:
|
|---|
| 830 | case AggregateDecl::Coroutine:
|
|---|
| 831 | case AggregateDecl::Monitor:
|
|---|
| 832 | case AggregateDecl::Thread:
|
|---|
| 833 | ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
|
|---|
| 834 | break;
|
|---|
| 835 | case AggregateDecl::Union:
|
|---|
| 836 | ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
|
|---|
| 837 | break;
|
|---|
| 838 | case AggregateDecl::Trait:
|
|---|
| 839 | ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
|
|---|
| 840 | break;
|
|---|
| 841 | default:
|
|---|
| 842 | assert( false );
|
|---|
| 843 | } // switch
|
|---|
| 844 | } // if
|
|---|
| 845 | return ret;
|
|---|
| 846 | }
|
|---|
| 847 | default:
|
|---|
| 848 | assert( false );
|
|---|
| 849 | } // switch
|
|---|
| 850 | } // buildAggInst
|
|---|
| 851 |
|
|---|
| 852 |
|
|---|
| 853 | ReferenceToType * buildAggInst( const TypeData * td ) {
|
|---|
| 854 | assert( td->kind == TypeData::AggregateInst );
|
|---|
| 855 |
|
|---|
| 856 | // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
|
|---|
| 857 | ReferenceToType * ret = nullptr;
|
|---|
| 858 | TypeData * type = td->aggInst.aggregate;
|
|---|
| 859 | switch ( type->kind ) {
|
|---|
| 860 | case TypeData::Enum: {
|
|---|
| 861 | return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
|
|---|
| 862 | }
|
|---|
| 863 | case TypeData::Aggregate: {
|
|---|
| 864 | switch ( type->aggregate.kind ) {
|
|---|
| 865 | case AggregateDecl::Struct:
|
|---|
| 866 | case AggregateDecl::Coroutine:
|
|---|
| 867 | case AggregateDecl::Monitor:
|
|---|
| 868 | case AggregateDecl::Thread:
|
|---|
| 869 | ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
|
|---|
| 870 | break;
|
|---|
| 871 | case AggregateDecl::Union:
|
|---|
| 872 | ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
|
|---|
| 873 | break;
|
|---|
| 874 | case AggregateDecl::Trait:
|
|---|
| 875 | ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
|
|---|
| 876 | break;
|
|---|
| 877 | default:
|
|---|
| 878 | assert( false );
|
|---|
| 879 | } // switch
|
|---|
| 880 | }
|
|---|
| 881 | break;
|
|---|
| 882 | default:
|
|---|
| 883 | assert( false );
|
|---|
| 884 | } // switch
|
|---|
| 885 |
|
|---|
| 886 | ret->set_hoistType( td->aggInst.hoistType );
|
|---|
| 887 | buildList( td->aggInst.params, ret->get_parameters() );
|
|---|
| 888 | buildForall( td->forall, ret->get_forall() );
|
|---|
| 889 | return ret;
|
|---|
| 890 | } // buildAggInst
|
|---|
| 891 |
|
|---|
| 892 |
|
|---|
| 893 | NamedTypeDecl * buildSymbolic( const TypeData * td, std::list< Attribute * > attributes, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
|
|---|
| 894 | assert( td->kind == TypeData::Symbolic );
|
|---|
| 895 | NamedTypeDecl * ret;
|
|---|
| 896 | assert( td->base );
|
|---|
| 897 | if ( td->symbolic.isTypedef ) {
|
|---|
| 898 | ret = new TypedefDecl( name, td->location, scs, typebuild( td->base ), linkage );
|
|---|
| 899 | } else {
|
|---|
| 900 | ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Dtype, true );
|
|---|
| 901 | } // if
|
|---|
| 902 | buildList( td->symbolic.assertions, ret->get_assertions() );
|
|---|
| 903 | ret->base->attributes.splice( ret->base->attributes.end(), attributes );
|
|---|
| 904 | return ret;
|
|---|
| 905 | } // buildSymbolic
|
|---|
| 906 |
|
|---|
| 907 |
|
|---|
| 908 | EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
|
|---|
| 909 | assert( td->kind == TypeData::Enum );
|
|---|
| 910 | EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage );
|
|---|
| 911 | buildList( td->enumeration.constants, ret->get_members() );
|
|---|
| 912 | list< Declaration * >::iterator members = ret->get_members().begin();
|
|---|
| 913 | for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
|
|---|
| 914 | if ( cur->has_enumeratorValue() ) {
|
|---|
| 915 | ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
|
|---|
| 916 | member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
|
|---|
| 917 | } // if
|
|---|
| 918 | } // for
|
|---|
| 919 | ret->set_body( td->enumeration.body );
|
|---|
| 920 | return ret;
|
|---|
| 921 | } // buildEnum
|
|---|
| 922 |
|
|---|
| 923 |
|
|---|
| 924 | TypeInstType * buildSymbolicInst( const TypeData * td ) {
|
|---|
| 925 | assert( td->kind == TypeData::SymbolicInst );
|
|---|
| 926 | TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
|
|---|
| 927 | buildList( td->symbolic.actuals, ret->get_parameters() );
|
|---|
| 928 | buildForall( td->forall, ret->get_forall() );
|
|---|
| 929 | return ret;
|
|---|
| 930 | } // buildSymbolicInst
|
|---|
| 931 |
|
|---|
| 932 |
|
|---|
| 933 | TupleType * buildTuple( const TypeData * td ) {
|
|---|
| 934 | assert( td->kind == TypeData::Tuple );
|
|---|
| 935 | std::list< Type * > types;
|
|---|
| 936 | buildTypeList( td->tuple, types );
|
|---|
| 937 | TupleType * ret = new TupleType( buildQualifiers( td ), types );
|
|---|
| 938 | buildForall( td->forall, ret->get_forall() );
|
|---|
| 939 | return ret;
|
|---|
| 940 | } // buildTuple
|
|---|
| 941 |
|
|---|
| 942 |
|
|---|
| 943 | TypeofType * buildTypeof( const TypeData * td ) {
|
|---|
| 944 | assert( td->kind == TypeData::Typeof || td->kind == TypeData::Basetypeof );
|
|---|
| 945 | assert( td->typeexpr );
|
|---|
| 946 | // assert( td->typeexpr->expr );
|
|---|
| 947 | return new TypeofType{
|
|---|
| 948 | buildQualifiers( td ), td->typeexpr->build(), td->kind == TypeData::Basetypeof };
|
|---|
| 949 | } // buildTypeof
|
|---|
| 950 |
|
|---|
| 951 |
|
|---|
| 952 | Declaration * buildDecl( const TypeData * td, const string &name, Type::StorageClasses scs, Expression * bitfieldWidth, Type::FuncSpecifiers funcSpec, LinkageSpec::Spec linkage, Expression *asmName, Initializer * init, std::list< Attribute * > attributes ) {
|
|---|
| 953 | if ( td->kind == TypeData::Function ) {
|
|---|
| 954 | if ( td->function.idList ) { // KR function ?
|
|---|
| 955 | buildKRFunction( td->function ); // transform into C11 function
|
|---|
| 956 | } // if
|
|---|
| 957 |
|
|---|
| 958 | FunctionDecl * decl;
|
|---|
| 959 | Statement * stmt = maybeBuild<Statement>( td->function.body );
|
|---|
| 960 | CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
|
|---|
| 961 | decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
|
|---|
| 962 | buildList( td->function.withExprs, decl->withExprs );
|
|---|
| 963 | return decl->set_asmName( asmName );
|
|---|
| 964 | } else if ( td->kind == TypeData::Aggregate ) {
|
|---|
| 965 | return buildAggregate( td, attributes, linkage );
|
|---|
| 966 | } else if ( td->kind == TypeData::Enum ) {
|
|---|
| 967 | return buildEnum( td, attributes, linkage );
|
|---|
| 968 | } else if ( td->kind == TypeData::Symbolic ) {
|
|---|
| 969 | return buildSymbolic( td, attributes, name, scs, linkage );
|
|---|
| 970 | } else {
|
|---|
| 971 | return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
|
|---|
| 972 | } // if
|
|---|
| 973 | return nullptr;
|
|---|
| 974 | } // buildDecl
|
|---|
| 975 |
|
|---|
| 976 |
|
|---|
| 977 | FunctionType * buildFunction( const TypeData * td ) {
|
|---|
| 978 | assert( td->kind == TypeData::Function );
|
|---|
| 979 | FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis );
|
|---|
| 980 | buildList( td->function.params, ft->parameters );
|
|---|
| 981 | buildForall( td->forall, ft->forall );
|
|---|
| 982 | if ( td->base ) {
|
|---|
| 983 | switch ( td->base->kind ) {
|
|---|
| 984 | case TypeData::Tuple:
|
|---|
| 985 | buildList( td->base->tuple, ft->returnVals );
|
|---|
| 986 | break;
|
|---|
| 987 | default:
|
|---|
| 988 | ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
|
|---|
| 989 | } // switch
|
|---|
| 990 | } else {
|
|---|
| 991 | ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
|
|---|
| 992 | } // if
|
|---|
| 993 | return ft;
|
|---|
| 994 | } // buildFunction
|
|---|
| 995 |
|
|---|
| 996 |
|
|---|
| 997 | // Transform KR routine declarations into C99 routine declarations:
|
|---|
| 998 | //
|
|---|
| 999 | // rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {}
|
|---|
| 1000 | //
|
|---|
| 1001 | // The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
|
|---|
| 1002 | // is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
|
|---|
| 1003 | // extra names are disallowed.
|
|---|
| 1004 | //
|
|---|
| 1005 | // Note, there is no KR routine-prototype syntax:
|
|---|
| 1006 | //
|
|---|
| 1007 | // rtn( a, b, c ) int a, c; double b; // invalid KR prototype
|
|---|
| 1008 | // rtn(); // valid KR prototype
|
|---|
| 1009 |
|
|---|
| 1010 | void buildKRFunction( const TypeData::Function_t & function ) {
|
|---|
| 1011 | assert( ! function.params );
|
|---|
| 1012 | // loop over declaration first as it is easier to spot errors
|
|---|
| 1013 | for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
|
|---|
| 1014 | // scan ALL parameter names for each declaration name to check for duplicates
|
|---|
| 1015 | for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
|
|---|
| 1016 | if ( *decl->name == *param->name ) {
|
|---|
| 1017 | // type set => parameter name already transformed by a declaration names so there is a duplicate
|
|---|
| 1018 | // declaration name attempting a second transformation
|
|---|
| 1019 | if ( param->type ) SemanticError( param->location, string( "duplicate declaration name " ) + *param->name );
|
|---|
| 1020 | // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
|
|---|
| 1021 | // parameter name attempting a second transformation
|
|---|
| 1022 | if ( ! decl->type ) SemanticError( param->location, string( "duplicate parameter name " ) + *param->name );
|
|---|
| 1023 | param->type = decl->type; // set copy declaration type to parameter type
|
|---|
| 1024 | decl->type = nullptr; // reset declaration type
|
|---|
| 1025 | param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
|
|---|
| 1026 | } // if
|
|---|
| 1027 | } // for
|
|---|
| 1028 | // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
|
|---|
| 1029 | if ( decl->type ) SemanticError( decl->location, string( "missing name in parameter list " ) + *decl->name );
|
|---|
| 1030 | } // for
|
|---|
| 1031 |
|
|---|
| 1032 | // Parameter names without a declaration default to type int:
|
|---|
| 1033 | //
|
|---|
| 1034 | // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
|
|---|
| 1035 |
|
|---|
| 1036 | for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
|
|---|
| 1037 | if ( ! param->type ) { // generate type int for empty parameter type
|
|---|
| 1038 | param->type = new TypeData( TypeData::Basic );
|
|---|
| 1039 | param->type->basictype = DeclarationNode::Int;
|
|---|
| 1040 | } // if
|
|---|
| 1041 | } // for
|
|---|
| 1042 |
|
|---|
| 1043 | function.params = function.idList; // newly modified idList becomes parameters
|
|---|
| 1044 | function.idList = nullptr; // idList now empty
|
|---|
| 1045 | delete function.oldDeclList; // deletes entire list
|
|---|
| 1046 | function.oldDeclList = nullptr; // reset
|
|---|
| 1047 | } // buildKRFunction
|
|---|
| 1048 |
|
|---|
| 1049 | // Local Variables: //
|
|---|
| 1050 | // tab-width: 4 //
|
|---|
| 1051 | // mode: c++ //
|
|---|
| 1052 | // compile-command: "make install" //
|
|---|
| 1053 | // End: //
|
|---|