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