From ed1773b26a2aa383ce61fef9551b881456375cf8 Mon Sep 17 00:00:00 2001 From: Anna Rift Date: Fri, 20 Dec 2024 11:09:08 -0800 Subject: [PATCH] Implement prim 'scalar promotion type' Signed-off-by: Anna Rift --- frontend/lib/resolution/prims.cpp | 14 +++++++++++++- frontend/test/resolution/testResolve.cpp | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/frontend/lib/resolution/prims.cpp b/frontend/lib/resolution/prims.cpp index 1545b0390cd8..d8ca89aa440e 100644 --- a/frontend/lib/resolution/prims.cpp +++ b/frontend/lib/resolution/prims.cpp @@ -390,6 +390,18 @@ static QualifiedType primTypeof(Context* context, PrimitiveTag prim, const CallI return QualifiedType(QualifiedType::TYPE, typePtr); } +static QualifiedType primPromotionType(Context* context, const CallInfo& ci) { + if (ci.numActuals() != 1) return QualifiedType(); + auto actualQt = ci.actual(0).type(); + + auto promoTy = getPromotionType(context, actualQt).type(); + + // We want a type result, even if the prim was passed a value. + auto promoQt = QualifiedType(QualifiedType::TYPE, promoTy); + + return promoQt; +} + static QualifiedType primGetSvecMember(Context* context, PrimitiveTag prim, const CallInfo& ci) { CHPL_ASSERT(prim == PRIM_GET_SVEC_MEMBER || @@ -1688,7 +1700,7 @@ CallResolutionResult resolvePrimCall(ResolutionContext* rc, break; case PRIM_SCALAR_PROMOTION_TYPE: - CHPL_UNIMPL("misc primitives"); + type = primPromotionType(context, ci); break; case PRIM_STATIC_FIELD_TYPE: type = staticFieldType(context, ci); diff --git a/frontend/test/resolution/testResolve.cpp b/frontend/test/resolution/testResolve.cpp index 7ae6419ae3af..4cbe3621cff7 100644 --- a/frontend/test/resolution/testResolve.cpp +++ b/frontend/test/resolution/testResolve.cpp @@ -1739,6 +1739,23 @@ static void testInfiniteCycleBug() { std::ignore = resolveQualifiedTypeOfX(context, program1); } +static void testPromotionPrim() { + Context* context = buildStdContext(); + ErrorGuard guard(context); + + std::string prog = + R"""( + var d : domain(1); + type t = __primitive("scalar promotion type", d); + param x = t == int; + )"""; + + auto x = resolveTypeOfXInit(context, prog); + ensureParamBool(x, true); + + assert(guard.realizeErrors() == 0); +} + int main() { test1(); test2(); @@ -1770,5 +1787,7 @@ int main() { testInfiniteCycleBug(); + testPromotionPrim(); + return 0; }