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