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