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