- Timestamp:
- Oct 26, 2024, 8:17:20 AM (8 weeks ago)
- Branches:
- master
- Children:
- 14c31eb
- Parents:
- 3a08cb1 (diff), d031f7f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Decl.cpp
r3a08cb1 r06d75931 169 169 } 170 170 171 const std::string EnumDecl::getUnmangeldArrayName( const ast::EnumAttribute attr ) const {172 switch( attr ) {173 case ast::EnumAttribute::Value: return "values_" + name ;174 case ast::EnumAttribute::Label: return "labels_" + name;175 default: /* Posn does not generate array */176 return "";177 }178 }179 180 unsigned EnumDecl::calChildOffset(const std::string & target) const{181 unsigned offset = 0;182 for (auto childEnum: inlinedDecl) {183 auto childDecl = childEnum->base;184 if (childDecl->name == target) {185 return offset;186 }187 offset += childDecl->members.size();188 }189 std::cerr << "Cannot find the target enum" << std::endl;190 return 0;191 }192 193 unsigned EnumDecl::calChildOffset(const ast::EnumInstType * target) const{194 return calChildOffset(target->base->name);195 }196 197 bool EnumDecl::isSubTypeOf(const ast::EnumDecl * other) const {198 if (name == other->name) return true;199 for (auto inlined: other->inlinedDecl) {200 if (isSubTypeOf(inlined->base)) return true;201 }202 return false;203 }204 205 171 bool EnumDecl::isTyped() const { return base; } 206 172 -
src/AST/Decl.hpp
r3a08cb1 r06d75931 302 302 }; 303 303 304 /// Enumeration attribute kind. 304 305 enum class EnumAttribute{ Value, Posn, Label }; 306 305 307 /// enum declaration `enum Foo { ... };` 306 308 class EnumDecl final : public AggregateDecl { … … 328 330 const char * typeString() const override { return aggrString( Enum ); } 329 331 330 const std::string getUnmangeldArrayName( const EnumAttribute attr ) const;331 332 unsigned calChildOffset(const std::string & childEnum) const;333 unsigned calChildOffset(const ast::EnumInstType * childEnum) const;334 335 bool isSubTypeOf(const ast::EnumDecl *) const;336 332 bool isTyped() const; 337 333 bool isOpaque() const; -
src/ResolvExpr/CandidateFinder.cpp
r3a08cb1 r06d75931 2136 2136 } 2137 2137 2138 /// If the target enum is a child, get the offset from the base to the target. 2139 static unsigned findChildOffset( 2140 const ast::EnumDecl * decl, const ast::EnumDecl * target ) { 2141 unsigned offset = 0; 2142 for ( auto inlined : decl->inlinedDecl ) { 2143 auto childDecl = inlined->base; 2144 if ( childDecl == target ) { 2145 return offset; 2146 } 2147 offset += childDecl->members.size(); 2148 } 2149 SemanticError( decl, "Cannot find the target enum." ); 2150 } 2151 2138 2152 const ast::Expr * CandidateFinder::makeEnumOffsetCast( const ast::EnumInstType * src, 2139 2153 const ast::EnumInstType * dst, const ast::Expr * expr, Cost minCost ) { … … 2147 2161 ast::CastExpr * castToDst; 2148 2162 if (c<minCost) { 2149 unsigned offset = dstDecl->calChildOffset(dstChild.get());2163 unsigned offset = findChildOffset( dstDecl, dstChild.get()->base ); 2150 2164 if (offset > 0) { 2151 2165 auto untyped = ast::UntypedExpr::createCall( 2152 expr->location, 2153 "?+?", 2166 expr->location, 2167 "?+?", 2154 2168 { new ast::CastExpr( expr->location, 2155 2169 expr, -
src/ResolvExpr/CommonType.cpp
r3a08cb1 r06d75931 636 636 void postvisit( const ast::UnionInstType * ) {} 637 637 638 /// Is the target enum a child of the base enum? 639 static bool isChildEnum( 640 const ast::EnumDecl * decl, const ast::EnumDecl * target ) { 641 if ( decl == target ) return true; 642 for ( auto inlined : decl->inlinedDecl ) { 643 if ( isChildEnum( inlined->base, target ) ) return true; 644 } 645 return false; 646 } 647 638 648 void postvisit( const ast::EnumInstType * param ) { 639 649 auto argAsEnumInst = dynamic_cast<const ast::EnumInstType *>(type2); … … 641 651 const ast::EnumDecl* paramDecl = param->base; 642 652 const ast::EnumDecl* argDecl = argAsEnumInst->base; 643 if (argDecl->isSubTypeOf(paramDecl)) result = param; 653 if ( isChildEnum( paramDecl, argDecl ) ) { 654 result = param; 655 } 644 656 } else if ( param->base && !param->base->isCfa ) { 645 657 auto basicType = new ast::BasicType( ast::BasicKind::UnsignedInt ); -
src/ResolvExpr/CurrentObject.cpp
r3a08cb1 r06d75931 60 60 /// Retrieve the list of possible (Type,Designation) pairs for the 61 61 /// current position in the current object. 62 virtual std::deque< InitAlternative > operator*() const = 0;62 virtual std::deque< InitAlternative > getOptions() const = 0; 63 63 64 64 /// True if the iterator is not currently at the end. … … 77 77 virtual const Type * getNext() = 0; 78 78 79 /// Helper for operator*; aggregates must add designator to each init80 /// alternative, but adding designators in operator*creates duplicates.79 /// Helper for getOptions; aggregates must add designator to each init 80 /// alternative, but adding designators in getOptions creates duplicates. 81 81 virtual std::deque< InitAlternative > first() const = 0; 82 82 }; … … 103 103 } 104 104 105 std::deque< InitAlternative > operator*() const override { return first(); }105 std::deque< InitAlternative > getOptions() const override { return first(); } 106 106 107 107 operator bool() const override { return type; } … … 169 169 } 170 170 171 std::deque< InitAlternative > operator*() const override { return first(); }171 std::deque< InitAlternative > getOptions() const override { return first(); } 172 172 173 173 operator bool() const override { return index < size; } … … 297 297 } 298 298 299 std::deque< InitAlternative > operator*() const final {299 std::deque< InitAlternative > getOptions() const final { 300 300 if ( memberIter && *memberIter ) { 301 301 std::deque< InitAlternative > ret = memberIter->first(); … … 594 594 PRINT( std::cerr << "____getting current options" << std::endl; ) 595 595 assertf( ! objStack.empty(), "objstack empty in getOptions" ); 596 return **objStack.back();596 return objStack.back()->getOptions(); 597 597 } 598 598 -
src/Validate/ImplementEnumFunc.cpp
r3a08cb1 r06d75931 78 78 std::vector<ast::ptr<ast::Init>> genValueInit() const; 79 79 ast::ObjectDecl* genAttrArrayProto( 80 const ast::EnumAttribute attr, const CodeLocation& location,81 std::vector<ast::ptr<ast::Init>>& inits) const;80 const CodeLocation& location, const std::string& prefix, 81 const ast::Type * type, std::vector<ast::ptr<ast::Init>>& inits ) const; 82 82 }; 83 83 … … 351 351 352 352 ast::ObjectDecl* EnumAttrFuncGenerator::genAttrArrayProto( 353 const ast::EnumAttribute attr, const CodeLocation& location,354 std::vector<ast::ptr<ast::Init>>& inits) const {353 const CodeLocation& location, const std::string& prefix, 354 const ast::Type * type, std::vector<ast::ptr<ast::Init>>& inits ) const { 355 355 ast::ArrayType* arrT = new ast::ArrayType( 356 attr == ast::EnumAttribute::Value 357 ? decl->base 358 : new ast::PointerType( 359 new ast::BasicType(ast::BasicKind::Char, ast::CV::Const)), 356 type, 360 357 ast::ConstantExpr::from_int(decl->location, decl->members.size()), 361 358 ast::LengthFlag::FixedLen, ast::DimensionFlag::DynamicDim); … … 363 360 ast::ObjectDecl* objDecl = 364 361 new ast::ObjectDecl( 365 decl->location, decl->getUnmangeldArrayName( attr ),362 decl->location, prefix + decl->name, 366 363 arrT, new ast::ListInit( location, std::move( inits ) ), 367 364 ast::Storage::Static, ast::Linkage::AutoGen ); … … 417 414 void EnumAttrFuncGenerator::genTypedEnumFunction(const ast::EnumAttribute attr) { 418 415 if (attr == ast::EnumAttribute::Value) { 419 if (decl->isTyped()) { 420 // TypedEnum's backing arrays 421 std::vector<ast::ptr<ast::Init>> inits = genValueInit(); 422 ast::ObjectDecl* arrayProto = 423 genAttrArrayProto(attr, getLocation(), inits); 424 forwards.push_back(arrayProto); 425 426 ast::FunctionDecl* funcProto = genValueProto(); 427 produceForwardDecl(funcProto); 428 genValueOrLabelBody(funcProto, arrayProto); 429 produceDecl(funcProto); 430 } 431 // else { 432 // ast::FunctionDecl* funcProto = genQuasiValueProto(); 433 // produceForwardDecl(funcProto); 434 // // genQuasiValueBody(funcProto); 435 // produceDecl(funcProto); 436 // } 416 if ( !decl->isTyped() ) return; 417 std::vector<ast::ptr<ast::Init>> inits = genValueInit(); 418 ast::ObjectDecl* arrayProto = 419 genAttrArrayProto( getLocation(), "values_", decl->base, inits ); 420 forwards.push_back(arrayProto); 421 422 ast::FunctionDecl* funcProto = genValueProto(); 423 produceForwardDecl(funcProto); 424 genValueOrLabelBody(funcProto, arrayProto); 425 produceDecl(funcProto); 437 426 } else if (attr == ast::EnumAttribute::Label) { 438 427 std::vector<ast::ptr<ast::Init>> inits = genLabelInit(); 428 const ast::Type * type = new ast::PointerType( 429 new ast::BasicType( ast::BasicKind::Char, ast::CV::Const ) ); 439 430 ast::ObjectDecl* arrayProto = 440 genAttrArrayProto( attr, getLocation(), inits);431 genAttrArrayProto( getLocation(), "labels_", type, inits ); 441 432 forwards.push_back(arrayProto); 442 433 ast::FunctionDecl* funcProto = genLabelProto(); … … 445 436 produceDecl(funcProto); 446 437 } else { 438 assert( attr == ast::EnumAttribute::Posn ); 447 439 ast::FunctionDecl* funcProto = genPosnProto(); 448 440 produceForwardDecl(funcProto);
Note: See TracChangeset
for help on using the changeset viewer.