[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 | // |
---|
[c92bdcc] | 7 | // TypeData.cpp -- |
---|
[b87a5ed] | 8 | // |
---|
| 9 | // Author : Rodolfo G. Esteves |
---|
| 10 | // Created On : Sat May 16 15:12:51 2015 |
---|
[b1f2007] | 11 | // Last Modified By : Peter A. Buhr |
---|
[4eb3a7c5] | 12 | // Last Modified On : Fri Feb 23 08:58:30 2024 |
---|
| 13 | // Update Count : 734 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[c92bdcc] | 16 | #include "TypeData.hpp" |
---|
[bb7422a] | 17 | |
---|
[c92bdcc] | 18 | #include <cassert> // for assert |
---|
| 19 | #include <ostream> // for operator<<, ostream, basic_ostream |
---|
[d180746] | 20 | |
---|
[c92bdcc] | 21 | #include "AST/Attribute.hpp" // for Attribute |
---|
| 22 | #include "AST/Decl.hpp" // for AggregateDecl, ObjectDecl, Type... |
---|
| 23 | #include "AST/Init.hpp" // for SingleInit, ListInit |
---|
| 24 | #include "AST/Print.hpp" // for print |
---|
| 25 | #include "AST/Type.hpp" // for Type |
---|
| 26 | #include "Common/SemanticError.hpp" // for SemanticError |
---|
| 27 | #include "Common/Utility.hpp" // for splice, spliceBegin |
---|
| 28 | #include "Common/Iterate.hpp" // for reverseIterate |
---|
| 29 | #include "Parser/ExpressionNode.hpp" // for ExpressionNode |
---|
| 30 | #include "Parser/StatementNode.hpp" // for StatementNode |
---|
[d180746] | 31 | |
---|
| 32 | class Attribute; |
---|
| 33 | |
---|
[2298f728] | 34 | using namespace std; |
---|
[51b7345] | 35 | |
---|
[e048ece] | 36 | // These must harmonize with the corresponding enumerations in the header. |
---|
| 37 | const char * TypeData::basicTypeNames[] = { |
---|
| 38 | "void", "_Bool", "char", "int", "int128", |
---|
| 39 | "float", "double", "long double", "float80", "float128", |
---|
| 40 | "_float16", "_float32", "_float32x", "_float64", "_float64x", "_float128", "_float128x", |
---|
| 41 | "NoBasicTypeNames" |
---|
| 42 | }; |
---|
| 43 | const char * TypeData::complexTypeNames[] = { |
---|
| 44 | "_Complex", "NoComplexTypeNames", "_Imaginary" |
---|
| 45 | }; // Imaginary unsupported => parse, but make invisible and print error message |
---|
| 46 | const char * TypeData::signednessNames[] = { |
---|
| 47 | "signed", "unsigned", "NoSignednessNames" |
---|
| 48 | }; |
---|
| 49 | const char * TypeData::lengthNames[] = { |
---|
| 50 | "short", "long", "long long", "NoLengthNames" |
---|
| 51 | }; |
---|
| 52 | const char * TypeData::builtinTypeNames[] = { |
---|
| 53 | "__builtin_va_list", "__auto_type", "zero_t", "one_t", "NoBuiltinTypeNames" |
---|
| 54 | }; |
---|
| 55 | |
---|
[0b0f1dd] | 56 | TypeData::TypeData( Kind k ) : location( yylloc ), kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ { |
---|
[b87a5ed] | 57 | switch ( kind ) { |
---|
[702e826] | 58 | case Unknown: |
---|
| 59 | case Pointer: |
---|
| 60 | case Reference: |
---|
| 61 | case EnumConstant: |
---|
| 62 | case GlobalScope: |
---|
| 63 | case Basic: |
---|
| 64 | // No unique data to initialize. |
---|
| 65 | break; |
---|
| 66 | case Array: |
---|
[2298f728] | 67 | array.dimension = nullptr; |
---|
[8f6f47d7] | 68 | array.isVarLen = false; |
---|
| 69 | array.isStatic = false; |
---|
[b87a5ed] | 70 | break; |
---|
[702e826] | 71 | case Function: |
---|
[2298f728] | 72 | function.params = nullptr; |
---|
| 73 | function.idList = nullptr; |
---|
| 74 | function.oldDeclList = nullptr; |
---|
| 75 | function.body = nullptr; |
---|
[84276ba] | 76 | function.withExprs = nullptr; |
---|
[b87a5ed] | 77 | break; |
---|
[702e826] | 78 | case Aggregate: |
---|
[bb7422a] | 79 | aggregate.kind = ast::AggregateDecl::NoAggregate; |
---|
[2298f728] | 80 | aggregate.name = nullptr; |
---|
| 81 | aggregate.params = nullptr; |
---|
| 82 | aggregate.actuals = nullptr; |
---|
| 83 | aggregate.fields = nullptr; |
---|
[4a9ccc3] | 84 | aggregate.body = false; |
---|
[3d7e53b] | 85 | aggregate.anon = false; |
---|
[99f7f37] | 86 | aggregate.isCfa = false; |
---|
[67467a3] | 87 | aggregate.hiding = EnumHiding::Visible; |
---|
[b87a5ed] | 88 | break; |
---|
[702e826] | 89 | case AggregateInst: |
---|
[2298f728] | 90 | aggInst.aggregate = nullptr; |
---|
| 91 | aggInst.params = nullptr; |
---|
[8f91c9ae] | 92 | aggInst.hoistType = false; |
---|
[b87a5ed] | 93 | break; |
---|
[702e826] | 94 | case Symbolic: |
---|
| 95 | case SymbolicInst: |
---|
[2298f728] | 96 | symbolic.name = nullptr; |
---|
| 97 | symbolic.params = nullptr; |
---|
| 98 | symbolic.actuals = nullptr; |
---|
| 99 | symbolic.assertions = nullptr; |
---|
[b87a5ed] | 100 | break; |
---|
[702e826] | 101 | case Tuple: |
---|
[8f6f47d7] | 102 | tuple = nullptr; |
---|
[b87a5ed] | 103 | break; |
---|
[702e826] | 104 | case Typeof: |
---|
| 105 | case Basetypeof: |
---|
[8f6f47d7] | 106 | typeexpr = nullptr; |
---|
[b87a5ed] | 107 | break; |
---|
[702e826] | 108 | case Vtable: |
---|
[bb7422a] | 109 | case Builtin: |
---|
[702e826] | 110 | // No unique data to initialize. |
---|
[93bbbc4] | 111 | break; |
---|
[702e826] | 112 | case Qualified: |
---|
[c194661] | 113 | qualified.parent = nullptr; |
---|
| 114 | qualified.child = nullptr; |
---|
[b87a5ed] | 115 | break; |
---|
[68cd1ce] | 116 | } // switch |
---|
[413ad05] | 117 | } // TypeData::TypeData |
---|
[51b7345] | 118 | |
---|
[201aeb9] | 119 | |
---|
[c8ffe20b] | 120 | TypeData::~TypeData() { |
---|
[b87a5ed] | 121 | delete base; |
---|
| 122 | delete forall; |
---|
| 123 | |
---|
| 124 | switch ( kind ) { |
---|
[702e826] | 125 | case Unknown: |
---|
| 126 | case Pointer: |
---|
| 127 | case Reference: |
---|
| 128 | case EnumConstant: |
---|
| 129 | case GlobalScope: |
---|
| 130 | case Basic: |
---|
[bb7422a] | 131 | // No unique data to deconstruct. |
---|
[b87a5ed] | 132 | break; |
---|
[702e826] | 133 | case Array: |
---|
[8f6f47d7] | 134 | delete array.dimension; |
---|
[b87a5ed] | 135 | break; |
---|
[702e826] | 136 | case Function: |
---|
[8f6f47d7] | 137 | delete function.params; |
---|
| 138 | delete function.idList; |
---|
| 139 | delete function.oldDeclList; |
---|
| 140 | delete function.body; |
---|
[84276ba] | 141 | delete function.withExprs; |
---|
[b87a5ed] | 142 | break; |
---|
[702e826] | 143 | case Aggregate: |
---|
[2298f728] | 144 | delete aggregate.name; |
---|
[8f6f47d7] | 145 | delete aggregate.params; |
---|
| 146 | delete aggregate.actuals; |
---|
| 147 | delete aggregate.fields; |
---|
[b87a5ed] | 148 | break; |
---|
[702e826] | 149 | case AggregateInst: |
---|
[8f6f47d7] | 150 | delete aggInst.aggregate; |
---|
| 151 | delete aggInst.params; |
---|
[b87a5ed] | 152 | break; |
---|
[702e826] | 153 | case Symbolic: |
---|
| 154 | case SymbolicInst: |
---|
[2298f728] | 155 | delete symbolic.name; |
---|
[8f6f47d7] | 156 | delete symbolic.params; |
---|
| 157 | delete symbolic.actuals; |
---|
| 158 | delete symbolic.assertions; |
---|
[b87a5ed] | 159 | break; |
---|
[702e826] | 160 | case Tuple: |
---|
[b87a5ed] | 161 | delete tuple; |
---|
| 162 | break; |
---|
[702e826] | 163 | case Typeof: |
---|
| 164 | case Basetypeof: |
---|
[b87a5ed] | 165 | delete typeexpr; |
---|
| 166 | break; |
---|
[702e826] | 167 | case Vtable: |
---|
| 168 | case Builtin: |
---|
| 169 | // No unique data to deconstruct. |
---|
[93bbbc4] | 170 | break; |
---|
[702e826] | 171 | case Qualified: |
---|
[c194661] | 172 | delete qualified.parent; |
---|
| 173 | delete qualified.child; |
---|
[702e826] | 174 | break; |
---|
[68cd1ce] | 175 | } // switch |
---|
[413ad05] | 176 | } // TypeData::~TypeData |
---|
[51b7345] | 177 | |
---|
[201aeb9] | 178 | |
---|
[413ad05] | 179 | TypeData * TypeData::clone() const { |
---|
| 180 | TypeData * newtype = new TypeData( kind ); |
---|
[738e304] | 181 | newtype->qualifiers = qualifiers; |
---|
[5bf685f] | 182 | newtype->base = maybeCopy( base ); |
---|
| 183 | newtype->forall = maybeCopy( forall ); |
---|
[b87a5ed] | 184 | |
---|
| 185 | switch ( kind ) { |
---|
[702e826] | 186 | case Unknown: |
---|
| 187 | case EnumConstant: |
---|
| 188 | case Pointer: |
---|
| 189 | case Reference: |
---|
| 190 | case GlobalScope: |
---|
[b87a5ed] | 191 | // nothing else to copy |
---|
| 192 | break; |
---|
[702e826] | 193 | case Basic: |
---|
[5b639ee] | 194 | newtype->basictype = basictype; |
---|
| 195 | newtype->complextype = complextype; |
---|
| 196 | newtype->signedness = signedness; |
---|
| 197 | newtype->length = length; |
---|
[b87a5ed] | 198 | break; |
---|
[702e826] | 199 | case Array: |
---|
[5bf685f] | 200 | newtype->array.dimension = maybeCopy( array.dimension ); |
---|
[8f6f47d7] | 201 | newtype->array.isVarLen = array.isVarLen; |
---|
| 202 | newtype->array.isStatic = array.isStatic; |
---|
[b87a5ed] | 203 | break; |
---|
[702e826] | 204 | case Function: |
---|
[5bf685f] | 205 | newtype->function.params = maybeCopy( function.params ); |
---|
| 206 | newtype->function.idList = maybeCopy( function.idList ); |
---|
| 207 | newtype->function.oldDeclList = maybeCopy( function.oldDeclList ); |
---|
| 208 | newtype->function.body = maybeCopy( function.body ); |
---|
| 209 | newtype->function.withExprs = maybeCopy( function.withExprs ); |
---|
[b87a5ed] | 210 | break; |
---|
[702e826] | 211 | case Aggregate: |
---|
[d15a45d] | 212 | newtype->aggregate.kind = aggregate.kind; |
---|
[2298f728] | 213 | newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr; |
---|
[5bf685f] | 214 | newtype->aggregate.params = maybeCopy( aggregate.params ); |
---|
| 215 | newtype->aggregate.actuals = maybeCopy( aggregate.actuals ); |
---|
| 216 | newtype->aggregate.fields = maybeCopy( aggregate.fields ); |
---|
[4eb3a7c5] | 217 | newtype->aggregate.attributes = aggregate.attributes; |
---|
[8f6f47d7] | 218 | newtype->aggregate.body = aggregate.body; |
---|
[3d7e53b] | 219 | newtype->aggregate.anon = aggregate.anon; |
---|
[99f7f37] | 220 | newtype->aggregate.isCfa = aggregate.isCfa; |
---|
| 221 | newtype->aggregate.hiding = aggregate.hiding; |
---|
[b87a5ed] | 222 | break; |
---|
[702e826] | 223 | case AggregateInst: |
---|
[5bf685f] | 224 | newtype->aggInst.aggregate = maybeCopy( aggInst.aggregate ); |
---|
| 225 | newtype->aggInst.params = maybeCopy( aggInst.params ); |
---|
[43c89a7] | 226 | newtype->aggInst.hoistType = aggInst.hoistType; |
---|
[b87a5ed] | 227 | break; |
---|
[702e826] | 228 | case Symbolic: |
---|
| 229 | case SymbolicInst: |
---|
[2298f728] | 230 | newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr; |
---|
[5bf685f] | 231 | newtype->symbolic.params = maybeCopy( symbolic.params ); |
---|
| 232 | newtype->symbolic.actuals = maybeCopy( symbolic.actuals ); |
---|
| 233 | newtype->symbolic.assertions = maybeCopy( symbolic.assertions ); |
---|
[8f6f47d7] | 234 | newtype->symbolic.isTypedef = symbolic.isTypedef; |
---|
[b87a5ed] | 235 | break; |
---|
[702e826] | 236 | case Tuple: |
---|
[5bf685f] | 237 | newtype->tuple = maybeCopy( tuple ); |
---|
[b87a5ed] | 238 | break; |
---|
[702e826] | 239 | case Typeof: |
---|
| 240 | case Basetypeof: |
---|
[5bf685f] | 241 | newtype->typeexpr = maybeCopy( typeexpr ); |
---|
[b87a5ed] | 242 | break; |
---|
[702e826] | 243 | case Vtable: |
---|
[93bbbc4] | 244 | break; |
---|
[702e826] | 245 | case Builtin: |
---|
[e048ece] | 246 | assert( builtintype == Zero || builtintype == One ); |
---|
[4cb935e] | 247 | newtype->builtintype = builtintype; |
---|
[90c3b1c] | 248 | break; |
---|
[702e826] | 249 | case Qualified: |
---|
[5bf685f] | 250 | newtype->qualified.parent = maybeCopy( qualified.parent ); |
---|
| 251 | newtype->qualified.child = maybeCopy( qualified.child ); |
---|
[c194661] | 252 | break; |
---|
[68cd1ce] | 253 | } // switch |
---|
[b87a5ed] | 254 | return newtype; |
---|
[413ad05] | 255 | } // TypeData::clone |
---|
[51b7345] | 256 | |
---|
[201aeb9] | 257 | |
---|
[2298f728] | 258 | void TypeData::print( ostream &os, int indent ) const { |
---|
[bb7422a] | 259 | ast::print( os, qualifiers ); |
---|
[b87a5ed] | 260 | |
---|
| 261 | if ( forall ) { |
---|
| 262 | os << "forall " << endl; |
---|
[721f17a] | 263 | forall->printList( os, indent + 4 ); |
---|
[68cd1ce] | 264 | } // if |
---|
[b87a5ed] | 265 | |
---|
| 266 | switch ( kind ) { |
---|
[702e826] | 267 | case Basic: |
---|
[e048ece] | 268 | if ( signedness != NoSignedness ) os << signednessNames[ signedness ] << " "; |
---|
| 269 | if ( length != NoLength ) os << lengthNames[ length ] << " "; |
---|
| 270 | if ( complextype != NoComplexType ) os << complexTypeNames[ complextype ] << " "; |
---|
| 271 | if ( basictype != NoBasicType ) os << basicTypeNames[ basictype ] << " "; |
---|
[b87a5ed] | 272 | break; |
---|
[702e826] | 273 | case Pointer: |
---|
[b87a5ed] | 274 | os << "pointer "; |
---|
| 275 | if ( base ) { |
---|
| 276 | os << "to "; |
---|
| 277 | base->print( os, indent ); |
---|
[68cd1ce] | 278 | } // if |
---|
[b87a5ed] | 279 | break; |
---|
[702e826] | 280 | case Reference: |
---|
[f7e4db27] | 281 | os << "reference "; |
---|
| 282 | if ( base ) { |
---|
| 283 | os << "to "; |
---|
| 284 | base->print( os, indent ); |
---|
| 285 | } // if |
---|
[b87a5ed] | 286 | break; |
---|
[702e826] | 287 | case Array: |
---|
[8f6f47d7] | 288 | if ( array.isStatic ) { |
---|
[b87a5ed] | 289 | os << "static "; |
---|
[68cd1ce] | 290 | } // if |
---|
[8f6f47d7] | 291 | if ( array.dimension ) { |
---|
[b87a5ed] | 292 | os << "array of "; |
---|
[8f6f47d7] | 293 | array.dimension->printOneLine( os, indent ); |
---|
| 294 | } else if ( array.isVarLen ) { |
---|
[b87a5ed] | 295 | os << "variable-length array of "; |
---|
| 296 | } else { |
---|
| 297 | os << "open array of "; |
---|
[68cd1ce] | 298 | } // if |
---|
[b87a5ed] | 299 | if ( base ) { |
---|
| 300 | base->print( os, indent ); |
---|
[68cd1ce] | 301 | } // if |
---|
[b87a5ed] | 302 | break; |
---|
[702e826] | 303 | case Function: |
---|
[b87a5ed] | 304 | os << "function" << endl; |
---|
[8f6f47d7] | 305 | if ( function.params ) { |
---|
[721f17a] | 306 | os << string( indent + 2, ' ' ) << "with parameters " << endl; |
---|
[8f6f47d7] | 307 | function.params->printList( os, indent + 4 ); |
---|
[b87a5ed] | 308 | } else { |
---|
[07ec1a2] | 309 | os << string( indent + 2, ' ' ) << "with no parameters" << endl; |
---|
[68cd1ce] | 310 | } // if |
---|
[8f6f47d7] | 311 | if ( function.idList ) { |
---|
[721f17a] | 312 | os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl; |
---|
[8f6f47d7] | 313 | function.idList->printList( os, indent + 4 ); |
---|
[68cd1ce] | 314 | } // if |
---|
[8f6f47d7] | 315 | if ( function.oldDeclList ) { |
---|
[721f17a] | 316 | os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl; |
---|
[8f6f47d7] | 317 | function.oldDeclList->printList( os, indent + 4 ); |
---|
[68cd1ce] | 318 | } // if |
---|
[721f17a] | 319 | os << string( indent + 2, ' ' ) << "returning "; |
---|
[b87a5ed] | 320 | if ( base ) { |
---|
[721f17a] | 321 | base->print( os, indent + 4 ); |
---|
[b87a5ed] | 322 | } else { |
---|
| 323 | os << "nothing "; |
---|
[68cd1ce] | 324 | } // if |
---|
[b87a5ed] | 325 | os << endl; |
---|
[8f6f47d7] | 326 | if ( function.body ) { |
---|
[ca1a547] | 327 | os << string( indent + 2, ' ' ) << "with body " << endl; |
---|
[8f6f47d7] | 328 | function.body->printList( os, indent + 2 ); |
---|
[68cd1ce] | 329 | } // if |
---|
[b87a5ed] | 330 | break; |
---|
[702e826] | 331 | case Aggregate: |
---|
[bb7422a] | 332 | os << ast::AggregateDecl::aggrString( aggregate.kind ) << ' ' << *aggregate.name << endl; |
---|
[8f6f47d7] | 333 | if ( aggregate.params ) { |
---|
[07ec1a2] | 334 | os << string( indent + 2, ' ' ) << "with type parameters" << endl; |
---|
[8f6f47d7] | 335 | aggregate.params->printList( os, indent + 4 ); |
---|
[68cd1ce] | 336 | } // if |
---|
[8f6f47d7] | 337 | if ( aggregate.actuals ) { |
---|
[07ec1a2] | 338 | os << string( indent + 2, ' ' ) << "instantiated with actual parameters" << endl; |
---|
[8f6f47d7] | 339 | aggregate.actuals->printList( os, indent + 4 ); |
---|
[68cd1ce] | 340 | } // if |
---|
[8f6f47d7] | 341 | if ( aggregate.fields ) { |
---|
[07ec1a2] | 342 | os << string( indent + 2, ' ' ) << "with members" << endl; |
---|
[8f6f47d7] | 343 | aggregate.fields->printList( os, indent + 4 ); |
---|
[5d125e4] | 344 | } // if |
---|
[8f6f47d7] | 345 | if ( aggregate.body ) { |
---|
[4eb3a7c5] | 346 | os << string( indent + 2, ' ' ) << "with body" << endl; |
---|
| 347 | } // if |
---|
| 348 | if ( ! aggregate.attributes.empty() ) { |
---|
| 349 | os << string( indent + 2, ' ' ) << "with attributes" << endl; |
---|
| 350 | for ( ast::ptr<ast::Attribute> const & attr : reverseIterate( aggregate.attributes ) ) { |
---|
| 351 | os << string( indent + 4, ' ' ); |
---|
| 352 | ast::print( os, attr, indent + 2 ); |
---|
| 353 | } // for |
---|
[68cd1ce] | 354 | } // if |
---|
[b87a5ed] | 355 | break; |
---|
[702e826] | 356 | case AggregateInst: |
---|
[8f6f47d7] | 357 | if ( aggInst.aggregate ) { |
---|
[b87a5ed] | 358 | os << "instance of " ; |
---|
[8f6f47d7] | 359 | aggInst.aggregate->print( os, indent ); |
---|
[b87a5ed] | 360 | } else { |
---|
| 361 | os << "instance of an unspecified aggregate "; |
---|
[68cd1ce] | 362 | } // if |
---|
[8f6f47d7] | 363 | if ( aggInst.params ) { |
---|
[07ec1a2] | 364 | os << string( indent + 2, ' ' ) << "with parameters" << endl; |
---|
[8f6f47d7] | 365 | aggInst.params->printList( os, indent + 2 ); |
---|
[68cd1ce] | 366 | } // if |
---|
[b87a5ed] | 367 | break; |
---|
[702e826] | 368 | case EnumConstant: |
---|
[f7e4db27] | 369 | os << "enumeration constant "; |
---|
[b87a5ed] | 370 | break; |
---|
[702e826] | 371 | case Symbolic: |
---|
[8f6f47d7] | 372 | if ( symbolic.isTypedef ) { |
---|
[b87a5ed] | 373 | os << "typedef definition "; |
---|
| 374 | } else { |
---|
| 375 | os << "type definition "; |
---|
[68cd1ce] | 376 | } // if |
---|
[8f6f47d7] | 377 | if ( symbolic.params ) { |
---|
[721f17a] | 378 | os << endl << string( indent + 2, ' ' ) << "with parameters" << endl; |
---|
[8f6f47d7] | 379 | symbolic.params->printList( os, indent + 2 ); |
---|
[68cd1ce] | 380 | } // if |
---|
[8f6f47d7] | 381 | if ( symbolic.assertions ) { |
---|
[721f17a] | 382 | os << endl << string( indent + 2, ' ' ) << "with assertions" << endl; |
---|
[8f6f47d7] | 383 | symbolic.assertions->printList( os, indent + 4 ); |
---|
[721f17a] | 384 | os << string( indent + 2, ' ' ); |
---|
[68cd1ce] | 385 | } // if |
---|
[b87a5ed] | 386 | if ( base ) { |
---|
| 387 | os << "for "; |
---|
| 388 | base->print( os, indent + 2 ); |
---|
[68cd1ce] | 389 | } // if |
---|
[b87a5ed] | 390 | break; |
---|
[702e826] | 391 | case SymbolicInst: |
---|
[f7e4db27] | 392 | os << *symbolic.name; |
---|
| 393 | if ( symbolic.actuals ) { |
---|
| 394 | os << "("; |
---|
| 395 | symbolic.actuals->printList( os, indent + 2 ); |
---|
| 396 | os << ")"; |
---|
| 397 | } // if |
---|
| 398 | break; |
---|
[702e826] | 399 | case Tuple: |
---|
[b87a5ed] | 400 | os << "tuple "; |
---|
[8f6f47d7] | 401 | if ( tuple ) { |
---|
[07ec1a2] | 402 | os << "with members" << endl; |
---|
[8f6f47d7] | 403 | tuple->printList( os, indent + 2 ); |
---|
[68cd1ce] | 404 | } // if |
---|
[b87a5ed] | 405 | break; |
---|
[702e826] | 406 | case Basetypeof: |
---|
[f855545] | 407 | os << "base-"; |
---|
[c45b304] | 408 | #if defined(__GNUC__) && __GNUC__ >= 7 |
---|
| 409 | __attribute__((fallthrough)); |
---|
| 410 | #endif |
---|
[66406f3] | 411 | // FALL THROUGH |
---|
[702e826] | 412 | case Typeof: |
---|
[b87a5ed] | 413 | os << "type-of expression "; |
---|
[8f6f47d7] | 414 | if ( typeexpr ) { |
---|
| 415 | typeexpr->print( os, indent + 2 ); |
---|
[68cd1ce] | 416 | } // if |
---|
[b87a5ed] | 417 | break; |
---|
[702e826] | 418 | case Vtable: |
---|
[66406f3] | 419 | os << "vtable"; |
---|
| 420 | break; |
---|
[702e826] | 421 | case Builtin: |
---|
[e048ece] | 422 | os << builtinTypeNames[builtintype]; |
---|
[90c3b1c] | 423 | break; |
---|
[702e826] | 424 | case GlobalScope: |
---|
[f7e4db27] | 425 | break; |
---|
[702e826] | 426 | case Qualified: |
---|
[f7e4db27] | 427 | qualified.parent->print( os ); |
---|
| 428 | os << "."; |
---|
| 429 | qualified.child->print( os ); |
---|
| 430 | break; |
---|
[702e826] | 431 | case Unknown: |
---|
[f7e4db27] | 432 | os << "entity of unknown type "; |
---|
| 433 | break; |
---|
[702e826] | 434 | default: |
---|
[5b639ee] | 435 | os << "internal error: TypeData::print " << kind << endl; |
---|
[1db21619] | 436 | assert( false ); |
---|
[68cd1ce] | 437 | } // switch |
---|
[413ad05] | 438 | } // TypeData::print |
---|
[51b7345] | 439 | |
---|
[7de22b28] | 440 | const std::string * TypeData::leafName() const { |
---|
| 441 | switch ( kind ) { |
---|
[702e826] | 442 | case Unknown: |
---|
| 443 | case Pointer: |
---|
| 444 | case Reference: |
---|
| 445 | case EnumConstant: |
---|
| 446 | case GlobalScope: |
---|
| 447 | case Array: |
---|
| 448 | case Basic: |
---|
| 449 | case Function: |
---|
| 450 | case AggregateInst: |
---|
| 451 | case Tuple: |
---|
| 452 | case Typeof: |
---|
| 453 | case Basetypeof: |
---|
| 454 | case Builtin: |
---|
| 455 | case Vtable: |
---|
[7de22b28] | 456 | assertf(false, "Tried to get leaf name from kind without a name: %d", kind); |
---|
| 457 | break; |
---|
[702e826] | 458 | case Aggregate: |
---|
[7de22b28] | 459 | return aggregate.name; |
---|
[702e826] | 460 | case Symbolic: |
---|
| 461 | case SymbolicInst: |
---|
[7de22b28] | 462 | return symbolic.name; |
---|
[702e826] | 463 | case Qualified: |
---|
[7de22b28] | 464 | return qualified.child->leafName(); |
---|
| 465 | } // switch |
---|
| 466 | assert(false); |
---|
| 467 | } |
---|
| 468 | |
---|
[af60383] | 469 | TypeData * TypeData::getLastBase() { |
---|
| 470 | TypeData * cur = this; |
---|
| 471 | while ( cur->base ) cur = cur->base; |
---|
| 472 | return cur; |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | void TypeData::setLastBase( TypeData * newBase ) { |
---|
| 476 | getLastBase()->base = newBase; |
---|
| 477 | } |
---|
| 478 | |
---|
[6cef439] | 479 | |
---|
[4a72fef] | 480 | // Wrap an aggregate up in an instance. Takes and gives ownership. |
---|
| 481 | static TypeData * makeInstance( TypeData * type ) { |
---|
| 482 | assert( TypeData::Aggregate == type->kind ); |
---|
| 483 | TypeData * out = new TypeData( TypeData::AggregateInst ); |
---|
| 484 | out->aggInst.aggregate = type; |
---|
| 485 | out->aggInst.params = maybeCopy( type->aggregate.actuals ); |
---|
| 486 | out->aggInst.hoistType = type->aggregate.body; |
---|
| 487 | out->qualifiers |= type->qualifiers; |
---|
| 488 | return out; |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | |
---|
[6cef439] | 492 | TypeData * build_type_qualifier( ast::CV::Qualifiers tq ) { |
---|
| 493 | TypeData * type = new TypeData; |
---|
| 494 | type->qualifiers = tq; |
---|
| 495 | return type; |
---|
| 496 | } |
---|
| 497 | |
---|
[e048ece] | 498 | TypeData * build_basic_type( TypeData::BasicType basic ) { |
---|
[6cef439] | 499 | TypeData * type = new TypeData( TypeData::Basic ); |
---|
| 500 | type->basictype = basic; |
---|
| 501 | return type; |
---|
| 502 | } |
---|
| 503 | |
---|
[e048ece] | 504 | TypeData * build_complex_type( TypeData::ComplexType complex ) { |
---|
[6cef439] | 505 | TypeData * type = new TypeData( TypeData::Basic ); |
---|
| 506 | type->complextype = complex; |
---|
| 507 | return type; |
---|
| 508 | } |
---|
| 509 | |
---|
[e048ece] | 510 | TypeData * build_signedness( TypeData::Signedness signedness ) { |
---|
[6cef439] | 511 | TypeData * type = new TypeData( TypeData::Basic ); |
---|
| 512 | type->signedness = signedness; |
---|
| 513 | return type; |
---|
| 514 | } |
---|
| 515 | |
---|
[e048ece] | 516 | TypeData * build_builtin_type( TypeData::BuiltinType bit ) { |
---|
[6cef439] | 517 | TypeData * type = new TypeData( TypeData::Builtin ); |
---|
| 518 | type->builtintype = bit; |
---|
| 519 | return type; |
---|
| 520 | } |
---|
| 521 | |
---|
[e048ece] | 522 | TypeData * build_length( TypeData::Length length ) { |
---|
[6cef439] | 523 | TypeData * type = new TypeData( TypeData::Basic ); |
---|
| 524 | type->length = length; |
---|
| 525 | return type; |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | TypeData * build_forall( DeclarationNode * forall ) { |
---|
| 529 | TypeData * type = new TypeData( TypeData::Unknown ); |
---|
| 530 | type->forall = forall; |
---|
| 531 | return type; |
---|
| 532 | } |
---|
| 533 | |
---|
| 534 | TypeData * build_global_scope() { |
---|
| 535 | return new TypeData( TypeData::GlobalScope ); |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | TypeData * build_qualified_type( TypeData * parent, TypeData * child ) { |
---|
| 539 | TypeData * type = new TypeData( TypeData::Qualified ); |
---|
| 540 | type->qualified.parent = parent; |
---|
| 541 | type->qualified.child = child; |
---|
| 542 | return type; |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | TypeData * build_typedef( const std::string * name ) { |
---|
| 546 | TypeData * type = new TypeData( TypeData::SymbolicInst ); |
---|
| 547 | type->symbolic.name = name; |
---|
| 548 | type->symbolic.isTypedef = true; |
---|
| 549 | type->symbolic.actuals = nullptr; |
---|
| 550 | return type; |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | TypeData * build_type_gen( const std::string * name, ExpressionNode * params ) { |
---|
| 554 | TypeData * type = new TypeData( TypeData::SymbolicInst ); |
---|
| 555 | type->symbolic.name = name; |
---|
| 556 | type->symbolic.isTypedef = false; |
---|
| 557 | type->symbolic.actuals = params; |
---|
| 558 | return type; |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | TypeData * build_vtable_type( TypeData * base ) { |
---|
| 562 | TypeData * type = new TypeData( TypeData::Vtable ); |
---|
| 563 | type->base = base; |
---|
| 564 | return type; |
---|
| 565 | } |
---|
| 566 | |
---|
[af60383] | 567 | // Takes ownership of src. |
---|
| 568 | static void addQualifiersToType( TypeData * dst, TypeData * src ) { |
---|
| 569 | if ( dst->base ) { |
---|
| 570 | addQualifiersToType( dst->base, src ); |
---|
| 571 | } else if ( dst->kind == TypeData::Function ) { |
---|
| 572 | dst->base = src; |
---|
| 573 | src = nullptr; |
---|
| 574 | } else { |
---|
| 575 | dst->qualifiers |= src->qualifiers; |
---|
| 576 | delete src; |
---|
| 577 | } // if |
---|
| 578 | } |
---|
| 579 | |
---|
| 580 | // Takes ownership of all arguments, gives ownership of return value. |
---|
[1cfe640] | 581 | TypeData * addQualifiers( TypeData * dst, TypeData * src ) { |
---|
| 582 | if ( src->forall ) { |
---|
| 583 | if ( dst->forall || TypeData::Aggregate != dst->kind ) { |
---|
| 584 | extend( dst->forall, src->forall ); |
---|
[af60383] | 585 | } else { |
---|
[1cfe640] | 586 | extend( dst->aggregate.params, src->forall ); |
---|
[af60383] | 587 | } |
---|
[1cfe640] | 588 | src->forall = nullptr; |
---|
[af60383] | 589 | } |
---|
| 590 | |
---|
[1cfe640] | 591 | addQualifiersToType( dst, src ); |
---|
| 592 | return dst; |
---|
[af60383] | 593 | } |
---|
| 594 | |
---|
| 595 | // Helper for addType and cloneBaseType. |
---|
| 596 | static void addTypeToType( TypeData *& dst, TypeData *& src ) { |
---|
| 597 | if ( src->forall && dst->kind == TypeData::Function ) { |
---|
[4a72fef] | 598 | extend( dst->forall, src->forall ); |
---|
[af60383] | 599 | src->forall = nullptr; |
---|
| 600 | } // if |
---|
| 601 | if ( dst->base ) { |
---|
| 602 | addTypeToType( dst->base, src ); |
---|
| 603 | return; |
---|
| 604 | } |
---|
| 605 | switch ( dst->kind ) { |
---|
| 606 | case TypeData::Unknown: |
---|
| 607 | src->qualifiers |= dst->qualifiers; |
---|
| 608 | // LEAKS dst? |
---|
| 609 | dst = src; |
---|
| 610 | src = nullptr; |
---|
| 611 | break; |
---|
| 612 | case TypeData::Basic: |
---|
| 613 | dst->qualifiers |= src->qualifiers; |
---|
| 614 | if ( src->kind != TypeData::Unknown ) { |
---|
| 615 | assert( src->kind == TypeData::Basic ); |
---|
| 616 | |
---|
[e048ece] | 617 | if ( dst->basictype == TypeData::NoBasicType ) { |
---|
[af60383] | 618 | dst->basictype = src->basictype; |
---|
[e048ece] | 619 | } else if ( src->basictype != TypeData::NoBasicType ) { |
---|
[af60383] | 620 | SemanticError( yylloc, "multiple declaration types \"%s\" and \"%s\".", |
---|
[e048ece] | 621 | TypeData::basicTypeNames[ dst->basictype ], |
---|
| 622 | TypeData::basicTypeNames[ src->basictype ] ); |
---|
[af60383] | 623 | } |
---|
[e048ece] | 624 | if ( dst->complextype == TypeData::NoComplexType ) { |
---|
[af60383] | 625 | dst->complextype = src->complextype; |
---|
[e048ece] | 626 | } else if ( src->complextype != TypeData::NoComplexType ) { |
---|
[af60383] | 627 | SemanticError( yylloc, "multiple declaration types \"%s\" and \"%s\".", |
---|
[e048ece] | 628 | TypeData::complexTypeNames[ src->complextype ], |
---|
| 629 | TypeData::complexTypeNames[ src->complextype ] ); |
---|
[af60383] | 630 | } |
---|
[e048ece] | 631 | if ( dst->signedness == TypeData::NoSignedness ) { |
---|
[af60383] | 632 | dst->signedness = src->signedness; |
---|
[e048ece] | 633 | } else if ( src->signedness != TypeData::NoSignedness ) { |
---|
[af60383] | 634 | SemanticError( yylloc, "conflicting type specifier \"%s\" and \"%s\".", |
---|
[e048ece] | 635 | TypeData::signednessNames[ dst->signedness ], |
---|
| 636 | TypeData::signednessNames[ src->signedness ] ); |
---|
[af60383] | 637 | } |
---|
[e048ece] | 638 | if ( dst->length == TypeData::NoLength ) { |
---|
[af60383] | 639 | dst->length = src->length; |
---|
[e048ece] | 640 | } else if ( dst->length == TypeData::Long && src->length == TypeData::Long ) { |
---|
| 641 | dst->length = TypeData::LongLong; |
---|
| 642 | } else if ( src->length != TypeData::NoLength ) { |
---|
[af60383] | 643 | SemanticError( yylloc, "conflicting type specifier \"%s\" and \"%s\".", |
---|
[e048ece] | 644 | TypeData::lengthNames[ dst->length ], |
---|
| 645 | TypeData::lengthNames[ src->length ] ); |
---|
[af60383] | 646 | } |
---|
| 647 | } // if |
---|
| 648 | break; |
---|
| 649 | default: |
---|
[4a72fef] | 650 | if ( TypeData::Aggregate == src->kind ) { |
---|
| 651 | dst->base = makeInstance( src ); |
---|
| 652 | } else { |
---|
| 653 | extend( dst->forall, src->forall ); |
---|
[af60383] | 654 | src->forall = nullptr; |
---|
| 655 | dst->base = src; |
---|
[4a72fef] | 656 | } |
---|
| 657 | src = nullptr; |
---|
[af60383] | 658 | } // switch |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | // Takes ownership of all arguments, gives ownership of return value. |
---|
[1cfe640] | 662 | TypeData * addType( TypeData * dst, TypeData * src, std::vector<ast::ptr<ast::Attribute>> & attributes ) { |
---|
| 663 | if ( dst ) { |
---|
| 664 | addTypeToType( dst, src ); |
---|
| 665 | } else if ( src->kind == TypeData::Aggregate ) { |
---|
[4a72fef] | 666 | // Hide type information aggregate instances. |
---|
[1cfe640] | 667 | dst = makeInstance( src ); |
---|
| 668 | dst->aggInst.aggregate->aggregate.attributes.swap( attributes ); |
---|
[af60383] | 669 | } else { |
---|
[1cfe640] | 670 | dst = src; |
---|
[af60383] | 671 | } // if |
---|
[1cfe640] | 672 | return dst; |
---|
[af60383] | 673 | } |
---|
| 674 | |
---|
[1cfe640] | 675 | TypeData * addType( TypeData * dst, TypeData * src ) { |
---|
[6cef439] | 676 | std::vector<ast::ptr<ast::Attribute>> attributes; |
---|
[1cfe640] | 677 | return addType( dst, src, attributes ); |
---|
[6cef439] | 678 | } |
---|
| 679 | |
---|
[af60383] | 680 | // Takes ownership of both arguments, gives ownership of return value. |
---|
| 681 | TypeData * cloneBaseType( TypeData * type, TypeData * other ) { |
---|
| 682 | TypeData * newType = type->getLastBase()->clone(); |
---|
| 683 | if ( newType->kind == TypeData::AggregateInst ) { |
---|
| 684 | // don't duplicate members |
---|
[67467a3] | 685 | assert( newType->aggInst.aggregate->kind == TypeData::Aggregate ); |
---|
| 686 | delete newType->aggInst.aggregate->aggregate.fields; |
---|
| 687 | newType->aggInst.aggregate->aggregate.fields = nullptr; |
---|
| 688 | newType->aggInst.aggregate->aggregate.body = false; |
---|
[af60383] | 689 | // don't hoist twice |
---|
| 690 | newType->aggInst.hoistType = false; |
---|
| 691 | } // if |
---|
| 692 | newType->forall = maybeCopy( type->forall ); |
---|
| 693 | |
---|
| 694 | if ( other ) { |
---|
| 695 | addTypeToType( other, newType ); |
---|
| 696 | delete newType; |
---|
| 697 | return other; |
---|
| 698 | } // if |
---|
| 699 | return newType; |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | TypeData * makeNewBase( TypeData * type ) { |
---|
[4a72fef] | 703 | return ( TypeData::Aggregate == type->kind ) ? makeInstance( type ) : type; |
---|
[af60383] | 704 | } |
---|
| 705 | |
---|
| 706 | |
---|
[bb7422a] | 707 | void buildForall( |
---|
| 708 | const DeclarationNode * firstNode, |
---|
| 709 | std::vector<ast::ptr<ast::TypeInstType>> &outputList ) { |
---|
| 710 | { |
---|
| 711 | std::vector<ast::ptr<ast::Type>> tmpList; |
---|
| 712 | buildTypeList( firstNode, tmpList ); |
---|
| 713 | for ( auto tmp : tmpList ) { |
---|
| 714 | outputList.emplace_back( |
---|
| 715 | strict_dynamic_cast<const ast::TypeInstType *>( |
---|
| 716 | tmp.release() ) ); |
---|
| 717 | } |
---|
| 718 | } |
---|
[f0ecf9b] | 719 | auto n = firstNode; |
---|
[bb7422a] | 720 | for ( auto i = outputList.begin() ; |
---|
| 721 | i != outputList.end() ; |
---|
[44adf1b] | 722 | ++i, n = n->next ) { |
---|
[bb7422a] | 723 | // Only the object type class adds additional assertions. |
---|
| 724 | if ( n->variable.tyClass != ast::TypeDecl::Otype ) { |
---|
| 725 | continue; |
---|
| 726 | } |
---|
| 727 | |
---|
| 728 | ast::TypeDecl const * td = i->strict_as<ast::TypeDecl>(); |
---|
| 729 | std::vector<ast::ptr<ast::DeclWithType>> newAssertions; |
---|
| 730 | auto mutTypeDecl = ast::mutate( td ); |
---|
| 731 | const CodeLocation & location = mutTypeDecl->location; |
---|
| 732 | *i = mutTypeDecl; |
---|
| 733 | |
---|
| 734 | // add assertion parameters to `type' tyvars in reverse order |
---|
| 735 | // add assignment operator: T * ?=?(T *, T) |
---|
| 736 | newAssertions.push_back( new ast::FunctionDecl( |
---|
| 737 | location, |
---|
| 738 | "?=?", |
---|
| 739 | {}, // forall |
---|
| 740 | {}, // assertions |
---|
| 741 | { |
---|
| 742 | new ast::ObjectDecl( |
---|
| 743 | location, |
---|
| 744 | "", |
---|
| 745 | new ast::ReferenceType( i->get() ), |
---|
| 746 | (ast::Init *)nullptr, |
---|
| 747 | ast::Storage::Classes(), |
---|
| 748 | ast::Linkage::Cforall, |
---|
| 749 | (ast::Expr *)nullptr |
---|
| 750 | ), |
---|
| 751 | new ast::ObjectDecl( |
---|
| 752 | location, |
---|
| 753 | "", |
---|
| 754 | i->get(), |
---|
| 755 | (ast::Init *)nullptr, |
---|
| 756 | ast::Storage::Classes(), |
---|
| 757 | ast::Linkage::Cforall, |
---|
| 758 | (ast::Expr *)nullptr |
---|
| 759 | ), |
---|
| 760 | }, // params |
---|
| 761 | { |
---|
| 762 | new ast::ObjectDecl( |
---|
| 763 | location, |
---|
| 764 | "", |
---|
| 765 | i->get(), |
---|
| 766 | (ast::Init *)nullptr, |
---|
| 767 | ast::Storage::Classes(), |
---|
| 768 | ast::Linkage::Cforall, |
---|
| 769 | (ast::Expr *)nullptr |
---|
| 770 | ), |
---|
| 771 | }, // returns |
---|
| 772 | (ast::CompoundStmt *)nullptr, |
---|
| 773 | ast::Storage::Classes(), |
---|
| 774 | ast::Linkage::Cforall |
---|
| 775 | ) ); |
---|
| 776 | |
---|
| 777 | // add default ctor: void ?{}(T *) |
---|
| 778 | newAssertions.push_back( new ast::FunctionDecl( |
---|
| 779 | location, |
---|
| 780 | "?{}", |
---|
| 781 | {}, // forall |
---|
| 782 | {}, // assertions |
---|
| 783 | { |
---|
| 784 | new ast::ObjectDecl( |
---|
| 785 | location, |
---|
| 786 | "", |
---|
| 787 | new ast::ReferenceType( i->get() ), |
---|
| 788 | (ast::Init *)nullptr, |
---|
| 789 | ast::Storage::Classes(), |
---|
| 790 | ast::Linkage::Cforall, |
---|
| 791 | (ast::Expr *)nullptr |
---|
| 792 | ), |
---|
| 793 | }, // params |
---|
| 794 | {}, // returns |
---|
| 795 | (ast::CompoundStmt *)nullptr, |
---|
| 796 | ast::Storage::Classes(), |
---|
| 797 | ast::Linkage::Cforall |
---|
| 798 | ) ); |
---|
| 799 | |
---|
| 800 | // add copy ctor: void ?{}(T *, T) |
---|
| 801 | newAssertions.push_back( new ast::FunctionDecl( |
---|
| 802 | location, |
---|
| 803 | "?{}", |
---|
| 804 | {}, // forall |
---|
| 805 | {}, // assertions |
---|
| 806 | { |
---|
| 807 | new ast::ObjectDecl( |
---|
| 808 | location, |
---|
| 809 | "", |
---|
| 810 | new ast::ReferenceType( i->get() ), |
---|
| 811 | (ast::Init *)nullptr, |
---|
| 812 | ast::Storage::Classes(), |
---|
| 813 | ast::Linkage::Cforall, |
---|
| 814 | (ast::Expr *)nullptr |
---|
| 815 | ), |
---|
| 816 | new ast::ObjectDecl( |
---|
| 817 | location, |
---|
| 818 | "", |
---|
| 819 | i->get(), |
---|
| 820 | (ast::Init *)nullptr, |
---|
| 821 | ast::Storage::Classes(), |
---|
| 822 | ast::Linkage::Cforall, |
---|
| 823 | (ast::Expr *)nullptr |
---|
| 824 | ), |
---|
| 825 | }, // params |
---|
| 826 | {}, // returns |
---|
| 827 | (ast::CompoundStmt *)nullptr, |
---|
| 828 | ast::Storage::Classes(), |
---|
| 829 | ast::Linkage::Cforall |
---|
| 830 | ) ); |
---|
| 831 | |
---|
| 832 | // add dtor: void ^?{}(T *) |
---|
| 833 | newAssertions.push_back( new ast::FunctionDecl( |
---|
| 834 | location, |
---|
| 835 | "^?{}", |
---|
| 836 | {}, // forall |
---|
| 837 | {}, // assertions |
---|
| 838 | { |
---|
| 839 | new ast::ObjectDecl( |
---|
| 840 | location, |
---|
| 841 | "", |
---|
| 842 | new ast::ReferenceType( i->get() ), |
---|
| 843 | (ast::Init *)nullptr, |
---|
| 844 | ast::Storage::Classes(), |
---|
| 845 | ast::Linkage::Cforall, |
---|
| 846 | (ast::Expr *)nullptr |
---|
| 847 | ), |
---|
| 848 | }, // params |
---|
| 849 | {}, // returns |
---|
| 850 | (ast::CompoundStmt *)nullptr, |
---|
| 851 | ast::Storage::Classes(), |
---|
| 852 | ast::Linkage::Cforall |
---|
| 853 | ) ); |
---|
| 854 | |
---|
| 855 | spliceBegin( mutTypeDecl->assertions, newAssertions ); |
---|
| 856 | } // for |
---|
| 857 | } |
---|
| 858 | |
---|
| 859 | |
---|
| 860 | void buildForall( |
---|
| 861 | const DeclarationNode * firstNode, |
---|
| 862 | std::vector<ast::ptr<ast::TypeDecl>> &outputForall ) { |
---|
| 863 | buildList( firstNode, outputForall ); |
---|
| 864 | auto n = firstNode; |
---|
| 865 | for ( auto i = outputForall.begin() ; |
---|
| 866 | i != outputForall.end() ; |
---|
[44adf1b] | 867 | ++i, n = n->next ) { |
---|
[bb7422a] | 868 | // Only the object type class adds additional assertions. |
---|
| 869 | if ( n->variable.tyClass != ast::TypeDecl::Otype ) { |
---|
| 870 | continue; |
---|
| 871 | } |
---|
| 872 | |
---|
| 873 | ast::TypeDecl const * td = i->strict_as<ast::TypeDecl>(); |
---|
| 874 | std::vector<ast::ptr<ast::DeclWithType>> newAssertions; |
---|
| 875 | auto mutTypeDecl = ast::mutate( td ); |
---|
| 876 | const CodeLocation & location = mutTypeDecl->location; |
---|
| 877 | *i = mutTypeDecl; |
---|
| 878 | |
---|
| 879 | // add assertion parameters to `type' tyvars in reverse order |
---|
| 880 | // add assignment operator: T * ?=?(T *, T) |
---|
| 881 | newAssertions.push_back( new ast::FunctionDecl( |
---|
| 882 | location, |
---|
| 883 | "?=?", |
---|
| 884 | {}, // forall |
---|
| 885 | {}, // assertions |
---|
| 886 | { |
---|
| 887 | new ast::ObjectDecl( |
---|
| 888 | location, |
---|
| 889 | "", |
---|
| 890 | new ast::ReferenceType( new ast::TypeInstType( td->name, *i ) ), |
---|
| 891 | (ast::Init *)nullptr, |
---|
| 892 | ast::Storage::Classes(), |
---|
| 893 | ast::Linkage::Cforall, |
---|
| 894 | (ast::Expr *)nullptr |
---|
| 895 | ), |
---|
| 896 | new ast::ObjectDecl( |
---|
| 897 | location, |
---|
| 898 | "", |
---|
| 899 | new ast::TypeInstType( td->name, *i ), |
---|
| 900 | (ast::Init *)nullptr, |
---|
| 901 | ast::Storage::Classes(), |
---|
| 902 | ast::Linkage::Cforall, |
---|
| 903 | (ast::Expr *)nullptr |
---|
| 904 | ), |
---|
| 905 | }, // params |
---|
| 906 | { |
---|
| 907 | new ast::ObjectDecl( |
---|
| 908 | location, |
---|
| 909 | "", |
---|
| 910 | new ast::TypeInstType( td->name, *i ), |
---|
| 911 | (ast::Init *)nullptr, |
---|
| 912 | ast::Storage::Classes(), |
---|
| 913 | ast::Linkage::Cforall, |
---|
| 914 | (ast::Expr *)nullptr |
---|
| 915 | ), |
---|
| 916 | }, // returns |
---|
| 917 | (ast::CompoundStmt *)nullptr, |
---|
| 918 | ast::Storage::Classes(), |
---|
| 919 | ast::Linkage::Cforall |
---|
| 920 | ) ); |
---|
| 921 | |
---|
| 922 | // add default ctor: void ?{}(T *) |
---|
| 923 | newAssertions.push_back( new ast::FunctionDecl( |
---|
| 924 | location, |
---|
| 925 | "?{}", |
---|
| 926 | {}, // forall |
---|
| 927 | {}, // assertions |
---|
| 928 | { |
---|
| 929 | new ast::ObjectDecl( |
---|
| 930 | location, |
---|
| 931 | "", |
---|
| 932 | new ast::ReferenceType( |
---|
| 933 | new ast::TypeInstType( td->name, i->get() ) ), |
---|
| 934 | (ast::Init *)nullptr, |
---|
| 935 | ast::Storage::Classes(), |
---|
| 936 | ast::Linkage::Cforall, |
---|
| 937 | (ast::Expr *)nullptr |
---|
| 938 | ), |
---|
| 939 | }, // params |
---|
| 940 | {}, // returns |
---|
| 941 | (ast::CompoundStmt *)nullptr, |
---|
| 942 | ast::Storage::Classes(), |
---|
| 943 | ast::Linkage::Cforall |
---|
| 944 | ) ); |
---|
| 945 | |
---|
| 946 | // add copy ctor: void ?{}(T *, T) |
---|
| 947 | newAssertions.push_back( new ast::FunctionDecl( |
---|
| 948 | location, |
---|
| 949 | "?{}", |
---|
| 950 | {}, // forall |
---|
| 951 | {}, // assertions |
---|
| 952 | { |
---|
| 953 | new ast::ObjectDecl( |
---|
| 954 | location, |
---|
| 955 | "", |
---|
| 956 | new ast::ReferenceType( |
---|
| 957 | new ast::TypeInstType( td->name, *i ) ), |
---|
| 958 | (ast::Init *)nullptr, |
---|
| 959 | ast::Storage::Classes(), |
---|
| 960 | ast::Linkage::Cforall, |
---|
| 961 | (ast::Expr *)nullptr |
---|
| 962 | ), |
---|
| 963 | new ast::ObjectDecl( |
---|
| 964 | location, |
---|
| 965 | "", |
---|
| 966 | new ast::TypeInstType( td->name, *i ), |
---|
| 967 | (ast::Init *)nullptr, |
---|
| 968 | ast::Storage::Classes(), |
---|
| 969 | ast::Linkage::Cforall, |
---|
| 970 | (ast::Expr *)nullptr |
---|
| 971 | ), |
---|
| 972 | }, // params |
---|
| 973 | {}, // returns |
---|
| 974 | (ast::CompoundStmt *)nullptr, |
---|
| 975 | ast::Storage::Classes(), |
---|
| 976 | ast::Linkage::Cforall |
---|
| 977 | ) ); |
---|
| 978 | |
---|
| 979 | // add dtor: void ^?{}(T *) |
---|
| 980 | newAssertions.push_back( new ast::FunctionDecl( |
---|
| 981 | location, |
---|
| 982 | "^?{}", |
---|
| 983 | {}, // forall |
---|
| 984 | {}, // assertions |
---|
| 985 | { |
---|
| 986 | new ast::ObjectDecl( |
---|
| 987 | location, |
---|
| 988 | "", |
---|
| 989 | new ast::ReferenceType( |
---|
| 990 | new ast::TypeInstType( i->get() ) |
---|
| 991 | ), |
---|
| 992 | (ast::Init *)nullptr, |
---|
| 993 | ast::Storage::Classes(), |
---|
| 994 | ast::Linkage::Cforall, |
---|
| 995 | (ast::Expr *)nullptr |
---|
| 996 | ), |
---|
| 997 | }, // params |
---|
| 998 | {}, // returns |
---|
| 999 | (ast::CompoundStmt *)nullptr, |
---|
| 1000 | ast::Storage::Classes(), |
---|
| 1001 | ast::Linkage::Cforall |
---|
| 1002 | ) ); |
---|
| 1003 | |
---|
| 1004 | spliceBegin( mutTypeDecl->assertions, newAssertions ); |
---|
[68cd1ce] | 1005 | } // for |
---|
[201aeb9] | 1006 | } // buildForall |
---|
| 1007 | |
---|
[51b7345] | 1008 | |
---|
[bb7422a] | 1009 | ast::Type * typebuild( const TypeData * td ) { |
---|
[413ad05] | 1010 | assert( td ); |
---|
| 1011 | switch ( td->kind ) { |
---|
[702e826] | 1012 | case TypeData::Unknown: |
---|
[07de76b] | 1013 | // fill in implicit int |
---|
[bb7422a] | 1014 | return new ast::BasicType( |
---|
[7a780ad] | 1015 | ast::BasicKind::SignedInt, |
---|
[bb7422a] | 1016 | buildQualifiers( td ) |
---|
| 1017 | ); |
---|
[702e826] | 1018 | case TypeData::Basic: |
---|
[07de76b] | 1019 | return buildBasicType( td ); |
---|
[702e826] | 1020 | case TypeData::Pointer: |
---|
[07de76b] | 1021 | return buildPointer( td ); |
---|
[702e826] | 1022 | case TypeData::Array: |
---|
[07de76b] | 1023 | return buildArray( td ); |
---|
[702e826] | 1024 | case TypeData::Reference: |
---|
[07de76b] | 1025 | return buildReference( td ); |
---|
[702e826] | 1026 | case TypeData::Function: |
---|
[bb7422a] | 1027 | return buildFunctionType( td ); |
---|
[702e826] | 1028 | case TypeData::AggregateInst: |
---|
[07de76b] | 1029 | return buildAggInst( td ); |
---|
[702e826] | 1030 | case TypeData::EnumConstant: |
---|
[bb7422a] | 1031 | return new ast::EnumInstType( "", buildQualifiers( td ) ); |
---|
[702e826] | 1032 | case TypeData::SymbolicInst: |
---|
[07de76b] | 1033 | return buildSymbolicInst( td ); |
---|
[702e826] | 1034 | case TypeData::Tuple: |
---|
[07de76b] | 1035 | return buildTuple( td ); |
---|
[702e826] | 1036 | case TypeData::Typeof: |
---|
| 1037 | case TypeData::Basetypeof: |
---|
[07de76b] | 1038 | return buildTypeof( td ); |
---|
[702e826] | 1039 | case TypeData::Vtable: |
---|
[93bbbc4] | 1040 | return buildVtable( td ); |
---|
[702e826] | 1041 | case TypeData::Builtin: |
---|
[07de76b] | 1042 | switch ( td->builtintype ) { |
---|
[e048ece] | 1043 | case TypeData::Zero: |
---|
[bb7422a] | 1044 | return new ast::ZeroType(); |
---|
[e048ece] | 1045 | case TypeData::One: |
---|
[bb7422a] | 1046 | return new ast::OneType(); |
---|
[702e826] | 1047 | default: |
---|
[bb7422a] | 1048 | return new ast::VarArgsType( buildQualifiers( td ) ); |
---|
[07de76b] | 1049 | } // switch |
---|
[702e826] | 1050 | case TypeData::GlobalScope: |
---|
[bb7422a] | 1051 | return new ast::GlobalScopeType(); |
---|
[702e826] | 1052 | case TypeData::Qualified: |
---|
[bb7422a] | 1053 | return new ast::QualifiedType( |
---|
| 1054 | typebuild( td->qualified.parent ), |
---|
| 1055 | typebuild( td->qualified.child ), |
---|
| 1056 | buildQualifiers( td ) |
---|
| 1057 | ); |
---|
[702e826] | 1058 | case TypeData::Symbolic: |
---|
| 1059 | case TypeData::Aggregate: |
---|
[07de76b] | 1060 | assert( false ); |
---|
[68cd1ce] | 1061 | } // switch |
---|
[c194661] | 1062 | |
---|
[2298f728] | 1063 | return nullptr; |
---|
[413ad05] | 1064 | } // typebuild |
---|
| 1065 | |
---|
[201aeb9] | 1066 | |
---|
[413ad05] | 1067 | TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) { |
---|
[2298f728] | 1068 | TypeData * ret = nullptr; |
---|
[51b7345] | 1069 | |
---|
[413ad05] | 1070 | switch ( td->kind ) { |
---|
[702e826] | 1071 | case TypeData::Aggregate: |
---|
[b2da0574] | 1072 | if ( ! toplevel && td->aggregate.body ) { |
---|
[413ad05] | 1073 | ret = td->clone(); |
---|
| 1074 | } // if |
---|
| 1075 | break; |
---|
[702e826] | 1076 | case TypeData::AggregateInst: |
---|
[8f6f47d7] | 1077 | if ( td->aggInst.aggregate ) { |
---|
| 1078 | ret = typeextractAggregate( td->aggInst.aggregate, false ); |
---|
[413ad05] | 1079 | } // if |
---|
| 1080 | break; |
---|
[702e826] | 1081 | default: |
---|
[413ad05] | 1082 | if ( td->base ) { |
---|
| 1083 | ret = typeextractAggregate( td->base, false ); |
---|
| 1084 | } // if |
---|
| 1085 | } // switch |
---|
| 1086 | return ret; |
---|
| 1087 | } // typeextractAggregate |
---|
| 1088 | |
---|
[201aeb9] | 1089 | |
---|
[bb7422a] | 1090 | ast::CV::Qualifiers buildQualifiers( const TypeData * td ) { |
---|
[738e304] | 1091 | return td->qualifiers; |
---|
[413ad05] | 1092 | } // buildQualifiers |
---|
[51b7345] | 1093 | |
---|
[201aeb9] | 1094 | |
---|
[e048ece] | 1095 | static string genTSError( string msg, TypeData::BasicType basictype ) { |
---|
| 1096 | SemanticError( yylloc, "invalid type specifier \"%s\" for type \"%s\".", msg.c_str(), TypeData::basicTypeNames[basictype] ); |
---|
[201aeb9] | 1097 | } // genTSError |
---|
| 1098 | |
---|
[bb7422a] | 1099 | ast::Type * buildBasicType( const TypeData * td ) { |
---|
[7a780ad] | 1100 | ast::BasicKind ret; |
---|
[b87a5ed] | 1101 | |
---|
[5b639ee] | 1102 | switch ( td->basictype ) { |
---|
[e048ece] | 1103 | case TypeData::Void: |
---|
| 1104 | if ( td->signedness != TypeData::NoSignedness ) { |
---|
| 1105 | genTSError( TypeData::signednessNames[ td->signedness ], td->basictype ); |
---|
[201aeb9] | 1106 | } // if |
---|
[e048ece] | 1107 | if ( td->length != TypeData::NoLength ) { |
---|
| 1108 | genTSError( TypeData::lengthNames[ td->length ], td->basictype ); |
---|
[68cd1ce] | 1109 | } // if |
---|
[bb7422a] | 1110 | return new ast::VoidType( buildQualifiers( td ) ); |
---|
[5b639ee] | 1111 | break; |
---|
| 1112 | |
---|
[e048ece] | 1113 | case TypeData::Bool: |
---|
| 1114 | if ( td->signedness != TypeData::NoSignedness ) { |
---|
| 1115 | genTSError( TypeData::signednessNames[ td->signedness ], td->basictype ); |
---|
[5b639ee] | 1116 | } // if |
---|
[e048ece] | 1117 | if ( td->length != TypeData::NoLength ) { |
---|
| 1118 | genTSError( TypeData::lengthNames[ td->length ], td->basictype ); |
---|
[68cd1ce] | 1119 | } // if |
---|
[51b7345] | 1120 | |
---|
[7a780ad] | 1121 | ret = ast::BasicKind::Bool; |
---|
[5b639ee] | 1122 | break; |
---|
| 1123 | |
---|
[e048ece] | 1124 | case TypeData::Char: |
---|
[5b639ee] | 1125 | // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the |
---|
| 1126 | // character types. The implementation shall define char to have the same range, representation, and behavior as |
---|
| 1127 | // either signed char or unsigned char. |
---|
[7a780ad] | 1128 | static ast::BasicKind chartype[] = { ast::BasicKind::SignedChar, ast::BasicKind::UnsignedChar, ast::BasicKind::Char }; |
---|
[5b639ee] | 1129 | |
---|
[e048ece] | 1130 | if ( td->length != TypeData::NoLength ) { |
---|
| 1131 | genTSError( TypeData::lengthNames[ td->length ], td->basictype ); |
---|
[5b639ee] | 1132 | } // if |
---|
| 1133 | |
---|
| 1134 | ret = chartype[ td->signedness ]; |
---|
| 1135 | break; |
---|
| 1136 | |
---|
[e048ece] | 1137 | case TypeData::Int: |
---|
[7a780ad] | 1138 | static ast::BasicKind inttype[2][4] = { |
---|
| 1139 | { ast::BasicKind::ShortSignedInt, ast::BasicKind::LongSignedInt, ast::BasicKind::LongLongSignedInt, ast::BasicKind::SignedInt }, |
---|
| 1140 | { ast::BasicKind::ShortUnsignedInt, ast::BasicKind::LongUnsignedInt, ast::BasicKind::LongLongUnsignedInt, ast::BasicKind::UnsignedInt }, |
---|
[5b639ee] | 1141 | }; |
---|
| 1142 | |
---|
[702e826] | 1143 | Integral: ; |
---|
[e048ece] | 1144 | if ( td->signedness == TypeData::NoSignedness ) { |
---|
| 1145 | const_cast<TypeData *>(td)->signedness = TypeData::Signed; |
---|
[5b639ee] | 1146 | } // if |
---|
| 1147 | ret = inttype[ td->signedness ][ td->length ]; |
---|
| 1148 | break; |
---|
| 1149 | |
---|
[e048ece] | 1150 | case TypeData::Int128: |
---|
[7a780ad] | 1151 | ret = td->signedness == TypeData::Unsigned ? ast::BasicKind::UnsignedInt128 : ast::BasicKind::SignedInt128; |
---|
[e048ece] | 1152 | if ( td->length != TypeData::NoLength ) { |
---|
| 1153 | genTSError( TypeData::lengthNames[ td->length ], td->basictype ); |
---|
[201aeb9] | 1154 | } // if |
---|
| 1155 | break; |
---|
| 1156 | |
---|
[e048ece] | 1157 | case TypeData::Float: |
---|
| 1158 | case TypeData::Double: |
---|
| 1159 | case TypeData::LongDouble: // not set until below |
---|
| 1160 | case TypeData::uuFloat80: |
---|
| 1161 | case TypeData::uuFloat128: |
---|
| 1162 | case TypeData::uFloat16: |
---|
| 1163 | case TypeData::uFloat32: |
---|
| 1164 | case TypeData::uFloat32x: |
---|
| 1165 | case TypeData::uFloat64: |
---|
| 1166 | case TypeData::uFloat64x: |
---|
| 1167 | case TypeData::uFloat128: |
---|
| 1168 | case TypeData::uFloat128x: |
---|
[7a780ad] | 1169 | static ast::BasicKind floattype[2][12] = { |
---|
| 1170 | { ast::BasicKind::FloatComplex, ast::BasicKind::DoubleComplex, ast::BasicKind::LongDoubleComplex, (ast::BasicKind)-1, (ast::BasicKind)-1, ast::BasicKind::uFloat16Complex, ast::BasicKind::uFloat32Complex, ast::BasicKind::uFloat32xComplex, ast::BasicKind::uFloat64Complex, ast::BasicKind::uFloat64xComplex, ast::BasicKind::uFloat128Complex, ast::BasicKind::uFloat128xComplex, }, |
---|
| 1171 | { ast::BasicKind::Float, ast::BasicKind::Double, ast::BasicKind::LongDouble, ast::BasicKind::uuFloat80, ast::BasicKind::uuFloat128, ast::BasicKind::uFloat16, ast::BasicKind::uFloat32, ast::BasicKind::uFloat32x, ast::BasicKind::uFloat64, ast::BasicKind::uFloat64x, ast::BasicKind::uFloat128, ast::BasicKind::uFloat128x, }, |
---|
[5b639ee] | 1172 | }; |
---|
| 1173 | |
---|
[702e826] | 1174 | FloatingPoint: ; |
---|
[e048ece] | 1175 | if ( td->signedness != TypeData::NoSignedness ) { |
---|
| 1176 | genTSError( TypeData::signednessNames[ td->signedness ], td->basictype ); |
---|
[5b639ee] | 1177 | } // if |
---|
[e048ece] | 1178 | if ( td->length == TypeData::Short || td->length == TypeData::LongLong ) { |
---|
| 1179 | genTSError( TypeData::lengthNames[ td->length ], td->basictype ); |
---|
[5b639ee] | 1180 | } // if |
---|
[e048ece] | 1181 | if ( td->basictype != TypeData::Double && td->length == TypeData::Long ) { |
---|
| 1182 | genTSError( TypeData::lengthNames[ td->length ], td->basictype ); |
---|
[5b639ee] | 1183 | } // if |
---|
[e048ece] | 1184 | if ( td->complextype == TypeData::Imaginary ) { |
---|
| 1185 | genTSError( TypeData::complexTypeNames[ td->complextype ], td->basictype ); |
---|
[ba01b14] | 1186 | } // if |
---|
[e048ece] | 1187 | if ( (td->basictype == TypeData::uuFloat80 || td->basictype == TypeData::uuFloat128) && td->complextype == TypeData::Complex ) { // gcc unsupported |
---|
| 1188 | genTSError( TypeData::complexTypeNames[ td->complextype ], td->basictype ); |
---|
[ba01b14] | 1189 | } // if |
---|
[e048ece] | 1190 | if ( td->length == TypeData::Long ) { |
---|
| 1191 | const_cast<TypeData *>(td)->basictype = TypeData::LongDouble; |
---|
[5b639ee] | 1192 | } // if |
---|
| 1193 | |
---|
[e048ece] | 1194 | ret = floattype[ td->complextype ][ td->basictype - TypeData::Float ]; |
---|
| 1195 | //printf( "XXXX %d %d %d %d\n", td->complextype, td->basictype, TypeData::Float, ret ); |
---|
[5b639ee] | 1196 | break; |
---|
| 1197 | |
---|
[e048ece] | 1198 | case TypeData::NoBasicType: |
---|
[5b639ee] | 1199 | // No basic type in declaration => default double for Complex/Imaginary and int type for integral types |
---|
[e048ece] | 1200 | if ( td->complextype == TypeData::Complex || td->complextype == TypeData::Imaginary ) { |
---|
| 1201 | const_cast<TypeData *>(td)->basictype = TypeData::Double; |
---|
[5b639ee] | 1202 | goto FloatingPoint; |
---|
| 1203 | } // if |
---|
| 1204 | |
---|
[e048ece] | 1205 | const_cast<TypeData *>(td)->basictype = TypeData::Int; |
---|
[5b639ee] | 1206 | goto Integral; |
---|
[702e826] | 1207 | default: |
---|
| 1208 | assertf( false, "unknown basic type" ); |
---|
[2ee5426] | 1209 | return nullptr; |
---|
[5b639ee] | 1210 | } // switch |
---|
| 1211 | |
---|
[bb7422a] | 1212 | ast::BasicType * bt = new ast::BasicType( ret, buildQualifiers( td ) ); |
---|
[b87a5ed] | 1213 | return bt; |
---|
[413ad05] | 1214 | } // buildBasicType |
---|
[51b7345] | 1215 | |
---|
[201aeb9] | 1216 | |
---|
[a3525c4] | 1217 | static ast::Type * buildDefaultType( const TypeData * td ) { |
---|
[7a780ad] | 1218 | return ( td ) ? typebuild( td ) : new ast::BasicType( ast::BasicKind::SignedInt ); |
---|
[a3525c4] | 1219 | } // buildDefaultType |
---|
| 1220 | |
---|
| 1221 | |
---|
[bb7422a] | 1222 | ast::PointerType * buildPointer( const TypeData * td ) { |
---|
[a3525c4] | 1223 | return new ast::PointerType( |
---|
| 1224 | buildDefaultType( td->base ), |
---|
| 1225 | buildQualifiers( td ) |
---|
| 1226 | ); |
---|
[413ad05] | 1227 | } // buildPointer |
---|
[51b7345] | 1228 | |
---|
[201aeb9] | 1229 | |
---|
[bb7422a] | 1230 | ast::ArrayType * buildArray( const TypeData * td ) { |
---|
[a3525c4] | 1231 | return new ast::ArrayType( |
---|
| 1232 | buildDefaultType( td->base ), |
---|
| 1233 | maybeBuild( td->array.dimension ), |
---|
| 1234 | td->array.isVarLen ? ast::VariableLen : ast::FixedLen, |
---|
| 1235 | td->array.isStatic ? ast::StaticDim : ast::DynamicDim, |
---|
| 1236 | buildQualifiers( td ) |
---|
| 1237 | ); |
---|
[ce8c12f] | 1238 | } // buildArray |
---|
| 1239 | |
---|
[201aeb9] | 1240 | |
---|
[bb7422a] | 1241 | ast::ReferenceType * buildReference( const TypeData * td ) { |
---|
[a3525c4] | 1242 | return new ast::ReferenceType( |
---|
| 1243 | buildDefaultType( td->base ), |
---|
| 1244 | buildQualifiers( td ) |
---|
| 1245 | ); |
---|
[ce8c12f] | 1246 | } // buildReference |
---|
[51b7345] | 1247 | |
---|
[201aeb9] | 1248 | |
---|
[bb7422a] | 1249 | ast::AggregateDecl * buildAggregate( const TypeData * td, std::vector<ast::ptr<ast::Attribute>> attributes, ast::Linkage::Spec linkage ) { |
---|
[413ad05] | 1250 | assert( td->kind == TypeData::Aggregate ); |
---|
[bb7422a] | 1251 | ast::AggregateDecl * at; |
---|
[8f6f47d7] | 1252 | switch ( td->aggregate.kind ) { |
---|
[bb7422a] | 1253 | case ast::AggregateDecl::Struct: |
---|
| 1254 | case ast::AggregateDecl::Coroutine: |
---|
| 1255 | case ast::AggregateDecl::Exception: |
---|
| 1256 | case ast::AggregateDecl::Generator: |
---|
| 1257 | case ast::AggregateDecl::Monitor: |
---|
| 1258 | case ast::AggregateDecl::Thread: |
---|
| 1259 | at = new ast::StructDecl( td->location, |
---|
| 1260 | *td->aggregate.name, |
---|
| 1261 | td->aggregate.kind, |
---|
| 1262 | std::move( attributes ), |
---|
| 1263 | linkage |
---|
| 1264 | ); |
---|
| 1265 | buildForall( td->aggregate.params, at->params ); |
---|
| 1266 | break; |
---|
| 1267 | case ast::AggregateDecl::Union: |
---|
| 1268 | at = new ast::UnionDecl( td->location, |
---|
| 1269 | *td->aggregate.name, |
---|
| 1270 | std::move( attributes ), |
---|
| 1271 | linkage |
---|
| 1272 | ); |
---|
| 1273 | buildForall( td->aggregate.params, at->params ); |
---|
| 1274 | break; |
---|
[67467a3] | 1275 | case ast::AggregateDecl::Enum: |
---|
| 1276 | return buildEnum( td, std::move( attributes ), linkage ); |
---|
[bb7422a] | 1277 | case ast::AggregateDecl::Trait: |
---|
| 1278 | at = new ast::TraitDecl( td->location, |
---|
| 1279 | *td->aggregate.name, |
---|
| 1280 | std::move( attributes ), |
---|
| 1281 | linkage |
---|
| 1282 | ); |
---|
| 1283 | buildList( td->aggregate.params, at->params ); |
---|
[b87a5ed] | 1284 | break; |
---|
[702e826] | 1285 | default: |
---|
[b87a5ed] | 1286 | assert( false ); |
---|
[68cd1ce] | 1287 | } // switch |
---|
[4e06c1e] | 1288 | |
---|
[bb7422a] | 1289 | buildList( td->aggregate.fields, at->members ); |
---|
[8f6f47d7] | 1290 | at->set_body( td->aggregate.body ); |
---|
[b87a5ed] | 1291 | |
---|
| 1292 | return at; |
---|
[413ad05] | 1293 | } // buildAggregate |
---|
[51b7345] | 1294 | |
---|
[201aeb9] | 1295 | |
---|
[bb7422a] | 1296 | ast::BaseInstType * buildComAggInst( |
---|
[4eb3a7c5] | 1297 | const TypeData * td, |
---|
[bb7422a] | 1298 | std::vector<ast::ptr<ast::Attribute>> && attributes, |
---|
| 1299 | ast::Linkage::Spec linkage ) { |
---|
[4eb3a7c5] | 1300 | switch ( td->kind ) { |
---|
[702e826] | 1301 | case TypeData::Aggregate: |
---|
[4eb3a7c5] | 1302 | if ( td->aggregate.body ) { |
---|
[bb7422a] | 1303 | ast::AggregateDecl * typedecl = |
---|
[4eb3a7c5] | 1304 | buildAggregate( td, std::move( attributes ), linkage ); |
---|
| 1305 | switch ( td->aggregate.kind ) { |
---|
[bb7422a] | 1306 | case ast::AggregateDecl::Struct: |
---|
| 1307 | case ast::AggregateDecl::Coroutine: |
---|
| 1308 | case ast::AggregateDecl::Monitor: |
---|
| 1309 | case ast::AggregateDecl::Thread: |
---|
| 1310 | return new ast::StructInstType( |
---|
| 1311 | strict_dynamic_cast<ast::StructDecl *>( typedecl ), |
---|
[4eb3a7c5] | 1312 | buildQualifiers( td ) |
---|
[bb7422a] | 1313 | ); |
---|
| 1314 | case ast::AggregateDecl::Union: |
---|
| 1315 | return new ast::UnionInstType( |
---|
| 1316 | strict_dynamic_cast<ast::UnionDecl *>( typedecl ), |
---|
[4eb3a7c5] | 1317 | buildQualifiers( td ) |
---|
[bb7422a] | 1318 | ); |
---|
[67467a3] | 1319 | case ast::AggregateDecl::Enum: |
---|
| 1320 | return new ast::EnumInstType( |
---|
| 1321 | strict_dynamic_cast<ast::EnumDecl *>( typedecl ), |
---|
| 1322 | buildQualifiers( td ) |
---|
| 1323 | ); |
---|
[bb7422a] | 1324 | case ast::AggregateDecl::Trait: |
---|
[702e826] | 1325 | assert( false ); |
---|
| 1326 | break; |
---|
| 1327 | default: |
---|
| 1328 | assert( false ); |
---|
| 1329 | } // switch |
---|
| 1330 | } else { |
---|
[4eb3a7c5] | 1331 | switch ( td->aggregate.kind ) { |
---|
[bb7422a] | 1332 | case ast::AggregateDecl::Struct: |
---|
| 1333 | case ast::AggregateDecl::Coroutine: |
---|
| 1334 | case ast::AggregateDecl::Monitor: |
---|
| 1335 | case ast::AggregateDecl::Thread: |
---|
| 1336 | return new ast::StructInstType( |
---|
[4eb3a7c5] | 1337 | *td->aggregate.name, |
---|
| 1338 | buildQualifiers( td ) |
---|
[bb7422a] | 1339 | ); |
---|
| 1340 | case ast::AggregateDecl::Union: |
---|
| 1341 | return new ast::UnionInstType( |
---|
[4eb3a7c5] | 1342 | *td->aggregate.name, |
---|
| 1343 | buildQualifiers( td ) |
---|
[bb7422a] | 1344 | ); |
---|
[67467a3] | 1345 | case ast::AggregateDecl::Enum: |
---|
| 1346 | return new ast::EnumInstType( |
---|
| 1347 | *td->aggregate.name, |
---|
| 1348 | buildQualifiers( td ) |
---|
| 1349 | ); |
---|
[bb7422a] | 1350 | case ast::AggregateDecl::Trait: |
---|
| 1351 | return new ast::TraitInstType( |
---|
[4eb3a7c5] | 1352 | *td->aggregate.name, |
---|
| 1353 | buildQualifiers( td ) |
---|
[bb7422a] | 1354 | ); |
---|
[702e826] | 1355 | default: |
---|
| 1356 | assert( false ); |
---|
| 1357 | } // switch |
---|
[bb7422a] | 1358 | break; |
---|
[702e826] | 1359 | } // if |
---|
[bb7422a] | 1360 | break; |
---|
[702e826] | 1361 | default: |
---|
[43c89a7] | 1362 | assert( false ); |
---|
| 1363 | } // switch |
---|
[bb7422a] | 1364 | assert( false ); |
---|
[43c89a7] | 1365 | } // buildAggInst |
---|
| 1366 | |
---|
[201aeb9] | 1367 | |
---|
[bb7422a] | 1368 | ast::BaseInstType * buildAggInst( const TypeData * td ) { |
---|
[413ad05] | 1369 | assert( td->kind == TypeData::AggregateInst ); |
---|
[b87a5ed] | 1370 | |
---|
[bb7422a] | 1371 | ast::BaseInstType * ret = nullptr; |
---|
[43c89a7] | 1372 | TypeData * type = td->aggInst.aggregate; |
---|
| 1373 | switch ( type->kind ) { |
---|
[702e826] | 1374 | case TypeData::Aggregate: |
---|
| 1375 | switch ( type->aggregate.kind ) { |
---|
[bb7422a] | 1376 | case ast::AggregateDecl::Struct: |
---|
| 1377 | case ast::AggregateDecl::Coroutine: |
---|
| 1378 | case ast::AggregateDecl::Monitor: |
---|
| 1379 | case ast::AggregateDecl::Thread: |
---|
| 1380 | ret = new ast::StructInstType( |
---|
| 1381 | *type->aggregate.name, |
---|
| 1382 | buildQualifiers( type ) |
---|
| 1383 | ); |
---|
[702e826] | 1384 | break; |
---|
[bb7422a] | 1385 | case ast::AggregateDecl::Union: |
---|
| 1386 | ret = new ast::UnionInstType( |
---|
| 1387 | *type->aggregate.name, |
---|
| 1388 | buildQualifiers( type ) |
---|
| 1389 | ); |
---|
[702e826] | 1390 | break; |
---|
[67467a3] | 1391 | case ast::AggregateDecl::Enum: |
---|
| 1392 | ret = new ast::EnumInstType( |
---|
| 1393 | *type->aggregate.name, |
---|
| 1394 | buildQualifiers( type ) |
---|
| 1395 | ); |
---|
| 1396 | break; |
---|
[bb7422a] | 1397 | case ast::AggregateDecl::Trait: |
---|
| 1398 | ret = new ast::TraitInstType( |
---|
| 1399 | *type->aggregate.name, |
---|
| 1400 | buildQualifiers( type ) |
---|
| 1401 | ); |
---|
[702e826] | 1402 | break; |
---|
| 1403 | default: |
---|
| 1404 | assert( false ); |
---|
| 1405 | } // switch |
---|
| 1406 | break; |
---|
| 1407 | default: |
---|
[43c89a7] | 1408 | assert( false ); |
---|
| 1409 | } // switch |
---|
| 1410 | |
---|
[bb7422a] | 1411 | ret->hoistType = td->aggInst.hoistType; |
---|
| 1412 | buildList( td->aggInst.params, ret->params ); |
---|
[b87a5ed] | 1413 | return ret; |
---|
[413ad05] | 1414 | } // buildAggInst |
---|
| 1415 | |
---|
[201aeb9] | 1416 | |
---|
[bb7422a] | 1417 | ast::NamedTypeDecl * buildSymbolic( |
---|
| 1418 | const TypeData * td, |
---|
| 1419 | std::vector<ast::ptr<ast::Attribute>> attributes, |
---|
| 1420 | const std::string & name, |
---|
| 1421 | ast::Storage::Classes scs, |
---|
| 1422 | ast::Linkage::Spec linkage ) { |
---|
[413ad05] | 1423 | assert( td->kind == TypeData::Symbolic ); |
---|
[bb7422a] | 1424 | ast::NamedTypeDecl * ret; |
---|
[413ad05] | 1425 | assert( td->base ); |
---|
[8f6f47d7] | 1426 | if ( td->symbolic.isTypedef ) { |
---|
[bb7422a] | 1427 | ret = new ast::TypedefDecl( |
---|
| 1428 | td->location, |
---|
| 1429 | name, |
---|
| 1430 | scs, |
---|
| 1431 | typebuild( td->base ), |
---|
| 1432 | linkage |
---|
| 1433 | ); |
---|
[b87a5ed] | 1434 | } else { |
---|
[bb7422a] | 1435 | ret = new ast::TypeDecl( |
---|
| 1436 | td->location, |
---|
| 1437 | name, |
---|
| 1438 | scs, |
---|
| 1439 | typebuild( td->base ), |
---|
| 1440 | ast::TypeDecl::Dtype, |
---|
| 1441 | true |
---|
| 1442 | ); |
---|
[68cd1ce] | 1443 | } // if |
---|
[bb7422a] | 1444 | buildList( td->symbolic.assertions, ret->assertions ); |
---|
| 1445 | splice( ret->base.get_and_mutate()->attributes, attributes ); |
---|
[b87a5ed] | 1446 | return ret; |
---|
[413ad05] | 1447 | } // buildSymbolic |
---|
[51b7345] | 1448 | |
---|
[201aeb9] | 1449 | |
---|
[bb7422a] | 1450 | ast::EnumDecl * buildEnum( |
---|
| 1451 | const TypeData * td, |
---|
| 1452 | std::vector<ast::ptr<ast::Attribute>> && attributes, |
---|
| 1453 | ast::Linkage::Spec linkage ) { |
---|
[67467a3] | 1454 | assert( td->kind == TypeData::Aggregate ); |
---|
| 1455 | assert( td->aggregate.kind == ast::AggregateDecl::Enum ); |
---|
[bb7422a] | 1456 | ast::Type * baseType = td->base ? typebuild(td->base) : nullptr; |
---|
| 1457 | ast::EnumDecl * ret = new ast::EnumDecl( |
---|
| 1458 | td->location, |
---|
[67467a3] | 1459 | *td->aggregate.name, |
---|
[99f7f37] | 1460 | td->aggregate.isCfa, |
---|
[bb7422a] | 1461 | std::move( attributes ), |
---|
| 1462 | linkage, |
---|
| 1463 | baseType |
---|
| 1464 | ); |
---|
[67467a3] | 1465 | buildList( td->aggregate.fields, ret->members ); |
---|
[bb7422a] | 1466 | auto members = ret->members.begin(); |
---|
[67467a3] | 1467 | ret->hide = td->aggregate.hiding == EnumHiding::Hide ? ast::EnumDecl::EnumHiding::Hide : ast::EnumDecl::EnumHiding::Visible; |
---|
| 1468 | for ( const DeclarationNode * cur = td->aggregate.fields ; cur != nullptr ; cur = cur->next, ++members ) { |
---|
[85855b0] | 1469 | if (cur->enumInLine) continue; |
---|
| 1470 | ast::Decl * member = members->get_and_mutate(); |
---|
| 1471 | ast::ObjectDecl * object = strict_dynamic_cast<ast::ObjectDecl *>( member ); |
---|
| 1472 | object->isHidden = ast::EnumDecl::EnumHiding::Hide == ret->hide; |
---|
[8c55d34] | 1473 | if ( ret->isOpaque() && cur->has_enumeratorValue() ) { |
---|
[5ccc733] | 1474 | SemanticError( td->location, "Opague cannot have an explicit initializer value." ); |
---|
[b0d9ff7] | 1475 | } else if ( cur->has_enumeratorValue() ) { |
---|
[bb7422a] | 1476 | object->init = new ast::SingleInit( |
---|
| 1477 | td->location, |
---|
| 1478 | maybeMoveBuild( cur->consume_enumeratorValue() ), |
---|
| 1479 | ast::NoConstruct |
---|
| 1480 | ); |
---|
[8c55d34] | 1481 | } |
---|
[9e7236f4] | 1482 | // else cur is a List Initializer and has been set as init in buildList() |
---|
| 1483 | // if |
---|
[90c3b1c] | 1484 | } // for |
---|
[67467a3] | 1485 | ret->body = td->aggregate.body; |
---|
[b87a5ed] | 1486 | return ret; |
---|
[413ad05] | 1487 | } // buildEnum |
---|
[51b7345] | 1488 | |
---|
[201aeb9] | 1489 | |
---|
[bb7422a] | 1490 | ast::TypeInstType * buildSymbolicInst( const TypeData * td ) { |
---|
[413ad05] | 1491 | assert( td->kind == TypeData::SymbolicInst ); |
---|
[bb7422a] | 1492 | ast::TypeInstType * ret = new ast::TypeInstType( |
---|
| 1493 | *td->symbolic.name, |
---|
| 1494 | ast::TypeDecl::Dtype, |
---|
| 1495 | buildQualifiers( td ) |
---|
| 1496 | ); |
---|
| 1497 | buildList( td->symbolic.actuals, ret->params ); |
---|
[b87a5ed] | 1498 | return ret; |
---|
[413ad05] | 1499 | } // buildSymbolicInst |
---|
[51b7345] | 1500 | |
---|
[201aeb9] | 1501 | |
---|
[bb7422a] | 1502 | ast::TupleType * buildTuple( const TypeData * td ) { |
---|
[413ad05] | 1503 | assert( td->kind == TypeData::Tuple ); |
---|
[bb7422a] | 1504 | std::vector<ast::ptr<ast::Type>> types; |
---|
[62423350] | 1505 | buildTypeList( td->tuple, types ); |
---|
[bb7422a] | 1506 | ast::TupleType * ret = new ast::TupleType( |
---|
| 1507 | std::move( types ), |
---|
| 1508 | buildQualifiers( td ) |
---|
| 1509 | ); |
---|
[b87a5ed] | 1510 | return ret; |
---|
[413ad05] | 1511 | } // buildTuple |
---|
| 1512 | |
---|
[201aeb9] | 1513 | |
---|
[bb7422a] | 1514 | ast::TypeofType * buildTypeof( const TypeData * td ) { |
---|
[f855545] | 1515 | assert( td->kind == TypeData::Typeof || td->kind == TypeData::Basetypeof ); |
---|
[413ad05] | 1516 | assert( td->typeexpr ); |
---|
[bb7422a] | 1517 | return new ast::TypeofType( |
---|
| 1518 | td->typeexpr->build(), |
---|
| 1519 | td->kind == TypeData::Typeof |
---|
| 1520 | ? ast::TypeofType::Typeof : ast::TypeofType::Basetypeof, |
---|
| 1521 | buildQualifiers( td ) |
---|
| 1522 | ); |
---|
[413ad05] | 1523 | } // buildTypeof |
---|
| 1524 | |
---|
[201aeb9] | 1525 | |
---|
[bb7422a] | 1526 | ast::VTableType * buildVtable( const TypeData * td ) { |
---|
[93bbbc4] | 1527 | assert( td->base ); |
---|
[bb7422a] | 1528 | return new ast::VTableType( |
---|
| 1529 | typebuild( td->base ), |
---|
| 1530 | buildQualifiers( td ) |
---|
| 1531 | ); |
---|
[93bbbc4] | 1532 | } // buildVtable |
---|
| 1533 | |
---|
| 1534 | |
---|
[bb7422a] | 1535 | ast::FunctionDecl * buildFunctionDecl( |
---|
| 1536 | const TypeData * td, |
---|
| 1537 | const string &name, |
---|
| 1538 | ast::Storage::Classes scs, |
---|
| 1539 | ast::Function::Specs funcSpec, |
---|
| 1540 | ast::Linkage::Spec linkage, |
---|
| 1541 | ast::Expr * asmName, |
---|
| 1542 | std::vector<ast::ptr<ast::Attribute>> && attributes ) { |
---|
| 1543 | assert( td->kind == TypeData::Function ); |
---|
| 1544 | // For some reason FunctionDecl takes a bool instead of an ArgumentFlag. |
---|
| 1545 | bool isVarArgs = !td->function.params || td->function.params->hasEllipsis; |
---|
| 1546 | ast::CV::Qualifiers cvq = buildQualifiers( td ); |
---|
| 1547 | std::vector<ast::ptr<ast::TypeDecl>> forall; |
---|
| 1548 | std::vector<ast::ptr<ast::DeclWithType>> assertions; |
---|
| 1549 | std::vector<ast::ptr<ast::DeclWithType>> params; |
---|
| 1550 | std::vector<ast::ptr<ast::DeclWithType>> returns; |
---|
| 1551 | buildList( td->function.params, params ); |
---|
| 1552 | buildForall( td->forall, forall ); |
---|
| 1553 | // Functions do not store their assertions there anymore. |
---|
| 1554 | for ( ast::ptr<ast::TypeDecl> & type_param : forall ) { |
---|
| 1555 | auto mut = type_param.get_and_mutate(); |
---|
| 1556 | splice( assertions, mut->assertions ); |
---|
| 1557 | } |
---|
| 1558 | if ( td->base ) { |
---|
| 1559 | switch ( td->base->kind ) { |
---|
| 1560 | case TypeData::Tuple: |
---|
| 1561 | buildList( td->base->tuple, returns ); |
---|
| 1562 | break; |
---|
| 1563 | default: |
---|
| 1564 | returns.push_back( dynamic_cast<ast::DeclWithType *>( |
---|
| 1565 | buildDecl( |
---|
| 1566 | td->base, |
---|
| 1567 | "", |
---|
| 1568 | ast::Storage::Classes(), |
---|
| 1569 | (ast::Expr *)nullptr, // bitfieldWidth |
---|
| 1570 | ast::Function::Specs(), |
---|
| 1571 | ast::Linkage::Cforall, |
---|
| 1572 | (ast::Expr *)nullptr // asmName |
---|
| 1573 | ) |
---|
| 1574 | ) ); |
---|
| 1575 | } // switch |
---|
| 1576 | } else { |
---|
| 1577 | returns.push_back( new ast::ObjectDecl( |
---|
| 1578 | td->location, |
---|
| 1579 | "", |
---|
[7a780ad] | 1580 | new ast::BasicType( ast::BasicKind::SignedInt ), |
---|
[bb7422a] | 1581 | (ast::Init *)nullptr, |
---|
| 1582 | ast::Storage::Classes(), |
---|
| 1583 | ast::Linkage::Cforall |
---|
| 1584 | ) ); |
---|
| 1585 | } // if |
---|
| 1586 | ast::Stmt * stmt = maybeBuild( td->function.body ); |
---|
| 1587 | ast::CompoundStmt * body = dynamic_cast<ast::CompoundStmt *>( stmt ); |
---|
| 1588 | ast::FunctionDecl * decl = new ast::FunctionDecl( td->location, |
---|
| 1589 | name, |
---|
| 1590 | std::move( forall ), |
---|
| 1591 | std::move( assertions ), |
---|
| 1592 | std::move( params ), |
---|
| 1593 | std::move( returns ), |
---|
| 1594 | body, |
---|
| 1595 | scs, |
---|
| 1596 | linkage, |
---|
| 1597 | std::move( attributes ), |
---|
| 1598 | funcSpec, |
---|
[3e94a23] | 1599 | (isVarArgs) ? ast::VariableArgs : ast::FixedArgs |
---|
[bb7422a] | 1600 | ); |
---|
| 1601 | buildList( td->function.withExprs, decl->withExprs ); |
---|
| 1602 | decl->asmName = asmName; |
---|
| 1603 | // This may be redundant on a declaration. |
---|
| 1604 | decl->type.get_and_mutate()->qualifiers = cvq; |
---|
| 1605 | return decl; |
---|
| 1606 | } // buildFunctionDecl |
---|
| 1607 | |
---|
| 1608 | |
---|
| 1609 | ast::Decl * buildDecl( |
---|
| 1610 | const TypeData * td, |
---|
| 1611 | const string &name, |
---|
| 1612 | ast::Storage::Classes scs, |
---|
| 1613 | ast::Expr * bitfieldWidth, |
---|
| 1614 | ast::Function::Specs funcSpec, |
---|
| 1615 | ast::Linkage::Spec linkage, |
---|
| 1616 | ast::Expr * asmName, |
---|
| 1617 | ast::Init * init, |
---|
| 1618 | std::vector<ast::ptr<ast::Attribute>> && attributes ) { |
---|
[413ad05] | 1619 | if ( td->kind == TypeData::Function ) { |
---|
[ca1a547] | 1620 | if ( td->function.idList ) { // KR function ? |
---|
| 1621 | buildKRFunction( td->function ); // transform into C11 function |
---|
[3a5131ed] | 1622 | } // if |
---|
| 1623 | |
---|
[bb7422a] | 1624 | return buildFunctionDecl( |
---|
| 1625 | td, name, scs, funcSpec, linkage, |
---|
| 1626 | asmName, std::move( attributes ) ); |
---|
[413ad05] | 1627 | } else if ( td->kind == TypeData::Aggregate ) { |
---|
[bb7422a] | 1628 | return buildAggregate( td, std::move( attributes ), linkage ); |
---|
[413ad05] | 1629 | } else if ( td->kind == TypeData::Symbolic ) { |
---|
[bb7422a] | 1630 | return buildSymbolic( td, std::move( attributes ), name, scs, linkage ); |
---|
[413ad05] | 1631 | } else { |
---|
[bb7422a] | 1632 | auto ret = new ast::ObjectDecl( td->location, |
---|
| 1633 | name, |
---|
| 1634 | typebuild( td ), |
---|
| 1635 | init, |
---|
| 1636 | scs, |
---|
| 1637 | linkage, |
---|
| 1638 | bitfieldWidth, |
---|
| 1639 | std::move( attributes ) |
---|
| 1640 | ); |
---|
| 1641 | ret->asmName = asmName; |
---|
| 1642 | return ret; |
---|
[413ad05] | 1643 | } // if |
---|
[2298f728] | 1644 | return nullptr; |
---|
[413ad05] | 1645 | } // buildDecl |
---|
| 1646 | |
---|
[201aeb9] | 1647 | |
---|
[bb7422a] | 1648 | ast::FunctionType * buildFunctionType( const TypeData * td ) { |
---|
[413ad05] | 1649 | assert( td->kind == TypeData::Function ); |
---|
[bb7422a] | 1650 | ast::FunctionType * ft = new ast::FunctionType( |
---|
| 1651 | ( !td->function.params || td->function.params->hasEllipsis ) |
---|
| 1652 | ? ast::VariableArgs : ast::FixedArgs, |
---|
| 1653 | buildQualifiers( td ) |
---|
| 1654 | ); |
---|
| 1655 | buildTypeList( td->function.params, ft->params ); |
---|
[704d11e] | 1656 | buildForall( td->forall, ft->forall ); |
---|
[413ad05] | 1657 | if ( td->base ) { |
---|
| 1658 | switch ( td->base->kind ) { |
---|
[702e826] | 1659 | case TypeData::Tuple: |
---|
[bb7422a] | 1660 | buildTypeList( td->base->tuple, ft->returns ); |
---|
[413ad05] | 1661 | break; |
---|
[702e826] | 1662 | default: |
---|
[bb7422a] | 1663 | ft->returns.push_back( typebuild( td->base ) ); |
---|
| 1664 | break; |
---|
[413ad05] | 1665 | } // switch |
---|
| 1666 | } else { |
---|
[bb7422a] | 1667 | ft->returns.push_back( |
---|
[7a780ad] | 1668 | new ast::BasicType( ast::BasicKind::SignedInt ) ); |
---|
[413ad05] | 1669 | } // if |
---|
| 1670 | return ft; |
---|
[bb7422a] | 1671 | } // buildFunctionType |
---|
[b87a5ed] | 1672 | |
---|
[201aeb9] | 1673 | |
---|
[a7c4921] | 1674 | // Transform KR routine declarations into C99 routine declarations: |
---|
| 1675 | // |
---|
| 1676 | // rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {} |
---|
| 1677 | // |
---|
| 1678 | // The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration |
---|
| 1679 | // is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and |
---|
| 1680 | // extra names are disallowed. |
---|
| 1681 | // |
---|
| 1682 | // Note, there is no KR routine-prototype syntax: |
---|
| 1683 | // |
---|
| 1684 | // rtn( a, b, c ) int a, c; double b; // invalid KR prototype |
---|
| 1685 | // rtn(); // valid KR prototype |
---|
| 1686 | |
---|
[3a5131ed] | 1687 | void buildKRFunction( const TypeData::Function_t & function ) { |
---|
| 1688 | assert( ! function.params ); |
---|
[a7c4921] | 1689 | // loop over declaration first as it is easier to spot errors |
---|
[44adf1b] | 1690 | for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = decl->next ) { |
---|
[a7c4921] | 1691 | // scan ALL parameter names for each declaration name to check for duplicates |
---|
[44adf1b] | 1692 | for ( DeclarationNode * param = function.idList; param != nullptr; param = param->next ) { |
---|
[3a5131ed] | 1693 | if ( *decl->name == *param->name ) { |
---|
[a7c4921] | 1694 | // type set => parameter name already transformed by a declaration names so there is a duplicate |
---|
| 1695 | // declaration name attempting a second transformation |
---|
[ca9d65e] | 1696 | if ( param->type ) SemanticError( param->location, "duplicate declaration name \"%s\".", param->name->c_str() ); |
---|
[a7c4921] | 1697 | // declaration type reset => declaration already transformed by a parameter name so there is a duplicate |
---|
| 1698 | // parameter name attempting a second transformation |
---|
[ca9d65e] | 1699 | if ( ! decl->type ) SemanticError( param->location, "duplicate parameter name \"%s\".", param->name->c_str() ); |
---|
[a7c4921] | 1700 | param->type = decl->type; // set copy declaration type to parameter type |
---|
| 1701 | decl->type = nullptr; // reset declaration type |
---|
[bb7422a] | 1702 | // Copy and reset attributes from declaration to parameter: |
---|
| 1703 | splice( param->attributes, decl->attributes ); |
---|
[3a5131ed] | 1704 | } // if |
---|
| 1705 | } // for |
---|
[a7c4921] | 1706 | // declaration type still set => type not moved to a matching parameter so there is a missing parameter name |
---|
[b1f2007] | 1707 | if ( decl->type ) SemanticError( decl->location, "missing name in parameter list %s", decl->name->c_str() ); |
---|
[3a5131ed] | 1708 | } // for |
---|
[a7c4921] | 1709 | |
---|
| 1710 | // Parameter names without a declaration default to type int: |
---|
| 1711 | // |
---|
| 1712 | // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {} |
---|
| 1713 | |
---|
[44adf1b] | 1714 | for ( DeclarationNode * param = function.idList; param != nullptr; param = param->next ) { |
---|
[ca1a547] | 1715 | if ( ! param->type ) { // generate type int for empty parameter type |
---|
[3a5131ed] | 1716 | param->type = new TypeData( TypeData::Basic ); |
---|
[e048ece] | 1717 | param->type->basictype = TypeData::Int; |
---|
[3a5131ed] | 1718 | } // if |
---|
| 1719 | } // for |
---|
| 1720 | |
---|
[a7c4921] | 1721 | function.params = function.idList; // newly modified idList becomes parameters |
---|
| 1722 | function.idList = nullptr; // idList now empty |
---|
| 1723 | delete function.oldDeclList; // deletes entire list |
---|
| 1724 | function.oldDeclList = nullptr; // reset |
---|
[3a5131ed] | 1725 | } // buildKRFunction |
---|
| 1726 | |
---|
[b87a5ed] | 1727 | // Local Variables: // |
---|
| 1728 | // tab-width: 4 // |
---|
| 1729 | // mode: c++ // |
---|
| 1730 | // compile-command: "make install" // |
---|
| 1731 | // End: // |
---|