Changeset 5ccc733
- Timestamp:
- Jun 29, 2024, 5:02:06 AM (3 months ago)
- Branches:
- master
- Children:
- 4117761
- Parents:
- 7552fde
- Location:
- src
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Decl.cpp
r7552fde r5ccc733 203 203 } 204 204 205 bool EnumDecl::isTyped() const { return base;} 206 207 bool EnumDecl::isOpague() const { return isCfa && !isTyped(); } 205 208 } 206 209 -
src/AST/Decl.hpp
r7552fde r5ccc733 308 308 class EnumDecl final : public AggregateDecl { 309 309 public: 310 // is Typedindicated if the enum has a declaration like:310 // isCfa indicated if the enum has a declaration like: 311 311 // enum (type_optional) Name {...} 312 bool is Typed;313 // if is Typed == true && base.get() == nullptr, it is a "void" typeenum312 bool isCfa; 313 // if isCfa == true && base.get() == nullptr, it is a "opague" enum 314 314 ptr<Type> base; 315 315 enum class EnumHiding { Visible, Hide } hide; 316 316 std::vector< ast::ptr<ast::EnumInstType>> inlinedDecl; // child enums 317 317 318 EnumDecl( const CodeLocation& loc, const std::string& name, bool is Typed= false,318 EnumDecl( const CodeLocation& loc, const std::string& name, bool isCfa = false, 319 319 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall, 320 320 Type const * base = nullptr, EnumHiding hide = EnumHiding::Hide, 321 321 std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() ) 322 : AggregateDecl( loc, name, std::move(attrs), linkage ), is Typed(isTyped), base(base), hide(hide), enumValues(enumValues) {}322 : AggregateDecl( loc, name, std::move(attrs), linkage ), isCfa(isCfa), base(base), hide(hide), enumValues(enumValues) {} 323 323 324 324 /// gets the integer value for this enumerator, returning true iff value found … … 336 336 337 337 bool isSubTypeOf(const ast::EnumDecl *) const; 338 bool isTyped() const; 339 bool isOpague() const; 338 340 private: 339 341 EnumDecl * clone() const override { return new EnumDecl{ *this }; } -
src/ControlStruct/TranslateEnumRange.cpp
r7552fde r5ccc733 91 91 ast::UntypedExpr * condition = ast::UntypedExpr::createCall( location, 92 92 "?<=?", 93 { 94 ast::UntypedExpr::createCall(location, "posn", { new ast::NameExpr( location, indexName ) } ), 95 ast::ConstantExpr::from_ulong( location, enumDecl->members.size()-1 ) 96 }); 93 {new ast::CastExpr(location, 94 new ast::NameExpr( location, indexName ), 95 new ast::BasicType( ast::BasicKind::UnsignedInt ), 96 ast::GeneratedFlag::ExplicitCast), 97 ast::ConstantExpr::from_ulong( location, enumDecl->members.size()-1 ) }); 98 97 99 auto increment = ast::UntypedExpr::createCall( location, 98 stmt->is_inc? " succ": "pred",100 stmt->is_inc? "?++": "?--", 99 101 { new ast::NameExpr( location, indexName ) }); 102 100 103 auto assig = ast::UntypedExpr::createAssign( location, new ast::NameExpr( location, indexName ), increment ); 101 104 auto mut = ast::mutate_field( stmt, &ast::ForStmt::cond, condition ); -
src/Parser/TypeData.cpp
r7552fde r5ccc733 1469 1469 ast::ObjectDecl * object = strict_dynamic_cast<ast::ObjectDecl *>( member ); 1470 1470 object->isHidden = ast::EnumDecl::EnumHiding::Hide == ret->hide; 1471 if ( ret->is Typed && !ret->base&& cur->has_enumeratorValue() ) {1472 SemanticError( td->location, " Enumerator of enum(void)cannot have an explicit initializer value." );1471 if ( ret->isOpague() && cur->has_enumeratorValue() ) { 1472 SemanticError( td->location, "Opague cannot have an explicit initializer value." ); 1473 1473 } else if ( cur->has_enumeratorValue() ) { 1474 1474 object->init = new ast::SingleInit( -
src/ResolvExpr/CastCost.cpp
r7552fde r5ccc733 54 54 cost = conversionCost( enumInst, dst, srcIsLvalue, symtab, env ); 55 55 56 if ( Cost::unsafe < cost) {56 if ( !enumInst->base->isCfa ) { 57 57 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) }; 58 58 Cost intCost = costCalc( integer, dst, srcIsLvalue, symtab, env ); 59 59 cost = intCost < cost? intCost: cost; 60 } 61 if ( enumInst->base->isTyped && enumInst->base->base ) { 60 } else if ( enumInst->base->isTyped() ) { 62 61 auto baseConversionCost = 63 62 castCost( enumInst->base->base, dst, srcIsLvalue, symtab, env ); … … 74 73 cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env ); 75 74 if ( Cost::unsafe < cost ) { 76 if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) { 77 // Always explict cast only for typed enum 78 if (enumInst->base->isTyped) cost = Cost::unsafe; 75 if ( dynamic_cast<const ast::EnumInstType *>(dst)) { 76 cost = Cost::unsafe; 79 77 } 80 78 } … … 85 83 cost = conversionCost( zero, dst, srcIsLvalue, symtab, env ); 86 84 if ( Cost::unsafe < cost ) { 87 if ( auto enumInst =dynamic_cast<const ast::EnumInstType *>(dst)) {88 if (enumInst->base->isTyped)cost = Cost::unsafe;85 if ( dynamic_cast<const ast::EnumInstType *>(dst)) { 86 cost = Cost::unsafe; 89 87 } 90 88 } … … 94 92 cost = conversionCost( one, dst, srcIsLvalue, symtab, env ); 95 93 if ( Cost::unsafe < cost ) { 96 if ( auto enumInst =dynamic_cast<const ast::EnumInstType *>(dst)) {97 if (enumInst->base->isTyped)cost = Cost::unsafe;94 if ( dynamic_cast<const ast::EnumInstType *>(dst)) { 95 cost = Cost::unsafe; 98 96 } 99 97 } -
src/ResolvExpr/CommonType.cpp
r7552fde r5ccc733 386 386 } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( type2 ) ) { 387 387 const ast::EnumDecl* enumDecl = enumInst->base; 388 if ( !enumDecl-> base) {388 if ( !enumDecl->isCfa ) { 389 389 ast::BasicKind kind = commonTypes[ basic->kind ][ ast::BasicKind::SignedInt ]; 390 390 if ( … … 642 642 const ast::EnumDecl* argDecl = argAsEnumInst->base; 643 643 if (argDecl->isSubTypeOf(paramDecl)) result = param; 644 } else if ( param->base && !param->base->is Typed) {644 } else if ( param->base && !param->base->isCfa ) { 645 645 auto basicType = new ast::BasicType( ast::BasicKind::UnsignedInt ); 646 646 result = commonType( basicType, type2, tenv, need, have, open, widen); -
src/ResolvExpr/ConversionCost.cpp
r7552fde r5ccc733 235 235 return ast::Pass<ConversionCost>::read( src, dst, srcIsLvalue, symtab, env, conversionCost ); 236 236 } 237 if (const ast::EnumInstType * srcAsInst = dynamic_cast< const ast::EnumInstType * >( src )) { 238 if (srcAsInst->base && !srcAsInst->base->isCfa) { 239 static const ast::BasicType* integer = new ast::BasicType( ast::BasicKind::UnsignedInt ); 240 return ast::Pass<ConversionCost>::read( integer, dst, srcIsLvalue, symtab, env, conversionCost ); 241 } 242 } 237 243 } else { 238 244 assert( -1 == diff ); 239 245 const ast::ReferenceType * dstAsRef = dynamic_cast< const ast::ReferenceType * >( dst ); 240 246 assert( dstAsRef ); 241 if ( typesCompatibleIgnoreQualifiers( src, dstAsRef->base, env ) ) { 247 auto dstBaseType = dstAsRef->base; 248 const ast::Type * newSrc = src; 249 if ( dynamic_cast< const ast::EnumInstType * >( src ) && dstBaseType.as<ast::BasicType>() ) { 250 newSrc = new ast::BasicType( ast::BasicKind::UnsignedInt ); 251 } 252 if ( typesCompatibleIgnoreQualifiers( newSrc, dstAsRef->base, env ) ) { 242 253 if ( srcIsLvalue ) { 243 254 if ( src->qualifiers == dstAsRef->base->qualifiers ) { … … 285 296 conversionCostFromBasicToBasic( basicType, dstAsBasic ); 286 297 } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) { 287 if ( dstAsEnumInst->base && !dstAsEnumInst->base->is Typed) {288 cost = Cost:: unsafe;298 if ( dstAsEnumInst->base && !dstAsEnumInst->base->isCfa ) { 299 cost = Cost::safe; 289 300 } 290 301 } … … 366 377 if ( auto dstInst = dynamic_cast<const ast::EnumInstType *>( dst ) ) { 367 378 cost = enumCastCost(inst, dstInst, symtab, env); 368 return; 369 } 370 371 if ( !inst->base->isTyped ) { 379 } else if ( !inst->base->isCfa ) { 372 380 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) }; 373 381 cost = costCalc( integer, dst, srcIsLvalue, symtab, env ); 374 if ( cost < Cost::unsafe ) {375 cost.incSafe();376 }377 return;378 382 } 379 383 // cost.incUnsafe(); … … 455 459 // assuming 0p is supposed to be used for pointers? 456 460 } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) { 457 if ( dstAsEnumInst->base && !dstAsEnumInst->base->is Typed) {458 cost = Cost:: unsafe;461 if ( dstAsEnumInst->base && !dstAsEnumInst->base->isCfa ) { 462 cost = Cost::safe; 459 463 } 460 464 } … … 476 480 } 477 481 } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) { 478 if ( dstAsEnumInst->base && !dstAsEnumInst->base->is Typed) {479 cost = Cost:: unsafe;482 if ( dstAsEnumInst->base && !dstAsEnumInst->base->isCfa ) { 483 cost = Cost::safe; 480 484 } 481 485 } -
src/Validate/ImplementEnumFunc.cpp
r7552fde r5ccc733 476 476 477 477 void ImplementEnumFunc::previsit(const ast::EnumDecl* enumDecl) { 478 if (!enumDecl->body || !enumDecl->is Typed) return;478 if (!enumDecl->body || !enumDecl->isCfa) return; 479 479 ast::EnumInstType enumInst(enumDecl->name); 480 480 enumInst.base = enumDecl;
Note: See TracChangeset
for help on using the changeset viewer.