Commit Graph

14 Commits

Author SHA1 Message Date
Faisal Vali 8bc2bc71f6 A quick fix to PR17877 that was introduced by r194188 (generic-lambda-capturing) that broke libc++.
See http://lists.cs.uiuc.edu/pipermail/cfe-dev/2013-November/033369.html for discussion on cfe-dev.

This fix explicitly checks whether we are within the declcontext of a lambda's call operator - which is what I had intended to be true (and assumed would be true if getCurLambda returns a valid pointer) before checking whether a lambda can capture the potential-captures of the innermost lambda.

A deeper fix (that addresses why getCurLambda() returns a valid pointer when perhaps it shouldn't?) - as proposed by Richard Smith in http://llvm.org/bugs/show_bug.cgi?id=17877 - has been suggested as a FIXME.

Patch was LGTM'd by Richard (just barely :)

http://llvm-reviews.chandlerc.com/D2144

llvm-svn: 194448
2013-11-12 03:48:27 +00:00
Faisal Vali a17d19fb41 This patch implements capturing of variables within generic lambdas.
Both Richard and I felt that the current wording in the working paper needed some tweaking - Please see http://llvm-reviews.chandlerc.com/D2035 for additional context and references to core-reflector messages that discuss wording tweaks.

What is implemented is what we had intended to specify in Bristol; but, recently felt that the specification might benefit from some tweaking and fleshing.  

As a rough attempt to explain the semantics: If a nested lambda with a default-capture names a variable within its body, and if the enclosing full expression that contains the name of that variable is instantiation-dependent - then an enclosing lambda that is capture-ready (i.e. within a non-dependent context) must capture that variable, if all intervening nested lambdas can potentially capture that variable if they need to, and all intervening parent lambdas of the capture-ready lambda can and do capture the variable.      

Of note, 'this' capturing is also currently underspecified in the working paper for generic lambdas.  What is implemented here is if the set of candidate functions in a nested generic lambda includes both static and non-static member functions (regardless of viability checking - i.e. num and type of parameters/arguments) - and if all intervening nested-inner lambdas between the capture-ready lambda and the function-call containing nested lambda can capture 'this' and if all enclosing lambdas of the capture-ready lambda can capture 'this', then 'this' is speculatively captured by that capture-ready lambda.

Hopefully a paper for the C++ committee (that Richard and I had started some preliminary work on) is forthcoming. 

This essentially makes generic lambdas feature complete, except for known bugs. The more prominent ones (and the ones I am currently aware of) being:
  - generic lambdas and init-captures are broken - but a patch that fixes this is already in the works ...
  - nested variadic expansions such as:
    auto K = [](auto ... OuterArgs) {
      vp([=](auto ... Is) {
          decltype(OuterArgs) OA = OuterArgs;
          return 0;
        }(5)...);
      return 0;
    };
    auto M = K('a', ' ', 1, " -- ", 3.14); 
   currently cause crashes.  I think I know how to fix this (since I had done so in my initial implementation) - but it will probably take some work and back & forth with Doug and Richard.

A warm thanks to all who provided feedback - and especially to Doug Gregor and Richard Smith for their pivotal guidance: their insight and prestidigitation in such matters is boundless!

Now let's hope this commit doesn't upset the buildbot gods ;)

Thanks!

llvm-svn: 194188
2013-11-07 05:17:06 +00:00
Faisal Vali b9657033cc Support return type deduction for templates in -fdelayed-template-parsing (microsoft) mode
Please see http://llvm-reviews.chandlerc.com/D2053 for discussion and Richard's stamp.

llvm-svn: 193849
2013-11-01 02:01:01 +00:00
Faisal Vali 2b3a301ead Refactor: Extract specializing the generic lambda call operator during conversion to fptr deduction into its own function.
No functionality change.

All clang regression tests pass.

Thanks!

llvm-svn: 193383
2013-10-24 23:40:02 +00:00
Faisal Vali 58d0d36006 Fix a test file option from -emit-llvm -o to -emit-llvm-only.
As requested by Rafael Espindola here: http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20131021/091556.html

llvm-svn: 193296
2013-10-24 01:11:55 +00:00
Faisal Vali 66605d40a7 Fix an instantiation bug with nested generic lambdas and conversion to fptrs.
This patch fixes the typelocs of the conversion-operator and the conversion-operator-name and adds the parameters of the call operator to the FunctionProtoTypeLoc of the respective entities. Thus, when the template declarations (conversion operators) undergo deduction and instantiation/transformation/substitution - they add themselves to the local instantiation scope if needed.

This patch supports the following:

auto L = [](auto b) {
  return [](auto a) ->decltype(a) { return a; };
};
int (*fp)(int) = L(8);

Richard LGTM'd this patch: http://llvm-reviews.chandlerc.com/D1831

Thanks!

llvm-svn: 193294
2013-10-24 01:05:22 +00:00
Faisal Vali 2cba133818 And Again: Teach TreeTransform how to transform nested generic lambdas.
A previous attempt http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20130930/090049.html resulted in PR 17476, and was reverted,

The original TransformLambdaExpr (pre generic-lambdas) transformed the TypeSourceInfo of the Call operator in its own instantiation scope via TransformType.  This resulted in the parameters of the call operator being mapped to their transformed counterparts in an instantiation scope that would get popped off.
Then a call to TransformFunctionParameters would add the parameters and their transformed mappings (but newly created ones!) to the current instantiation scope. This would result in a disconnect between the new call operator's TSI parameters and those used to construct the call operator declaration. This was ok in the non-generic lambda world - but would cause issues with nested transformations (when non-generic and generics were interleaved) in the generic lambda world - that I somewhat kludged around initially - but this resulted in PR17476.

