[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 |
---|
[413ad05] | 12 | // Last Modified On : Sun Aug 28 18:28:58 2016 |
---|
| 13 | // Update Count : 223 |
---|
[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" |
---|
[51b7345] | 26 | |
---|
[c8ffe20b] | 27 | TypeData::TypeData( Kind k ) : kind( k ), base( 0 ), forall( 0 ) { |
---|
[b87a5ed] | 28 | switch ( kind ) { |
---|
| 29 | case Unknown: |
---|
| 30 | case Pointer: |
---|
| 31 | case EnumConstant: |
---|
| 32 | // nothing else to initialize |
---|
| 33 | break; |
---|
| 34 | case Basic: |
---|
| 35 | basic = new Basic_t; |
---|
| 36 | break; |
---|
| 37 | case Array: |
---|
| 38 | array = new Array_t; |
---|
| 39 | array->dimension = 0; |
---|
| 40 | array->isVarLen = false; |
---|
| 41 | array->isStatic = false; |
---|
| 42 | break; |
---|
| 43 | case Function: |
---|
| 44 | function = new Function_t; |
---|
| 45 | function->params = 0; |
---|
| 46 | function->idList = 0; |
---|
| 47 | function->oldDeclList = 0; |
---|
| 48 | function->body = 0; |
---|
| 49 | function->hasBody = false; |
---|
| 50 | function->newStyle = false; |
---|
| 51 | break; |
---|
| 52 | case Aggregate: |
---|
| 53 | aggregate = new Aggregate_t; |
---|
| 54 | aggregate->params = 0; |
---|
| 55 | aggregate->actuals = 0; |
---|
[721f17a] | 56 | aggregate->fields = 0; |
---|
[b87a5ed] | 57 | break; |
---|
| 58 | case AggregateInst: |
---|
| 59 | aggInst = new AggInst_t; |
---|
| 60 | aggInst->aggregate = 0; |
---|
| 61 | aggInst->params = 0; |
---|
| 62 | break; |
---|
| 63 | case Enum: |
---|
| 64 | enumeration = new Enumeration_t; |
---|
| 65 | enumeration->constants = 0; |
---|
| 66 | break; |
---|
| 67 | case Symbolic: |
---|
| 68 | case SymbolicInst: |
---|
| 69 | symbolic = new Symbolic_t; |
---|
| 70 | symbolic->params = 0; |
---|
| 71 | symbolic->actuals = 0; |
---|
| 72 | symbolic->assertions = 0; |
---|
| 73 | break; |
---|
| 74 | case Variable: |
---|
| 75 | variable = new Variable_t; |
---|
| 76 | variable->tyClass = DeclarationNode::Type; |
---|
| 77 | variable->assertions = 0; |
---|
| 78 | break; |
---|
| 79 | case Tuple: |
---|
| 80 | tuple = new Tuple_t; |
---|
| 81 | tuple->members = 0; |
---|
| 82 | break; |
---|
| 83 | case Typeof: |
---|
| 84 | typeexpr = new Typeof_t; |
---|
| 85 | typeexpr->expr = 0; |
---|
| 86 | break; |
---|
[90c3b1c] | 87 | case Builtin: |
---|
| 88 | builtin = new Builtin_t; |
---|
| 89 | break; |
---|
[b87a5ed] | 90 | case Attr: |
---|
| 91 | attr = new Attr_t; |
---|
| 92 | attr->expr = 0; |
---|
| 93 | attr->type = 0; |
---|
| 94 | break; |
---|
[68cd1ce] | 95 | } // switch |
---|
[413ad05] | 96 | } // TypeData::TypeData |
---|
[51b7345] | 97 | |
---|
[c8ffe20b] | 98 | TypeData::~TypeData() { |
---|
[b87a5ed] | 99 | delete base; |
---|
| 100 | delete forall; |
---|
| 101 | |
---|
| 102 | switch ( kind ) { |
---|
| 103 | case Unknown: |
---|
| 104 | case Pointer: |
---|
| 105 | case EnumConstant: |
---|
| 106 | // nothing to destroy |
---|
| 107 | break; |
---|
| 108 | case Basic: |
---|
| 109 | delete basic; |
---|
| 110 | break; |
---|
| 111 | case Array: |
---|
| 112 | delete array->dimension; |
---|
| 113 | delete array; |
---|
| 114 | break; |
---|
| 115 | case Function: |
---|
| 116 | delete function->params; |
---|
| 117 | delete function->idList; |
---|
| 118 | delete function->oldDeclList; |
---|
| 119 | delete function->body; |
---|
| 120 | delete function; |
---|
| 121 | break; |
---|
| 122 | case Aggregate: |
---|
| 123 | delete aggregate->params; |
---|
| 124 | delete aggregate->actuals; |
---|
[721f17a] | 125 | delete aggregate->fields; |
---|
[b87a5ed] | 126 | delete aggregate; |
---|
| 127 | break; |
---|
| 128 | case AggregateInst: |
---|
| 129 | delete aggInst->aggregate; |
---|
| 130 | delete aggInst->params; |
---|
| 131 | delete aggInst; |
---|
| 132 | break; |
---|
| 133 | case Enum: |
---|
| 134 | delete enumeration->constants; |
---|
| 135 | delete enumeration; |
---|
| 136 | break; |
---|
| 137 | case Symbolic: |
---|
| 138 | case SymbolicInst: |
---|
| 139 | delete symbolic->params; |
---|
| 140 | delete symbolic->actuals; |
---|
| 141 | delete symbolic->assertions; |
---|
| 142 | delete symbolic; |
---|
| 143 | break; |
---|
| 144 | case Variable: |
---|
| 145 | delete variable->assertions; |
---|
| 146 | delete variable; |
---|
| 147 | break; |
---|
| 148 | case Tuple: |
---|
| 149 | delete tuple->members; |
---|
| 150 | delete tuple; |
---|
| 151 | break; |
---|
| 152 | case Typeof: |
---|
| 153 | delete typeexpr->expr; |
---|
| 154 | delete typeexpr; |
---|
| 155 | break; |
---|
[90c3b1c] | 156 | case Builtin: |
---|
| 157 | delete builtin; |
---|
| 158 | break; |
---|
[b87a5ed] | 159 | case Attr: |
---|
| 160 | delete attr->expr; |
---|
| 161 | delete attr->type; |
---|
| 162 | delete attr; |
---|
| 163 | break; |
---|
[68cd1ce] | 164 | } // switch |
---|
[413ad05] | 165 | } // TypeData::~TypeData |
---|
[51b7345] | 166 | |
---|
[413ad05] | 167 | TypeData * TypeData::clone() const { |
---|
| 168 | TypeData * newtype = new TypeData( kind ); |
---|
[b87a5ed] | 169 | newtype->qualifiers = qualifiers; |
---|
| 170 | newtype->base = maybeClone( base ); |
---|
| 171 | newtype->forall = maybeClone( forall ); |
---|
| 172 | |
---|
| 173 | switch ( kind ) { |
---|
| 174 | case Unknown: |
---|
| 175 | case EnumConstant: |
---|
| 176 | case Pointer: |
---|
| 177 | // nothing else to copy |
---|
| 178 | break; |
---|
| 179 | case Basic: |
---|
| 180 | newtype->basic->typeSpec = basic->typeSpec; |
---|
| 181 | newtype->basic->modifiers = basic->modifiers; |
---|
| 182 | break; |
---|
| 183 | case Array: |
---|
[f39096c] | 184 | newtype->array->dimension = maybeClone( array->dimension ); |
---|
[b87a5ed] | 185 | newtype->array->isVarLen = array->isVarLen; |
---|
| 186 | newtype->array->isStatic = array->isStatic; |
---|
| 187 | break; |
---|
| 188 | case Function: |
---|
| 189 | newtype->function->params = maybeClone( function->params ); |
---|
| 190 | newtype->function->idList = maybeClone( function->idList ); |
---|
| 191 | newtype->function->oldDeclList = maybeClone( function->oldDeclList ); |
---|
| 192 | newtype->function->body = maybeClone( function->body ); |
---|
| 193 | newtype->function->hasBody = function->hasBody; |
---|
| 194 | newtype->function->newStyle = function->newStyle; |
---|
| 195 | break; |
---|
| 196 | case Aggregate: |
---|
| 197 | newtype->aggregate->params = maybeClone( aggregate->params ); |
---|
| 198 | newtype->aggregate->actuals = maybeClone( aggregate->actuals ); |
---|
[721f17a] | 199 | newtype->aggregate->fields = maybeClone( aggregate->fields ); |
---|
[b87a5ed] | 200 | newtype->aggregate->name = aggregate->name; |
---|
| 201 | newtype->aggregate->kind = aggregate->kind; |
---|
[5d125e4] | 202 | newtype->aggregate->body = aggregate->body; |
---|
[b87a5ed] | 203 | break; |
---|
| 204 | case AggregateInst: |
---|
| 205 | newtype->aggInst->aggregate = maybeClone( aggInst->aggregate ); |
---|
| 206 | newtype->aggInst->params = maybeClone( aggInst->params ); |
---|
| 207 | break; |
---|
| 208 | case Enum: |
---|
| 209 | newtype->enumeration->name = enumeration->name; |
---|
| 210 | newtype->enumeration->constants = maybeClone( enumeration->constants ); |
---|
| 211 | break; |
---|
| 212 | case Symbolic: |
---|
| 213 | case SymbolicInst: |
---|
| 214 | newtype->symbolic->params = maybeClone( symbolic->params ); |
---|
| 215 | newtype->symbolic->actuals = maybeClone( symbolic->actuals ); |
---|
| 216 | newtype->symbolic->assertions = maybeClone( symbolic->assertions ); |
---|
| 217 | newtype->symbolic->isTypedef = symbolic->isTypedef; |
---|
| 218 | newtype->symbolic->name = symbolic->name; |
---|
| 219 | break; |
---|
| 220 | case Variable: |
---|
| 221 | newtype->variable->assertions = maybeClone( variable->assertions ); |
---|
| 222 | newtype->variable->name = variable->name; |
---|
| 223 | newtype->variable->tyClass = variable->tyClass; |
---|
| 224 | break; |
---|
| 225 | case Tuple: |
---|
| 226 | newtype->tuple->members = maybeClone( tuple->members ); |
---|
| 227 | break; |
---|
| 228 | case Typeof: |
---|
| 229 | newtype->typeexpr->expr = maybeClone( typeexpr->expr ); |
---|
| 230 | break; |
---|
[90c3b1c] | 231 | case Builtin: |
---|
| 232 | newtype->builtin->type = builtin->type; |
---|
| 233 | break; |
---|
[b87a5ed] | 234 | case Attr: |
---|
| 235 | newtype->attr->expr = maybeClone( attr->expr ); |
---|
| 236 | newtype->attr->type = maybeClone( attr->type ); |
---|
| 237 | break; |
---|
[68cd1ce] | 238 | } // switch |
---|
[b87a5ed] | 239 | return newtype; |
---|
[413ad05] | 240 | } // TypeData::clone |
---|
[51b7345] | 241 | |
---|
[c8ffe20b] | 242 | void TypeData::print( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 243 | using std::endl; |
---|
| 244 | using std::string; |
---|
| 245 | |
---|
[c1c1112] | 246 | for ( int i = 0; i < DeclarationNode::NoOfQualifier; i += 1 ) { |
---|
| 247 | if ( qualifiers[i] ) os << DeclarationNode::qualifierName[ i ] << ' '; |
---|
| 248 | } // for |
---|
[b87a5ed] | 249 | |
---|
| 250 | if ( forall ) { |
---|
| 251 | os << "forall " << endl; |
---|
[721f17a] | 252 | forall->printList( os, indent + 4 ); |
---|
[68cd1ce] | 253 | } // if |
---|
[b87a5ed] | 254 | |
---|
| 255 | switch ( kind ) { |
---|
| 256 | case Unknown: |
---|
| 257 | os << "entity of unknown type "; |
---|
| 258 | break; |
---|
| 259 | case Pointer: |
---|
| 260 | os << "pointer "; |
---|
| 261 | if ( base ) { |
---|
| 262 | os << "to "; |
---|
| 263 | base->print( os, indent ); |
---|
[68cd1ce] | 264 | } // if |
---|
[b87a5ed] | 265 | break; |
---|
| 266 | case EnumConstant: |
---|
| 267 | os << "enumeration constant "; |
---|
| 268 | break; |
---|
| 269 | case Basic: |
---|
| 270 | printEnums( basic->modifiers.begin(), basic->modifiers.end(), DeclarationNode::modifierName, os ); |
---|
| 271 | printEnums( basic->typeSpec.begin(), basic->typeSpec.end(), DeclarationNode::basicTypeName, os ); |
---|
| 272 | break; |
---|
| 273 | case Array: |
---|
| 274 | if ( array->isStatic ) { |
---|
| 275 | os << "static "; |
---|
[68cd1ce] | 276 | } // if |
---|
[b87a5ed] | 277 | if ( array->dimension ) { |
---|
| 278 | os << "array of "; |
---|
| 279 | array->dimension->printOneLine( os, indent ); |
---|
| 280 | } else if ( array->isVarLen ) { |
---|
| 281 | os << "variable-length array of "; |
---|
| 282 | } else { |
---|
| 283 | os << "open array of "; |
---|
[68cd1ce] | 284 | } // if |
---|
[b87a5ed] | 285 | if ( base ) { |
---|
| 286 | base->print( os, indent ); |
---|
[68cd1ce] | 287 | } // if |
---|
[b87a5ed] | 288 | break; |
---|
| 289 | case Function: |
---|
| 290 | os << "function" << endl; |
---|
| 291 | if ( function->params ) { |
---|
[721f17a] | 292 | os << string( indent + 2, ' ' ) << "with parameters " << endl; |
---|
| 293 | function->params->printList( os, indent + 4 ); |
---|
[b87a5ed] | 294 | } else { |
---|
[721f17a] | 295 | os << string( indent + 2, ' ' ) << "with no parameters " << endl; |
---|
[68cd1ce] | 296 | } // if |
---|
[b87a5ed] | 297 | if ( function->idList ) { |
---|
[721f17a] | 298 | os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl; |
---|
| 299 | function->idList->printList( os, indent + 4 ); |
---|
[68cd1ce] | 300 | } // if |
---|
[b87a5ed] | 301 | if ( function->oldDeclList ) { |
---|
[721f17a] | 302 | os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl; |
---|
| 303 | function->oldDeclList->printList( os, indent + 4 ); |
---|
[68cd1ce] | 304 | } // if |
---|
[721f17a] | 305 | os << string( indent + 2, ' ' ) << "returning "; |
---|
[b87a5ed] | 306 | if ( base ) { |
---|
[721f17a] | 307 | base->print( os, indent + 4 ); |
---|
[b87a5ed] | 308 | } else { |
---|
| 309 | os << "nothing "; |
---|
[68cd1ce] | 310 | } // if |
---|
[b87a5ed] | 311 | os << endl; |
---|
| 312 | if ( function->hasBody ) { |
---|
[721f17a] | 313 | os << string( indent + 2, ' ' ) << "with body " << endl; |
---|
[68cd1ce] | 314 | } // if |
---|
[b87a5ed] | 315 | if ( function->body ) { |
---|
[721f17a] | 316 | function->body->printList( os, indent + 2 ); |
---|
[68cd1ce] | 317 | } // if |
---|
[b87a5ed] | 318 | break; |
---|
| 319 | case Aggregate: |
---|
[68cd1ce] | 320 | os << DeclarationNode::aggregateName[ aggregate->kind ] << ' ' << aggregate->name << endl; |
---|
[b87a5ed] | 321 | if ( aggregate->params ) { |
---|
[721f17a] | 322 | os << string( indent + 2, ' ' ) << "with type parameters " << endl; |
---|
| 323 | aggregate->params->printList( os, indent + 4 ); |
---|
[68cd1ce] | 324 | } // if |
---|
[b87a5ed] | 325 | if ( aggregate->actuals ) { |
---|
[721f17a] | 326 | os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl; |
---|
| 327 | aggregate->actuals->printList( os, indent + 4 ); |
---|
[68cd1ce] | 328 | } // if |
---|
[721f17a] | 329 | if ( aggregate->fields ) { |
---|
| 330 | os << string( indent + 2, ' ' ) << "with members " << endl; |
---|
| 331 | aggregate->fields->printList( os, indent + 4 ); |
---|
[5d125e4] | 332 | } // if |
---|
| 333 | if ( aggregate->body ) { |
---|
| 334 | os << string( indent + 2, ' ' ) << " with body " << endl; |
---|
[68cd1ce] | 335 | } // if |
---|
[b87a5ed] | 336 | break; |
---|
| 337 | case AggregateInst: |
---|
| 338 | if ( aggInst->aggregate ) { |
---|
| 339 | os << "instance of " ; |
---|
| 340 | aggInst->aggregate->print( os, indent ); |
---|
| 341 | } else { |
---|
| 342 | os << "instance of an unspecified aggregate "; |
---|
[68cd1ce] | 343 | } // if |
---|
[b87a5ed] | 344 | if ( aggInst->params ) { |
---|
[721f17a] | 345 | os << string( indent + 2, ' ' ) << "with parameters " << endl; |
---|
| 346 | aggInst->params->printList( os, indent + 2 ); |
---|
[68cd1ce] | 347 | } // if |
---|
[b87a5ed] | 348 | break; |
---|
| 349 | case Enum: |
---|
| 350 | os << "enumeration "; |
---|
| 351 | if ( enumeration->constants ) { |
---|
| 352 | os << "with constants" << endl; |
---|
[721f17a] | 353 | enumeration->constants->printList( os, indent + 2 ); |
---|
[68cd1ce] | 354 | } // if |
---|
[b87a5ed] | 355 | break; |
---|
| 356 | case SymbolicInst: |
---|
| 357 | os << "instance of type " << symbolic->name; |
---|
| 358 | if ( symbolic->actuals ) { |
---|
| 359 | os << " with parameters" << endl; |
---|
| 360 | symbolic->actuals->printList( os, indent + 2 ); |
---|
[68cd1ce] | 361 | } // if |
---|
[b87a5ed] | 362 | break; |
---|
| 363 | case Symbolic: |
---|
| 364 | if ( symbolic->isTypedef ) { |
---|
| 365 | os << "typedef definition "; |
---|
| 366 | } else { |
---|
| 367 | os << "type definition "; |
---|
[68cd1ce] | 368 | } // if |
---|
[b87a5ed] | 369 | if ( symbolic->params ) { |
---|
[721f17a] | 370 | os << endl << string( indent + 2, ' ' ) << "with parameters" << endl; |
---|
[b87a5ed] | 371 | symbolic->params->printList( os, indent + 2 ); |
---|
[68cd1ce] | 372 | } // if |
---|
[b87a5ed] | 373 | if ( symbolic->assertions ) { |
---|
[721f17a] | 374 | os << endl << string( indent + 2, ' ' ) << "with assertions" << endl; |
---|
[b87a5ed] | 375 | symbolic->assertions->printList( os, indent + 4 ); |
---|
[721f17a] | 376 | os << string( indent + 2, ' ' ); |
---|
[68cd1ce] | 377 | } // if |
---|
[b87a5ed] | 378 | if ( base ) { |
---|
| 379 | os << "for "; |
---|
| 380 | base->print( os, indent + 2 ); |
---|
[68cd1ce] | 381 | } // if |
---|
[b87a5ed] | 382 | break; |
---|
| 383 | case Variable: |
---|
| 384 | os << DeclarationNode::typeClassName[ variable->tyClass ] << " variable "; |
---|
| 385 | if ( variable->assertions ) { |
---|
[721f17a] | 386 | os << endl << string( indent + 2, ' ' ) << "with assertions" << endl; |
---|
[b87a5ed] | 387 | variable->assertions->printList( os, indent + 4 ); |
---|
[721f17a] | 388 | os << string( indent + 2, ' ' ); |
---|
[68cd1ce] | 389 | } // if |
---|
[b87a5ed] | 390 | break; |
---|
| 391 | case Tuple: |
---|
| 392 | os << "tuple "; |
---|
| 393 | if ( tuple->members ) { |
---|
| 394 | os << "with members " << endl; |
---|
| 395 | tuple->members->printList( os, indent + 2 ); |
---|
[68cd1ce] | 396 | } // if |
---|
[b87a5ed] | 397 | break; |
---|
| 398 | case Typeof: |
---|
| 399 | os << "type-of expression "; |
---|
| 400 | if ( typeexpr->expr ) { |
---|
| 401 | typeexpr->expr->print( os, indent + 2 ); |
---|
[68cd1ce] | 402 | } // if |
---|
[b87a5ed] | 403 | break; |
---|
| 404 | case Attr: |
---|
| 405 | os << "attribute type decl " << attr->name << " applied to "; |
---|
| 406 | if ( attr->expr ) { |
---|
| 407 | attr->expr->print( os, indent + 2 ); |
---|
[68cd1ce] | 408 | } // if |
---|
[b87a5ed] | 409 | if ( attr->type ) { |
---|
| 410 | attr->type->print( os, indent + 2 ); |
---|
[68cd1ce] | 411 | } // if |
---|
[b87a5ed] | 412 | break; |
---|
[90c3b1c] | 413 | case Builtin: |
---|
| 414 | os << "gcc builtin type"; |
---|
| 415 | break; |
---|
[1db21619] | 416 | default: |
---|
| 417 | os << "internal error: TypeData::print " << kind << endl; |
---|
| 418 | assert( false ); |
---|
[68cd1ce] | 419 | } // switch |
---|
[413ad05] | 420 | } // TypeData::print |
---|
[51b7345] | 421 | |
---|
[413ad05] | 422 | void buildForall( const DeclarationNode * firstNode, std::list< TypeDecl* > &outputList ) { |
---|
[b87a5ed] | 423 | buildList( firstNode, outputList ); |
---|
| 424 | for ( std::list< TypeDecl* >::iterator i = outputList.begin(); i != outputList.end(); ++i ) { |
---|
| 425 | if ( (*i)->get_kind() == TypeDecl::Any ) { |
---|
[6943f051] | 426 | // add assertion parameters to `type' tyvars in reverse order |
---|
| 427 | // add dtor: void ^?{}(T *) |
---|
[413ad05] | 428 | FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false ); |
---|
[6943f051] | 429 | dtorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) ); |
---|
| 430 | (*i)->get_assertions().push_front( new FunctionDecl( "^?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, dtorType, 0, false, false ) ); |
---|
[4cc4286] | 431 | |
---|
[a9a259c] | 432 | // add copy ctor: void ?{}(T *, T) |
---|
[413ad05] | 433 | FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false ); |
---|
[a9a259c] | 434 | copyCtorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) ); |
---|
| 435 | copyCtorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) ); |
---|
| 436 | (*i)->get_assertions().push_front( new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, copyCtorType, 0, false, false ) ); |
---|
| 437 | |
---|
[6943f051] | 438 | // add default ctor: void ?{}(T *) |
---|
[413ad05] | 439 | FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false ); |
---|
[6943f051] | 440 | ctorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) ); |
---|
| 441 | (*i)->get_assertions().push_front( new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, ctorType, 0, false, false ) ); |
---|
| 442 | |
---|
| 443 | // add assignment operator: T * ?=?(T *, T) |
---|
[413ad05] | 444 | FunctionType * assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
[6943f051] | 445 | assignType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) ); |
---|
| 446 | assignType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) ); |
---|
| 447 | assignType->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) ); |
---|
| 448 | (*i)->get_assertions().push_front( new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, assignType, 0, false, false ) ); |
---|
[68cd1ce] | 449 | } // if |
---|
| 450 | } // for |
---|
[51b7345] | 451 | } |
---|
| 452 | |
---|
[413ad05] | 453 | Type * typebuild( const TypeData * td ) { |
---|
| 454 | assert( td ); |
---|
| 455 | switch ( td->kind ) { |
---|
| 456 | case TypeData::Unknown: |
---|
[b87a5ed] | 457 | // fill in implicit int |
---|
[413ad05] | 458 | return new BasicType( buildQualifiers( td ), BasicType::SignedInt ); |
---|
| 459 | case TypeData::Basic: |
---|
| 460 | return buildBasicType( td ); |
---|
| 461 | case TypeData::Pointer: |
---|
| 462 | return buildPointer( td ); |
---|
| 463 | case TypeData::Array: |
---|
| 464 | return buildArray( td ); |
---|
| 465 | case TypeData::Function: |
---|
| 466 | return buildFunction( td ); |
---|
| 467 | case TypeData::AggregateInst: |
---|
| 468 | return buildAggInst( td ); |
---|
| 469 | case TypeData::EnumConstant: |
---|
[b87a5ed] | 470 | // the name gets filled in later -- by SymTab::Validate |
---|
[413ad05] | 471 | return new EnumInstType( buildQualifiers( td ), "" ); |
---|
| 472 | case TypeData::SymbolicInst: |
---|
| 473 | return buildSymbolicInst( td );; |
---|
| 474 | case TypeData::Tuple: |
---|
| 475 | return buildTuple( td ); |
---|
| 476 | case TypeData::Typeof: |
---|
| 477 | return buildTypeof( td ); |
---|
| 478 | case TypeData::Builtin: |
---|
| 479 | return new VarArgsType( buildQualifiers( td ) ); |
---|
| 480 | case TypeData::Attr: |
---|
| 481 | return buildAttr( td ); |
---|
| 482 | case TypeData::Symbolic: |
---|
| 483 | case TypeData::Enum: |
---|
| 484 | case TypeData::Aggregate: |
---|
| 485 | case TypeData::Variable: |
---|
[b87a5ed] | 486 | assert( false ); |
---|
[68cd1ce] | 487 | } // switch |
---|
[b87a5ed] | 488 | return 0; |
---|
[413ad05] | 489 | } // typebuild |
---|
| 490 | |
---|
| 491 | TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) { |
---|
| 492 | TypeData * ret = 0; |
---|
[51b7345] | 493 | |
---|
[413ad05] | 494 | switch ( td->kind ) { |
---|
| 495 | case TypeData::Aggregate: |
---|
| 496 | if ( ! toplevel && td->aggregate->fields ) { |
---|
| 497 | ret = td->clone(); |
---|
| 498 | } // if |
---|
| 499 | break; |
---|
| 500 | case TypeData::Enum: |
---|
| 501 | if ( ! toplevel && td->enumeration->constants ) { |
---|
| 502 | ret = td->clone(); |
---|
| 503 | } // if |
---|
| 504 | break; |
---|
| 505 | case TypeData::AggregateInst: |
---|
| 506 | if ( td->aggInst->aggregate ) { |
---|
| 507 | ret = typeextractAggregate( td->aggInst->aggregate, false ); |
---|
| 508 | } // if |
---|
| 509 | break; |
---|
| 510 | default: |
---|
| 511 | if ( td->base ) { |
---|
| 512 | ret = typeextractAggregate( td->base, false ); |
---|
| 513 | } // if |
---|
| 514 | } // switch |
---|
| 515 | return ret; |
---|
| 516 | } // typeextractAggregate |
---|
| 517 | |
---|
| 518 | Type::Qualifiers buildQualifiers( const TypeData * td ) { |
---|
[b87a5ed] | 519 | Type::Qualifiers q; |
---|
[413ad05] | 520 | q.isConst = td->qualifiers[ DeclarationNode::Const ]; |
---|
| 521 | q.isVolatile = td->qualifiers[ DeclarationNode::Volatile ]; |
---|
| 522 | q.isRestrict = td->qualifiers[ DeclarationNode::Restrict ]; |
---|
| 523 | q.isLvalue = td->qualifiers[ DeclarationNode::Lvalue ]; |
---|
| 524 | q.isAtomic = td->qualifiers[ DeclarationNode::Atomic ];; |
---|
[b87a5ed] | 525 | return q; |
---|
[413ad05] | 526 | } // buildQualifiers |
---|
[51b7345] | 527 | |
---|
[413ad05] | 528 | Type * buildBasicType( const TypeData * td ) { |
---|
[b87a5ed] | 529 | static const BasicType::Kind kindMap[] = { BasicType::Char, BasicType::SignedInt, BasicType::Float, BasicType::Double, |
---|
| 530 | BasicType::Char /* void */, BasicType::Bool, BasicType::DoubleComplex, |
---|
| 531 | BasicType::DoubleImaginary }; |
---|
| 532 | bool init = false; |
---|
| 533 | bool sawDouble = false; |
---|
| 534 | bool sawSigned = false; |
---|
| 535 | BasicType::Kind ret; |
---|
| 536 | |
---|
[413ad05] | 537 | for ( std::list< DeclarationNode::BasicType >::const_iterator i = td->basic->typeSpec.begin(); i != td->basic->typeSpec.end(); ++i ) { |
---|
[a32b204] | 538 | if ( ! init ) { |
---|
[b87a5ed] | 539 | init = true; |
---|
| 540 | if ( *i == DeclarationNode::Void ) { |
---|
[413ad05] | 541 | if ( td->basic->typeSpec.size() != 1 || ! td->basic->modifiers.empty() ) { |
---|
| 542 | throw SemanticError( "invalid type specifier \"void\" in type: ", td ); |
---|
[b87a5ed] | 543 | } else { |
---|
[413ad05] | 544 | return new VoidType( buildQualifiers( td ) ); |
---|
[68cd1ce] | 545 | } // if |
---|
[b87a5ed] | 546 | } else { |
---|
| 547 | ret = kindMap[ *i ]; |
---|
[68cd1ce] | 548 | } // if |
---|
[c8ffe20b] | 549 | } else { |
---|
[b87a5ed] | 550 | switch ( *i ) { |
---|
| 551 | case DeclarationNode::Float: |
---|
| 552 | if ( sawDouble ) { |
---|
[413ad05] | 553 | throw SemanticError( "invalid type specifier \"float\" in type: ", td ); |
---|
[b87a5ed] | 554 | } else { |
---|
| 555 | switch ( ret ) { |
---|
| 556 | case BasicType::DoubleComplex: |
---|
| 557 | ret = BasicType::FloatComplex; |
---|
| 558 | break; |
---|
| 559 | case BasicType::DoubleImaginary: |
---|
| 560 | ret = BasicType::FloatImaginary; |
---|
| 561 | break; |
---|
| 562 | default: |
---|
[413ad05] | 563 | throw SemanticError( "invalid type specifier \"float\" in type: ", td ); |
---|
[68cd1ce] | 564 | } // switch |
---|
| 565 | } // if |
---|
[b87a5ed] | 566 | break; |
---|
| 567 | case DeclarationNode::Double: |
---|
| 568 | if ( sawDouble ) { |
---|
[413ad05] | 569 | throw SemanticError( "duplicate type specifier \"double\" in type: ", td ); |
---|
[b87a5ed] | 570 | } else { |
---|
| 571 | switch ( ret ) { |
---|
| 572 | case BasicType::DoubleComplex: |
---|
| 573 | case BasicType::DoubleImaginary: |
---|
| 574 | break; |
---|
| 575 | default: |
---|
[413ad05] | 576 | throw SemanticError( "invalid type specifier \"double\" in type: ", td ); |
---|
[68cd1ce] | 577 | } // switch |
---|
| 578 | } // if |
---|
[b87a5ed] | 579 | break; |
---|
| 580 | case DeclarationNode::Complex: |
---|
| 581 | switch ( ret ) { |
---|
| 582 | case BasicType::Float: |
---|
| 583 | ret = BasicType::FloatComplex; |
---|
| 584 | break; |
---|
| 585 | case BasicType::Double: |
---|
| 586 | ret = BasicType::DoubleComplex; |
---|
| 587 | break; |
---|
| 588 | default: |
---|
[413ad05] | 589 | throw SemanticError( "invalid type specifier \"_Complex\" in type: ", td ); |
---|
[68cd1ce] | 590 | } // switch |
---|
[b87a5ed] | 591 | break; |
---|
| 592 | case DeclarationNode::Imaginary: |
---|
| 593 | switch ( ret ) { |
---|
| 594 | case BasicType::Float: |
---|
| 595 | ret = BasicType::FloatImaginary; |
---|
| 596 | break; |
---|
| 597 | case BasicType::Double: |
---|
| 598 | ret = BasicType::DoubleImaginary; |
---|
| 599 | break; |
---|
| 600 | default: |
---|
[413ad05] | 601 | throw SemanticError( "invalid type specifier \"_Imaginary\" in type: ", td ); |
---|
[68cd1ce] | 602 | } // switch |
---|
[b87a5ed] | 603 | break; |
---|
| 604 | default: |
---|
[413ad05] | 605 | throw SemanticError( std::string( "invalid type specifier \"" ) + DeclarationNode::basicTypeName[ *i ] + "\" in type: ", td ); |
---|
[68cd1ce] | 606 | } // switch |
---|
| 607 | } // if |
---|
[b87a5ed] | 608 | if ( *i == DeclarationNode::Double ) { |
---|
| 609 | sawDouble = true; |
---|
[68cd1ce] | 610 | } // if |
---|
| 611 | } // for |
---|
[b87a5ed] | 612 | |
---|
[413ad05] | 613 | for ( std::list< DeclarationNode::Modifier >::const_iterator i = td->basic->modifiers.begin(); i != td->basic->modifiers.end(); ++i ) { |
---|
[b87a5ed] | 614 | switch ( *i ) { |
---|
| 615 | case DeclarationNode::Long: |
---|
[a32b204] | 616 | if ( ! init ) { |
---|
[b87a5ed] | 617 | init = true; |
---|
| 618 | ret = BasicType::LongSignedInt; |
---|
| 619 | } else { |
---|
| 620 | switch ( ret ) { |
---|
| 621 | case BasicType::SignedInt: |
---|
| 622 | ret = BasicType::LongSignedInt; |
---|
| 623 | break; |
---|
| 624 | case BasicType::UnsignedInt: |
---|
| 625 | ret = BasicType::LongUnsignedInt; |
---|
| 626 | break; |
---|
| 627 | case BasicType::LongSignedInt: |
---|
| 628 | ret = BasicType::LongLongSignedInt; |
---|
| 629 | break; |
---|
| 630 | case BasicType::LongUnsignedInt: |
---|
| 631 | ret = BasicType::LongLongUnsignedInt; |
---|
| 632 | break; |
---|
| 633 | case BasicType::Double: |
---|
| 634 | ret = BasicType::LongDouble; |
---|
| 635 | break; |
---|
| 636 | case BasicType::DoubleComplex: |
---|
| 637 | ret = BasicType::LongDoubleComplex; |
---|
| 638 | break; |
---|
| 639 | case BasicType::DoubleImaginary: |
---|
| 640 | ret = BasicType::LongDoubleImaginary; |
---|
| 641 | break; |
---|
| 642 | default: |
---|
[413ad05] | 643 | throw SemanticError( "invalid type modifier \"long\" in type: ", td ); |
---|
[68cd1ce] | 644 | } // switch |
---|
| 645 | } // if |
---|
[c8ffe20b] | 646 | break; |
---|
[b87a5ed] | 647 | case DeclarationNode::Short: |
---|
[a32b204] | 648 | if ( ! init ) { |
---|
[b87a5ed] | 649 | init = true; |
---|
| 650 | ret = BasicType::ShortSignedInt; |
---|
| 651 | } else { |
---|
| 652 | switch ( ret ) { |
---|
| 653 | case BasicType::SignedInt: |
---|
| 654 | ret = BasicType::ShortSignedInt; |
---|
| 655 | break; |
---|
| 656 | case BasicType::UnsignedInt: |
---|
| 657 | ret = BasicType::ShortUnsignedInt; |
---|
| 658 | break; |
---|
| 659 | default: |
---|
[413ad05] | 660 | throw SemanticError( "invalid type modifier \"short\" in type: ", td ); |
---|
[68cd1ce] | 661 | } // switch |
---|
| 662 | } // if |
---|
[c8ffe20b] | 663 | break; |
---|
[b87a5ed] | 664 | case DeclarationNode::Signed: |
---|
[a32b204] | 665 | if ( ! init ) { |
---|
[b87a5ed] | 666 | init = true; |
---|
| 667 | ret = BasicType::SignedInt; |
---|
| 668 | } else if ( sawSigned ) { |
---|
[413ad05] | 669 | throw SemanticError( "duplicate type modifer \"signed\" in type: ", td ); |
---|
[b87a5ed] | 670 | } else { |
---|
| 671 | switch ( ret ) { |
---|
[59db689] | 672 | case BasicType::LongLongSignedInt: |
---|
[b87a5ed] | 673 | ret = BasicType::LongLongUnsignedInt; |
---|
| 674 | break; |
---|
| 675 | case BasicType::LongSignedInt: |
---|
| 676 | ret = BasicType::LongUnsignedInt; |
---|
| 677 | break; |
---|
| 678 | case BasicType::SignedInt: |
---|
| 679 | case BasicType::ShortSignedInt: |
---|
| 680 | break; |
---|
| 681 | case BasicType::Char: |
---|
| 682 | ret = BasicType::SignedChar; |
---|
| 683 | break; |
---|
| 684 | default: |
---|
[413ad05] | 685 | throw SemanticError( "invalid type modifer \"signed\" in type: ", td ); |
---|
[68cd1ce] | 686 | } // switch |
---|
| 687 | } // if |
---|
[b87a5ed] | 688 | break; |
---|
| 689 | case DeclarationNode::Unsigned: |
---|
[a32b204] | 690 | if ( ! init ) { |
---|
[b87a5ed] | 691 | init = true; |
---|
| 692 | ret = BasicType::UnsignedInt; |
---|
| 693 | } else if ( sawSigned ) { |
---|
[413ad05] | 694 | throw SemanticError( "invalid type modifer \"unsigned\" in type: ", td ); |
---|
[b87a5ed] | 695 | } else { |
---|
| 696 | switch ( ret ) { |
---|
[59db689] | 697 | case BasicType::LongLongSignedInt: |
---|
[b87a5ed] | 698 | ret = BasicType::LongLongUnsignedInt; |
---|
| 699 | break; |
---|
| 700 | case BasicType::LongSignedInt: |
---|
| 701 | ret = BasicType::LongUnsignedInt; |
---|
| 702 | break; |
---|
| 703 | case BasicType::SignedInt: |
---|
| 704 | ret = BasicType::UnsignedInt; |
---|
| 705 | break; |
---|
| 706 | case BasicType::ShortSignedInt: |
---|
| 707 | ret = BasicType::ShortUnsignedInt; |
---|
| 708 | break; |
---|
| 709 | case BasicType::Char: |
---|
| 710 | ret = BasicType::UnsignedChar; |
---|
| 711 | break; |
---|
| 712 | default: |
---|
[413ad05] | 713 | throw SemanticError( "invalid type modifer \"unsigned\" in type: ", td ); |
---|
[68cd1ce] | 714 | } // switch |
---|
| 715 | } // if |
---|
[c8ffe20b] | 716 | break; |
---|
[68cd1ce] | 717 | } // switch |
---|
[b87a5ed] | 718 | |
---|
| 719 | if ( *i == DeclarationNode::Signed ) { |
---|
| 720 | sawSigned = true; |
---|
[68cd1ce] | 721 | } // if |
---|
| 722 | } // for |
---|
[51b7345] | 723 | |
---|
[413ad05] | 724 | BasicType * bt; |
---|
[a32b204] | 725 | if ( ! init ) { |
---|
[413ad05] | 726 | bt = new BasicType( buildQualifiers( td ), BasicType::SignedInt ); |
---|
[b87a5ed] | 727 | } else { |
---|
[413ad05] | 728 | bt = new BasicType( buildQualifiers( td ), ret ); |
---|
[68cd1ce] | 729 | } // if |
---|
[413ad05] | 730 | buildForall( td->forall, bt->get_forall() ); |
---|
[b87a5ed] | 731 | return bt; |
---|
[413ad05] | 732 | } // buildBasicType |
---|
[51b7345] | 733 | |
---|
[413ad05] | 734 | PointerType * buildPointer( const TypeData * td ) { |
---|
| 735 | PointerType * pt; |
---|
| 736 | if ( td->base ) { |
---|
| 737 | pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) ); |
---|
[b87a5ed] | 738 | } else { |
---|
[413ad05] | 739 | pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
[68cd1ce] | 740 | } // if |
---|
[413ad05] | 741 | buildForall( td->forall, pt->get_forall() ); |
---|
[b87a5ed] | 742 | return pt; |
---|
[413ad05] | 743 | } // buildPointer |
---|
[51b7345] | 744 | |
---|
[413ad05] | 745 | ArrayType * buildArray( const TypeData * td ) { |
---|
| 746 | ArrayType * at; |
---|
| 747 | if ( td->base ) { |
---|
| 748 | at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array->dimension ), |
---|
| 749 | td->array->isVarLen, td->array->isStatic ); |
---|
[b87a5ed] | 750 | } else { |
---|
[413ad05] | 751 | at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ), |
---|
| 752 | maybeBuild< Expression >( td->array->dimension ), td->array->isVarLen, td->array->isStatic ); |
---|
[68cd1ce] | 753 | } // if |
---|
[413ad05] | 754 | buildForall( td->forall, at->get_forall() ); |
---|
[b87a5ed] | 755 | return at; |
---|
[413ad05] | 756 | } // buildPointer |
---|
[51b7345] | 757 | |
---|
[413ad05] | 758 | AggregateDecl * buildAggregate( const TypeData * td ) { |
---|
| 759 | assert( td->kind == TypeData::Aggregate ); |
---|
| 760 | AggregateDecl * at; |
---|
| 761 | switch ( td->aggregate->kind ) { |
---|
[b87a5ed] | 762 | case DeclarationNode::Struct: |
---|
[413ad05] | 763 | at = new StructDecl( td->aggregate->name ); |
---|
| 764 | buildForall( td->aggregate->params, at->get_parameters() ); |
---|
[b87a5ed] | 765 | break; |
---|
| 766 | case DeclarationNode::Union: |
---|
[413ad05] | 767 | at = new UnionDecl( td->aggregate->name ); |
---|
| 768 | buildForall( td->aggregate->params, at->get_parameters() ); |
---|
[b87a5ed] | 769 | break; |
---|
[4040425] | 770 | case DeclarationNode::Trait: |
---|
[413ad05] | 771 | at = new TraitDecl( td->aggregate->name ); |
---|
| 772 | buildList( td->aggregate->params, at->get_parameters() ); |
---|
[b87a5ed] | 773 | break; |
---|
| 774 | default: |
---|
| 775 | assert( false ); |
---|
[68cd1ce] | 776 | } // switch |
---|
[4e06c1e] | 777 | |
---|
[413ad05] | 778 | buildList( td->aggregate->fields, at->get_members() ); |
---|
| 779 | at->set_body( td->aggregate->body ); |
---|
[b87a5ed] | 780 | |
---|
| 781 | return at; |
---|
[413ad05] | 782 | } // buildAggregate |
---|
[51b7345] | 783 | |
---|
[413ad05] | 784 | ReferenceToType * buildAggInst( const TypeData * td ) { |
---|
| 785 | assert( td->kind == TypeData::AggregateInst ); |
---|
[b87a5ed] | 786 | |
---|
[413ad05] | 787 | ReferenceToType * ret; |
---|
| 788 | if ( td->aggInst->aggregate->kind == TypeData::Enum ) { |
---|
| 789 | ret = new EnumInstType( buildQualifiers( td ), td->aggInst->aggregate->enumeration->name ); |
---|
[b87a5ed] | 790 | } else { |
---|
[413ad05] | 791 | assert( td->aggInst->aggregate->kind == TypeData::Aggregate ); |
---|
| 792 | switch ( td->aggInst->aggregate->aggregate->kind ) { |
---|
[b87a5ed] | 793 | case DeclarationNode::Struct: |
---|
[413ad05] | 794 | ret = new StructInstType( buildQualifiers( td ), td->aggInst->aggregate->aggregate->name ); |
---|
[b87a5ed] | 795 | break; |
---|
| 796 | case DeclarationNode::Union: |
---|
[413ad05] | 797 | ret = new UnionInstType( buildQualifiers( td ), td->aggInst->aggregate->aggregate->name ); |
---|
[b87a5ed] | 798 | break; |
---|
[4040425] | 799 | case DeclarationNode::Trait: |
---|
[413ad05] | 800 | ret = new TraitInstType( buildQualifiers( td ), td->aggInst->aggregate->aggregate->name ); |
---|
[b87a5ed] | 801 | break; |
---|
| 802 | default: |
---|
| 803 | assert( false ); |
---|
[68cd1ce] | 804 | } // switch |
---|
| 805 | } // if |
---|
[413ad05] | 806 | buildList( td->aggInst->params, ret->get_parameters() ); |
---|
| 807 | buildForall( td->forall, ret->get_forall() ); |
---|
[b87a5ed] | 808 | return ret; |
---|
[413ad05] | 809 | } // buildAggInst |
---|
| 810 | |
---|
| 811 | NamedTypeDecl * buildSymbolic( const TypeData * td, const std::string & name, DeclarationNode::StorageClass sc ) { |
---|
| 812 | assert( td->kind == TypeData::Symbolic ); |
---|
| 813 | NamedTypeDecl * ret; |
---|
| 814 | assert( td->base ); |
---|
| 815 | if ( td->symbolic->isTypedef ) { |
---|
| 816 | ret = new TypedefDecl( name, sc, typebuild( td->base ) ); |
---|
[b87a5ed] | 817 | } else { |
---|
[413ad05] | 818 | ret = new TypeDecl( name, sc, typebuild( td->base ), TypeDecl::Any ); |
---|
[68cd1ce] | 819 | } // if |
---|
[413ad05] | 820 | buildList( td->symbolic->params, ret->get_parameters() ); |
---|
| 821 | buildList( td->symbolic->assertions, ret->get_assertions() ); |
---|
[b87a5ed] | 822 | return ret; |
---|
[413ad05] | 823 | } // buildSymbolic |
---|
[51b7345] | 824 | |
---|
[413ad05] | 825 | TypeDecl * buildVariable( const TypeData * td ) { |
---|
| 826 | assert( td->kind == TypeData::Variable ); |
---|
[b87a5ed] | 827 | static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype }; |
---|
[51b7345] | 828 | |
---|
[413ad05] | 829 | TypeDecl * ret = new TypeDecl( td->variable->name, DeclarationNode::NoStorageClass, 0, kindMap[ td->variable->tyClass ] ); |
---|
| 830 | buildList( td->variable->assertions, ret->get_assertions() ); |
---|
[b87a5ed] | 831 | return ret; |
---|
[413ad05] | 832 | } // buildSymbolic |
---|
[51b7345] | 833 | |
---|
[413ad05] | 834 | EnumDecl * buildEnum( const TypeData * td ) { |
---|
| 835 | assert( td->kind == TypeData::Enum ); |
---|
| 836 | EnumDecl * ret = new EnumDecl( td->enumeration->name ); |
---|
| 837 | buildList( td->enumeration->constants, ret->get_members() ); |
---|
[90c3b1c] | 838 | std::list< Declaration * >::iterator members = ret->get_members().begin(); |
---|
[413ad05] | 839 | for ( const DeclarationNode * cur = td->enumeration-> constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) { |
---|
[4f147cc] | 840 | if ( cur->has_enumeratorValue() ) { |
---|
[413ad05] | 841 | ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members); |
---|
[4f147cc] | 842 | member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ), std::list< Expression * >() ) ); |
---|
[90c3b1c] | 843 | } // if |
---|
| 844 | } // for |
---|
[b87a5ed] | 845 | return ret; |
---|
[413ad05] | 846 | } // buildEnum |
---|
[51b7345] | 847 | |
---|
[413ad05] | 848 | TypeInstType * buildSymbolicInst( const TypeData * td ) { |
---|
| 849 | assert( td->kind == TypeData::SymbolicInst ); |
---|
| 850 | TypeInstType * ret = new TypeInstType( buildQualifiers( td ), td->symbolic->name, false ); |
---|
| 851 | buildList( td->symbolic->actuals, ret->get_parameters() ); |
---|
| 852 | buildForall( td->forall, ret->get_forall() ); |
---|
[b87a5ed] | 853 | return ret; |
---|
[413ad05] | 854 | } // buildSymbolicInst |
---|
[51b7345] | 855 | |
---|
[413ad05] | 856 | TupleType * buildTuple( const TypeData * td ) { |
---|
| 857 | assert( td->kind == TypeData::Tuple ); |
---|
| 858 | TupleType * ret = new TupleType( buildQualifiers( td ) ); |
---|
| 859 | buildTypeList( td->tuple->members, ret->get_types() ); |
---|
| 860 | buildForall( td->forall, ret->get_forall() ); |
---|
[b87a5ed] | 861 | return ret; |
---|
[413ad05] | 862 | } // buildTuple |
---|
| 863 | |
---|
| 864 | TypeofType * buildTypeof( const TypeData * td ) { |
---|
| 865 | assert( td->kind == TypeData::Typeof ); |
---|
| 866 | assert( td->typeexpr ); |
---|
| 867 | assert( td->typeexpr->expr ); |
---|
| 868 | return new TypeofType( buildQualifiers( td ), td->typeexpr->expr->build() ); |
---|
| 869 | } // buildTypeof |
---|
| 870 | |
---|
| 871 | AttrType * buildAttr( const TypeData * td ) { |
---|
| 872 | assert( td->kind == TypeData::Attr ); |
---|
| 873 | assert( td->attr ); |
---|
| 874 | AttrType * ret; |
---|
| 875 | if ( td->attr->expr ) { |
---|
| 876 | ret = new AttrType( buildQualifiers( td ), td->attr->name, td->attr->expr->build() ); |
---|
[b87a5ed] | 877 | } else { |
---|
[413ad05] | 878 | assert( td->attr->type ); |
---|
| 879 | ret = new AttrType( buildQualifiers( td ), td->attr->name, td->attr->type->buildType() ); |
---|
[68cd1ce] | 880 | } // if |
---|
[b87a5ed] | 881 | return ret; |
---|
[413ad05] | 882 | } // buildAttr |
---|
| 883 | |
---|
| 884 | Declaration * buildDecl( const TypeData * td, std::string name, DeclarationNode::StorageClass sc, Expression * bitfieldWidth, bool isInline, bool isNoreturn, LinkageSpec::Spec linkage, Initializer * init ) { |
---|
| 885 | if ( td->kind == TypeData::Function ) { |
---|
| 886 | FunctionDecl * decl; |
---|
| 887 | if ( td->function->hasBody ) { |
---|
| 888 | if ( td->function->body ) { |
---|
| 889 | Statement * stmt = td->function->body->build(); |
---|
| 890 | CompoundStmt * body = dynamic_cast< CompoundStmt* >( stmt ); |
---|
| 891 | assert( body ); |
---|
| 892 | decl = new FunctionDecl( name, sc, linkage, buildFunction( td ), body, isInline, isNoreturn ); |
---|
| 893 | } else { |
---|
| 894 | // std::list< Label > ls; |
---|
| 895 | decl = new FunctionDecl( name, sc, linkage, buildFunction( td ), new CompoundStmt( std::list< Label >() ), isInline, isNoreturn ); |
---|
| 896 | } // if |
---|
| 897 | } else { |
---|
| 898 | decl = new FunctionDecl( name, sc, linkage, buildFunction( td ), 0, isInline, isNoreturn ); |
---|
| 899 | } // if |
---|
| 900 | for ( DeclarationNode * cur = td->function->idList; cur != 0; cur = dynamic_cast< DeclarationNode* >( cur->get_next() ) ) { |
---|
| 901 | if ( cur->get_name() != "" ) { |
---|
| 902 | decl->get_oldIdents().insert( decl->get_oldIdents().end(), cur->get_name() ); |
---|
| 903 | } // if |
---|
| 904 | } // for |
---|
| 905 | buildList( td->function->oldDeclList, decl->get_oldDecls() ); |
---|
| 906 | return decl; |
---|
| 907 | } else if ( td->kind == TypeData::Aggregate ) { |
---|
| 908 | return buildAggregate( td ); |
---|
| 909 | } else if ( td->kind == TypeData::Enum ) { |
---|
| 910 | return buildEnum( td ); |
---|
| 911 | } else if ( td->kind == TypeData::Symbolic ) { |
---|
| 912 | return buildSymbolic( td, name, sc ); |
---|
| 913 | } else if ( td->kind == TypeData::Variable ) { |
---|
| 914 | return buildVariable( td ); |
---|
| 915 | } else { |
---|
| 916 | return new ObjectDecl( name, sc, linkage, bitfieldWidth, typebuild( td ), init, std::list< Attribute * >(), isInline, isNoreturn ); |
---|
| 917 | } // if |
---|
| 918 | return 0; |
---|
| 919 | } // buildDecl |
---|
| 920 | |
---|
| 921 | FunctionType * buildFunction( const TypeData * td ) { |
---|
| 922 | assert( td->kind == TypeData::Function ); |
---|
| 923 | bool hasEllipsis = td->function->params ? td->function->params->get_hasEllipsis() : true; |
---|
| 924 | if ( ! td->function->params ) hasEllipsis = ! td->function->newStyle; |
---|
| 925 | FunctionType * ft = new FunctionType( buildQualifiers( td ), hasEllipsis ); |
---|
| 926 | buildList( td->function->params, ft->get_parameters() ); |
---|
| 927 | buildForall( td->forall, ft->get_forall() ); |
---|
| 928 | if ( td->base ) { |
---|
| 929 | switch ( td->base->kind ) { |
---|
| 930 | case TypeData::Tuple: |
---|
| 931 | buildList( td->base->tuple->members, ft->get_returnVals() ); |
---|
| 932 | break; |
---|
| 933 | default: |
---|
| 934 | ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType* >( buildDecl( td->base, "", DeclarationNode::NoStorageClass, 0, false, false, LinkageSpec::Cforall ) ) ); |
---|
| 935 | } // switch |
---|
| 936 | } else { |
---|
| 937 | ft->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 ) ); |
---|
| 938 | } // if |
---|
| 939 | return ft; |
---|
| 940 | } // buildFunction |
---|
[b87a5ed] | 941 | |
---|
| 942 | // Local Variables: // |
---|
| 943 | // tab-width: 4 // |
---|
| 944 | // mode: c++ // |
---|
| 945 | // compile-command: "make install" // |
---|
| 946 | // End: // |
---|