From 3a5214b65b2a5efdbcf9bf6fb4d7603e9ee63234 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Thu, 18 Oct 2018 09:30:38 -0700 Subject: Add support for static methods in interfaces (#680) This change allows an interface to include `static` methods as requirements, so that types that conform to the interface will need to satisfy the requirement with a `static` method. The essence of the check is simple: when checking that a method satisfies a requirement, we enforce that both are `static` or both are non-`static`. Making that simple change and adding a test change broke a few other places in the compiler that this change tries to fix. The main fix is to handle cases where we might look up an "effectively static" member of a type through an instance, and to make sure that we replace the instance-based lookup with type-based lookup. There was already logic along these lines in `lower-to-ir.cpp`, so this change centralizes it in `check.cpp` where it seems to logically belong. --- source/slang/lower-to-ir.cpp | 41 +++++++---------------------------------- 1 file changed, 7 insertions(+), 34 deletions(-) (limited to 'source/slang/lower-to-ir.cpp') diff --git a/source/slang/lower-to-ir.cpp b/source/slang/lower-to-ir.cpp index 3c4067af5..7b63f510f 100644 --- a/source/slang/lower-to-ir.cpp +++ b/source/slang/lower-to-ir.cpp @@ -384,6 +384,12 @@ void setValue(IRGenContext* context, Decl* decl, LoweredValInfo value) context->env->mapDeclToValue[decl] = value; } + + /// Should the given `decl` nested in `parentDecl` be treated as a static rather than instance declaration? +bool isEffectivelyStatic( + Decl* decl, + ContainerDecl* parentDecl); + // Ensure that a version of the given declaration has been emitted to the IR LoweredValInfo ensureDecl( IRGenContext* context, @@ -4446,46 +4452,13 @@ struct DeclLoweringVisitor : DeclVisitor // // We also need to be able to detect whether a declaration is // either explicitly or implicitly treated as `static`: - bool isMemberDeclarationEffectivelyStatic( - Decl* decl, - ContainerDecl* parentDecl) - { - // Anything explicitly marked `static` counts. - // - // There is a subtle detail here with a global-scope `static` - // variable not really meaning `static` in the same way, but - // it doesn't matter because the module shouldn't introduce - // any parameters we care about. - if(decl->HasModifier()) - return true; - - // Next we need to deal with cases where a declaration is - // effectively `static` even if the language doesn't make - // the user say so. Most languages make the default assumption - // that nested types are `static` even if they don't say - // so (Java is an exception here, perhaps due to some - // includence from the Scandanavian OOP tradition). - if(dynamic_cast(decl)) - return true; - - // Things nested inside functions may have dependencies - // on values from the enclosing scope, but this needs to - // be dealt with via "capture" so they are also effectively - // `static` - if(dynamic_cast(parentDecl)) - return true; - - return false; - } - // We also need to be able to detect whether a declaration is - // either explicitly or implicitly treated as `static`: ParameterListCollectMode getModeForCollectingParentParameters( Decl* decl, ContainerDecl* parentDecl) { // If we have a `static` parameter, then it is obvious // that we should use the `static` mode - if(isMemberDeclarationEffectivelyStatic(decl, parentDecl)) + if(isEffectivelyStatic(decl, parentDecl)) return kParameterListCollectMode_Static; // Otherwise, let's default to collecting everything -- cgit v1.2.3