From e0389f5a1f32cb611e5a595a5974ee1d5c15f43d Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Thu, 15 Jun 2017 16:35:10 -0700 Subject: Replace `DeclRef` approach For context: a `DeclRef` is supposed to capture both a pointer to a particualr declaration, and also any information needed to specialize that declaration for a context (e.g., generic parameter substitutions). The existing approach had a hiearchy of specialized decl-ref types that mirrored the AST hierarchy, but that led to a lot of boilerplate where you had to recapitulate the exact same hierarchy. The new appraoch basically treats `DeclRef` as a sort of "smart pointer" in that it wraps a pointer to a `T` (the declaration), plus a side field for the specialization info, and then allows it to be cast as needed to other types (where the pointer cast would be allowed), while carrying along the side info. To enable this, all the things that used to be member functions of declaration-reference types are now free functions that take a `DeclRef` for some specific `T` as a parameter. --- source/slang/emit.cpp | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'source/slang/emit.cpp') diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 69bdaebf6..a2a1e9e21 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -43,7 +43,7 @@ static void EmitType(EmitContext* context, RefPtr type, String c static void EmitType(EmitContext* context, RefPtr type); static void EmitExpr(EmitContext* context, RefPtr expr); static void EmitStmt(EmitContext* context, RefPtr stmt); -static void EmitDeclRef(EmitContext* context, DeclRef declRef); +static void EmitDeclRef(EmitContext* context, DeclRef declRef); // Low-level emit logic @@ -457,7 +457,7 @@ static void emitCallExpr( if (auto funcDeclRefExpr = funcExpr.As()) { auto funcDeclRef = funcDeclRefExpr->declRef; - auto funcDecl = funcDeclRef.GetDecl(); + auto funcDecl = funcDeclRef.getDecl(); if (auto intrinsicOpModifier = funcDecl->FindModifier()) { switch (intrinsicOpModifier->op) @@ -617,7 +617,7 @@ static void emitCallExpr( // We might be calling an intrinsic subscript operation, // and should desugar it accordingly - if(auto subscriptDeclRef = funcDeclRef.As()) + if(auto subscriptDeclRef = funcDeclRef.As()) { // We expect any subscript operation to be invoked as a member, // so the function expression had better be in the correct form. @@ -647,7 +647,7 @@ static void emitCallExpr( if (auto funcDeclRefExpr = funcExpr.As()) { auto declRef = funcDeclRefExpr->declRef; - if (auto ctorDeclRef = declRef.As()) + if (auto ctorDeclRef = declRef.As()) { // We really want to emit a reference to the type begin constructed EmitType(context, callExpr->Type); @@ -1604,7 +1604,7 @@ static void EmitVal(EmitContext* context, RefPtr val) } } -static void EmitDeclRef(EmitContext* context, DeclRef declRef) +static void EmitDeclRef(EmitContext* context, DeclRef declRef) { // TODO: need to qualify a declaration name based on parent scopes/declarations @@ -1614,10 +1614,10 @@ static void EmitDeclRef(EmitContext* context, DeclRef declRef) // If the declaration is nested directly in a generic, then // we need to output the generic arguments here auto parentDeclRef = declRef.GetParent(); - if (auto genericDeclRef = parentDeclRef.As()) + if (auto genericDeclRef = parentDeclRef.As()) { // Only do this for declarations of appropriate flavors - if(auto funcDeclRef = declRef.As()) + if(auto funcDeclRef = declRef.As()) { // Don't emit generic arguments for functions, because HLSL doesn't allow them return; @@ -1869,16 +1869,16 @@ static void EmitStructDecl(EmitContext* context, RefPtr decl) } // Shared emit logic for variable declarations (used for parameters, locals, globals, fields) -static void EmitVarDeclCommon(EmitContext* context, VarDeclBaseRef declRef) +static void EmitVarDeclCommon(EmitContext* context, DeclRef declRef) { - EmitModifiers(context, declRef.GetDecl()); + EmitModifiers(context, declRef.getDecl()); - EmitType(context, declRef.GetType(), declRef.GetName()); + EmitType(context, GetType(declRef), declRef.GetName()); - EmitSemantics(context, declRef.GetDecl()); + EmitSemantics(context, declRef.getDecl()); // TODO(tfoley): technically have to apply substitution here too... - if (auto initExpr = declRef.GetDecl()->Expr) + if (auto initExpr = declRef.getDecl()->Expr) { Emit(context, " = "); EmitExpr(context, initExpr); @@ -1888,7 +1888,7 @@ static void EmitVarDeclCommon(EmitContext* context, VarDeclBaseRef declRef) // Shared emit logic for variable declarations (used for parameters, locals, globals, fields) static void EmitVarDeclCommon(EmitContext* context, RefPtr decl) { - EmitVarDeclCommon(context, DeclRef(decl.Ptr(), nullptr).As()); + EmitVarDeclCommon(context, DeclRef(decl.Ptr(), nullptr).As()); } // Emit a single `regsiter` semantic, as appropriate for a given resource-type-specific layout info @@ -2033,14 +2033,14 @@ static void emitHLSLParameterBlockDecl( emitHLSLRegisterSemantic(context, *info); Emit(context, "\n{\n"); - if (auto structRef = declRefType->declRef.As()) + if (auto structRef = declRefType->declRef.As()) { - for (auto field : structRef.GetMembersOfType()) + for (auto field : getMembersOfType(structRef)) { EmitVarDeclCommon(context, field); RefPtr fieldLayout; - structTypeLayout->mapVarToLayout.TryGetValue(field.GetDecl(), fieldLayout); + structTypeLayout->mapVarToLayout.TryGetValue(field.getDecl(), fieldLayout); assert(fieldLayout); // Emit explicit layout annotations for every field @@ -2197,12 +2197,12 @@ static void emitGLSLParameterBlockDecl( } Emit(context, "\n{\n"); - if (auto structRef = declRefType->declRef.As()) + if (auto structRef = declRefType->declRef.As()) { - for (auto field : structRef.GetMembersOfType()) + for (auto field : getMembersOfType(structRef)) { RefPtr fieldLayout; - structTypeLayout->mapVarToLayout.TryGetValue(field.GetDecl(), fieldLayout); + structTypeLayout->mapVarToLayout.TryGetValue(field.getDecl(), fieldLayout); assert(fieldLayout); // TODO(tfoley): We may want to emit *some* of these, @@ -2292,7 +2292,7 @@ static void EmitFuncDecl(EmitContext* context, RefPtr decl) Emit(context, "("); bool first = true; - for (auto paramDecl : decl->GetMembersOfType()) + for (auto paramDecl : decl->getMembersOfType()) { if (!first) Emit(context, ", "); EmitParamDecl(context, paramDecl); -- cgit v1.2.3