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