The new approach seems cleaner. We only do the transformation of the TypeSourceInfo - but we make sure to do it in the current instantiation scope so we don't lose the untransformed to transformed mappings of the ParmVarDecls when they get created.   

Another attempt caused a test to fail (http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20131021/091533.html) and also had to be reverted - my apologies - in my haste, i did not run all the tests - argh!

Now all the tests seem to pass - but a Fixme has been added - since I suspect Richard will find the fix a little inelegant ;) I shall try and work on a more elegant fix once I have had a chance to discuss with Richard or Doug at a later date.

Hopefully the third time;s a charm *fingers crossed*

This does not yet include capturing.

Please see test file for examples.

This patch was LGTM'd by Doug:
http://llvm-reviews.chandlerc.com/D1784

llvm-svn: 193230
2013-10-23 06:44:28 +00:00
Rafael Espindola 09b00e34fa Revert r193223 and r193216.
They were causing CodeGenCXX/mangle-exprs.cpp to fail.

Revert "Remove the circular reference to LambdaExpr in CXXRecordDecl."

Revert "Again: Teach TreeTransform and family how to transform generic lambdas nested within templates and themselves."

llvm-svn: 193226
2013-10-23 04:12:23 +00:00
Faisal Vali 6eac881f66 Again: Teach TreeTransform and family how to transform generic
lambdas nested within templates and themselves.

A previous attempt http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20130930/090049.html resulted in PR 17476, and was reverted,


The original TransformLambdaExpr (pre generic-lambdas) transformed the TypeSourceInfo of the Call operator in its own instantiation scope via TransformType.  This resulted in the parameters of the call operator being mapped to their transformed counterparts in an instantiation scope that would get popped off.
Then a call to TransformFunctionParameters would add the parameters and their transformed mappings (but newly created ones!) to the current instantiation scope. This would result in a disconnect between the new call operator's TSI parameters and those used to construct the call operator declaration. This was ok in the non-generic lambda world - but would cause issues with nested transformations (when non-generic and generics were interleaved) in the generic lambda world - that I somewhat kludged around initially - but this resulted in PR17476.

The new approach seems cleaner. We only do the transformation of the TypeSourceInfo - but we make sure to do it in the current instantiation scope so we don't lose the untransformed to transformed mappings of the ParmVarDecls when they get created.   

This does not yet include capturing.

Please see test file for examples.

This patch was LGTM'd by Doug:
http://llvm-reviews.chandlerc.com/D1784

llvm-svn: 193216
2013-10-23 00:51:58 +00:00
Rafael Espindola 4b35f27206 Revert "Teach TreeTransform and family how to transform generic lambdas within templates and nested within themselves."
This reverts commit r191879. It caused llvm.org/pr17476.

llvm-svn: 191955
2013-10-04 14:28:51 +00:00
Faisal Vali 8ec4036669 Teach TreeTransform and family how to transform generic lambdas within templates and nested within themselves.
This does not yet include capturing (that is next).

Please see test file for examples.

This patch was LGTM'd by Doug:
http://llvm-reviews.chandlerc.com/D1784
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20130930/090048.html

When I first committed this patch - a bunch of buildbots were unable to compile the code that VS2010 seemed to compile.  Seems like there was a dependency on Sema/Template.h which VS did not seem to need, but I have now added for the other compilers.  It still compiles on Visual Studio 2010 - lets hope the buildbots remain quiet (please!)

llvm-svn: 191879
2013-10-03 06:29:33 +00:00
Faisal Vali 10a00ee485 Revert changes from the nested lambdas commit till i figure out
why the buildbots are failing.

llvm-svn: 191876
2013-10-03 05:58:37 +00:00
Faisal Vali ba78d34347 Teach TreeTransform and family how to transform generic lambdas within templates and nested within themselves.
This does not yet include capturing (that is next).

Please see test file for examples.

This patch was LGTM'd by Doug:
http://llvm-reviews.chandlerc.com/D1784

llvm-svn: 191875
2013-10-03 05:32:48 +00:00
Faisal Vali 571df12581 Implement conversion to function pointer for generic lambdas without captures.
The general strategy is to create template versions of the conversion function and static invoker and then during template argument deduction of the conversion function, create the corresponding call-operator and static invoker specializations, and when the conversion function is marked referenced generate the body of the conversion function using the corresponding static-invoker specialization.  Similarly, Codegen does something similar - when asked to emit the IR for a specialized static invoker of a generic lambda, it forwards emission to the corresponding call operator. 

This patch has been reviewed in person both by Doug and Richard.  Richard gave me the LGTM.

A few minor changes:
  - per Richard's request i added a simple check to gracefully inform that captures (init, explicit or default) have not been added to generic lambdas just yet (instead of the assertion violation).
  - I removed a few lines of code that added the call operators instantiated parameters to the currentinstantiationscope. Not only did it not handle parameter packs, but it is more relevant in the patch for nested lambdas which will follow this one, and fix that problem more comprehensively.
  - Doug had commented that the original implementation strategy of using the TypeSourceInfo of the call operator to create the static-invoker was flawed and allowed const as a member qualifier to creep into the type of the static-invoker.  I currently kludge around it - but after my initial discussion with Doug, with a follow up session with Richard, I have added a FIXME so that a more elegant solution that involves the use of TrivialTypeSourceInfo call followed by the correct wiring of the template parameters to the functionprototypeloc is forthcoming.

Thanks! 
 

llvm-svn: 191634
2013-09-29 08:45:24 +00:00