| [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_... | 
|---|
| [60aaa51d] | 18 | #include <deque> | 
|---|
| [ea6332d] | 19 | #include <iostream>                    // for ostream, operator<<, basic_ost... | 
|---|
|  | 20 | #include <stack>                       // for stack | 
|---|
|  | 21 | #include <string>                      // for string, operator<<, allocator | 
|---|
| [e4d829b] | 22 |  | 
|---|
| [8e1467d] | 23 | #include "AST/Copy.hpp"                // for shallowCopy | 
|---|
| [2b59f55] | 24 | #include "AST/Expr.hpp"                // for InitAlternative | 
|---|
| [60aaa51d] | 25 | #include "AST/GenericSubstitution.hpp" // for genericSubstitution | 
|---|
| [2b59f55] | 26 | #include "AST/Init.hpp"                // for Designation | 
|---|
|  | 27 | #include "AST/Node.hpp"                // for readonly | 
|---|
| [2890212] | 28 | #include "AST/Print.hpp"                // for readonly | 
|---|
| [60aaa51d] | 29 | #include "AST/Type.hpp" | 
|---|
| [ea6332d] | 30 | #include "Common/Indenter.h"           // for Indenter, operator<< | 
|---|
|  | 31 | #include "Common/SemanticError.h"      // for SemanticError | 
|---|
|  | 32 | #include "Common/utility.h"            // for toString | 
|---|
| [e4d829b] | 33 | #include "CurrentObject.h" | 
|---|
| [ea6332d] | 34 | #include "SynTree/Constant.h"          // for Constant | 
|---|
|  | 35 | #include "SynTree/Declaration.h"       // for ObjectDecl, Declaration, Struc... | 
|---|
|  | 36 | #include "SynTree/Expression.h"        // for InitAlternative, VariableExpr | 
|---|
|  | 37 | #include "SynTree/Initializer.h"       // for Designation, operator<< | 
|---|
|  | 38 | #include "SynTree/Type.h"              // for Type, StructInstType, UnionIns... | 
|---|
|  | 39 | #include "SynTree/TypeSubstitution.h"  // for TypeSubstitution | 
|---|
| [e4d829b] | 40 |  | 
|---|
|  | 41 | #if 0 | 
|---|
|  | 42 | #define PRINT(x) x | 
|---|
|  | 43 | #else | 
|---|
|  | 44 | #define PRINT(x) | 
|---|
|  | 45 | #endif | 
|---|
|  | 46 |  | 
|---|
|  | 47 | namespace ResolvExpr { | 
|---|
| [62423350] | 48 | template< typename AggrInst > | 
|---|
|  | 49 | TypeSubstitution makeGenericSubstitution( AggrInst * inst ) { | 
|---|
|  | 50 | assert( inst ); | 
|---|
|  | 51 | assert( inst->get_baseParameters() ); | 
|---|
|  | 52 | std::list< TypeDecl * > baseParams = *inst->get_baseParameters(); | 
|---|
|  | 53 | std::list< Expression * > typeSubs = inst->get_parameters(); | 
|---|
|  | 54 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() ); | 
|---|
|  | 55 | return subs; | 
|---|
|  | 56 | } | 
|---|
|  | 57 |  | 
|---|
|  | 58 | TypeSubstitution makeGenericSubstitution( Type * type ) { | 
|---|
|  | 59 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) { | 
|---|
|  | 60 | return makeGenericSubstitution( inst ); | 
|---|
|  | 61 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) { | 
|---|
|  | 62 | return makeGenericSubstitution( inst ); | 
|---|
|  | 63 | } else { | 
|---|
|  | 64 | return TypeSubstitution(); | 
|---|
|  | 65 | } | 
|---|
|  | 66 | } | 
|---|
|  | 67 |  | 
|---|
| [e4d829b] | 68 | class MemberIterator { | 
|---|
|  | 69 | public: | 
|---|
|  | 70 | virtual ~MemberIterator() {} | 
|---|
|  | 71 |  | 
|---|
| [84e8423] | 72 | /// walks the current object using the given designators as a guide | 
|---|
| [e4d829b] | 73 | virtual void setPosition( std::list< Expression * > & designators ) = 0; | 
|---|
| [84e8423] | 74 |  | 
|---|
|  | 75 | /// retrieve the list of possible Type/Designaton pairs for the current position in the currect object | 
|---|
| [e4d829b] | 76 | virtual std::list<InitAlternative> operator*() const = 0; | 
|---|
| [84e8423] | 77 |  | 
|---|
|  | 78 | /// true if the iterator is not currently at the end | 
|---|
| [e4d829b] | 79 | virtual operator bool() const = 0; | 
|---|
| [84e8423] | 80 |  | 
|---|
|  | 81 | /// moves the iterator by one member in the current object | 
|---|
| [e4d829b] | 82 | virtual MemberIterator & bigStep() = 0; | 
|---|
| [84e8423] | 83 |  | 
|---|
|  | 84 | /// moves the iterator by one member in the current subobject | 
|---|
| [e4d829b] | 85 | virtual MemberIterator & smallStep() = 0; | 
|---|
| [84e8423] | 86 |  | 
|---|
|  | 87 | /// the type of the current object | 
|---|
| [e4d829b] | 88 | virtual Type * getType() = 0; | 
|---|
| [84e8423] | 89 |  | 
|---|
|  | 90 | /// the type of the current subobject | 
|---|
| [e4d829b] | 91 | virtual Type * getNext() = 0; | 
|---|
|  | 92 |  | 
|---|
| [84e8423] | 93 | /// printing for debug | 
|---|
| [e4d829b] | 94 | virtual void print( std::ostream & out, Indenter indent ) const = 0; | 
|---|
|  | 95 |  | 
|---|
| [84e8423] | 96 | /// helper for operator*; aggregates must add designator to each init alternative, but | 
|---|
|  | 97 | /// adding designators in operator* creates duplicates. | 
|---|
| [e4d829b] | 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; ) | 
|---|
| [f072892] | 149 | base = at->base; | 
|---|
| [e4d829b] | 150 | memberIter = createMemberIterator( base ); | 
|---|
| [f072892] | 151 | if ( at->isVarLen ) SemanticError( at, "VLA initialization does not support @=: " ); | 
|---|
|  | 152 | setSize( at->dimension ); | 
|---|
| [e4d829b] | 153 | } | 
|---|
|  | 154 |  | 
|---|
|  | 155 | ~ArrayIterator() { | 
|---|
|  | 156 | delete memberIter; | 
|---|
|  | 157 | } | 
|---|
|  | 158 |  | 
|---|
|  | 159 | private: | 
|---|
| [ba4a1d8] | 160 | void setSize( Expression * expr ) { // replace this logic with an eval call | 
|---|
|  | 161 | auto res = eval(expr); | 
|---|
|  | 162 | if (res.second) { | 
|---|
|  | 163 | size = res.first; | 
|---|
| [e4d829b] | 164 | } else { | 
|---|
| [ba4a1d8] | 165 | SemanticError( expr->location, toString("Array designator must be a constant expression: ", expr) ); | 
|---|
| [e4d829b] | 166 | } | 
|---|
|  | 167 | } | 
|---|
|  | 168 |  | 
|---|
|  | 169 | public: | 
|---|
|  | 170 | void setPosition( Expression * expr ) { | 
|---|
|  | 171 | // need to permit integer-constant-expressions, including: integer constants, enumeration constants, character constants, sizeof expressions, _Alignof expressions, cast expressions | 
|---|
|  | 172 | if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) { | 
|---|
| [4b97770] | 173 | try { | 
|---|
|  | 174 | index = constExpr->intValue(); | 
|---|
| [a16764a6] | 175 | } catch( SemanticErrorException & ) { | 
|---|
|  | 176 | SemanticError( expr, "Constant expression of non-integral type in array designator: " ); | 
|---|
| [4b97770] | 177 | } | 
|---|
| [e4d829b] | 178 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { | 
|---|
|  | 179 | setPosition( castExpr->get_arg() ); | 
|---|
|  | 180 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { | 
|---|
| [f64d9bc] | 181 | EnumInstType * inst = dynamic_cast<EnumInstType *>( varExpr->get_result() ); | 
|---|
|  | 182 | assertf( inst, "ArrayIterator given variable that isn't an enum constant : %s", toString( expr ).c_str() ); | 
|---|
|  | 183 | long long int value; | 
|---|
|  | 184 | if ( inst->baseEnum->valueOf( varExpr->var, value ) ) { | 
|---|
|  | 185 | index = value; | 
|---|
|  | 186 | } | 
|---|
| [e4d829b] | 187 | } else if ( dynamic_cast< SizeofExpr * >( expr ) || dynamic_cast< AlignofExpr * >( expr ) ) { | 
|---|
|  | 188 | index = 0; // xxx - get actual sizeof/alignof value? | 
|---|
|  | 189 | } else { | 
|---|
|  | 190 | assertf( false, "bad designator given to ArrayIterator: %s", toString( expr ).c_str() ); | 
|---|
|  | 191 | } | 
|---|
|  | 192 | } | 
|---|
|  | 193 |  | 
|---|
|  | 194 | virtual void setPosition( std::list< Expression * > & designators ) { | 
|---|
|  | 195 | if ( ! designators.empty() ) { | 
|---|
|  | 196 | setPosition( designators.front() ); | 
|---|
|  | 197 | designators.pop_front(); | 
|---|
|  | 198 | memberIter->setPosition( designators ); | 
|---|
|  | 199 | } | 
|---|
|  | 200 | } | 
|---|
|  | 201 |  | 
|---|
|  | 202 | virtual std::list<InitAlternative> operator*() const { | 
|---|
|  | 203 | return first(); | 
|---|
|  | 204 | } | 
|---|
|  | 205 |  | 
|---|
| [4b97770] | 206 | virtual operator bool() const { return index < size; } | 
|---|
| [e4d829b] | 207 |  | 
|---|
|  | 208 | virtual MemberIterator & bigStep() { | 
|---|
|  | 209 | PRINT( std::cerr << "bigStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) | 
|---|
|  | 210 | ++index; | 
|---|
|  | 211 | delete memberIter; | 
|---|
| [4b97770] | 212 | if ( index < size ) memberIter = createMemberIterator( base ); | 
|---|
| [e4d829b] | 213 | else memberIter = nullptr; | 
|---|
|  | 214 | return *this; | 
|---|
|  | 215 | } | 
|---|
|  | 216 |  | 
|---|
|  | 217 | virtual MemberIterator & smallStep() { | 
|---|
|  | 218 | PRINT( std::cerr << "smallStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) | 
|---|
|  | 219 | if ( memberIter ) { | 
|---|
|  | 220 | PRINT( std::cerr << "has member iter: " << *memberIter << std::endl; ) | 
|---|
|  | 221 | memberIter->smallStep(); | 
|---|
|  | 222 | if ( *memberIter ) { | 
|---|
|  | 223 | PRINT( std::cerr << "has valid member iter" << std::endl; ) | 
|---|
|  | 224 | return *this; | 
|---|
|  | 225 | } | 
|---|
|  | 226 | } | 
|---|
|  | 227 | return bigStep(); | 
|---|
|  | 228 | } | 
|---|
|  | 229 |  | 
|---|
|  | 230 | virtual Type * getType() { return array; } | 
|---|
|  | 231 | virtual Type * getNext() { return base; } | 
|---|
|  | 232 |  | 
|---|
|  | 233 | virtual std::list<InitAlternative> first() const { | 
|---|
|  | 234 | PRINT( std::cerr << "first in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) | 
|---|
|  | 235 | if ( memberIter && *memberIter ) { | 
|---|
|  | 236 | std::list<InitAlternative> ret = memberIter->first(); | 
|---|
|  | 237 | for ( InitAlternative & alt : ret ) { | 
|---|
|  | 238 | alt.designation->get_designators().push_front( new ConstantExpr( Constant::from_ulong( index ) ) ); | 
|---|
|  | 239 | } | 
|---|
|  | 240 | return ret; | 
|---|
|  | 241 | } | 
|---|
|  | 242 | return std::list<InitAlternative>(); | 
|---|
|  | 243 | } | 
|---|
|  | 244 |  | 
|---|
|  | 245 | virtual void print( std::ostream & out, Indenter indent ) const { | 
|---|
|  | 246 | out << "ArrayIterator(Array of " << base << ")"; | 
|---|
|  | 247 | if ( memberIter ) { | 
|---|
|  | 248 | Indenter childIndent = indent+1; | 
|---|
|  | 249 | out << std::endl << childIndent; | 
|---|
|  | 250 | memberIter->print( out, childIndent ); | 
|---|
|  | 251 | } | 
|---|
|  | 252 | } | 
|---|
|  | 253 |  | 
|---|
|  | 254 | private: | 
|---|
|  | 255 | ArrayType * array = nullptr; | 
|---|
|  | 256 | Type * base = nullptr; | 
|---|
|  | 257 | size_t index = 0; | 
|---|
|  | 258 | size_t size = 0; | 
|---|
|  | 259 | MemberIterator * memberIter = nullptr; | 
|---|
|  | 260 | }; | 
|---|
|  | 261 |  | 
|---|
|  | 262 | class AggregateIterator : public MemberIterator { | 
|---|
|  | 263 | public: | 
|---|
| [62423350] | 264 | typedef std::list<Declaration *> MemberList; | 
|---|
|  | 265 | typedef MemberList::const_iterator iterator; | 
|---|
|  | 266 | std::string kind = ""; // for debug | 
|---|
|  | 267 | std::string name; | 
|---|
|  | 268 | Type * inst = nullptr; | 
|---|
|  | 269 | const MemberList & members; | 
|---|
| [e4d829b] | 270 | iterator curMember; | 
|---|
| [62423350] | 271 | bool atbegin = true; // false at first {small,big}Step -- this aggr type is only added to the possibilities at the beginning | 
|---|
| [e4d829b] | 272 | Type * curType = nullptr; | 
|---|
|  | 273 | MemberIterator * memberIter = nullptr; | 
|---|
| [62423350] | 274 | mutable TypeSubstitution sub; | 
|---|
| [e4d829b] | 275 |  | 
|---|
| [62423350] | 276 | 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] | 277 | PRINT( std::cerr << "Creating " << kind << "(" << name << ")"; ) | 
|---|
| [e4d829b] | 278 | init(); | 
|---|
|  | 279 | } | 
|---|
|  | 280 |  | 
|---|
|  | 281 | virtual ~AggregateIterator() { | 
|---|
|  | 282 | delete memberIter; | 
|---|
|  | 283 | } | 
|---|
|  | 284 |  | 
|---|
|  | 285 | bool init() { | 
|---|
| [62423350] | 286 | PRINT( std::cerr << "--init()--" << members.size() << std::endl; ) | 
|---|
|  | 287 | if ( curMember != members.end() ) { | 
|---|
| [e4d829b] | 288 | if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( *curMember ) ) { | 
|---|
|  | 289 | PRINT( std::cerr << "incremented to field: " << field << std::endl; ) | 
|---|
|  | 290 | curType = field->get_type(); | 
|---|
|  | 291 | memberIter = createMemberIterator( curType ); | 
|---|
|  | 292 | return true; | 
|---|
|  | 293 | } | 
|---|
|  | 294 | } | 
|---|
|  | 295 | return false; | 
|---|
|  | 296 | } | 
|---|
|  | 297 |  | 
|---|
|  | 298 | virtual std::list<InitAlternative> operator*() const { | 
|---|
|  | 299 | if (memberIter && *memberIter) { | 
|---|
|  | 300 | std::list<InitAlternative> ret = memberIter->first(); | 
|---|
| [62423350] | 301 | PRINT( std::cerr << "sub: " << sub << std::endl; ) | 
|---|
| [e4d829b] | 302 | for ( InitAlternative & alt : ret ) { | 
|---|
|  | 303 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) | 
|---|
| [e3e16bc] | 304 | alt.designation->get_designators().push_front( new VariableExpr( strict_dynamic_cast< ObjectDecl * >( *curMember ) ) ); | 
|---|
| [62423350] | 305 | // need to substitute for generic types, so that casts are to concrete types | 
|---|
|  | 306 | PRINT( std::cerr << "  type is: " << alt.type; ) | 
|---|
|  | 307 | sub.apply( alt.type ); // also apply to designation?? | 
|---|
|  | 308 | PRINT( std::cerr << " ==> " << alt.type << std::endl; ) | 
|---|
| [e4d829b] | 309 | } | 
|---|
|  | 310 | return ret; | 
|---|
|  | 311 | } | 
|---|
|  | 312 | return std::list<InitAlternative>(); | 
|---|
|  | 313 | } | 
|---|
|  | 314 |  | 
|---|
|  | 315 | virtual void setPosition( std::list< Expression * > & designators ) { | 
|---|
|  | 316 | if ( ! designators.empty() ) { | 
|---|
|  | 317 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( designators.front() ) ) { | 
|---|
| [62423350] | 318 | for ( curMember = members.begin(); curMember != members.end(); ++curMember ) { | 
|---|
| [e4d829b] | 319 | if ( *curMember == varExpr->get_var() ) { | 
|---|
|  | 320 | designators.pop_front(); | 
|---|
|  | 321 | delete memberIter; | 
|---|
|  | 322 | memberIter = createMemberIterator( varExpr->get_result() ); | 
|---|
|  | 323 | curType = varExpr->get_result(); | 
|---|
| [62423350] | 324 | atbegin = curMember == members.begin() && designators.empty(); // xxx - is this the right condition for atbegin?? | 
|---|
| [e4d829b] | 325 | memberIter->setPosition( designators ); | 
|---|
|  | 326 | return; | 
|---|
|  | 327 | } // if | 
|---|
|  | 328 | } // for | 
|---|
| [62423350] | 329 | assertf( false, "could not find member in %s: %s", kind.c_str(), toString( varExpr ).c_str() ); | 
|---|
| [e4d829b] | 330 | } else { | 
|---|
| [62423350] | 331 | assertf( false, "bad designator given to %s: %s", kind.c_str(), toString( designators.front() ).c_str() ); | 
|---|
| [e4d829b] | 332 | } // if | 
|---|
|  | 333 | } // if | 
|---|
|  | 334 | } | 
|---|
|  | 335 |  | 
|---|
|  | 336 | virtual MemberIterator & smallStep() { | 
|---|
|  | 337 | PRINT( std::cerr << "smallStep in " << kind << std::endl; ) | 
|---|
|  | 338 | atbegin = false; | 
|---|
|  | 339 | if ( memberIter ) { | 
|---|
|  | 340 | PRINT( std::cerr << "has member iter, incrementing..." << std::endl; ) | 
|---|
|  | 341 | memberIter->smallStep(); | 
|---|
|  | 342 | if ( *memberIter ) { | 
|---|
|  | 343 | PRINT( std::cerr << "success!" << std::endl; ) | 
|---|
|  | 344 | return *this; | 
|---|
|  | 345 | } | 
|---|
|  | 346 | } | 
|---|
|  | 347 | return bigStep(); | 
|---|
|  | 348 | } | 
|---|
|  | 349 |  | 
|---|
|  | 350 | virtual Type * getType() { return inst; } | 
|---|
|  | 351 | virtual Type * getNext() { | 
|---|
|  | 352 | if ( memberIter && *memberIter ) return memberIter->getType(); // xxx - ??? recursive call??? | 
|---|
|  | 353 | return nullptr; | 
|---|
|  | 354 | } | 
|---|
|  | 355 |  | 
|---|
|  | 356 | virtual std::list<InitAlternative> first() const { | 
|---|
|  | 357 | std::list<InitAlternative> ret; | 
|---|
|  | 358 | PRINT( std::cerr << "first " << kind << std::endl; ) | 
|---|
|  | 359 | if ( memberIter && *memberIter ) { // might not need *memberIter?? | 
|---|
|  | 360 | PRINT( std::cerr << "adding children" << std::endl; ) | 
|---|
|  | 361 | ret = memberIter->first(); | 
|---|
|  | 362 | for ( InitAlternative & alt : ret ) { | 
|---|
|  | 363 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) | 
|---|
| [e3e16bc] | 364 | alt.designation->get_designators().push_front( new VariableExpr( strict_dynamic_cast< ObjectDecl * >( *curMember ) ) ); | 
|---|
| [e4d829b] | 365 | } | 
|---|
|  | 366 | } | 
|---|
| [84e8423] | 367 | if ( atbegin ) { | 
|---|
| [e4d829b] | 368 | // xxx - what about case of empty struct?? | 
|---|
|  | 369 | // only add self if at the very beginning of the structure | 
|---|
|  | 370 | PRINT( std::cerr << "adding self" << std::endl; ) | 
|---|
|  | 371 | ret.push_front( { inst->clone(), new Designation( {} ) } ); | 
|---|
|  | 372 | } | 
|---|
|  | 373 | return ret; | 
|---|
|  | 374 | } | 
|---|
|  | 375 |  | 
|---|
|  | 376 | virtual void print( std::ostream & out, Indenter indent ) const { | 
|---|
| [62423350] | 377 | out << kind << "(" << name << ")"; | 
|---|
| [e4d829b] | 378 | if ( memberIter ) { | 
|---|
|  | 379 | Indenter childIndent = indent+1; | 
|---|
|  | 380 | out << std::endl << childIndent; | 
|---|
|  | 381 | memberIter->print( out, childIndent ); | 
|---|
|  | 382 | } | 
|---|
|  | 383 | } | 
|---|
|  | 384 | }; | 
|---|
|  | 385 |  | 
|---|
|  | 386 | class UnionIterator : public AggregateIterator { | 
|---|
|  | 387 | public: | 
|---|
| [62423350] | 388 | UnionIterator( UnionInstType * inst ) : AggregateIterator( "UnionIterator", inst->get_name(), inst, inst->get_baseUnion()->get_members() ) {} | 
|---|
| [e4d829b] | 389 |  | 
|---|
|  | 390 | virtual operator bool() const { return (memberIter && *memberIter); } | 
|---|
|  | 391 | virtual MemberIterator & bigStep() { | 
|---|
|  | 392 | // unions only initialize one member | 
|---|
|  | 393 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) | 
|---|
|  | 394 | atbegin = false; | 
|---|
|  | 395 | delete memberIter; | 
|---|
|  | 396 | memberIter = nullptr; | 
|---|
|  | 397 | curType = nullptr; | 
|---|
| [62423350] | 398 | curMember = members.end(); | 
|---|
| [e4d829b] | 399 | return *this; | 
|---|
|  | 400 | } | 
|---|
|  | 401 | }; | 
|---|
|  | 402 |  | 
|---|
|  | 403 | class StructIterator : public AggregateIterator { | 
|---|
|  | 404 | public: | 
|---|
| [62423350] | 405 | StructIterator( StructInstType * inst ) : AggregateIterator( "StructIterator", inst->get_name(), inst, inst->get_baseStruct()->get_members() ) {} | 
|---|
|  | 406 |  | 
|---|
|  | 407 | virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); } | 
|---|
|  | 408 |  | 
|---|
|  | 409 | virtual MemberIterator & bigStep() { | 
|---|
|  | 410 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) | 
|---|
|  | 411 | atbegin = false; | 
|---|
|  | 412 | delete memberIter; | 
|---|
|  | 413 | memberIter = nullptr; | 
|---|
|  | 414 | curType = nullptr; | 
|---|
|  | 415 | for ( ; curMember != members.end(); ) { | 
|---|
|  | 416 | ++curMember; | 
|---|
|  | 417 | if ( init() ) { | 
|---|
|  | 418 | return *this; | 
|---|
|  | 419 | } | 
|---|
|  | 420 | } | 
|---|
|  | 421 | return *this; | 
|---|
|  | 422 | } | 
|---|
|  | 423 | }; | 
|---|
|  | 424 |  | 
|---|
|  | 425 | class TupleIterator : public AggregateIterator { | 
|---|
|  | 426 | public: | 
|---|
|  | 427 | TupleIterator( TupleType * inst ) : AggregateIterator( "TupleIterator", toString("Tuple", inst->size()), inst, inst->get_members() ) {} | 
|---|
| [e4d829b] | 428 |  | 
|---|
| [62423350] | 429 | virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); } | 
|---|
| [e4d829b] | 430 |  | 
|---|
|  | 431 | virtual MemberIterator & bigStep() { | 
|---|
|  | 432 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) | 
|---|
|  | 433 | atbegin = false; | 
|---|
|  | 434 | delete memberIter; | 
|---|
|  | 435 | memberIter = nullptr; | 
|---|
|  | 436 | curType = nullptr; | 
|---|
| [62423350] | 437 | for ( ; curMember != members.end(); ) { | 
|---|
| [e4d829b] | 438 | ++curMember; | 
|---|
|  | 439 | if ( init() ) { | 
|---|
|  | 440 | return *this; | 
|---|
|  | 441 | } | 
|---|
|  | 442 | } | 
|---|
|  | 443 | return *this; | 
|---|
|  | 444 | } | 
|---|
|  | 445 | }; | 
|---|
|  | 446 |  | 
|---|
|  | 447 | MemberIterator * createMemberIterator( Type * type ) { | 
|---|
|  | 448 | if ( ReferenceToType * aggr = dynamic_cast< ReferenceToType * >( type ) ) { | 
|---|
|  | 449 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( aggr ) ) { | 
|---|
|  | 450 | return new StructIterator( sit ); | 
|---|
|  | 451 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( aggr ) ) { | 
|---|
|  | 452 | return new UnionIterator( uit ); | 
|---|
|  | 453 | } else { | 
|---|
| [78754d7] | 454 | assertf( dynamic_cast< EnumInstType * >( type ) || dynamic_cast< TypeInstType * >( type ), "Encountered unhandled ReferenceToType in createMemberIterator: %s", toString( type ).c_str() ); | 
|---|
| [62423350] | 455 | return new SimpleIterator( type ); | 
|---|
| [e4d829b] | 456 | } | 
|---|
|  | 457 | } else if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { | 
|---|
|  | 458 | return new ArrayIterator( at ); | 
|---|
| [62423350] | 459 | } else if ( TupleType * tt = dynamic_cast< TupleType * >( type ) ) { | 
|---|
|  | 460 | return new TupleIterator( tt ); | 
|---|
| [e4d829b] | 461 | } else { | 
|---|
|  | 462 | return new SimpleIterator( type ); | 
|---|
|  | 463 | } | 
|---|
|  | 464 | } | 
|---|
|  | 465 |  | 
|---|
|  | 466 | CurrentObject::CurrentObject() {} | 
|---|
|  | 467 | CurrentObject::CurrentObject( Type * type ) { | 
|---|
|  | 468 | objStack.push( new SimpleIterator( type ) ); | 
|---|
|  | 469 | } | 
|---|
|  | 470 |  | 
|---|
|  | 471 |  | 
|---|
|  | 472 | void CurrentObject::setNext( Designation * designation ) { | 
|---|
|  | 473 | assertf( ! objStack.empty(), "obj stack empty in setNext" ); | 
|---|
|  | 474 | PRINT( std::cerr << "____setNext" << designation << std::endl; ) | 
|---|
|  | 475 | objStack.top()->setPosition( designation->get_designators() ); | 
|---|
|  | 476 | } | 
|---|
|  | 477 |  | 
|---|
|  | 478 | Designation * CurrentObject::findNext( Designation * designation ) { | 
|---|
|  | 479 | typedef std::list< Expression * > DesignatorChain; | 
|---|
|  | 480 | PRINT( std::cerr << "___findNext" << std::endl; ) | 
|---|
|  | 481 | // find all the d's | 
|---|
|  | 482 | std::list<DesignatorChain> desigAlts{ { } }, newDesigAlts; | 
|---|
|  | 483 | std::list<Type *> curTypes { (objStack.top())->getType() }, newTypes; | 
|---|
|  | 484 | for ( Expression * expr : designation->get_designators() ) { | 
|---|
|  | 485 | PRINT( std::cerr << "____untyped: " << expr << std::endl; ) | 
|---|
|  | 486 | std::list<DesignatorChain>::iterator dit = desigAlts.begin(); | 
|---|
|  | 487 | if ( NameExpr * nexpr = dynamic_cast<NameExpr *>(expr) ) { | 
|---|
|  | 488 | for ( Type * t : curTypes ) { | 
|---|
|  | 489 | assert( dit != desigAlts.end() ); | 
|---|
|  | 490 | DesignatorChain & d = *dit; | 
|---|
|  | 491 | PRINT( std::cerr << "____actual: " << t << std::endl; ) | 
|---|
|  | 492 | ReferenceToType * refType = dynamic_cast<ReferenceToType *>(t); | 
|---|
|  | 493 | std::list<Declaration *> members; | 
|---|
|  | 494 | if ( refType ) { | 
|---|
|  | 495 | refType->lookup( nexpr->get_name(), members ); // concatenate identical field name | 
|---|
|  | 496 | // xxx - need to also include anonymous members in this somehow... | 
|---|
|  | 497 | for ( Declaration * mem: members ) { | 
|---|
|  | 498 | if ( ObjectDecl * field = dynamic_cast<ObjectDecl *>(mem) ) { | 
|---|
|  | 499 | PRINT( std::cerr << "____alt: " << field->get_type() << std::endl; ) | 
|---|
|  | 500 | DesignatorChain newD = d; | 
|---|
|  | 501 | newD.push_back( new VariableExpr( field ) ); | 
|---|
|  | 502 | newDesigAlts.push_back( newD ); | 
|---|
|  | 503 | newTypes.push_back( field->get_type() ); | 
|---|
|  | 504 | } // if | 
|---|
|  | 505 | } // for | 
|---|
|  | 506 | } // if | 
|---|
|  | 507 | ++dit; | 
|---|
|  | 508 | } // for | 
|---|
|  | 509 | } else { | 
|---|
|  | 510 | for ( Type * t : curTypes ) { | 
|---|
|  | 511 | assert( dit != desigAlts.end() ); | 
|---|
|  | 512 | DesignatorChain & d = *dit; | 
|---|
|  | 513 | if ( ArrayType * at = dynamic_cast< ArrayType * > ( t ) ) { | 
|---|
|  | 514 | PRINT( std::cerr << "____alt: " << at->get_base() << std::endl; ) | 
|---|
|  | 515 | d.push_back( expr ); | 
|---|
|  | 516 | newDesigAlts.push_back( d ); | 
|---|
|  | 517 | newTypes.push_back( at->get_base() ); | 
|---|
|  | 518 | } | 
|---|
|  | 519 | ++dit; | 
|---|
|  | 520 | } // for | 
|---|
|  | 521 | } // if | 
|---|
|  | 522 | desigAlts = newDesigAlts; | 
|---|
|  | 523 | newDesigAlts.clear(); | 
|---|
|  | 524 | curTypes = newTypes; | 
|---|
|  | 525 | newTypes.clear(); | 
|---|
| [56e49b0] | 526 | assertf( desigAlts.size() == curTypes.size(), "Designator alternatives (%zu) and current types (%zu) out of sync", desigAlts.size(), curTypes.size() ); | 
|---|
| [e4d829b] | 527 | } // for | 
|---|
|  | 528 | if ( desigAlts.size() > 1 ) { | 
|---|
| [a16764a6] | 529 | SemanticError( designation, toString("Too many alternatives (", desigAlts.size(), ") for designation: ") ); | 
|---|
| [e4d829b] | 530 | } else if ( desigAlts.size() == 0 ) { | 
|---|
| [a16764a6] | 531 | SemanticError( designation, "No reasonable alternatives for designation: " ); | 
|---|
| [e4d829b] | 532 | } | 
|---|
|  | 533 | DesignatorChain & d = desigAlts.back(); | 
|---|
|  | 534 | PRINT( for ( Expression * expr : d ) { | 
|---|
|  | 535 | std::cerr << "____desig: " << expr << std::endl; | 
|---|
|  | 536 | } ) // for | 
|---|
|  | 537 | assertf( ! curTypes.empty(), "empty designator chosen"); | 
|---|
|  | 538 |  | 
|---|
|  | 539 | // set new designators | 
|---|
|  | 540 | assertf( ! objStack.empty(), "empty object stack when setting designation" ); | 
|---|
|  | 541 | Designation * actualDesignation = new Designation( d ); | 
|---|
|  | 542 | objStack.top()->setPosition( d ); // destroys d | 
|---|
|  | 543 | return actualDesignation; | 
|---|
|  | 544 | } | 
|---|
|  | 545 |  | 
|---|
|  | 546 | void CurrentObject::increment() { | 
|---|
|  | 547 | PRINT( std::cerr << "____increment" << std::endl; ) | 
|---|
|  | 548 | if ( ! objStack.empty() ) { | 
|---|
|  | 549 | PRINT( std::cerr << *objStack.top() << std::endl; ) | 
|---|
|  | 550 | objStack.top()->smallStep(); | 
|---|
|  | 551 | } | 
|---|
|  | 552 | } | 
|---|
|  | 553 |  | 
|---|
|  | 554 | void CurrentObject::enterListInit() { | 
|---|
|  | 555 | PRINT( std::cerr << "____entering list init" << std::endl; ) | 
|---|
|  | 556 | assertf( ! objStack.empty(), "empty obj stack entering list init" ); | 
|---|
|  | 557 | Type * type = objStack.top()->getNext(); | 
|---|
|  | 558 | if ( type ) { | 
|---|
|  | 559 | objStack.push( createMemberIterator( type ) ); | 
|---|
|  | 560 | } else { | 
|---|
|  | 561 | assertf( false, "not sure about this case..." ); | 
|---|
|  | 562 | } | 
|---|
|  | 563 | } | 
|---|
|  | 564 |  | 
|---|
|  | 565 | void CurrentObject::exitListInit() { | 
|---|
|  | 566 | PRINT( std::cerr << "____exiting list init" << std::endl; ) | 
|---|
|  | 567 | assertf( ! objStack.empty(), "objstack empty" ); | 
|---|
|  | 568 | delete objStack.top(); | 
|---|
|  | 569 | objStack.pop(); | 
|---|
|  | 570 | if ( ! objStack.empty() ) { | 
|---|
|  | 571 | PRINT( std::cerr << *objStack.top() << std::endl; ) | 
|---|
|  | 572 | objStack.top()->bigStep(); | 
|---|
|  | 573 | } | 
|---|
|  | 574 | } | 
|---|
|  | 575 |  | 
|---|
|  | 576 | std::list< InitAlternative > CurrentObject::getOptions() { | 
|---|
|  | 577 | PRINT( std::cerr << "____getting current options" << std::endl; ) | 
|---|
|  | 578 | assertf( ! objStack.empty(), "objstack empty in getOptions" ); | 
|---|
|  | 579 | return **objStack.top(); | 
|---|
|  | 580 | } | 
|---|
| [62423350] | 581 |  | 
|---|
|  | 582 | Type * CurrentObject::getCurrentType() { | 
|---|
|  | 583 | PRINT( std::cerr << "____getting current type" << std::endl; ) | 
|---|
|  | 584 | assertf( ! objStack.empty(), "objstack empty in getCurrentType" ); | 
|---|
|  | 585 | return objStack.top()->getNext(); | 
|---|
|  | 586 | } | 
|---|
| [e4d829b] | 587 | } // namespace ResolvExpr | 
|---|
|  | 588 |  | 
|---|
| [b7d92b96] | 589 | namespace ast { | 
|---|
| [60aaa51d] | 590 | /// create a new MemberIterator that traverses a type correctly | 
|---|
|  | 591 | MemberIterator * createMemberIterator( const CodeLocation & loc, const Type * type ); | 
|---|
| [b7d92b96] | 592 |  | 
|---|
|  | 593 | /// Iterates "other" types (e.g. basic, pointer) which do not change at list initializer entry | 
|---|
|  | 594 | class SimpleIterator final : public MemberIterator { | 
|---|
| [2b59f55] | 595 | CodeLocation location; | 
|---|
| [954c954] | 596 | const Type * type = nullptr; | 
|---|
| [b7d92b96] | 597 | public: | 
|---|
| [2b59f55] | 598 | SimpleIterator( const CodeLocation & loc, const Type * t ) : location( loc ), type( t ) {} | 
|---|
|  | 599 |  | 
|---|
| [2890212] | 600 | void setPosition( | 
|---|
|  | 601 | std::deque< ptr< Expr > >::const_iterator begin, | 
|---|
| [60aaa51d] | 602 | std::deque< ptr< Expr > >::const_iterator end | 
|---|
|  | 603 | ) override { | 
|---|
|  | 604 | if ( begin != end ) { | 
|---|
|  | 605 | SemanticError( location, "Un-designated initializer given non-empty designator" ); | 
|---|
|  | 606 | } | 
|---|
|  | 607 | } | 
|---|
| [2b59f55] | 608 |  | 
|---|
| [60aaa51d] | 609 | std::deque< InitAlternative > operator* () const override { return first(); } | 
|---|
|  | 610 |  | 
|---|
|  | 611 | operator bool() const override { return type; } | 
|---|
|  | 612 |  | 
|---|
|  | 613 | SimpleIterator & bigStep() override { return smallStep(); } | 
|---|
|  | 614 | SimpleIterator & smallStep() override { | 
|---|
|  | 615 | type = nullptr;  // empty on increment because no members | 
|---|
|  | 616 | return *this; | 
|---|
|  | 617 | } | 
|---|
|  | 618 |  | 
|---|
|  | 619 | const Type * getType() override { return type; } | 
|---|
|  | 620 |  | 
|---|
|  | 621 | const Type * getNext() override { return type; } | 
|---|
|  | 622 |  | 
|---|
|  | 623 | std::deque< InitAlternative > first() const override { | 
|---|
| [2b59f55] | 624 | if ( type ) return { InitAlternative{ type, new Designation{ location } } }; | 
|---|
|  | 625 | return {}; | 
|---|
|  | 626 | } | 
|---|
| [b7d92b96] | 627 | }; | 
|---|
|  | 628 |  | 
|---|
| [60aaa51d] | 629 | /// Iterates array types | 
|---|
|  | 630 | class ArrayIterator final : public MemberIterator { | 
|---|
|  | 631 | CodeLocation location; | 
|---|
| [954c954] | 632 | const ArrayType * array = nullptr; | 
|---|
|  | 633 | const Type * base = nullptr; | 
|---|
| [60aaa51d] | 634 | size_t index = 0; | 
|---|
|  | 635 | size_t size = 0; | 
|---|
|  | 636 | std::unique_ptr< MemberIterator > memberIter; | 
|---|
|  | 637 |  | 
|---|
|  | 638 | void setSize( const Expr * expr ) { | 
|---|
|  | 639 | auto res = eval(expr); | 
|---|
|  | 640 | if ( ! res.second ) { | 
|---|
| [2890212] | 641 | SemanticError( location, | 
|---|
| [60aaa51d] | 642 | toString("Array designator must be a constant expression: ", expr ) ); | 
|---|
|  | 643 | } | 
|---|
|  | 644 | size = res.first; | 
|---|
|  | 645 | } | 
|---|
|  | 646 |  | 
|---|
|  | 647 | public: | 
|---|
| [2890212] | 648 | ArrayIterator( const CodeLocation & loc, const ArrayType * at ) | 
|---|
| [60aaa51d] | 649 | : location( loc ), array( at ), base( at->base ) { | 
|---|
|  | 650 | PRINT( std::cerr << "Creating array iterator: " << at << std::endl; ) | 
|---|
|  | 651 | memberIter.reset( createMemberIterator( loc, base ) ); | 
|---|
|  | 652 | if ( at->isVarLen ) { | 
|---|
|  | 653 | SemanticError( location, at, "VLA initialization does not support @=: " ); | 
|---|
|  | 654 | } | 
|---|
|  | 655 | setSize( at->dimension ); | 
|---|
|  | 656 | } | 
|---|
|  | 657 |  | 
|---|
|  | 658 | void setPosition( const Expr * expr ) { | 
|---|
| [2890212] | 659 | // need to permit integer-constant-expressions, including: integer constants, | 
|---|
|  | 660 | // enumeration constants, character constants, sizeof expressions, alignof expressions, | 
|---|
| [60aaa51d] | 661 | // cast expressions | 
|---|
|  | 662 | if ( auto constExpr = dynamic_cast< const ConstantExpr * >( expr ) ) { | 
|---|
|  | 663 | try { | 
|---|
|  | 664 | index = constExpr->intValue(); | 
|---|
|  | 665 | } catch ( SemanticErrorException & ) { | 
|---|
| [2890212] | 666 | SemanticError( expr, | 
|---|
| [60aaa51d] | 667 | "Constant expression of non-integral type in array designator: " ); | 
|---|
|  | 668 | } | 
|---|
|  | 669 | } else if ( auto castExpr = dynamic_cast< const CastExpr * >( expr ) ) { | 
|---|
|  | 670 | setPosition( castExpr->arg ); | 
|---|
| [2890212] | 671 | } else if ( | 
|---|
|  | 672 | dynamic_cast< const SizeofExpr * >( expr ) | 
|---|
|  | 673 | || dynamic_cast< const AlignofExpr * >( expr ) | 
|---|
| [60aaa51d] | 674 | ) { | 
|---|
|  | 675 | index = 0; | 
|---|
|  | 676 | } else { | 
|---|
| [2890212] | 677 | assertf( false, | 
|---|
| [60aaa51d] | 678 | "bad designator given to ArrayIterator: %s", toString( expr ).c_str() ); | 
|---|
|  | 679 | } | 
|---|
|  | 680 | } | 
|---|
|  | 681 |  | 
|---|
| [2890212] | 682 | void setPosition( | 
|---|
|  | 683 | std::deque< ptr< Expr > >::const_iterator begin, | 
|---|
| [60aaa51d] | 684 | std::deque< ptr< Expr > >::const_iterator end | 
|---|
|  | 685 | ) override { | 
|---|
|  | 686 | if ( begin == end ) return; | 
|---|
|  | 687 |  | 
|---|
|  | 688 | setPosition( *begin ); | 
|---|
|  | 689 | memberIter->setPosition( ++begin, end ); | 
|---|
|  | 690 | } | 
|---|
|  | 691 |  | 
|---|
|  | 692 | std::deque< InitAlternative > operator* () const override { return first(); } | 
|---|
|  | 693 |  | 
|---|
|  | 694 | operator bool() const override { return index < size; } | 
|---|
|  | 695 |  | 
|---|
|  | 696 | ArrayIterator & bigStep() override { | 
|---|
|  | 697 | PRINT( std::cerr << "bigStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) | 
|---|
|  | 698 | ++index; | 
|---|
|  | 699 | memberIter.reset( index < size ? createMemberIterator( location, base ) : nullptr ); | 
|---|
|  | 700 | return *this; | 
|---|
|  | 701 | } | 
|---|
|  | 702 |  | 
|---|
|  | 703 | ArrayIterator & smallStep() override { | 
|---|
|  | 704 | PRINT( std::cerr << "smallStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) | 
|---|
|  | 705 | if ( memberIter ) { | 
|---|
|  | 706 | PRINT( std::cerr << "has member iter: " << *memberIter << std::endl; ) | 
|---|
|  | 707 | memberIter->smallStep(); | 
|---|
|  | 708 | if ( *memberIter ) { | 
|---|
|  | 709 | PRINT( std::cerr << "has valid member iter" << std::endl; ) | 
|---|
|  | 710 | return *this; | 
|---|
|  | 711 | } | 
|---|
|  | 712 | } | 
|---|
|  | 713 | return bigStep(); | 
|---|
|  | 714 | } | 
|---|
|  | 715 |  | 
|---|
|  | 716 | const Type * getType() override { return array; } | 
|---|
|  | 717 |  | 
|---|
|  | 718 | const Type * getNext() override { return base; } | 
|---|
|  | 719 |  | 
|---|
|  | 720 | std::deque< InitAlternative > first() const override { | 
|---|
|  | 721 | PRINT( std::cerr << "first in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) | 
|---|
|  | 722 | if ( memberIter && *memberIter ) { | 
|---|
|  | 723 | std::deque< InitAlternative > ret = memberIter->first(); | 
|---|
|  | 724 | for ( InitAlternative & alt : ret ) { | 
|---|
|  | 725 | alt.designation.get_and_mutate()->designators.emplace_front( | 
|---|
|  | 726 | ConstantExpr::from_ulong( location, index ) ); | 
|---|
|  | 727 | } | 
|---|
|  | 728 | return ret; | 
|---|
|  | 729 | } | 
|---|
|  | 730 | return {}; | 
|---|
|  | 731 | } | 
|---|
|  | 732 | }; | 
|---|
|  | 733 |  | 
|---|
|  | 734 | class AggregateIterator : public MemberIterator { | 
|---|
|  | 735 | protected: | 
|---|
|  | 736 | using MemberList = std::vector< ptr< Decl > >; | 
|---|
|  | 737 |  | 
|---|
|  | 738 | CodeLocation location; | 
|---|
|  | 739 | std::string kind;  // for debug | 
|---|
|  | 740 | std::string name; | 
|---|
|  | 741 | const Type * inst; | 
|---|
|  | 742 | const MemberList & members; | 
|---|
|  | 743 | MemberList::const_iterator curMember; | 
|---|
|  | 744 | bool atbegin = true;  // false at first {small,big}Step | 
|---|
|  | 745 | const Type * curType = nullptr; | 
|---|
|  | 746 | std::unique_ptr< MemberIterator > memberIter = nullptr; | 
|---|
|  | 747 | TypeSubstitution sub; | 
|---|
|  | 748 |  | 
|---|
|  | 749 | bool init() { | 
|---|
|  | 750 | PRINT( std::cerr << "--init()--" << members.size() << std::endl; ) | 
|---|
|  | 751 | if ( curMember != members.end() ) { | 
|---|
|  | 752 | if ( auto field = curMember->as< ObjectDecl >() ) { | 
|---|
|  | 753 | PRINT( std::cerr << "incremented to field: " << field << std::endl; ) | 
|---|
|  | 754 | curType = field->get_type(); | 
|---|
|  | 755 | memberIter.reset( createMemberIterator( location, curType ) ); | 
|---|
|  | 756 | return true; | 
|---|
|  | 757 | } | 
|---|
|  | 758 | } | 
|---|
|  | 759 | return false; | 
|---|
|  | 760 | } | 
|---|
|  | 761 |  | 
|---|
| [2890212] | 762 | AggregateIterator( | 
|---|
|  | 763 | const CodeLocation & loc, const std::string k, const std::string & n, const Type * i, | 
|---|
| [60aaa51d] | 764 | const MemberList & ms ) | 
|---|
| [2890212] | 765 | : location( loc ), kind( k ), name( n ), inst( i ), members( ms ), curMember( ms.begin() ), | 
|---|
| [60aaa51d] | 766 | sub( genericSubstitution( i ) ) { | 
|---|
|  | 767 | PRINT( std::cerr << "Creating " << kind << "(" << name << ")"; ) | 
|---|
|  | 768 | init(); | 
|---|
|  | 769 | } | 
|---|
|  | 770 |  | 
|---|
|  | 771 | public: | 
|---|
| [2890212] | 772 | void setPosition( | 
|---|
|  | 773 | std::deque< ptr< Expr > >::const_iterator begin, | 
|---|
| [60aaa51d] | 774 | std::deque< ptr< Expr > >::const_iterator end | 
|---|
|  | 775 | ) final { | 
|---|
|  | 776 | if ( begin == end ) return; | 
|---|
|  | 777 |  | 
|---|
|  | 778 | if ( auto varExpr = begin->as< VariableExpr >() ) { | 
|---|
|  | 779 | for ( curMember = members.begin(); curMember != members.end(); ++curMember ) { | 
|---|
|  | 780 | if ( *curMember != varExpr->var ) continue; | 
|---|
|  | 781 |  | 
|---|
|  | 782 | ++begin; | 
|---|
|  | 783 |  | 
|---|
|  | 784 | memberIter.reset( createMemberIterator( location, varExpr->result ) ); | 
|---|
|  | 785 | curType = varExpr->result; | 
|---|
|  | 786 | atbegin = curMember == members.begin() && begin == end; | 
|---|
|  | 787 | memberIter->setPosition( begin, end ); | 
|---|
|  | 788 | return; | 
|---|
|  | 789 | } | 
|---|
| [2890212] | 790 | assertf( false, | 
|---|
| [60aaa51d] | 791 | "could not find member in %s: %s", kind.c_str(), toString( varExpr ).c_str() ); | 
|---|
|  | 792 | } else { | 
|---|
| [2890212] | 793 | assertf( false, | 
|---|
| [60aaa51d] | 794 | "bad designator given to %s: %s", kind.c_str(), toString( *begin ).c_str() ); | 
|---|
|  | 795 | } | 
|---|
|  | 796 | } | 
|---|
|  | 797 |  | 
|---|
|  | 798 | std::deque< InitAlternative > operator* () const final { | 
|---|
|  | 799 | if ( memberIter && *memberIter ) { | 
|---|
|  | 800 | std::deque< InitAlternative > ret = memberIter->first(); | 
|---|
|  | 801 | PRINT( std::cerr << "sub: " << sub << std::endl; ) | 
|---|
|  | 802 | for ( InitAlternative & alt : ret ) { | 
|---|
|  | 803 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) | 
|---|
|  | 804 | alt.designation.get_and_mutate()->designators.emplace_front( | 
|---|
|  | 805 | new VariableExpr{ location, curMember->strict_as< ObjectDecl >() } ); | 
|---|
|  | 806 | // need to substitute for generic types so that casts are to concrete types | 
|---|
| [8e1467d] | 807 | alt.type = shallowCopy(alt.type.get()); | 
|---|
| [60aaa51d] | 808 | PRINT( std::cerr << "  type is: " << alt.type; ) | 
|---|
|  | 809 | sub.apply( alt.type ); // also apply to designation?? | 
|---|
|  | 810 | PRINT( std::cerr << " ==> " << alt.type << std::endl; ) | 
|---|
|  | 811 | } | 
|---|
|  | 812 | return ret; | 
|---|
|  | 813 | } | 
|---|
|  | 814 | return {}; | 
|---|
|  | 815 | } | 
|---|
|  | 816 |  | 
|---|
|  | 817 | AggregateIterator & smallStep() final { | 
|---|
|  | 818 | PRINT( std::cerr << "smallStep in " << kind << std::endl; ) | 
|---|
|  | 819 | atbegin = false; | 
|---|
|  | 820 | if ( memberIter ) { | 
|---|
|  | 821 | PRINT( std::cerr << "has member iter, incrementing..." << std::endl; ) | 
|---|
|  | 822 | memberIter->smallStep(); | 
|---|
|  | 823 | if ( *memberIter ) { | 
|---|
|  | 824 | PRINT( std::cerr << "success!" << std::endl; ) | 
|---|
|  | 825 | return *this; | 
|---|
|  | 826 | } | 
|---|
|  | 827 | } | 
|---|
|  | 828 | return bigStep(); | 
|---|
|  | 829 | } | 
|---|
|  | 830 |  | 
|---|
|  | 831 | AggregateIterator & bigStep() override = 0; | 
|---|
|  | 832 |  | 
|---|
|  | 833 | const Type * getType() final { return inst; } | 
|---|
|  | 834 |  | 
|---|
|  | 835 | const Type * getNext() final { | 
|---|
|  | 836 | return ( memberIter && *memberIter ) ? memberIter->getType() : nullptr; | 
|---|
|  | 837 | } | 
|---|
|  | 838 |  | 
|---|
|  | 839 | std::deque< InitAlternative > first() const final { | 
|---|
|  | 840 | std::deque< InitAlternative > ret; | 
|---|
|  | 841 | PRINT( std::cerr << "first " << kind << std::endl; ) | 
|---|
|  | 842 | if ( memberIter && *memberIter ) { | 
|---|
|  | 843 | PRINT( std::cerr << "adding children" << std::endl; ) | 
|---|
|  | 844 | ret = memberIter->first(); | 
|---|
|  | 845 | for ( InitAlternative & alt : ret ) { | 
|---|
|  | 846 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) | 
|---|
| [2890212] | 847 | alt.designation.get_and_mutate()->designators.emplace_front( | 
|---|
| [60aaa51d] | 848 | new VariableExpr{ location, curMember->strict_as< ObjectDecl >() } ); | 
|---|
|  | 849 | } | 
|---|
|  | 850 | } | 
|---|
|  | 851 | if ( atbegin ) { | 
|---|
|  | 852 | // only add self if at the very beginning of the structure | 
|---|
|  | 853 | PRINT( std::cerr << "adding self" << std::endl; ) | 
|---|
|  | 854 | ret.emplace_front( inst, new Designation{ location } ); | 
|---|
|  | 855 | } | 
|---|
|  | 856 | return ret; | 
|---|
|  | 857 | } | 
|---|
|  | 858 | }; | 
|---|
|  | 859 |  | 
|---|
|  | 860 | class StructIterator final : public AggregateIterator { | 
|---|
|  | 861 | public: | 
|---|
|  | 862 | StructIterator( const CodeLocation & loc, const StructInstType * inst ) | 
|---|
|  | 863 | : AggregateIterator( loc, "StructIterator", inst->name, inst, inst->base->members ) {} | 
|---|
|  | 864 |  | 
|---|
|  | 865 | operator bool() const override { | 
|---|
|  | 866 | return curMember != members.end() || (memberIter && *memberIter); | 
|---|
|  | 867 | } | 
|---|
|  | 868 |  | 
|---|
|  | 869 | StructIterator & bigStep() override { | 
|---|
|  | 870 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) | 
|---|
|  | 871 | atbegin = false; | 
|---|
|  | 872 | memberIter = nullptr; | 
|---|
|  | 873 | curType = nullptr; | 
|---|
|  | 874 | while ( curMember != members.end() ) { | 
|---|
|  | 875 | ++curMember; | 
|---|
|  | 876 | if ( init() ) return *this; | 
|---|
|  | 877 | } | 
|---|
|  | 878 | return *this; | 
|---|
|  | 879 | } | 
|---|
|  | 880 | }; | 
|---|
|  | 881 |  | 
|---|
|  | 882 | class UnionIterator final : public AggregateIterator { | 
|---|
|  | 883 | public: | 
|---|
|  | 884 | UnionIterator( const CodeLocation & loc, const UnionInstType * inst ) | 
|---|
|  | 885 | : AggregateIterator( loc, "UnionIterator", inst->name, inst, inst->base->members ) {} | 
|---|
|  | 886 |  | 
|---|
|  | 887 | operator bool() const override { return memberIter && *memberIter; } | 
|---|
|  | 888 |  | 
|---|
|  | 889 | UnionIterator & bigStep() override { | 
|---|
|  | 890 | // unions only initialize one member | 
|---|
|  | 891 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) | 
|---|
|  | 892 | atbegin = false; | 
|---|
|  | 893 | memberIter = nullptr; | 
|---|
|  | 894 | curType = nullptr; | 
|---|
|  | 895 | curMember = members.end(); | 
|---|
|  | 896 | return *this; | 
|---|
|  | 897 | } | 
|---|
|  | 898 | }; | 
|---|
|  | 899 |  | 
|---|
|  | 900 | class TupleIterator final : public AggregateIterator { | 
|---|
|  | 901 | public: | 
|---|
| [2890212] | 902 | TupleIterator( const CodeLocation & loc, const TupleType * inst ) | 
|---|
|  | 903 | : AggregateIterator( | 
|---|
|  | 904 | loc, "TupleIterator", toString("Tuple", inst->size()), inst, inst->members | 
|---|
| [60aaa51d] | 905 | ) {} | 
|---|
|  | 906 |  | 
|---|
|  | 907 | operator bool() const override { | 
|---|
|  | 908 | return curMember != members.end() || (memberIter && *memberIter); | 
|---|
|  | 909 | } | 
|---|
|  | 910 |  | 
|---|
|  | 911 | TupleIterator & bigStep() override { | 
|---|
|  | 912 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) | 
|---|
|  | 913 | atbegin = false; | 
|---|
|  | 914 | memberIter = nullptr; | 
|---|
|  | 915 | curType = nullptr; | 
|---|
|  | 916 | while ( curMember != members.end() ) { | 
|---|
|  | 917 | ++curMember; | 
|---|
|  | 918 | if ( init() ) return *this; | 
|---|
|  | 919 | } | 
|---|
|  | 920 | return *this; | 
|---|
|  | 921 | } | 
|---|
|  | 922 | }; | 
|---|
|  | 923 |  | 
|---|
|  | 924 | MemberIterator * createMemberIterator( const CodeLocation & loc, const Type * type ) { | 
|---|
| [98e8b3b] | 925 | if ( auto aggr = dynamic_cast< const BaseInstType * >( type ) ) { | 
|---|
| [60aaa51d] | 926 | if ( auto sit = dynamic_cast< const StructInstType * >( aggr ) ) { | 
|---|
|  | 927 | return new StructIterator{ loc, sit }; | 
|---|
|  | 928 | } else if ( auto uit = dynamic_cast< const UnionInstType * >( aggr ) ) { | 
|---|
|  | 929 | return new UnionIterator{ loc, uit }; | 
|---|
|  | 930 | } else { | 
|---|
| [2890212] | 931 | assertf( | 
|---|
|  | 932 | dynamic_cast< const EnumInstType * >( type ) | 
|---|
|  | 933 | || dynamic_cast< const TypeInstType * >( type ), | 
|---|
| [98e8b3b] | 934 | "Encountered unhandled BaseInstType in createMemberIterator: %s", | 
|---|
| [60aaa51d] | 935 | toString( type ).c_str() ); | 
|---|
|  | 936 | return new SimpleIterator{ loc, type }; | 
|---|
|  | 937 | } | 
|---|
|  | 938 | } else if ( auto at = dynamic_cast< const ArrayType * >( type ) ) { | 
|---|
|  | 939 | return new ArrayIterator{ loc, at }; | 
|---|
|  | 940 | } else if ( auto tt = dynamic_cast< const TupleType * >( type ) ) { | 
|---|
|  | 941 | return new TupleIterator{ loc, tt }; | 
|---|
|  | 942 | } else { | 
|---|
|  | 943 | return new SimpleIterator{ loc, type }; | 
|---|
|  | 944 | } | 
|---|
|  | 945 | } | 
|---|
|  | 946 |  | 
|---|
| [2b59f55] | 947 | CurrentObject::CurrentObject( const CodeLocation & loc, const Type * type ) : objStack() { | 
|---|
|  | 948 | objStack.emplace_back( new SimpleIterator{ loc, type } ); | 
|---|
| [b7d92b96] | 949 | } | 
|---|
|  | 950 |  | 
|---|
| [2d11663] | 951 | const Designation * CurrentObject::findNext( const Designation * designation ) { | 
|---|
|  | 952 | using DesignatorChain = std::deque< ptr< Expr > >; | 
|---|
|  | 953 | PRINT( std::cerr << "___findNext" << std::endl; ) | 
|---|
| [2890212] | 954 |  | 
|---|
| [2d11663] | 955 | // find all the d's | 
|---|
|  | 956 | std::vector< DesignatorChain > desigAlts{ {} }, newDesigAlts; | 
|---|
|  | 957 | std::deque< const Type * > curTypes{ objStack.back()->getType() }, newTypes; | 
|---|
|  | 958 | for ( const Expr * expr : designation->designators ) { | 
|---|
|  | 959 | PRINT( std::cerr << "____untyped: " << expr << std::endl; ) | 
|---|
|  | 960 | auto dit = desigAlts.begin(); | 
|---|
|  | 961 | if ( auto nexpr = dynamic_cast< const NameExpr * >( expr ) ) { | 
|---|
|  | 962 | for ( const Type * t : curTypes ) { | 
|---|
|  | 963 | assert( dit != desigAlts.end() ); | 
|---|
|  | 964 |  | 
|---|
|  | 965 | DesignatorChain & d = *dit; | 
|---|
|  | 966 | PRINT( std::cerr << "____actual: " << t << std::endl; ) | 
|---|
| [98e8b3b] | 967 | if ( auto refType = dynamic_cast< const BaseInstType * >( t ) ) { | 
|---|
| [2d11663] | 968 | // concatenate identical field names | 
|---|
|  | 969 | for ( const Decl * mem : refType->lookup( nexpr->name ) ) { | 
|---|
|  | 970 | if ( auto field = dynamic_cast< const ObjectDecl * >( mem ) ) { | 
|---|
|  | 971 | PRINT( std::cerr << "____alt: " << field->type << std::endl; ) | 
|---|
|  | 972 | DesignatorChain d2 = d; | 
|---|
|  | 973 | d2.emplace_back( new VariableExpr{ expr->location, field } ); | 
|---|
|  | 974 | newDesigAlts.emplace_back( std::move( d2 ) ); | 
|---|
|  | 975 | newTypes.emplace_back( field->type ); | 
|---|
|  | 976 | } | 
|---|
|  | 977 | } | 
|---|
|  | 978 | } | 
|---|
|  | 979 |  | 
|---|
|  | 980 | ++dit; | 
|---|
|  | 981 | } | 
|---|
|  | 982 | } else { | 
|---|
|  | 983 | for ( const Type * t : curTypes ) { | 
|---|
|  | 984 | assert( dit != desigAlts.end() ); | 
|---|
|  | 985 |  | 
|---|
|  | 986 | DesignatorChain & d = *dit; | 
|---|
|  | 987 | if ( auto at = dynamic_cast< const ArrayType * >( t ) ) { | 
|---|
|  | 988 | PRINT( std::cerr << "____alt: " << at->get_base() << std::endl; ) | 
|---|
|  | 989 | d.emplace_back( expr ); | 
|---|
|  | 990 | newDesigAlts.emplace_back( d ); | 
|---|
|  | 991 | newTypes.emplace_back( at->base ); | 
|---|
|  | 992 | } | 
|---|
|  | 993 | } | 
|---|
|  | 994 | } | 
|---|
|  | 995 |  | 
|---|
|  | 996 | // reset queue | 
|---|
|  | 997 | desigAlts = std::move( newDesigAlts ); | 
|---|
|  | 998 | newDesigAlts.clear(); | 
|---|
|  | 999 | curTypes = std::move( newTypes ); | 
|---|
|  | 1000 | newTypes.clear(); | 
|---|
|  | 1001 | assertf( desigAlts.size() == curTypes.size(), "Designator alternatives (%zu) and current types (%zu) out of sync", desigAlts.size(), curTypes.size() ); | 
|---|
|  | 1002 | } | 
|---|
|  | 1003 |  | 
|---|
|  | 1004 | if ( desigAlts.size() > 1 ) { | 
|---|
|  | 1005 | SemanticError( designation, toString("Too many alternatives (", desigAlts.size(), ") for designation: ") ); | 
|---|
|  | 1006 | } else if ( desigAlts.empty() ) { | 
|---|
|  | 1007 | SemanticError( designation, "No reasonable alternatives for designation: " ); | 
|---|
|  | 1008 | } | 
|---|
|  | 1009 |  | 
|---|
|  | 1010 | DesignatorChain & d = desigAlts.back(); | 
|---|
|  | 1011 | PRINT( for ( Expression * expr : d ) { | 
|---|
|  | 1012 | std::cerr << "____desig: " << expr << std::endl; | 
|---|
|  | 1013 | } ) // for | 
|---|
|  | 1014 | assertf( ! curTypes.empty(), "empty designator chosen"); | 
|---|
|  | 1015 |  | 
|---|
|  | 1016 | // set new designators | 
|---|
|  | 1017 | assertf( ! objStack.empty(), "empty object stack when setting designation" ); | 
|---|
| [2890212] | 1018 | Designation * actualDesignation = | 
|---|
| [2d11663] | 1019 | new Designation{ designation->location, DesignatorChain{d} }; | 
|---|
|  | 1020 | objStack.back()->setPosition( d ); // destroys d | 
|---|
|  | 1021 | return actualDesignation; | 
|---|
|  | 1022 | } | 
|---|
|  | 1023 |  | 
|---|
|  | 1024 | void CurrentObject::setNext( const Designation * designation ) { | 
|---|
| [60aaa51d] | 1025 | PRINT( std::cerr << "____setNext" << designation << std::endl; ) | 
|---|
|  | 1026 | assertf( ! objStack.empty(), "obj stack empty in setNext" ); | 
|---|
|  | 1027 | objStack.back()->setPosition( designation->designators ); | 
|---|
|  | 1028 | } | 
|---|
|  | 1029 |  | 
|---|
|  | 1030 | void CurrentObject::increment() { | 
|---|
|  | 1031 | PRINT( std::cerr << "____increment" << std::endl; ) | 
|---|
|  | 1032 | if ( objStack.empty() ) return; | 
|---|
|  | 1033 | PRINT( std::cerr << *objStack.back() << std::endl; ) | 
|---|
|  | 1034 | objStack.back()->smallStep(); | 
|---|
|  | 1035 | } | 
|---|
|  | 1036 |  | 
|---|
|  | 1037 | void CurrentObject::enterListInit( const CodeLocation & loc ) { | 
|---|
|  | 1038 | PRINT( std::cerr << "____entering list init" << std::endl; ) | 
|---|
|  | 1039 | assertf( ! objStack.empty(), "empty obj stack entering list init" ); | 
|---|
|  | 1040 | const ast::Type * type = objStack.back()->getNext(); | 
|---|
|  | 1041 | assert( type ); | 
|---|
|  | 1042 | objStack.emplace_back( createMemberIterator( loc, type ) ); | 
|---|
|  | 1043 | } | 
|---|
|  | 1044 |  | 
|---|
|  | 1045 | void CurrentObject::exitListInit() { | 
|---|
|  | 1046 | PRINT( std::cerr << "____exiting list init" << std::endl; ) | 
|---|
|  | 1047 | assertf( ! objStack.empty(), "objstack empty" ); | 
|---|
|  | 1048 | objStack.pop_back(); | 
|---|
|  | 1049 | if ( ! objStack.empty() ) { | 
|---|
|  | 1050 | PRINT( std::cerr << *objStack.back() << std::endl; ) | 
|---|
|  | 1051 | objStack.back()->bigStep(); | 
|---|
|  | 1052 | } | 
|---|
|  | 1053 | } | 
|---|
|  | 1054 |  | 
|---|
|  | 1055 | std::deque< InitAlternative > CurrentObject::getOptions() { | 
|---|
| [2b59f55] | 1056 | PRINT( std::cerr << "____getting current options" << std::endl; ) | 
|---|
|  | 1057 | assertf( ! objStack.empty(), "objstack empty in getOptions" ); | 
|---|
|  | 1058 | return **objStack.back(); | 
|---|
|  | 1059 | } | 
|---|
| [60aaa51d] | 1060 |  | 
|---|
|  | 1061 | const Type * CurrentObject::getCurrentType() { | 
|---|
|  | 1062 | PRINT( std::cerr << "____getting current type" << std::endl; ) | 
|---|
|  | 1063 | assertf( ! objStack.empty(), "objstack empty in getCurrentType" ); | 
|---|
|  | 1064 | return objStack.back()->getNext(); | 
|---|
|  | 1065 | } | 
|---|
| [b7d92b96] | 1066 | } | 
|---|
|  | 1067 |  | 
|---|
| [e4d829b] | 1068 | // Local Variables: // | 
|---|
|  | 1069 | // tab-width: 4 // | 
|---|
|  | 1070 | // mode: c++ // | 
|---|
|  | 1071 | // compile-command: "make install" // | 
|---|
|  | 1072 | // End: // | 
|---|