From c2d31347ea06c768045e7c503ef0188e0e5356de Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 28 May 2020 14:01:51 -0400 Subject: WIP: ASTBuilder (#1358) * Compiles. * Small tidy up around session/ASTBuilder. * Tests are now passing. * Fix Visual Studio project. * Fix using new X to use builder when protectedness of Ctor is not enough. Substitute->substitute * Add some missing ast nodes created outside of ASTBuilder. * Compile time check that ASTBuilder is making an AST type. * Moced findClasInfo and findSyntaxClass (essentially the same thing) to SharedASTBuilder from Session. --- source/slang/slang-check-expr.cpp | 95 ++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 47 deletions(-) (limited to 'source/slang/slang-check-expr.cpp') diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index 6bf096ae9..e0f439ab3 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -31,7 +31,7 @@ namespace Slang template RefPtr SemanticsVisitor::moveTemp(RefPtr const& expr, F const& func) { - RefPtr varDecl = new VarDecl(); + RefPtr varDecl = m_astBuilder->create(); varDecl->parentDecl = nullptr; // TODO: need to fill this in somehow! varDecl->checkState = DeclCheckState::Checked; varDecl->nameAndLoc.loc = expr->loc; @@ -40,7 +40,7 @@ namespace Slang auto varDeclRef = makeDeclRef(varDecl.Ptr()); - RefPtr letExpr = new LetExpr(); + RefPtr letExpr = m_astBuilder->create(); letExpr->decl = varDecl; auto body = func(varDeclRef); @@ -102,23 +102,23 @@ namespace Slang auto interfaceDecl = interfaceDeclRef.getDecl(); return maybeMoveTemp(expr, [&](DeclRef varDeclRef) { - RefPtr openedType = new ExtractExistentialType(); + RefPtr openedType = m_astBuilder->create(); openedType->declRef = varDeclRef; - RefPtr openedWitness = new ExtractExistentialSubtypeWitness(); + RefPtr openedWitness = m_astBuilder->create(); openedWitness->sub = openedType; openedWitness->sup = expr->type.type; openedWitness->declRef = varDeclRef; - RefPtr openedThisType = new ThisTypeSubstitution(); + RefPtr openedThisType = m_astBuilder->create(); openedThisType->outer = interfaceDeclRef.substitutions.substitutions; openedThisType->interfaceDecl = interfaceDecl; openedThisType->witness = openedWitness; DeclRef substDeclRef = DeclRef(interfaceDecl, openedThisType); - auto substDeclRefType = DeclRefType::Create(getSession(), substDeclRef); + auto substDeclRefType = DeclRefType::create(m_astBuilder, substDeclRef); - RefPtr openedValue = new ExtractExistentialValueExpr(); + RefPtr openedValue = m_astBuilder->create(); openedValue->declRef = varDeclRef; openedValue->type = QualType(substDeclRefType); @@ -218,7 +218,7 @@ namespace Slang declRef.GetName()); } - auto expr = new StaticMemberExpr(); + auto expr = m_astBuilder->create(); expr->loc = loc; expr->type = type; expr->baseExpression = baseExpr; @@ -230,11 +230,11 @@ namespace Slang { // Extract the type of the baseExpr auto baseExprType = baseExpr->type.type; - RefPtr baseTypeExpr = new SharedTypeExpr(); + RefPtr baseTypeExpr = m_astBuilder->create(); baseTypeExpr->base.type = baseExprType; - baseTypeExpr->type.type = getTypeType(baseExprType); + baseTypeExpr->type.type = m_astBuilder->getTypeType(baseExprType); - auto expr = new StaticMemberExpr(); + auto expr = m_astBuilder->create(); expr->loc = loc; expr->type = type; expr->baseExpression = baseTypeExpr; @@ -247,7 +247,7 @@ namespace Slang // If the base expression wasn't a type, then this // is a normal member expression. // - auto expr = new MemberExpr(); + auto expr = m_astBuilder->create(); expr->loc = loc; expr->type = type; expr->baseExpression = baseExpr; @@ -274,7 +274,7 @@ namespace Slang // If there is no base expression, then the result must // be an ordinary variable expression. // - auto expr = new VarExpr(); + auto expr = m_astBuilder->create(); expr->loc = loc; expr->name = declRef.GetName(); expr->type = type; @@ -290,7 +290,7 @@ namespace Slang auto ptrLikeType = as(base->type); SLANG_ASSERT(ptrLikeType); - auto derefExpr = new DerefExpr(); + auto derefExpr = m_astBuilder->create(); derefExpr->loc = loc; derefExpr->base = base; derefExpr->type = QualType(ptrLikeType->elementType); @@ -364,9 +364,9 @@ namespace Slang // will be `typeof(This)`, which conceptually // `typeof(typeof(this))` // - auto thisTypeType = getTypeType(thisType); + auto thisTypeType = m_astBuilder->getTypeType(thisType); - auto typeExpr = new SharedTypeExpr(); + auto typeExpr = m_astBuilder->create(); typeExpr->type.type = thisTypeType; typeExpr->base.type = thisType; @@ -380,7 +380,7 @@ namespace Slang // refernece to `this.someStaticMember` will be translated // over to `This.someStaticMember`. // - RefPtr expr = new ThisExpr(); + RefPtr expr = m_astBuilder->create(); expr->type.type = thisType; expr->loc = loc; @@ -412,11 +412,11 @@ namespace Slang { if (lookupResult.isOverloaded()) { - auto overloadedExpr = new OverloadedExpr(); + auto overloadedExpr = m_astBuilder->create(); overloadedExpr->name = name; overloadedExpr->loc = loc; overloadedExpr->type = QualType( - getSession()->getOverloadedType()); + m_astBuilder->getOverloadedType()); overloadedExpr->base = baseExpr; overloadedExpr->lookupResult2 = lookupResult; return overloadedExpr; @@ -588,7 +588,7 @@ namespace Slang RefPtr SemanticsVisitor::CreateErrorExpr(Expr* expr) { - expr->type = QualType(getSession()->getErrorType()); + expr->type = QualType(m_astBuilder->getErrorType()); return expr; } @@ -617,7 +617,7 @@ namespace Slang RefPtr SemanticsExprVisitor::visitBoolLiteralExpr(BoolLiteralExpr* expr) { - expr->type = getSession()->getBoolType(); + expr->type = m_astBuilder->getBoolType(); return expr; } @@ -636,7 +636,7 @@ namespace Slang // if(!expr->type.type) { - expr->type = getSession()->getIntType(); + expr->type = m_astBuilder->getIntType(); } return expr; } @@ -645,21 +645,21 @@ namespace Slang { if(!expr->type.type) { - expr->type = getSession()->getFloatType(); + expr->type = m_astBuilder->getFloatType(); } return expr; } RefPtr SemanticsExprVisitor::visitStringLiteralExpr(StringLiteralExpr* expr) { - expr->type = getSession()->getStringType(); + expr->type = m_astBuilder->getStringType(); return expr; } IntVal* SemanticsVisitor::GetIntVal(IntegerLiteralExpr* expr) { // TODO(tfoley): don't keep allocating here! - return new ConstantIntVal(expr->value); + return m_astBuilder->create(expr->value); } RefPtr SemanticsVisitor::TryConstantFoldExpr( @@ -784,7 +784,7 @@ namespace Slang return nullptr; } - RefPtr result = new ConstantIntVal(resultValue); + RefPtr result = m_astBuilder->create(resultValue); return result; } @@ -811,7 +811,7 @@ namespace Slang if (auto genericValParamRef = declRef.as()) { // TODO(tfoley): handle the case of non-`int` value parameters... - return new GenericParamIntVal(genericValParamRef); + return m_astBuilder->create(genericValParamRef); } // We may also need to check for references to variables that @@ -826,7 +826,7 @@ namespace Slang if(auto constAttr = varDecl->findModifier()) { // HLSL `static const` can be used as a constant expression - if(auto initExpr = getInitExpr(varRef)) + if(auto initExpr = getInitExpr(m_astBuilder, varRef)) { return TryConstantFoldExpr(initExpr.Ptr()); } @@ -836,7 +836,7 @@ namespace Slang else if(auto enumRef = declRef.as()) { // The cases in an `enum` declaration can also be used as constant expressions, - if(auto tagExpr = getTagExpr(enumRef)) + if(auto tagExpr = getTagExpr(m_astBuilder, enumRef)) { return TryConstantFoldExpr(tagExpr.Ptr()); } @@ -882,7 +882,7 @@ namespace Slang if(IsErrorExpr(inExpr)) return nullptr; // First coerce the expression to the expected type - auto expr = coerce(getSession()->getIntType(),inExpr); + auto expr = coerce(m_astBuilder->getIntType(),inExpr); // No need to issue further errors if the type coercion failed. if(IsErrorExpr(expr)) return nullptr; @@ -923,8 +923,8 @@ namespace Slang auto baseExpr = subscriptExpr->baseExpression; auto indexExpr = subscriptExpr->indexExpression; - if (!indexExpr->type->equals(getSession()->getIntType()) && - !indexExpr->type->equals(getSession()->getUIntType())) + if (!indexExpr->type->equals(m_astBuilder->getIntType()) && + !indexExpr->type->equals(m_astBuilder->getUIntType())) { getSink()->diagnose(indexExpr, Diagnostics::subscriptIndexNonInteger); return CreateErrorExpr(subscriptExpr.Ptr()); @@ -973,10 +973,11 @@ namespace Slang auto elementType = CoerceToUsableType(TypeExp(baseExpr, baseTypeType->type)); auto arrayType = getArrayType( + m_astBuilder, elementType, elementCount); - subscriptExpr->type = QualType(getTypeType(arrayType)); + subscriptExpr->type = QualType(m_astBuilder->getTypeType(arrayType)); return subscriptExpr; } else if (auto baseArrayType = as(baseType)) @@ -1010,7 +1011,7 @@ namespace Slang { Name* name = getName("operator[]"); LookupResult lookupResult = lookUpMember( - getSession(), + m_astBuilder, this, name, baseType); @@ -1028,7 +1029,7 @@ namespace Slang RefPtr subscriptFuncExpr = createLookupResultExpr( name, lookupResult, subscriptExpr->baseExpression, subscriptExpr->loc); - RefPtr subscriptCallExpr = new InvokeExpr(); + RefPtr subscriptCallExpr = m_astBuilder->create(); subscriptCallExpr->loc = subscriptExpr->loc; subscriptCallExpr->functionExpr = subscriptFuncExpr; @@ -1223,9 +1224,9 @@ namespace Slang if (expr->declRef) return expr; - expr->type = QualType(getSession()->getErrorType()); + expr->type = QualType(m_astBuilder->getErrorType()); auto lookupResult = lookUp( - getSession(), + m_astBuilder, this, expr->name, expr->scope); if (lookupResult.isValid()) { @@ -1316,7 +1317,7 @@ namespace Slang // of explicit default initializers for `struct` fields to // make this a major concern (since they aren't supported in HLSL). // - RefPtr initListExpr = new InitializerListExpr(); + RefPtr initListExpr = m_astBuilder->create(); auto checkedInitListExpr = visitInitializerListExpr(initListExpr); return coerce(typeExp.type, initListExpr); } @@ -1342,7 +1343,7 @@ namespace Slang auto elementType = QualType(pointerLikeType->elementType); elementType.IsLeftValue = baseType.IsLeftValue; - auto derefExpr = new DerefExpr(); + auto derefExpr = m_astBuilder->create(); derefExpr->base = expr; derefExpr->type = elementType; @@ -1360,7 +1361,7 @@ namespace Slang RefPtr baseElementType, IntegerLiteralValue baseElementCount) { - RefPtr swizExpr = new SwizzleExpr(); + RefPtr swizExpr = m_astBuilder->create(); swizExpr->loc = memberRefExpr->loc; swizExpr->base = memberRefExpr->baseExpression; @@ -1439,7 +1440,7 @@ namespace Slang // here if the input type had a sugared name... swizExpr->type = QualType(createVectorType( baseElementType, - new ConstantIntVal(elementCount))); + m_astBuilder->create(elementCount))); } // A swizzle can be used as an l-value as long as there @@ -1484,7 +1485,7 @@ namespace Slang // we can reference the declaration here. // LookupResult lookupResult = lookUpDirectAndTransparentMembers( - getSession(), + m_astBuilder, this, expr->name, namespaceDeclRef); @@ -1515,7 +1516,7 @@ namespace Slang } LookupResult lookupResult = lookUpMember( - getSession(), + m_astBuilder, this, expr->name, type); @@ -1640,7 +1641,7 @@ namespace Slang SLANG_ASSERT(as(expr) || as(expr)); getSink()->diagnose(expr, Diagnostics::noMemberOfNameInType, expr->name, baseType); - expr->type = QualType(getSession()->getErrorType()); + expr->type = QualType(m_astBuilder->getErrorType()); return expr; } @@ -1703,7 +1704,7 @@ namespace Slang else { LookupResult lookupResult = lookUpMember( - getSession(), + m_astBuilder, this, expr->name, baseType.Ptr()); @@ -1734,7 +1735,7 @@ namespace Slang arg = CheckTerm(arg); } - expr->type = getSession()->getInitializerListType(); + expr->type = m_astBuilder->getInitializerListType(); return expr; } @@ -1814,7 +1815,7 @@ namespace Slang if( auto typeOrExtensionDecl = as(containerDecl) ) { auto thisType = calcThisType(makeDeclRef(typeOrExtensionDecl)); - auto thisTypeType = getTypeType(thisType); + auto thisTypeType = m_astBuilder->getTypeType(thisType); expr->type.type = thisTypeType; return expr; -- cgit v1.2.3