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