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