[e4d829b] | 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 | //
|
---|
| 7 | // CurrentObject.h --
|
---|
| 8 | //
|
---|
| 9 | // Author : Rob Schluntz
|
---|
| 10 | // Created On : Tue Jun 13 15:28:32 2017
|
---|
| 11 | // Last Modified By : Rob Schluntz
|
---|
| 12 | // Last Modified On : Tue Jun 13 15:28:44 2017
|
---|
| 13 | // Update Count : 2
|
---|
| 14 | //
|
---|
| 15 |
|
---|
| 16 | #include <stack>
|
---|
| 17 | #include <iostream>
|
---|
| 18 |
|
---|
| 19 | #include "CurrentObject.h"
|
---|
| 20 |
|
---|
| 21 | #include "SynTree/Declaration.h"
|
---|
| 22 | #include "SynTree/Initializer.h"
|
---|
| 23 | #include "SynTree/Type.h"
|
---|
[62423350] | 24 | #include "SynTree/TypeSubstitution.h"
|
---|
[e4d829b] | 25 |
|
---|
| 26 | #if 0
|
---|
| 27 | #define PRINT(x) x
|
---|
| 28 | #else
|
---|
| 29 | #define PRINT(x)
|
---|
| 30 | #endif
|
---|
| 31 |
|
---|
| 32 | namespace ResolvExpr {
|
---|
| 33 | long long int getConstValue( ConstantExpr * constExpr ) {
|
---|
| 34 | if ( BasicType * basicType = dynamic_cast< BasicType * >( constExpr->get_result() ) ) {
|
---|
[62423350] | 35 | if ( basicType->isInteger() ) {
|
---|
| 36 | return constExpr->get_constant()->get_ival();
|
---|
[e4d829b] | 37 | } else {
|
---|
[62423350] | 38 | assertf( false, "Non-integer constant expression in getConstValue", toString( constExpr ).c_str() ); // xxx - might be semantic error
|
---|
[e4d829b] | 39 | }
|
---|
[62423350] | 40 | } else if ( dynamic_cast< OneType * >( constExpr->get_result() ) ) {
|
---|
| 41 | return 1;
|
---|
| 42 | } else if ( dynamic_cast< ZeroType * >( constExpr->get_result() ) ) {
|
---|
| 43 | return 0;
|
---|
[e4d829b] | 44 | } else {
|
---|
[62423350] | 45 | assertf( false, "unhandled type on getConstValue %s", toString( constExpr->get_result() ).c_str() ); // xxx - might be semantic error
|
---|
[e4d829b] | 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | struct Indenter {
|
---|
| 50 | static const int amt = 2;
|
---|
| 51 | unsigned int indent = 0;
|
---|
| 52 |
|
---|
| 53 | Indenter & operator+=(int nlevels) { indent += amt*nlevels; return *this; }
|
---|
| 54 | Indenter & operator-=(int nlevels) { indent -= amt*nlevels; return *this; }
|
---|
| 55 | Indenter operator+(int nlevels) { Indenter indenter = *this; return indenter += nlevels; }
|
---|
| 56 | Indenter operator-(int nlevels) { Indenter indenter = *this; return indenter -= nlevels; }
|
---|
| 57 | Indenter & operator++() { return *this += 1; }
|
---|
| 58 | Indenter & operator--() { return *this -= 1; }
|
---|
| 59 | };
|
---|
| 60 | std::ostream & operator<<( std::ostream & out, Indenter & indent ) {
|
---|
| 61 | return out << std::string(indent.indent, ' ');
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[62423350] | 64 | template< typename AggrInst >
|
---|
| 65 | TypeSubstitution makeGenericSubstitution( AggrInst * inst ) {
|
---|
| 66 | assert( inst );
|
---|
| 67 | assert( inst->get_baseParameters() );
|
---|
| 68 | std::list< TypeDecl * > baseParams = *inst->get_baseParameters();
|
---|
| 69 | std::list< Expression * > typeSubs = inst->get_parameters();
|
---|
| 70 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
|
---|
| 71 | return subs;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | TypeSubstitution makeGenericSubstitution( Type * type ) {
|
---|
| 75 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) {
|
---|
| 76 | return makeGenericSubstitution( inst );
|
---|
| 77 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) {
|
---|
| 78 | return makeGenericSubstitution( inst );
|
---|
| 79 | } else {
|
---|
| 80 | return TypeSubstitution();
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[e4d829b] | 84 | class MemberIterator {
|
---|
| 85 | public:
|
---|
| 86 | virtual ~MemberIterator() {}
|
---|
| 87 |
|
---|
| 88 | virtual void setPosition( std::list< Expression * > & designators ) = 0;
|
---|
| 89 | virtual std::list<InitAlternative> operator*() const = 0;
|
---|
| 90 | virtual operator bool() const = 0;
|
---|
| 91 | virtual MemberIterator & bigStep() = 0;
|
---|
| 92 | virtual MemberIterator & smallStep() = 0;
|
---|
| 93 | virtual Type * getType() = 0;
|
---|
| 94 | virtual Type * getNext() = 0;
|
---|
| 95 |
|
---|
| 96 | virtual void print( std::ostream & out, Indenter indent ) const = 0;
|
---|
| 97 |
|
---|
| 98 | virtual std::list<InitAlternative> first() const = 0; // should be protected
|
---|
| 99 | };
|
---|
| 100 |
|
---|
| 101 | std::ostream & operator<<(std::ostream & out, const MemberIterator & it) {
|
---|
| 102 | Indenter indenter;
|
---|
| 103 | it.print( out, indenter );
|
---|
| 104 | return out;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /// create a new MemberIterator that traverses a type correctly
|
---|
| 108 | MemberIterator * createMemberIterator( Type * type );
|
---|
| 109 |
|
---|
| 110 | /// iterates "other" types, e.g. basic types, pointer types, etc. which do not change at list initializer entry
|
---|
| 111 | class SimpleIterator : public MemberIterator {
|
---|
| 112 | public:
|
---|
| 113 | SimpleIterator( Type * type ) : type( type ) {}
|
---|
| 114 |
|
---|
| 115 | virtual void setPosition( std::list< Expression * > & designators ) {
|
---|
| 116 | assertf( designators.empty(), "simple iterator given non-empty designator..." ); // xxx - might be semantic error
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | virtual std::list<InitAlternative> operator*() const { return first(); }
|
---|
| 120 | virtual operator bool() const { return type; }
|
---|
| 121 |
|
---|
| 122 | // big step is the same as small step
|
---|
| 123 | virtual MemberIterator & bigStep() { return smallStep(); }
|
---|
| 124 | virtual MemberIterator & smallStep() {
|
---|
| 125 | type = nullptr; // type is nullified on increment since SimpleIterators do not have members
|
---|
| 126 | return *this;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[9ff56e7] | 129 | virtual void print( std::ostream & out, __attribute__((unused)) Indenter indent ) const {
|
---|
[e4d829b] | 130 | out << "SimpleIterator(" << type << ")";
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | virtual Type * getType() { return type; }
|
---|
| 134 | virtual Type * getNext() { return type; }
|
---|
| 135 |
|
---|
| 136 | protected:
|
---|
| 137 | virtual std::list<InitAlternative> first() const {
|
---|
| 138 | if ( type ) return std::list<InitAlternative>{ { type->clone(), new Designation( {} ) } };
|
---|
| 139 | else return std::list<InitAlternative>{};
|
---|
| 140 | }
|
---|
| 141 | private:
|
---|
| 142 | Type * type = nullptr;
|
---|
| 143 | };
|
---|
| 144 |
|
---|
| 145 | class ArrayIterator : public MemberIterator {
|
---|
| 146 | public:
|
---|
| 147 | ArrayIterator( ArrayType * at ) : array( at ) {
|
---|
| 148 | PRINT( std::cerr << "Creating array iterator: " << at << std::endl; )
|
---|
| 149 | base = at->get_base();
|
---|
| 150 | memberIter = createMemberIterator( base );
|
---|
| 151 | setSize( at->get_dimension() );
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | ~ArrayIterator() {
|
---|
| 155 | delete memberIter;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | private:
|
---|
| 159 | void setSize( Expression * expr ) {
|
---|
| 160 | if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
|
---|
[62423350] | 161 | size = getConstValue( constExpr );
|
---|
[e4d829b] | 162 | PRINT( std::cerr << "array type with size: " << size << std::endl; )
|
---|
| 163 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
|
---|
[62423350] | 164 | setSize( castExpr->get_arg() ); // xxx - need to perform the conversion specified by the cast
|
---|
[e4d829b] | 165 | } else {
|
---|
[62423350] | 166 | assertf( false, "unhandled expression in setSize: %s", toString( expr ).c_str() ); // xxx - if not a constant expression, it's not simple to determine how long the array actually is, which is necessary for initialization to be done correctly -- fix this
|
---|
[e4d829b] | 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | public:
|
---|
| 171 | void setPosition( Expression * expr ) {
|
---|
| 172 | // need to permit integer-constant-expressions, including: integer constants, enumeration constants, character constants, sizeof expressions, _Alignof expressions, cast expressions
|
---|
| 173 | if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
|
---|
| 174 | index = getConstValue( constExpr );
|
---|
| 175 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
|
---|
| 176 | setPosition( castExpr->get_arg() );
|
---|
| 177 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) {
|
---|
| 178 | assertf( dynamic_cast<EnumInstType *> ( varExpr->get_result() ), "ArrayIterator given variable that isn't an enum constant", toString( expr ).c_str() );
|
---|
| 179 | index = 0; // xxx - get actual value of enum constant
|
---|
| 180 | } else if ( dynamic_cast< SizeofExpr * >( expr ) || dynamic_cast< AlignofExpr * >( expr ) ) {
|
---|
| 181 | index = 0; // xxx - get actual sizeof/alignof value?
|
---|
| 182 | } else {
|
---|
| 183 | assertf( false, "bad designator given to ArrayIterator: %s", toString( expr ).c_str() );
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | virtual void setPosition( std::list< Expression * > & designators ) {
|
---|
| 188 | if ( ! designators.empty() ) {
|
---|
| 189 | setPosition( designators.front() );
|
---|
| 190 | designators.pop_front();
|
---|
| 191 | memberIter->setPosition( designators );
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | virtual std::list<InitAlternative> operator*() const {
|
---|
| 196 | return first();
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | virtual operator bool() const { return index < size; }
|
---|
| 200 |
|
---|
| 201 | virtual MemberIterator & bigStep() {
|
---|
| 202 | PRINT( std::cerr << "bigStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; )
|
---|
| 203 | ++index;
|
---|
| 204 | delete memberIter;
|
---|
| 205 | if ( index < size ) memberIter = createMemberIterator( base );
|
---|
| 206 | else memberIter = nullptr;
|
---|
| 207 | return *this;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | virtual MemberIterator & smallStep() {
|
---|
| 211 | PRINT( std::cerr << "smallStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; )
|
---|
| 212 | if ( memberIter ) {
|
---|
| 213 | PRINT( std::cerr << "has member iter: " << *memberIter << std::endl; )
|
---|
| 214 | memberIter->smallStep();
|
---|
| 215 | if ( *memberIter ) {
|
---|
| 216 | PRINT( std::cerr << "has valid member iter" << std::endl; )
|
---|
| 217 | return *this;
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 | return bigStep();
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | virtual Type * getType() { return array; }
|
---|
| 224 | virtual Type * getNext() { return base; }
|
---|
| 225 |
|
---|
| 226 | virtual std::list<InitAlternative> first() const {
|
---|
| 227 | PRINT( std::cerr << "first in ArrayIterator (" << index << "/" << size << ")" << std::endl; )
|
---|
| 228 | if ( memberIter && *memberIter ) {
|
---|
| 229 | std::list<InitAlternative> ret = memberIter->first();
|
---|
| 230 | for ( InitAlternative & alt : ret ) {
|
---|
| 231 | alt.designation->get_designators().push_front( new ConstantExpr( Constant::from_ulong( index ) ) );
|
---|
| 232 | }
|
---|
| 233 | return ret;
|
---|
| 234 | }
|
---|
| 235 | return std::list<InitAlternative>();
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | virtual void print( std::ostream & out, Indenter indent ) const {
|
---|
| 239 | out << "ArrayIterator(Array of " << base << ")";
|
---|
| 240 | if ( memberIter ) {
|
---|
| 241 | Indenter childIndent = indent+1;
|
---|
| 242 | out << std::endl << childIndent;
|
---|
| 243 | memberIter->print( out, childIndent );
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | private:
|
---|
| 248 | ArrayType * array = nullptr;
|
---|
| 249 | Type * base = nullptr;
|
---|
| 250 | size_t index = 0;
|
---|
| 251 | size_t size = 0;
|
---|
| 252 | MemberIterator * memberIter = nullptr;
|
---|
| 253 | };
|
---|
| 254 |
|
---|
| 255 | class AggregateIterator : public MemberIterator {
|
---|
| 256 | public:
|
---|
[62423350] | 257 | typedef std::list<Declaration *> MemberList;
|
---|
| 258 | typedef MemberList::const_iterator iterator;
|
---|
| 259 | std::string kind = ""; // for debug
|
---|
| 260 | std::string name;
|
---|
| 261 | Type * inst = nullptr;
|
---|
| 262 | const MemberList & members;
|
---|
[e4d829b] | 263 | iterator curMember;
|
---|
[62423350] | 264 | bool atbegin = true; // false at first {small,big}Step -- this aggr type is only added to the possibilities at the beginning
|
---|
[e4d829b] | 265 | Type * curType = nullptr;
|
---|
| 266 | MemberIterator * memberIter = nullptr;
|
---|
[62423350] | 267 | mutable TypeSubstitution sub;
|
---|
[e4d829b] | 268 |
|
---|
[62423350] | 269 | AggregateIterator( const std::string & kind, const std::string & name, Type * inst, const MemberList & members ) : kind( kind ), name( name ), inst( inst ), members( members ), curMember( members.begin() ), sub( makeGenericSubstitution( inst ) ) {
|
---|
[e4d829b] | 270 | init();
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | virtual ~AggregateIterator() {
|
---|
| 274 | delete memberIter;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | bool init() {
|
---|
[62423350] | 278 | PRINT( std::cerr << "--init()--" << members.size() << std::endl; )
|
---|
| 279 | if ( curMember != members.end() ) {
|
---|
[e4d829b] | 280 | if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( *curMember ) ) {
|
---|
| 281 | PRINT( std::cerr << "incremented to field: " << field << std::endl; )
|
---|
| 282 | curType = field->get_type();
|
---|
| 283 | memberIter = createMemberIterator( curType );
|
---|
| 284 | return true;
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 | return false;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | virtual std::list<InitAlternative> operator*() const {
|
---|
| 291 | if (memberIter && *memberIter) {
|
---|
| 292 | std::list<InitAlternative> ret = memberIter->first();
|
---|
[62423350] | 293 | PRINT( std::cerr << "sub: " << sub << std::endl; )
|
---|
[e4d829b] | 294 | for ( InitAlternative & alt : ret ) {
|
---|
| 295 | PRINT( std::cerr << "iterating and adding designators" << std::endl; )
|
---|
| 296 | alt.designation->get_designators().push_front( new VariableExpr( safe_dynamic_cast< ObjectDecl * >( *curMember ) ) );
|
---|
[62423350] | 297 | // need to substitute for generic types, so that casts are to concrete types
|
---|
| 298 | PRINT( std::cerr << " type is: " << alt.type; )
|
---|
| 299 | sub.apply( alt.type ); // also apply to designation??
|
---|
| 300 | PRINT( std::cerr << " ==> " << alt.type << std::endl; )
|
---|
[e4d829b] | 301 | }
|
---|
| 302 | return ret;
|
---|
| 303 | }
|
---|
| 304 | return std::list<InitAlternative>();
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | virtual void setPosition( std::list< Expression * > & designators ) {
|
---|
| 308 | if ( ! designators.empty() ) {
|
---|
| 309 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( designators.front() ) ) {
|
---|
[62423350] | 310 | for ( curMember = members.begin(); curMember != members.end(); ++curMember ) {
|
---|
[e4d829b] | 311 | if ( *curMember == varExpr->get_var() ) {
|
---|
| 312 | designators.pop_front();
|
---|
| 313 | delete memberIter;
|
---|
| 314 | memberIter = createMemberIterator( varExpr->get_result() );
|
---|
| 315 | curType = varExpr->get_result();
|
---|
[62423350] | 316 | atbegin = curMember == members.begin() && designators.empty(); // xxx - is this the right condition for atbegin??
|
---|
[e4d829b] | 317 | memberIter->setPosition( designators );
|
---|
| 318 | return;
|
---|
| 319 | } // if
|
---|
| 320 | } // for
|
---|
[62423350] | 321 | assertf( false, "could not find member in %s: %s", kind.c_str(), toString( varExpr ).c_str() );
|
---|
[e4d829b] | 322 | } else {
|
---|
[62423350] | 323 | assertf( false, "bad designator given to %s: %s", kind.c_str(), toString( designators.front() ).c_str() );
|
---|
[e4d829b] | 324 | } // if
|
---|
| 325 | } // if
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | virtual MemberIterator & smallStep() {
|
---|
| 329 | PRINT( std::cerr << "smallStep in " << kind << std::endl; )
|
---|
| 330 | atbegin = false;
|
---|
| 331 | if ( memberIter ) {
|
---|
| 332 | PRINT( std::cerr << "has member iter, incrementing..." << std::endl; )
|
---|
| 333 | memberIter->smallStep();
|
---|
| 334 | if ( *memberIter ) {
|
---|
| 335 | PRINT( std::cerr << "success!" << std::endl; )
|
---|
| 336 | return *this;
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | return bigStep();
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | virtual Type * getType() { return inst; }
|
---|
| 343 | virtual Type * getNext() {
|
---|
| 344 | if ( memberIter && *memberIter ) return memberIter->getType(); // xxx - ??? recursive call???
|
---|
| 345 | return nullptr;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | virtual std::list<InitAlternative> first() const {
|
---|
| 349 | std::list<InitAlternative> ret;
|
---|
| 350 | PRINT( std::cerr << "first " << kind << std::endl; )
|
---|
| 351 | if ( memberIter && *memberIter ) { // might not need *memberIter??
|
---|
| 352 | PRINT( std::cerr << "adding children" << std::endl; )
|
---|
| 353 | ret = memberIter->first();
|
---|
| 354 | for ( InitAlternative & alt : ret ) {
|
---|
| 355 | PRINT( std::cerr << "iterating and adding designators" << std::endl; )
|
---|
| 356 | alt.designation->get_designators().push_front( new VariableExpr( safe_dynamic_cast< ObjectDecl * >( *curMember ) ) );
|
---|
| 357 | }
|
---|
| 358 | }
|
---|
| 359 | // if ( curMember == std::next( decl->get_members().begin(), 1 ) ) { // xxx - this never triggers because curMember is incremented immediately on construction
|
---|
| 360 | if ( atbegin ) { // xxx - this never triggers because curMember is incremented immediately on construction
|
---|
| 361 | // xxx - what about case of empty struct??
|
---|
| 362 | // only add self if at the very beginning of the structure
|
---|
| 363 | PRINT( std::cerr << "adding self" << std::endl; )
|
---|
| 364 | ret.push_front( { inst->clone(), new Designation( {} ) } );
|
---|
| 365 | }
|
---|
| 366 | return ret;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | virtual void print( std::ostream & out, Indenter indent ) const {
|
---|
[62423350] | 370 | out << kind << "(" << name << ")";
|
---|
[e4d829b] | 371 | if ( memberIter ) {
|
---|
| 372 | Indenter childIndent = indent+1;
|
---|
| 373 | out << std::endl << childIndent;
|
---|
| 374 | memberIter->print( out, childIndent );
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 | };
|
---|
| 378 |
|
---|
| 379 | class UnionIterator : public AggregateIterator {
|
---|
| 380 | public:
|
---|
[62423350] | 381 | UnionIterator( UnionInstType * inst ) : AggregateIterator( "UnionIterator", inst->get_name(), inst, inst->get_baseUnion()->get_members() ) {}
|
---|
[e4d829b] | 382 |
|
---|
| 383 | virtual operator bool() const { return (memberIter && *memberIter); }
|
---|
| 384 | virtual MemberIterator & bigStep() {
|
---|
| 385 | // unions only initialize one member
|
---|
| 386 | PRINT( std::cerr << "bigStep in " << kind << std::endl; )
|
---|
| 387 | atbegin = false;
|
---|
| 388 | delete memberIter;
|
---|
| 389 | memberIter = nullptr;
|
---|
| 390 | curType = nullptr;
|
---|
[62423350] | 391 | curMember = members.end();
|
---|
[e4d829b] | 392 | return *this;
|
---|
| 393 | }
|
---|
| 394 | virtual std::list<InitAlternative> first() const { return std::list<InitAlternative>{}; }
|
---|
| 395 | };
|
---|
| 396 |
|
---|
| 397 | class StructIterator : public AggregateIterator {
|
---|
| 398 | public:
|
---|
[62423350] | 399 | StructIterator( StructInstType * inst ) : AggregateIterator( "StructIterator", inst->get_name(), inst, inst->get_baseStruct()->get_members() ) {}
|
---|
| 400 |
|
---|
| 401 | virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); }
|
---|
| 402 |
|
---|
| 403 | virtual MemberIterator & bigStep() {
|
---|
| 404 | PRINT( std::cerr << "bigStep in " << kind << std::endl; )
|
---|
| 405 | atbegin = false;
|
---|
| 406 | delete memberIter;
|
---|
| 407 | memberIter = nullptr;
|
---|
| 408 | curType = nullptr;
|
---|
| 409 | for ( ; curMember != members.end(); ) {
|
---|
| 410 | ++curMember;
|
---|
| 411 | if ( init() ) {
|
---|
| 412 | return *this;
|
---|
| 413 | }
|
---|
| 414 | }
|
---|
| 415 | return *this;
|
---|
| 416 | }
|
---|
| 417 | };
|
---|
| 418 |
|
---|
| 419 | class TupleIterator : public AggregateIterator {
|
---|
| 420 | public:
|
---|
| 421 | TupleIterator( TupleType * inst ) : AggregateIterator( "TupleIterator", toString("Tuple", inst->size()), inst, inst->get_members() ) {}
|
---|
[e4d829b] | 422 |
|
---|
[62423350] | 423 | virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); }
|
---|
[e4d829b] | 424 |
|
---|
| 425 | virtual MemberIterator & bigStep() {
|
---|
| 426 | PRINT( std::cerr << "bigStep in " << kind << std::endl; )
|
---|
| 427 | atbegin = false;
|
---|
| 428 | delete memberIter;
|
---|
| 429 | memberIter = nullptr;
|
---|
| 430 | curType = nullptr;
|
---|
[62423350] | 431 | for ( ; curMember != members.end(); ) {
|
---|
[e4d829b] | 432 | ++curMember;
|
---|
| 433 | if ( init() ) {
|
---|
| 434 | return *this;
|
---|
| 435 | }
|
---|
| 436 | }
|
---|
| 437 | return *this;
|
---|
| 438 | }
|
---|
| 439 | };
|
---|
| 440 |
|
---|
| 441 | MemberIterator * createMemberIterator( Type * type ) {
|
---|
| 442 | if ( ReferenceToType * aggr = dynamic_cast< ReferenceToType * >( type ) ) {
|
---|
| 443 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( aggr ) ) {
|
---|
| 444 | return new StructIterator( sit );
|
---|
| 445 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( aggr ) ) {
|
---|
| 446 | return new UnionIterator( uit );
|
---|
| 447 | } else {
|
---|
[62423350] | 448 | assertf( dynamic_cast< TypeInstType * >( type ), "some other reftotype" );
|
---|
| 449 | return new SimpleIterator( type );
|
---|
[e4d829b] | 450 | }
|
---|
| 451 | } else if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
|
---|
| 452 | return new ArrayIterator( at );
|
---|
[62423350] | 453 | } else if ( TupleType * tt = dynamic_cast< TupleType * >( type ) ) {
|
---|
| 454 | return new TupleIterator( tt );
|
---|
[e4d829b] | 455 | } else {
|
---|
| 456 | return new SimpleIterator( type );
|
---|
| 457 | }
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | CurrentObject::CurrentObject() {}
|
---|
| 461 | CurrentObject::CurrentObject( Type * type ) {
|
---|
| 462 | objStack.push( new SimpleIterator( type ) );
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 |
|
---|
| 466 | void CurrentObject::setNext( Designation * designation ) {
|
---|
| 467 | assertf( ! objStack.empty(), "obj stack empty in setNext" );
|
---|
| 468 | PRINT( std::cerr << "____setNext" << designation << std::endl; )
|
---|
| 469 | objStack.top()->setPosition( designation->get_designators() );
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | Designation * CurrentObject::findNext( Designation * designation ) {
|
---|
| 473 | typedef std::list< Expression * > DesignatorChain;
|
---|
| 474 | PRINT( std::cerr << "___findNext" << std::endl; )
|
---|
| 475 | // find all the d's
|
---|
| 476 | std::list<DesignatorChain> desigAlts{ { } }, newDesigAlts;
|
---|
| 477 | std::list<Type *> curTypes { (objStack.top())->getType() }, newTypes;
|
---|
| 478 | for ( Expression * expr : designation->get_designators() ) {
|
---|
| 479 | PRINT( std::cerr << "____untyped: " << expr << std::endl; )
|
---|
| 480 | std::list<DesignatorChain>::iterator dit = desigAlts.begin();
|
---|
| 481 | if ( NameExpr * nexpr = dynamic_cast<NameExpr *>(expr) ) {
|
---|
| 482 | for ( Type * t : curTypes ) {
|
---|
| 483 | assert( dit != desigAlts.end() );
|
---|
| 484 | DesignatorChain & d = *dit;
|
---|
| 485 | PRINT( std::cerr << "____actual: " << t << std::endl; )
|
---|
| 486 | ReferenceToType * refType = dynamic_cast<ReferenceToType *>(t);
|
---|
| 487 | std::list<Declaration *> members;
|
---|
| 488 | if ( refType ) {
|
---|
| 489 | refType->lookup( nexpr->get_name(), members ); // concatenate identical field name
|
---|
| 490 | // xxx - need to also include anonymous members in this somehow...
|
---|
| 491 | for ( Declaration * mem: members ) {
|
---|
| 492 | if ( ObjectDecl * field = dynamic_cast<ObjectDecl *>(mem) ) {
|
---|
| 493 | PRINT( std::cerr << "____alt: " << field->get_type() << std::endl; )
|
---|
| 494 | DesignatorChain newD = d;
|
---|
| 495 | newD.push_back( new VariableExpr( field ) );
|
---|
| 496 | newDesigAlts.push_back( newD );
|
---|
| 497 | newTypes.push_back( field->get_type() );
|
---|
| 498 | } // if
|
---|
| 499 | } // for
|
---|
| 500 | } // if
|
---|
| 501 | ++dit;
|
---|
| 502 | } // for
|
---|
| 503 | } else {
|
---|
| 504 | for ( Type * t : curTypes ) {
|
---|
| 505 | assert( dit != desigAlts.end() );
|
---|
| 506 | DesignatorChain & d = *dit;
|
---|
| 507 | if ( ArrayType * at = dynamic_cast< ArrayType * > ( t ) ) {
|
---|
| 508 | PRINT( std::cerr << "____alt: " << at->get_base() << std::endl; )
|
---|
| 509 | d.push_back( expr );
|
---|
| 510 | newDesigAlts.push_back( d );
|
---|
| 511 | newTypes.push_back( at->get_base() );
|
---|
| 512 | }
|
---|
| 513 | ++dit;
|
---|
| 514 | } // for
|
---|
| 515 | } // if
|
---|
| 516 | desigAlts = newDesigAlts;
|
---|
| 517 | newDesigAlts.clear();
|
---|
| 518 | curTypes = newTypes;
|
---|
| 519 | newTypes.clear();
|
---|
| 520 | assertf( desigAlts.size() == curTypes.size(), "Designator alternatives (%d) and current types (%d) out of sync", desigAlts.size(), curTypes.size() );
|
---|
| 521 | } // for
|
---|
| 522 | if ( desigAlts.size() > 1 ) {
|
---|
| 523 | throw SemanticError( toString("Too many alternatives (", desigAlts.size(), ") for designation: "), designation );
|
---|
| 524 | } else if ( desigAlts.size() == 0 ) {
|
---|
| 525 | throw SemanticError( "No reasonable alternatives for designation: ", designation );
|
---|
| 526 | }
|
---|
| 527 | DesignatorChain & d = desigAlts.back();
|
---|
| 528 | PRINT( for ( Expression * expr : d ) {
|
---|
| 529 | std::cerr << "____desig: " << expr << std::endl;
|
---|
| 530 | } ) // for
|
---|
| 531 | assertf( ! curTypes.empty(), "empty designator chosen");
|
---|
| 532 |
|
---|
| 533 | // set new designators
|
---|
| 534 | assertf( ! objStack.empty(), "empty object stack when setting designation" );
|
---|
| 535 | Designation * actualDesignation = new Designation( d );
|
---|
| 536 | objStack.top()->setPosition( d ); // destroys d
|
---|
| 537 | return actualDesignation;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | void CurrentObject::increment() {
|
---|
| 541 | PRINT( std::cerr << "____increment" << std::endl; )
|
---|
| 542 | if ( ! objStack.empty() ) {
|
---|
| 543 | PRINT( std::cerr << *objStack.top() << std::endl; )
|
---|
| 544 | objStack.top()->smallStep();
|
---|
| 545 | }
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | void CurrentObject::enterListInit() {
|
---|
| 549 | PRINT( std::cerr << "____entering list init" << std::endl; )
|
---|
| 550 | assertf( ! objStack.empty(), "empty obj stack entering list init" );
|
---|
| 551 | Type * type = objStack.top()->getNext();
|
---|
| 552 | if ( type ) {
|
---|
| 553 | objStack.push( createMemberIterator( type ) );
|
---|
| 554 | } else {
|
---|
| 555 | assertf( false, "not sure about this case..." );
|
---|
| 556 | }
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | void CurrentObject::exitListInit() {
|
---|
| 560 | PRINT( std::cerr << "____exiting list init" << std::endl; )
|
---|
| 561 | assertf( ! objStack.empty(), "objstack empty" );
|
---|
| 562 | delete objStack.top();
|
---|
| 563 | objStack.pop();
|
---|
| 564 | if ( ! objStack.empty() ) {
|
---|
| 565 | PRINT( std::cerr << *objStack.top() << std::endl; )
|
---|
| 566 | objStack.top()->bigStep();
|
---|
| 567 | }
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | std::list< InitAlternative > CurrentObject::getOptions() {
|
---|
| 571 | PRINT( std::cerr << "____getting current options" << std::endl; )
|
---|
| 572 | assertf( ! objStack.empty(), "objstack empty in getOptions" );
|
---|
| 573 | return **objStack.top();
|
---|
| 574 | }
|
---|
[62423350] | 575 |
|
---|
| 576 | Type * CurrentObject::getCurrentType() {
|
---|
| 577 | PRINT( std::cerr << "____getting current type" << std::endl; )
|
---|
| 578 | assertf( ! objStack.empty(), "objstack empty in getCurrentType" );
|
---|
| 579 | return objStack.top()->getNext();
|
---|
| 580 | }
|
---|
[e4d829b] | 581 | } // namespace ResolvExpr
|
---|
| 582 |
|
---|
| 583 | // Local Variables: //
|
---|
| 584 | // tab-width: 4 //
|
---|
| 585 | // mode: c++ //
|
---|
| 586 | // compile-command: "make install" //
|
---|
| 587 | // End: //
|
---|