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