diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2018-03-29 10:39:21 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-29 10:39:21 -0700 |
| commit | b4c4dc92c0b21f0253c4ccf34107638f7c36b62e (patch) | |
| tree | 9ead4b91a302fa3ad1e940686022d4ccbab3329c /source/slang/check.cpp | |
| parent | 184dc5cd6998b6277881fae4a68d91de86e2d63f (diff) | |
Add support for default parameter values in IR codegen (#459)
Fixes #61
When lowering from AST to IR, if a call site doesn't supply an argument expression for each of the parameters to the callee, then use the default value expressions (stored as the "initializer" of the parameter decl) for each omitted parameter. This relies on the front-end to have already checked the call site for validity.
Along the way I also cleaned up some of the checking of parameter declarations so that it is more like the checking of ordinary variable declarations (although the code is not yet shared). I also cleaned out some dead cases in the lowering logic for when we don't actually have a declaration available for a callee (these would only matter if we supported functions as first-class values).
I added a simple test case to confirm that call sites both with and without the optional parameter work as expected.
The strategy in this change is extremely simplistic, and might only be appropriate for default parameter value expressions that are compile-time constants (which should be the 99% case). This may require a major overhaul if we decide to handle default parameter values differently (e.g., by generating extra functions to ensure that the separate compilation story is what we want).
Another issue that could change a lot of this logic would be if we start to support by-name parameters at call sites, since we could no longer assume that the argument and parameter lists align one-to-one (with the argument list possibly being shorter). Any work to add more flexible argument passing conventions would need to build a suitable structure to map from arguments to parameters, or vice-versa.
Diffstat (limited to 'source/slang/check.cpp')
| -rw-r--r-- | source/slang/check.cpp | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/source/slang/check.cpp b/source/slang/check.cpp index 2b7f8f2fc..e06578c86 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -2820,18 +2820,38 @@ namespace Slang // Nothing to do } - void visitParamDecl(ParamDecl* para) + void visitParamDecl(ParamDecl* paramDecl) { - // TODO: This needs to bottleneck through the common variable checks + // TODO: This logic should be shared with the other cases of + // variable declarations. The main reason I am not doing it + // yet is that we use a `ParamDecl` with a null type as a + // special case in attribute declarations, and that could + // trip up the ordinary variable checks. - if(para->type.exp) + auto typeExpr = paramDecl->type; + if(typeExpr.exp) { - para->type = CheckUsableType(para->type); - - if (para->type.Equals(getSession()->getVoidType())) - { - getSink()->diagnose(para, Diagnostics::parameterCannotBeVoid); - } + typeExpr = CheckUsableType(typeExpr); + paramDecl->type = typeExpr; + } + + // The "initializer" expression for a parameter represents + // a default argument value to use if an explicit one is + // not supplied. + if(auto initExpr = paramDecl->initExpr) + { + // We must check the expression and coerce it to the + // actual type of the parameter. + // + initExpr = CheckExpr(initExpr); + initExpr = Coerce(typeExpr.type, initExpr); + paramDecl->initExpr = initExpr; + + // TODO: a default argument expression needs to + // conform to other constraints to be valid. + // For example, it should not be allowed to refer + // to other parameters of the same function (or maybe + // only the parameters to its left...). } } |
