From ccea5702442a7a8303e6735a038be86939c1ce7a Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Tue, 7 Nov 2017 10:22:53 -0800 Subject: Emit pointer-type parameters as out params The IR encodes `out` and `in out` function parameters as pointer types, so the emit logic needs to handle it. We had code to handle translation of pointers types into `out` declarations for function *declarations* but weren't handling it for function *definitions*. This change unifies the logic so that it is shared by function definitions and decalrations. This change does *not* deal with the following issues that need to be addressed sometime soon-ish: - We currently always translate pointers into `out`, even if they should be `in out`. This is obviously wrong. - If/when we eventually have targets that support true pointers (e.g., CUDA, NVIDIA OpenGL, etc.) we'll need a way to tell the difference between an `in` pointer parameter, and an `out` parameter. Both of these issues are meant to be addressed by having a few special cases of pointer types, for the `out` and `in out` cases, and only translating those (not all pointers). We need to plumb those through the IR more completely, but I'm not dealing with that here. --- source/slang/emit.cpp | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) (limited to 'source') diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index c67bb7b29..617e25d71 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -5698,7 +5698,8 @@ emitDeclImpl(decl, nullptr); emit(", "); auto paramName = getIRName(pp); - emitIRType(ctx, pp->getType(), paramName); + auto paramType = pp->getType(); + emitIRParamType(ctx, paramType, paramName); emitIRSemantics(ctx, pp); } @@ -5724,6 +5725,33 @@ emitDeclImpl(decl, nullptr); } } + void emitIRParamType( + EmitContext* ctx, + Type* type, + String const& name) + { + // An `out` or `inout` parameter will have been + // encoded as a parameter of pointer type, so + // we need to decode that here. + // + if( auto ptrType = type->As() ) + { + // TODO: we need a way to distinguish `out` + // from `inout`. The easiest way to do + // that might be to have each be a distinct + // sub-case of `IRPtrType` - this would also + // ensure that they can be distinguished from + // real pointers when the user means to use + // them. + + emit("out "); + + type = ptrType->getValueType(); + } + + emitIRType(ctx, type, name); + } + void emitIRFuncDecl( EmitContext* ctx, IRFunc* func) @@ -5771,26 +5799,7 @@ emitDeclImpl(decl, nullptr); paramName.append(pp); auto paramType = funcType->getParamType(pp); - // An `out` or `inout` parameter will have been - // encoded as a parameter of pointer type, so - // we need to decode that here. - // - if( auto ptrType = paramType->As() ) - { - // TODO: we need a way to distinguish `out` - // from `inout`. The easiest way to do - // that might be to have each be a distinct - // sub-case of `IRPtrType` - this would also - // ensure that they can be distinguished from - // real pointers when the user means to use - // them. - - emit("out "); - - paramType = ptrType->getValueType(); - } - - emitIRType(ctx, paramType, paramName); + emitIRParamType(ctx, paramType, paramName); } emit(");\n"); } -- cgit v1.2.3