| [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 |  | 
|---|
| [ea6332d] | 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 | 
|---|
| [e4d829b] | 21 |  | 
|---|
| [ea6332d] | 22 | #include "Common/Indenter.h"           // for Indenter, operator<< | 
|---|
|  | 23 | #include "Common/SemanticError.h"      // for SemanticError | 
|---|
|  | 24 | #include "Common/utility.h"            // for toString | 
|---|
| [e4d829b] | 25 | #include "CurrentObject.h" | 
|---|
| [ea6332d] | 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 | 
|---|
| [e4d829b] | 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() ) ) { | 
|---|
| [62423350] | 42 | if ( basicType->isInteger() ) { | 
|---|
|  | 43 | return constExpr->get_constant()->get_ival(); | 
|---|
| [e4d829b] | 44 | } else { | 
|---|
| [56e49b0] | 45 | assertf( false, "Non-integer constant expression in getConstValue %s", toString( constExpr ).c_str() ); // xxx - might be semantic error | 
|---|
| [e4d829b] | 46 | } | 
|---|
| [62423350] | 47 | } else if ( dynamic_cast< OneType * >( constExpr->get_result() ) ) { | 
|---|
|  | 48 | return 1; | 
|---|
|  | 49 | } else if ( dynamic_cast< ZeroType * >( constExpr->get_result() ) ) { | 
|---|
|  | 50 | return 0; | 
|---|
| [e4d829b] | 51 | } else { | 
|---|
| [62423350] | 52 | assertf( false, "unhandled type on getConstValue %s", toString( constExpr->get_result() ).c_str() ); // xxx - might be semantic error | 
|---|
| [e4d829b] | 53 | } | 
|---|
|  | 54 | } | 
|---|
|  | 55 |  | 
|---|
| [62423350] | 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 |  | 
|---|
| [e4d829b] | 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 |  | 
|---|
| [9ff56e7] | 121 | virtual void print( std::ostream & out, __attribute__((unused)) Indenter indent ) const { | 
|---|
| [e4d829b] | 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 ) ) { | 
|---|
| [62423350] | 153 | size = getConstValue( constExpr ); | 
|---|
| [e4d829b] | 154 | PRINT( std::cerr << "array type with size: " << size << std::endl; ) | 
|---|
|  | 155 | }       else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { | 
|---|
| [62423350] | 156 | setSize( castExpr->get_arg() ); // xxx - need to perform the conversion specified by the cast | 
|---|
| [e4d829b] | 157 | } else { | 
|---|
| [62423350] | 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 | 
|---|
| [e4d829b] | 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 ) ) { | 
|---|
| [56e49b0] | 170 | assertf( dynamic_cast<EnumInstType *> ( varExpr->get_result() ), "ArrayIterator given variable that isn't an enum constant : %s", toString( expr ).c_str() ); | 
|---|
| [e4d829b] | 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: | 
|---|
| [62423350] | 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; | 
|---|
| [e4d829b] | 255 | iterator curMember; | 
|---|
| [62423350] | 256 | bool atbegin = true; // false at first {small,big}Step -- this aggr type is only added to the possibilities at the beginning | 
|---|
| [e4d829b] | 257 | Type * curType = nullptr; | 
|---|
|  | 258 | MemberIterator * memberIter = nullptr; | 
|---|
| [62423350] | 259 | mutable TypeSubstitution sub; | 
|---|
| [e4d829b] | 260 |  | 
|---|
| [62423350] | 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 ) ) { | 
|---|
| [c6747a1] | 262 | PRINT( std::cerr << "Creating " << kind << "(" << name << ")"; ) | 
|---|
| [e4d829b] | 263 | init(); | 
|---|
|  | 264 | } | 
|---|
|  | 265 |  | 
|---|
|  | 266 | virtual ~AggregateIterator() { | 
|---|
|  | 267 | delete memberIter; | 
|---|
|  | 268 | } | 
|---|
|  | 269 |  | 
|---|
|  | 270 | bool init() { | 
|---|
| [62423350] | 271 | PRINT( std::cerr << "--init()--" << members.size() << std::endl; ) | 
|---|
|  | 272 | if ( curMember != members.end() ) { | 
|---|
| [e4d829b] | 273 | if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( *curMember ) ) { | 
|---|
|  | 274 | PRINT( std::cerr << "incremented to field: " << field << std::endl; ) | 
|---|
|  | 275 | curType = field->get_type(); | 
|---|
|  | 276 | memberIter = createMemberIterator( curType ); | 
|---|
|  | 277 | return true; | 
|---|
|  | 278 | } | 
|---|
|  | 279 | } | 
|---|
|  | 280 | return false; | 
|---|
|  | 281 | } | 
|---|
|  | 282 |  | 
|---|
|  | 283 | virtual std::list<InitAlternative> operator*() const { | 
|---|
|  | 284 | if (memberIter && *memberIter) { | 
|---|
|  | 285 | std::list<InitAlternative> ret = memberIter->first(); | 
|---|
| [62423350] | 286 | PRINT( std::cerr << "sub: " << sub << std::endl; ) | 
|---|
| [e4d829b] | 287 | for ( InitAlternative & alt : ret ) { | 
|---|
|  | 288 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) | 
|---|
| [e3e16bc] | 289 | alt.designation->get_designators().push_front( new VariableExpr( strict_dynamic_cast< ObjectDecl * >( *curMember ) ) ); | 
|---|
| [62423350] | 290 | // need to substitute for generic types, so that casts are to concrete types | 
|---|
|  | 291 | PRINT( std::cerr << "  type is: " << alt.type; ) | 
|---|
|  | 292 | sub.apply( alt.type ); // also apply to designation?? | 
|---|
|  | 293 | PRINT( std::cerr << " ==> " << alt.type << std::endl; ) | 
|---|
| [e4d829b] | 294 | } | 
|---|
|  | 295 | return ret; | 
|---|
|  | 296 | } | 
|---|
|  | 297 | return std::list<InitAlternative>(); | 
|---|
|  | 298 | } | 
|---|
|  | 299 |  | 
|---|
|  | 300 | virtual void setPosition( std::list< Expression * > & designators ) { | 
|---|
|  | 301 | if ( ! designators.empty() ) { | 
|---|
|  | 302 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( designators.front() ) ) { | 
|---|
| [62423350] | 303 | for ( curMember = members.begin(); curMember != members.end(); ++curMember ) { | 
|---|
| [e4d829b] | 304 | if ( *curMember == varExpr->get_var() ) { | 
|---|
|  | 305 | designators.pop_front(); | 
|---|
|  | 306 | delete memberIter; | 
|---|
|  | 307 | memberIter = createMemberIterator( varExpr->get_result() ); | 
|---|
|  | 308 | curType = varExpr->get_result(); | 
|---|
| [62423350] | 309 | atbegin = curMember == members.begin() && designators.empty(); // xxx - is this the right condition for atbegin?? | 
|---|
| [e4d829b] | 310 | memberIter->setPosition( designators ); | 
|---|
|  | 311 | return; | 
|---|
|  | 312 | } // if | 
|---|
|  | 313 | } // for | 
|---|
| [62423350] | 314 | assertf( false, "could not find member in %s: %s", kind.c_str(), toString( varExpr ).c_str() ); | 
|---|
| [e4d829b] | 315 | } else { | 
|---|
| [62423350] | 316 | assertf( false, "bad designator given to %s: %s", kind.c_str(), toString( designators.front() ).c_str() ); | 
|---|
| [e4d829b] | 317 | } // if | 
|---|
|  | 318 | } // if | 
|---|
|  | 319 | } | 
|---|
|  | 320 |  | 
|---|
|  | 321 | virtual MemberIterator & smallStep() { | 
|---|
|  | 322 | PRINT( std::cerr << "smallStep in " << kind << std::endl; ) | 
|---|
|  | 323 | atbegin = false; | 
|---|
|  | 324 | if ( memberIter ) { | 
|---|
|  | 325 | PRINT( std::cerr << "has member iter, incrementing..." << std::endl; ) | 
|---|
|  | 326 | memberIter->smallStep(); | 
|---|
|  | 327 | if ( *memberIter ) { | 
|---|
|  | 328 | PRINT( std::cerr << "success!" << std::endl; ) | 
|---|
|  | 329 | return *this; | 
|---|
|  | 330 | } | 
|---|
|  | 331 | } | 
|---|
|  | 332 | return bigStep(); | 
|---|
|  | 333 | } | 
|---|
|  | 334 |  | 
|---|
|  | 335 | virtual Type * getType() { return inst; } | 
|---|
|  | 336 | virtual Type * getNext() { | 
|---|
|  | 337 | if ( memberIter && *memberIter ) return memberIter->getType(); // xxx - ??? recursive call??? | 
|---|
|  | 338 | return nullptr; | 
|---|
|  | 339 | } | 
|---|
|  | 340 |  | 
|---|
|  | 341 | virtual std::list<InitAlternative> first() const { | 
|---|
|  | 342 | std::list<InitAlternative> ret; | 
|---|
|  | 343 | PRINT( std::cerr << "first " << kind << std::endl; ) | 
|---|
|  | 344 | if ( memberIter && *memberIter ) { // might not need *memberIter?? | 
|---|
|  | 345 | PRINT( std::cerr << "adding children" << std::endl; ) | 
|---|
|  | 346 | ret = memberIter->first(); | 
|---|
|  | 347 | for ( InitAlternative & alt : ret ) { | 
|---|
|  | 348 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) | 
|---|
| [e3e16bc] | 349 | alt.designation->get_designators().push_front( new VariableExpr( strict_dynamic_cast< ObjectDecl * >( *curMember ) ) ); | 
|---|
| [e4d829b] | 350 | } | 
|---|
|  | 351 | } | 
|---|
|  | 352 | // if ( curMember == std::next( decl->get_members().begin(), 1 ) ) { // xxx - this never triggers because curMember is incremented immediately on construction | 
|---|
|  | 353 | if ( atbegin ) { // xxx - this never triggers because curMember is incremented immediately on construction | 
|---|
|  | 354 | // xxx - what about case of empty struct?? | 
|---|
|  | 355 | // only add self if at the very beginning of the structure | 
|---|
|  | 356 | PRINT( std::cerr << "adding self" << std::endl; ) | 
|---|
|  | 357 | ret.push_front( { inst->clone(), new Designation( {} ) } ); | 
|---|
|  | 358 | } | 
|---|
|  | 359 | return ret; | 
|---|
|  | 360 | } | 
|---|
|  | 361 |  | 
|---|
|  | 362 | virtual void print( std::ostream & out, Indenter indent ) const { | 
|---|
| [62423350] | 363 | out << kind << "(" << name << ")"; | 
|---|
| [e4d829b] | 364 | if ( memberIter ) { | 
|---|
|  | 365 | Indenter childIndent = indent+1; | 
|---|
|  | 366 | out << std::endl << childIndent; | 
|---|
|  | 367 | memberIter->print( out, childIndent ); | 
|---|
|  | 368 | } | 
|---|
|  | 369 | } | 
|---|
|  | 370 | }; | 
|---|
|  | 371 |  | 
|---|
|  | 372 | class UnionIterator : public AggregateIterator { | 
|---|
|  | 373 | public: | 
|---|
| [62423350] | 374 | UnionIterator( UnionInstType * inst ) : AggregateIterator( "UnionIterator", inst->get_name(), inst, inst->get_baseUnion()->get_members() ) {} | 
|---|
| [e4d829b] | 375 |  | 
|---|
|  | 376 | virtual operator bool() const { return (memberIter && *memberIter); } | 
|---|
|  | 377 | virtual MemberIterator & bigStep() { | 
|---|
|  | 378 | // unions only initialize one member | 
|---|
|  | 379 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) | 
|---|
|  | 380 | atbegin = false; | 
|---|
|  | 381 | delete memberIter; | 
|---|
|  | 382 | memberIter = nullptr; | 
|---|
|  | 383 | curType = nullptr; | 
|---|
| [62423350] | 384 | curMember = members.end(); | 
|---|
| [e4d829b] | 385 | return *this; | 
|---|
|  | 386 | } | 
|---|
|  | 387 | virtual std::list<InitAlternative> first() const { return std::list<InitAlternative>{}; } | 
|---|
|  | 388 | }; | 
|---|
|  | 389 |  | 
|---|
|  | 390 | class StructIterator : public AggregateIterator { | 
|---|
|  | 391 | public: | 
|---|
| [62423350] | 392 | StructIterator( StructInstType * inst ) : AggregateIterator( "StructIterator", inst->get_name(), inst, inst->get_baseStruct()->get_members() ) {} | 
|---|
|  | 393 |  | 
|---|
|  | 394 | virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); } | 
|---|
|  | 395 |  | 
|---|
|  | 396 | virtual MemberIterator & bigStep() { | 
|---|
|  | 397 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) | 
|---|
|  | 398 | atbegin = false; | 
|---|
|  | 399 | delete memberIter; | 
|---|
|  | 400 | memberIter = nullptr; | 
|---|
|  | 401 | curType = nullptr; | 
|---|
|  | 402 | for ( ; curMember != members.end(); ) { | 
|---|
|  | 403 | ++curMember; | 
|---|
|  | 404 | if ( init() ) { | 
|---|
|  | 405 | return *this; | 
|---|
|  | 406 | } | 
|---|
|  | 407 | } | 
|---|
|  | 408 | return *this; | 
|---|
|  | 409 | } | 
|---|
|  | 410 | }; | 
|---|
|  | 411 |  | 
|---|
|  | 412 | class TupleIterator : public AggregateIterator { | 
|---|
|  | 413 | public: | 
|---|
|  | 414 | TupleIterator( TupleType * inst ) : AggregateIterator( "TupleIterator", toString("Tuple", inst->size()), inst, inst->get_members() ) {} | 
|---|
| [e4d829b] | 415 |  | 
|---|
| [62423350] | 416 | virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); } | 
|---|
| [e4d829b] | 417 |  | 
|---|
|  | 418 | virtual MemberIterator & bigStep() { | 
|---|
|  | 419 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) | 
|---|
|  | 420 | atbegin = false; | 
|---|
|  | 421 | delete memberIter; | 
|---|
|  | 422 | memberIter = nullptr; | 
|---|
|  | 423 | curType = nullptr; | 
|---|
| [62423350] | 424 | for ( ; curMember != members.end(); ) { | 
|---|
| [e4d829b] | 425 | ++curMember; | 
|---|
|  | 426 | if ( init() ) { | 
|---|
|  | 427 | return *this; | 
|---|
|  | 428 | } | 
|---|
|  | 429 | } | 
|---|
|  | 430 | return *this; | 
|---|
|  | 431 | } | 
|---|
|  | 432 | }; | 
|---|
|  | 433 |  | 
|---|
|  | 434 | MemberIterator * createMemberIterator( Type * type ) { | 
|---|
|  | 435 | if ( ReferenceToType * aggr = dynamic_cast< ReferenceToType * >( type ) ) { | 
|---|
|  | 436 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( aggr ) ) { | 
|---|
|  | 437 | return new StructIterator( sit ); | 
|---|
|  | 438 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( aggr ) ) { | 
|---|
|  | 439 | return new UnionIterator( uit ); | 
|---|
|  | 440 | } else { | 
|---|
| [62423350] | 441 | assertf( dynamic_cast< TypeInstType * >( type ), "some other reftotype" ); | 
|---|
|  | 442 | return new SimpleIterator( type ); | 
|---|
| [e4d829b] | 443 | } | 
|---|
|  | 444 | } else if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { | 
|---|
|  | 445 | return new ArrayIterator( at ); | 
|---|
| [62423350] | 446 | } else if ( TupleType * tt = dynamic_cast< TupleType * >( type ) ) { | 
|---|
|  | 447 | return new TupleIterator( tt ); | 
|---|
| [e4d829b] | 448 | } else { | 
|---|
|  | 449 | return new SimpleIterator( type ); | 
|---|
|  | 450 | } | 
|---|
|  | 451 | } | 
|---|
|  | 452 |  | 
|---|
|  | 453 | CurrentObject::CurrentObject() {} | 
|---|
|  | 454 | CurrentObject::CurrentObject( Type * type ) { | 
|---|
|  | 455 | objStack.push( new SimpleIterator( type ) ); | 
|---|
|  | 456 | } | 
|---|
|  | 457 |  | 
|---|
|  | 458 |  | 
|---|
|  | 459 | void CurrentObject::setNext( Designation * designation ) { | 
|---|
|  | 460 | assertf( ! objStack.empty(), "obj stack empty in setNext" ); | 
|---|
|  | 461 | PRINT( std::cerr << "____setNext" << designation << std::endl; ) | 
|---|
|  | 462 | objStack.top()->setPosition( designation->get_designators() ); | 
|---|
|  | 463 | } | 
|---|
|  | 464 |  | 
|---|
|  | 465 | Designation * CurrentObject::findNext( Designation * designation ) { | 
|---|
|  | 466 | typedef std::list< Expression * > DesignatorChain; | 
|---|
|  | 467 | PRINT( std::cerr << "___findNext" << std::endl; ) | 
|---|
|  | 468 | // find all the d's | 
|---|
|  | 469 | std::list<DesignatorChain> desigAlts{ { } }, newDesigAlts; | 
|---|
|  | 470 | std::list<Type *> curTypes { (objStack.top())->getType() }, newTypes; | 
|---|
|  | 471 | for ( Expression * expr : designation->get_designators() ) { | 
|---|
|  | 472 | PRINT( std::cerr << "____untyped: " << expr << std::endl; ) | 
|---|
|  | 473 | std::list<DesignatorChain>::iterator dit = desigAlts.begin(); | 
|---|
|  | 474 | if ( NameExpr * nexpr = dynamic_cast<NameExpr *>(expr) ) { | 
|---|
|  | 475 | for ( Type * t : curTypes ) { | 
|---|
|  | 476 | assert( dit != desigAlts.end() ); | 
|---|
|  | 477 | DesignatorChain & d = *dit; | 
|---|
|  | 478 | PRINT( std::cerr << "____actual: " << t << std::endl; ) | 
|---|
|  | 479 | ReferenceToType * refType = dynamic_cast<ReferenceToType *>(t); | 
|---|
|  | 480 | std::list<Declaration *> members; | 
|---|
|  | 481 | if ( refType ) { | 
|---|
|  | 482 | refType->lookup( nexpr->get_name(), members ); // concatenate identical field name | 
|---|
|  | 483 | // xxx - need to also include anonymous members in this somehow... | 
|---|
|  | 484 | for ( Declaration * mem: members ) { | 
|---|
|  | 485 | if ( ObjectDecl * field = dynamic_cast<ObjectDecl *>(mem) ) { | 
|---|
|  | 486 | PRINT( std::cerr << "____alt: " << field->get_type() << std::endl; ) | 
|---|
|  | 487 | DesignatorChain newD = d; | 
|---|
|  | 488 | newD.push_back( new VariableExpr( field ) ); | 
|---|
|  | 489 | newDesigAlts.push_back( newD ); | 
|---|
|  | 490 | newTypes.push_back( field->get_type() ); | 
|---|
|  | 491 | } // if | 
|---|
|  | 492 | } // for | 
|---|
|  | 493 | } // if | 
|---|
|  | 494 | ++dit; | 
|---|
|  | 495 | } // for | 
|---|
|  | 496 | } else { | 
|---|
|  | 497 | for ( Type * t : curTypes ) { | 
|---|
|  | 498 | assert( dit != desigAlts.end() ); | 
|---|
|  | 499 | DesignatorChain & d = *dit; | 
|---|
|  | 500 | if ( ArrayType * at = dynamic_cast< ArrayType * > ( t ) ) { | 
|---|
|  | 501 | PRINT( std::cerr << "____alt: " << at->get_base() << std::endl; ) | 
|---|
|  | 502 | d.push_back( expr ); | 
|---|
|  | 503 | newDesigAlts.push_back( d ); | 
|---|
|  | 504 | newTypes.push_back( at->get_base() ); | 
|---|
|  | 505 | } | 
|---|
|  | 506 | ++dit; | 
|---|
|  | 507 | } // for | 
|---|
|  | 508 | } // if | 
|---|
|  | 509 | desigAlts = newDesigAlts; | 
|---|
|  | 510 | newDesigAlts.clear(); | 
|---|
|  | 511 | curTypes = newTypes; | 
|---|
|  | 512 | newTypes.clear(); | 
|---|
| [56e49b0] | 513 | assertf( desigAlts.size() == curTypes.size(), "Designator alternatives (%zu) and current types (%zu) out of sync", desigAlts.size(), curTypes.size() ); | 
|---|
| [e4d829b] | 514 | } // for | 
|---|
|  | 515 | if ( desigAlts.size() > 1 ) { | 
|---|
|  | 516 | throw SemanticError( toString("Too many alternatives (", desigAlts.size(), ") for designation: "), designation ); | 
|---|
|  | 517 | } else if ( desigAlts.size() == 0 ) { | 
|---|
|  | 518 | throw SemanticError( "No reasonable alternatives for designation: ", designation ); | 
|---|
|  | 519 | } | 
|---|
|  | 520 | DesignatorChain & d = desigAlts.back(); | 
|---|
|  | 521 | PRINT( for ( Expression * expr : d ) { | 
|---|
|  | 522 | std::cerr << "____desig: " << expr << std::endl; | 
|---|
|  | 523 | } ) // for | 
|---|
|  | 524 | assertf( ! curTypes.empty(), "empty designator chosen"); | 
|---|
|  | 525 |  | 
|---|
|  | 526 | // set new designators | 
|---|
|  | 527 | assertf( ! objStack.empty(), "empty object stack when setting designation" ); | 
|---|
|  | 528 | Designation * actualDesignation = new Designation( d ); | 
|---|
|  | 529 | objStack.top()->setPosition( d ); // destroys d | 
|---|
|  | 530 | return actualDesignation; | 
|---|
|  | 531 | } | 
|---|
|  | 532 |  | 
|---|
|  | 533 | void CurrentObject::increment() { | 
|---|
|  | 534 | PRINT( std::cerr << "____increment" << std::endl; ) | 
|---|
|  | 535 | if ( ! objStack.empty() ) { | 
|---|
|  | 536 | PRINT( std::cerr << *objStack.top() << std::endl; ) | 
|---|
|  | 537 | objStack.top()->smallStep(); | 
|---|
|  | 538 | } | 
|---|
|  | 539 | } | 
|---|
|  | 540 |  | 
|---|
|  | 541 | void CurrentObject::enterListInit() { | 
|---|
|  | 542 | PRINT( std::cerr << "____entering list init" << std::endl; ) | 
|---|
|  | 543 | assertf( ! objStack.empty(), "empty obj stack entering list init" ); | 
|---|
|  | 544 | Type * type = objStack.top()->getNext(); | 
|---|
|  | 545 | if ( type ) { | 
|---|
|  | 546 | objStack.push( createMemberIterator( type ) ); | 
|---|
|  | 547 | } else { | 
|---|
|  | 548 | assertf( false, "not sure about this case..." ); | 
|---|
|  | 549 | } | 
|---|
|  | 550 | } | 
|---|
|  | 551 |  | 
|---|
|  | 552 | void CurrentObject::exitListInit() { | 
|---|
|  | 553 | PRINT( std::cerr << "____exiting list init" << std::endl; ) | 
|---|
|  | 554 | assertf( ! objStack.empty(), "objstack empty" ); | 
|---|
|  | 555 | delete objStack.top(); | 
|---|
|  | 556 | objStack.pop(); | 
|---|
|  | 557 | if ( ! objStack.empty() ) { | 
|---|
|  | 558 | PRINT( std::cerr << *objStack.top() << std::endl; ) | 
|---|
|  | 559 | objStack.top()->bigStep(); | 
|---|
|  | 560 | } | 
|---|
|  | 561 | } | 
|---|
|  | 562 |  | 
|---|
|  | 563 | std::list< InitAlternative > CurrentObject::getOptions() { | 
|---|
|  | 564 | PRINT( std::cerr << "____getting current options" << std::endl; ) | 
|---|
|  | 565 | assertf( ! objStack.empty(), "objstack empty in getOptions" ); | 
|---|
|  | 566 | return **objStack.top(); | 
|---|
|  | 567 | } | 
|---|
| [62423350] | 568 |  | 
|---|
|  | 569 | Type * CurrentObject::getCurrentType() { | 
|---|
|  | 570 | PRINT( std::cerr << "____getting current type" << std::endl; ) | 
|---|
|  | 571 | assertf( ! objStack.empty(), "objstack empty in getCurrentType" ); | 
|---|
|  | 572 | return objStack.top()->getNext(); | 
|---|
|  | 573 | } | 
|---|
| [e4d829b] | 574 | } // namespace ResolvExpr | 
|---|
|  | 575 |  | 
|---|
|  | 576 | // Local Variables: // | 
|---|
|  | 577 | // tab-width: 4 // | 
|---|
|  | 578 | // mode: c++ // | 
|---|
|  | 579 | // compile-command: "make install" // | 
|---|
|  | 580 | // End: // | 
|---|