Commit Graph

379 Commits

Author SHA1 Message Date
Reid Kleckner d60b82f93e Handle use of default member initializers before end of outermost class
Specifically, when we have this situation:
  struct A {
    template <typename T> struct B {
      int m1 = sizeof(A);
    };
    B<int> m2;
  };

We can't parse m1's initializer eagerly because we need A to be
complete.  Therefore we wait until the end of A's class scope to parse
it. However, we can trigger instantiation of B before the end of A,
which will attempt to instantiate the field decls eagerly, and it would
build a bad field decl instantiation that said it had an initializer but
actually lacked one.

Fixed by deferring instantiation of default member initializers until
they are needed during constructor analysis. This addresses a long
standing FIXME in the code.

Fixes PR19195.

Reviewed By: rsmith

Differential Revision: http://reviews.llvm.org/D5690

llvm-svn: 222192
2014-11-17 23:36:45 +00:00
Richard Smith 0b3a46247e PR21437, final part of DR1330: delay-parsing of exception-specifications. This
is a re-commit of Doug's r154844 (modernized and updated to fit into current
Clang).

llvm-svn: 221918
2014-11-13 20:01:57 +00:00
Richard Smith 640775b428 PR21180: Lambda closure types are neither aggregates nor literal types.
llvm-svn: 219222
2014-10-07 18:01:33 +00:00
Richard Smith ec2748a8ad More fixes for isBetterOverloadCandidate not being a strict weak ordering. The
bug was obvious from inspection, figuring out a way to test it was... less so.

llvm-svn: 209060
2014-05-17 04:36:39 +00:00
Richard Smith 6c6bbfab19 PR19346: Adding 0 to a null pointer has defined behavior in C++. Allow it in constant expressions.
llvm-svn: 205757
2014-04-08 12:19:28 +00:00
David Blaikie abe1a398e3 Render anonymous entities as '(anonymous <thing>)' (and lambdas as '(lambda at ... )')
For namespaces, this is consistent with mangling and GCC's debug info
behavior. For structs, GCC uses <anonymous struct> but we prefer
consistency between all anonymous entities but don't want to confuse
them with template arguments, etc, so we'll just go with parens in all
cases.

llvm-svn: 205398
2014-04-02 05:58:29 +00:00
Yunzhong Gao fcdc45ff2d Creating a printing policy for "half":
Since "half" is an OpenCL keyword and clang accepts __fp16 as an extension for
other languages, error messages and metadata (and hence debug info) should refer
to the half-precision floating point as "__fp16" instead of "half" when
compiling for non-OpenCL languages. This patch creates a new printing policy for
half in a similar manner to what is done for bool and wchar_t.

Differential Revision: http://llvm-reviews.chandlerc.com/D2952

llvm-svn: 204164
2014-03-18 17:55:18 +00:00
Richard Smith 66204ecff9 DR1346: a parenthesized braced-init-list cannot be used as the initializer when
performing auto type deduction.

llvm-svn: 203683
2014-03-12 17:42:45 +00:00
David Majnemer b100410365 Normalize line endings
Some files had CRLF line terminators, some only had a mixture of
CRLF and LF.  Switch to LF.

llvm-svn: 202659
2014-03-02 18:46:05 +00:00
Richard Trieu 3bb8b56a5d PR16074, implement warnings to catch pointer to boolean true and pointer to
null comparison when the pointer is known to be non-null.

This catches the array to pointer decay, function to pointer decay and
address of variables.  This does not catch address of function since this
has been previously used to silence a warning.

Pointer to bool conversion is under -Wbool-conversion.
Pointer to null comparison is under -Wtautological-pointer-compare, a sub-group
of -Wtautological-compare.

void foo() {
  int arr[5];
  int x;
  // warn on these conditionals
  if (foo);
  if (arr);
  if (&x);
  if (foo == null);
  if (arr == null);
  if (&x == null);

  if (&foo);  // no warning
}

llvm-svn: 202216
2014-02-26 02:36:06 +00:00
Richard Smith 990a692f4a Don't allow 'this' within typedefs within classes that otherwise look like they
might be member function declarations. Patch by Harald van Dijk!

llvm-svn: 199512
2014-01-17 21:01:18 +00:00
Douglas Gregor d1e3ceb5ec Require the type of a by-copy capture to be complete before creating its field.
The problem here is more serious than the fix implies. Adding a field
to a class updates the triviality bits for the class (among other
things). Failing to require a complete type before adding the field
meant that these updates don't happen in the well-formed case where
the capture is an uninstantiated class template specialization,
leading the lambda itself to be treated as having a trivial copy
constructor when it shouldn't. Fixes <rdar://problem/15560464>.

llvm-svn: 197623
2013-12-18 23:02:36 +00:00
Alp Toker 6ed7251683 Revert "Don't require -re suffix on -verify directives with regexes."
This patch was submitted to the list for review and didn't receive a LGTM.

(In fact one explicit objection and one query were raised.)

This reverts commit r197295.

llvm-svn: 197299
2013-12-14 01:07:05 +00:00
Hans Wennborg 9b395ef284 Don't require -re suffix on -verify directives with regexes.
Differential Revision: http://llvm-reviews.chandlerc.com/D2392

llvm-svn: 197295
2013-12-14 00:46:53 +00:00
Hans Wennborg 86be54cc80 Tighten test regexes checking for __attribute__((thiscall)) on function types.
The tests were perhaps made too relaxed in r197164 when we switched to the new
MinGW ABI. This makes sure we check explicitly for an optional thiscall
attribute and nothing else.

We should still look into whether we should print these attributes at all in
these cases.

llvm-svn: 197252
2013-12-13 18:34:23 +00:00
Rafael Espindola 3497069784 Switch to the new MingW ABI.
GCC 4.7 changed the MingW ABI. On the clang side this means that methods now
have the thiscall calling convention by default.

llvm-svn: 197164
2013-12-12 16:07:11 +00:00
Faisal Vali 0a2de2f052 Delete the now unnecessary test/generic-lambda-unimplemented-1y.cpp
llvm-svn: 196664
2013-12-07 20:57:51 +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
David Majnemer 9adc361008 Sema: Do not allow lambda expressions to appear inside of constant expressions
We would previously not diagnose this which would lead to crashes (on
very strange code).

This fixes PR17675.

llvm-svn: 193397
2013-10-25 09:12:52 +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
Douglas Gregor 71fe0e8a83 Diagnose by-copy captures of abstract classes.
Fixes <rdar://problem/14468891>.

llvm-svn: 192419
2013-10-11 04:25:21 +00:00
David Majnemer 767c1f8428 Make wording for certain invalid unary expressions more consistent.
An invalid decltype expression like 'decltype int' gives:
error: expected '(' after 'decltype'

This makes it so 'sizeof int' gives a similar one:
error: expected parentheses around type name in sizeof expression

llvm-svn: 192258
2013-10-09 00:22:23 +00:00
Serge Pavlov 5815940edc Fixed messages in tests.
llvm-svn: 192208
2013-10-08 17:38:38 +00:00
Serge Pavlov aa57a64ef6 Add fixits suggesting parenthesis around type name in expressions like sizeof.
This fixes PR16992 - Fixit missing when "sizeof type" found.

llvm-svn: 192200
2013-10-08 16:56:30 +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
Richard Smith 5b013f5050 Add compat/extension warnings for init captures.
llvm-svn: 191609
2013-09-28 05:38:27 +00:00
Richard Smith bb13c9a49d Per latest drafting, switch to implementing init-captures as if by declaring
and capturing a variable declaration, and complete the implementation of them.

llvm-svn: 191605
2013-09-28 04:02:39 +00:00
Faisal Vali 7c9f3ca21a Fix the test files by removing the unnecessary -emit-llvm flag (should address Matt Beaumont-Gay's concern regarding failure on a read-only filesystem)
llvm-svn: 191531
2013-09-27 16:45:48 +00:00
Faisal Vali 2b391ab708 Implement a rudimentary form of generic lambdas.
Specifically, the following features are not included in this commit:
  - any sort of capturing within generic lambdas 
  - generic lambdas within template functions and nested 
    within other generic lambdas
  - conversion operator for captureless lambdas
  - ensuring all visitors are generic lambda aware
  (Although I have gotten some useful feedback on my patches of the above and will be incorporating that as I submit those patches for commit)

As an example of what compiles through this commit:

template <class F1, class F2>
struct overload : F1, F2 {
    using F1::operator();
    using F2::operator();
    overload(F1 f1, F2 f2) : F1(f1), F2(f2) { }
  };

  auto Recursive = [](auto Self, auto h, auto ... rest) {
    return 1 + Self(Self, rest...);
  };
  auto Base = [](auto Self, auto h) {
      return 1;
  };
  overload<decltype(Base), decltype(Recursive)> O(Base, Recursive);
  int num_params =  O(O, 5, 3, "abc", 3.14, 'a');

Please see attached tests for more examples.

This patch has been reviewed by Doug and Richard.  Minor changes (non-functionality affecting) have been made since both of them formally looked at it, but the changes involve removal of supernumerary return type deduction changes (since they are now redundant, with richard having committed a recent patch to address return type deduction for C++11 lambdas using C++14 semantics). 



Some implementation notes:

  - Add a new Declarator context => LambdaExprParameterContext to 
    clang::Declarator to allow the use of 'auto' in declaring generic
    lambda parameters
      
  - Add various helpers to CXXRecordDecl to facilitate identifying
    and querying a closure class
  
  - LambdaScopeInfo (which maintains the current lambda's Sema state)
    was augmented to house the current depth of the template being
    parsed (id est the Parser calls Sema::RecordParsingTemplateParameterDepth)
    so that SemaType.cpp::ConvertDeclSpecToType may use it to immediately 
    generate a template-parameter-type when 'auto' is parsed in a generic
    lambda parameter context.  (i.e we do NOT use AutoType deduced to 
    a template parameter type - Richard seemed ok with this approach).  
    We encode that this template type was generated from an auto by simply
    adding $auto to the name which can be used for better diagnostics if needed.

  - SemaLambda.h was added to hold some common lambda utility
    functions (this file is likely to grow ...)
    
  - Teach Sema::ActOnStartOfFunctionDef to check whether it
    is being called to instantiate a generic lambda's call
    operator, and if so, push an appropriately prepared
    LambdaScopeInfo object on the stack.
    
  - various tests were added - but much more will be needed.

There is obviously more work to be done, and both Richard (weakly) and Doug (strongly) 
have requested that LambdaExpr be removed form the CXXRecordDecl LambdaDefinitionaData
in a future patch which is forthcoming.

A greatful thanks to all reviewers including Eli Friedman, James Dennett, 
and especially the two gracious wizards (Richard Smith and Doug Gregor) 
who spent hours providing feedback (in person in Chicago and on the mailing lists).  
And yet I am certain that I have allowed unidentified bugs to creep in; bugs, that I will do my best to slay, once identified!

Thanks!

llvm-svn: 191453
2013-09-26 19:54:12 +00:00
Chandler Carruth b4a4326824 Mark that qualifiers can prefix the auto type. This seems to just have
been an oversight, as it definitely works. Every test which changed had
the const written on the LHS of the auto already.

Notably, this also makes things like cpp11-migrate's formation of 'const
auto &' variables much more familiar.

Yes, many people feel that 'const' and other qualifiers belong on the
RHS of the type. I'm not going to argue about that because Clang already
*overwhelming* places the qualifiers on the LHS when it can and on the
RHS when it must. We shouldn't diverge for auto. We should add a tool to
clang-tidy that fixes this in either direction, and then wire up
clang-tidy to tools like cpp11-migrate to fix their placement after
transforms.

llvm-svn: 189769
2013-09-02 19:20:06 +00:00
Manuel Klimek 2fdbea2819 Revert "Implement a rudimentary form of generic lambdas."
This reverts commit 606f5d7a99b11957e057e4cd1f55f931f66a42c7.

llvm-svn: 189004
2013-08-22 12:12:24 +00:00
Faisal Vali fd5277c063 Implement a rudimentary form of generic lambdas.
Specifically, the following features are not included in this commit:
  - any sort of capturing within generic lambdas 
  - nested lambdas
  - conversion operator for captureless lambdas
  - ensuring all visitors are generic lambda aware


As an example of what compiles:

template <class F1, class F2>
struct overload : F1, F2 {
    using F1::operator();
    using F2::operator();
    overload(F1 f1, F2 f2) : F1(f1), F2(f2) { }
  };

  auto Recursive = [](auto Self, auto h, auto ... rest) {
    return 1 + Self(Self, rest...);
  };
  auto Base = [](auto Self, auto h) {
      return 1;
  };
  overload<decltype(Base), decltype(Recursive)> O(Base, Recursive);
  int num_params =  O(O, 5, 3, "abc", 3.14, 'a');

Please see attached tests for more examples.

Some implementation notes:

  - Add a new Declarator context => LambdaExprParameterContext to 
    clang::Declarator to allow the use of 'auto' in declaring generic
    lambda parameters
    
  - Augment AutoType's constructor (similar to how variadic 
    template-type-parameters ala TemplateTypeParmDecl are implemented) to 
    accept an IsParameterPack to encode a generic lambda parameter pack.
  
  - Add various helpers to CXXRecordDecl to facilitate identifying
    and querying a closure class
  
  - LambdaScopeInfo (which maintains the current lambda's Sema state)
    was augmented to house the current depth of the template being
    parsed (id est the Parser calls Sema::RecordParsingTemplateParameterDepth)
    so that Sema::ActOnLambdaAutoParameter may use it to create the 
    appropriate list of corresponding TemplateTypeParmDecl for each
    auto parameter identified within the generic lambda (also stored
    within the current LambdaScopeInfo).  Additionally, 
    a TemplateParameterList data-member was added to hold the invented
    TemplateParameterList AST node which will be much more useful
    once we teach TreeTransform how to transform generic lambdas.
    
  - SemaLambda.h was added to hold some common lambda utility
    functions (this file is likely to grow ...)
    
  - Teach Sema::ActOnStartOfFunctionDef to check whether it
    is being called to instantiate a generic lambda's call
    operator, and if so, push an appropriately prepared
    LambdaScopeInfo object on the stack.
    
  - Teach Sema::ActOnStartOfLambdaDefinition to set the
    return type of a lambda without a trailing return type
    to 'auto' in C++1y mode, and teach the return type
    deduction machinery in SemaStmt.cpp to process either
    C++11 and C++14 lambda's correctly depending on the flag.    

  - various tests were added - but much more will be needed.

A greatful thanks to all reviewers including Eli Friedman,  
James Dennett and the ever illuminating Richard Smith.  And 
yet I am certain that I have allowed unidentified bugs to creep in; 
bugs, that I will do my best to slay, once identified!

Thanks!

llvm-svn: 188977
2013-08-22 01:49:11 +00:00
Eli Friedman 4e28b26589 sizeof(void) etc. should be a hard error in C++.
PR16872.

llvm-svn: 188324
2013-08-13 22:26:42 +00:00
Richard Smith d7293d7fcb Implement C++'s restrictions on the type of an expression passed to a vararg
function: it can't be 'void' and it can't be an initializer list. We give a
hard error for these rather than treating them as undefined behavior (we can
and probably should do the same for non-POD types in C++11, but as of this
change we don't).

Slightly rework the checking of variadic arguments in a function with a format
attribute to ensure that certain kinds of format string problem (non-literal
string, too many/too few arguments, ...) don't suppress this error.

llvm-svn: 187735
2013-08-05 18:49:43 +00:00
Richard Smith 1836e60023 Handle a difference in lambda return type deduction between C++11 and C++1y: if
no return type is specified, C++11 will deduce a cv-qualified return type in
some cases, but C++1y never will.

llvm-svn: 187275
2013-07-26 23:45:07 +00:00
Larisse Voufo 98b20f1278 FIXME fix: improving diagnostics for template arguments deduction of class templates and explicit specializations
This patch essentially removes all the FIXMEs following calls to DeduceTemplateArguments() that want to keep track of deduction failure info.

llvm-svn: 186730
2013-07-19 23:00:19 +00:00
Larisse Voufo 47c0845e0b Revert "Use function overloading instead of template specialization for diagnosis of bad template argument deductions."
This reverts commit a730f548325756d050d4caaa28fcbffdae8dfe95.

llvm-svn: 186729
2013-07-19 22:53:23 +00:00
Larisse Voufo 8d33da6d58 Use function overloading instead of template specialization for diagnosis of bad template argument deductions.
llvm-svn: 186727
2013-07-19 22:34:32 +00:00
Eli Friedman 95f501284a More fixes for block mangling.
Make sure we properly treat names defined inside a block as local
names.  There are basically three fixes here.  One, correctly
treat blocks as a context where we need to use local-name mangling using
the new isLocalContainerContext helper. Two, make
CXXNameMangler::manglePrefix handle local names in a consistent way.
Three, extend CXXNameMangler::mangleLocalName so it can mangle a block
correctly.

llvm-svn: 185450
2013-07-02 17:52:28 +00:00
Eli Friedman 92821745bf Don't skip lambdas when mangling local vars.
This commit rearranges the logic in CXXNameMangler::mangleLocalName and
GetLocalClassDecl so that it doesn't accidentally skip over lambdas.  It
also reduces code duplication a bit.

llvm-svn: 185402
2013-07-02 02:01:18 +00:00
Eli Friedman 7e346a8127 Fix mangling for block literals.
Blocks, like lambdas, can be written in contexts which are required to be
treated as the same under ODR.  Unlike lambdas, it isn't possible to actually
take the address of a block, so the mangling of the block itself doesn't
matter. However, objects like static variables inside a block do need to
be mangled in a consistent way.

There are basically three components here. One, block literals need a
consistent numbering.  Two, objects/types inside a block literal need
to be mangled using it.  Three, objects/types inside a block literal need
to have their linkage computed correctly.

llvm-svn: 185372
2013-07-01 20:22:57 +00:00
Eli Friedman 5a8738ffe0 Fix regression from r184810.
Specifically, CallExpr::getCalleeDecl() can return null, so make sure to
handle that correctly.

llvm-svn: 184813
2013-06-25 01:55:41 +00:00
Eli Friedman 0423b76be1 Fix noexcept for delete expressions.
Using "delete" on a pointer to an incomplete type can't throw.
While I'm here, clean up the signature of the canCalleeThrow() helper.

llvm-svn: 184810
2013-06-25 01:24:22 +00:00
Eli Friedman d0ee1294c1 Change mangling of objects inside block literals.
This changes the mangling of local static variables/etc. inside blocks
to do something simple and sane.  This avoids depending on the way we mangle
blocks, which isn't really appropriate here.

John, please take a look at this to make sure the mangling I chose is sane.

Fixes <rdar://problem/14074423>.

llvm-svn: 184780
2013-06-24 20:24:19 +00:00
Richard Smith 6b6f8aac72 PR16263: Implement current direction of core issue 1376. Binding a reference to
the result of a cast-to-reference-type lifetime-extends the object to which the
reference inside the cast binds.

This requires us to look for subobject adjustments on both the inside and the
outside of the MaterializeTemporaryExpr when looking for a temporary to
lifetime-extend (which we also need for core issue 616, and possibly 1213).

llvm-svn: 184024
2013-06-15 00:30:29 +00:00
Richard Smith 82c9b5183f Fix handling of const_cast from prvalue to rvalue reference: such a cast is
only permitted if the source object is of class type, and should materialize a
temporary for the reference to bind to.

llvm-svn: 184017
2013-06-14 22:27:52 +00:00
Eli Friedman 31a5bcc24e Unify return type checking for functions and ObjC methods. Move all the
random checks for ObjC object return types to SemaType.cpp.

Fixes issue with ObjC method type checking reported on cfe-dev.

llvm-svn: 184006
2013-06-14 21:14:10 +00:00
David Majnemer 66ad574f40 Implement DR61: Address of ambiguous bound methods should be disallowed
DR61 affirms that expressions containing unresolved member access should
be disallowed when performing "address of" operations.

llvm-svn: 183723
2013-06-11 03:56:29 +00:00
Richard Smith c3d2ebb60f PR16243: Use CXXThisOverride during template instantiation, and fix up the
places which weren't setting it up properly. This allows us to get the right
cv-qualifiers for 'this' when it appears outside a method body in a class
template.

llvm-svn: 183483
2013-06-07 02:33:37 +00:00
Richard Smith 844010455d Refactor constant expression evaluation to associate the complete object of a
materialized temporary with the corresponding MaterializeTemporaryExpr. This is
groundwork for providing C++11's guaranteed static initialization for global
references bound to lifetime-extended temporaries (if the initialization is a
constant expression).

In passing, fix a couple of bugs where some evaluation failures didn't trigger
diagnostics, and a rejects-valid where potential constant expression testing
would assume that it knew the dynamic type of *this and would reject programs
which relied on it being some derived type.

llvm-svn: 183093
2013-06-03 05:03:02 +00:00
Richard Smith ba71c08523 First pass of semantic analysis for init-captures: check the initializer, build
a FieldDecl from it, and propagate both into the closure type and the
LambdaExpr.

You can't do much useful with them yet -- you can't use them within the body
of the lambda, because we don't have a representation for "the this of the
lambda, not the this of the enclosing context". We also don't have support or a
representation for a nested capture of an init-capture yet, which was intended
to work despite not being allowed by the current standard wording.

llvm-svn: 181985
2013-05-16 06:20:58 +00:00
Richard Smith 9155be1e54 C++1y: provide full 'auto' return type deduction for lambda expressions. This
completes the implementation of N3638.

llvm-svn: 181669
2013-05-12 03:09:35 +00:00
John McCall d25db7ed0f Grab-bag of bit-field fixes:
- References to ObjC bit-field ivars are bit-field lvalues;
    fixes rdar://13794269, which got me started down this.
  - Introduce Expr::refersToBitField, switch a couple users to
    it where semantically important, and comment the difference
    between this and the existing API.
  - Discourage Expr::getBitField by making it a bit longer and
    less general-sounding.
  - Lock down on const_casts of bit-field gl-values until we
    hear back from the committee as to whether they're allowed.

llvm-svn: 181252
2013-05-06 21:39:12 +00:00
Richard Smith 034185c2f9 The 'constexpr implies const' rule for non-static member functions is gone in
C++1y, so stop adding the 'const' there. Provide a compatibility warning for
code relying on this in C++11, with a fix-it hint. Update our lazily-written
tests to add the const, except for those ones which were testing our
implementation of this rule.

llvm-svn: 179969
2013-04-21 01:08:50 +00:00
Richard Smith 3cb4c63073 DR974: Lambdas can have default arguments.
llvm-svn: 179688
2013-04-17 16:25:20 +00:00
Douglas Gregor 89c0a914ec <rdar://problem/13473493> Handle 'this->' insertion recovery within trailing return types.
llvm-svn: 178081
2013-03-26 22:43:55 +00:00
Richard Smith ad1bbb93d1 PR15290: 'this' is not permitted in the declaration of a friend function,
therefore references to members should not be transformed into implicit uses of
'this'. Patch by Ismail Pazarbasi!

llvm-svn: 177134
2013-03-15 00:41:52 +00:00
Douglas Gregor 0b7bc7f996 Don't crash while printing APValues that are lvalues casted to a
decidedly non-reference, non-pointer type. Fixes <rdar://problem/13090123>.

llvm-svn: 173747
2013-01-29 01:26:43 +00:00
Richard Smith 10876ef571 Implement C++11 semantics for [[noreturn]] attribute. This required splitting
it apart from [[gnu::noreturn]] / __attribute__((noreturn)), since their
semantics are not equivalent (for instance, we treat [[gnu::noreturn]] as
affecting the function type, whereas [[noreturn]] does not).

llvm-svn: 172691
2013-01-17 01:30:42 +00:00
Richard Smith de1a487402 Improve diagnostic wording for when an implicitly-deleted special member
function is selected by overload resolution.

llvm-svn: 171190
2012-12-28 12:23:24 +00:00
Richard Smith 56cb16dd92 Test that we correctly deal with multiple copy constructors when detecting
non-trivial special members for varargs calls.

llvm-svn: 168476
2012-11-22 00:21:13 +00:00
Michael Han 64536a6d25 Teach Clang parser to reject C++11 attributes that appertain to declaration specifiers.
We don't support any C++11 attributes that appertain to declaration specifiers so reject 
the attributes in parser until we support them; this also conforms to what g++ 4.8 is doing.

llvm-svn: 167481
2012-11-06 19:34:54 +00:00
Douglas Gregor d814a05f6b When capturing 'this' in a lambda, make sure to update the set of
array-index starting values for the 'this' capture. Fixes
<rdar://problem/12426831>.

llvm-svn: 166709
2012-10-25 18:39:16 +00:00
Andy Gibbs c6e68daac0 Prior to adding the new "expected-no-diagnostics" directive to VerifyDiagnosticConsumer, make the necessary adjustment to 580 test-cases which will henceforth require this new directive.
llvm-svn: 166280
2012-10-19 12:44:48 +00:00
Andy Gibbs ac51de6ec2 Fix directive parsing in VerifyDiagnosticConsumer so that it ensures that "expected" is at the start of the word and will no longer accept typos such as "junkexpected-*" as a valid "expected-*" directive. A very few test-cases had to be amended to adhere to the new rule.
Patch reviewed by David Blaikie.

llvm-svn: 166279
2012-10-19 12:36:49 +00:00
Richard Smith ca24ed473b Revert r163829. The world (or libstdc++, at least) is not ready.
llvm-svn: 163846
2012-09-13 22:00:12 +00:00
Richard Smith b67ada6687 Remove speculative fix for C++ core issue 1407, since it was resolved as NAD.
llvm-svn: 163829
2012-09-13 19:55:13 +00:00
Richard Smith 2589b9808e PR12057: Allow variadic template pack expansions to cross lambda boundaries.
Rather than adding a ContainsUnexpandedParameterPack bit to essentially every
AST node, we tunnel the bit directly up to the surrounding lambda expression
when we reach a context where an unexpanded pack can not normally appear.
Thus any statement or declaration within a lambda can now potentially contain
an unexpanded parameter pack.

llvm-svn: 160705
2012-07-25 03:56:55 +00:00
Eli Friedman 4eafb6b77b Don't treat overflow in floating-point conversions as a hard error in constant evaluation. <rdar://problem/11874571>.
llvm-svn: 160394
2012-07-17 21:03:05 +00:00
Fariborz Jahanian 6362803cfe block literal irgen: several improvements on naming block
literal helper functions. All helper functions (global
and locals) use block_invoke as their prefix. Local literal
helper names are prefixed by their enclosing mangled function
names. Blocks in non-local initializers (e.g. a global variable 
or a C++11 field) are prefixed by their mangled variable name. 
The descriminator number added to end of the name starts off 
with blank (for first block) and _<N> (for the N+2-th block).

llvm-svn: 159206
2012-06-26 16:06:38 +00:00
Douglas Gregor 7efd007cfb Check the parameter lists and return type of both blocks and lambdas
for unexpanded parameter packs. Fixes the crash-on-invalid in
PR13117.

llvm-svn: 158525
2012-06-15 16:59:29 +00:00
Aaron Ballman 8d4688718b Fixes some test cases that should have come along with r157943.
llvm-svn: 157947
2012-06-04 20:07:46 +00:00
Eli Friedman f798f65ccc Implement the C++11 discarded value expression rules for volatile lvalues. <rdar://problem/10790820>.
llvm-svn: 157420
2012-05-24 22:04:19 +00:00
Douglas Gregor a8182f9b66 Clean up r156925, so that we only mark the capturing DeclRefExpr of a
lambda as referring to a local in an enclosing scope if we're in the
enclosing scope of the lambda (not it's function call operator). Also,
turn the test into an IR generation test, since that's where the
crashes occurred. Really fixes PR12746 / <rdar://problem/11465120>.

llvm-svn: 156926
2012-05-16 17:01:33 +00:00
Douglas Gregor 26b51746ec Fix code generation of variables reference expressions when mixing
blocks and lambdas, based heavily on a patch from Meador Inge. Fixes
PR12746 / <rdar://problem/11465120>.

llvm-svn: 156925
2012-05-16 16:50:20 +00:00
David Blaikie 7555b6a4e5 Improve some of the conversion warnings to fire on conversion to bool.
Moves the bool bail-out down a little in SemaChecking - so now
-Wnull-conversion and -Wliteral-conversion can fire when the target type is
bool.

Also improve the wording/details in the -Wliteral-conversion warning to match
the -Wconstant-conversion.

llvm-svn: 156826
2012-05-15 16:56:36 +00:00
Richard Smith 38f7c7bca5 Disable our non-standard delayed parsing of exception specifications. Delaying
the parsing of such things appears to be a conforming extension, but it breaks
libstdc++4.7's std::pair.

llvm-svn: 155975
2012-05-02 01:29:43 +00:00
Richard Smith db05f1fb08 PR9546, DR1268: A prvalue cannot be reinterpret_cast to an rvalue reference
type. But a glvalue can be reinterpret_cast to either flavor of reference.

llvm-svn: 155789
2012-04-29 08:24:44 +00:00
Douglas Gregor 433e05306f Implement the last part of C++ [class.mem]p2, delaying the parsing of
exception specifications on member functions until after the closing
'}' for the containing class. This allows, for example, a member
function to throw an instance of its own class. Fixes PR12564 and a
fairly embarassing oversight in our C++98/03 support.

llvm-svn: 154844
2012-04-16 18:27:27 +00:00
Douglas Gregor 3024f07c12 Implement C++11 [expr.prim.general]p3, which permits the use of 'this'
in the declaration of a non-static member function after the
(optional) cv-qualifier-seq, which in practice means in the exception
specification and late-specified return type. 

The new scheme here used to manage 'this' outside of a member function
scope is more general than the Scope-based mechanism previously used
for non-static data member initializers and late-parsesd attributes,
because it can also handle the cv-qualifiers on the member
function. Note, however, that a separate pass is required for static
member functions to determine whether 'this' was used, because we
might not know that we have a static function until after declaration
matching.

Finally, this introduces name mangling for 'this' and for the implicit
'this', which is intended to match GCC's mangling. Independent
verification for the new mangling test case would be appreciated.

Fixes PR10036 and PR12450.

llvm-svn: 154799
2012-04-16 07:05:22 +00:00
Richard Smith fa0a1f531f Improve diagnostics for invalid use of non-static members / this:
* s/nonstatic/non-static/ in the diagnostics, since the latter form outvoted
  the former by 28-2 in our diagnostics.
* Fix the "use of member in static member function" diagnostic to correctly
  detect this situation inside a block or lambda.
* Produce a more specific "invalid use of non-static member" diagnostic for
  the case where a nested class member refers to a member of a
  lexically-surrounding class.

llvm-svn: 154073
2012-04-05 01:13:04 +00:00
Eli Friedman 2b22a6e61c Extend -Wc++11-narrowing to cover converted constant expressions as well as braced-initializers. <rdar://problem/11121178>.
llvm-svn: 153703
2012-03-29 23:39:39 +00:00
David Blaikie 631a486e6a Fix crash & accepts-invalid for array of arrays of user defined type.
Test case/other help by Richard Smith.
Code review by John McCall.

llvm-svn: 152519
2012-03-10 23:40:02 +00:00
Richard Smith 35ecb36fcd Ensure that we instantiate static reference data members of class templates
early, since their values can be used in constant expressions in C++11. For
odr-use checking, the opposite change is required, since references are
odr-used whether or not they satisfy the requirements for appearing in a
constant expression.

llvm-svn: 151881
2012-03-02 04:14:40 +00:00
Richard Smith 5e0cac784a Reject 'a = {0} = {0}' rather than parsing it as '(a = {0}) = {0}'. Also
improve the diagnostics for some attempts to use initializer lists in
expressions.

llvm-svn: 151794
2012-03-01 02:59:17 +00:00
Eli Friedman c6237c6e05 Make the odr-use logic work correctly for constant-expressions. PR12006.
llvm-svn: 151699
2012-02-29 03:16:56 +00:00
Sebastian Redl 6776673f09 Convert initializer lists to temporaries in CreateBuiltinBinOp. Allows assignment of init lists to built-in types and resolves PR12088.
llvm-svn: 151551
2012-02-27 20:34:02 +00:00
Richard Smith 2fbb3d84a2 Tests for r151508.
llvm-svn: 151509
2012-02-26 23:49:01 +00:00
Richard Smith eae99680e3 PR11956: C++11's special exception for accessing non-static data members from
unevaluated operands applies within member functions, too.

llvm-svn: 151443
2012-02-25 10:04:07 +00:00
Douglas Gregor 2837aa2932 Teach overload resolution to prefer user-defined conversion via a
lambda closure type's function pointer conversion over user-defined
conversion via a lambda closure type's block pointer conversion,
always. This is a preference for more-standard code (since blocks
are an extension)  and a nod to efficiency, since function pointers
don't require any memory management. Fixes PR12063.

llvm-svn: 151170
2012-02-22 17:32:19 +00:00
Douglas Gregor ed90df3800 Generate an AST for the conversion from a lambda closure type to a
block pointer that returns a block literal which captures (by copy)
the lambda closure itself. Some aspects of the block literal are left
unspecified, namely the capture variable (which doesn't actually
exist) and the body (which will be filled in by IRgen because it can't
be written as an AST).

Because we're switching to this model, this patch also eliminates
tracking the copy-initialization expression for the block capture of
the conversion function, since that information is now embedded in the
synthesized block literal. -1 side tables FTW.

llvm-svn: 151131
2012-02-22 05:02:47 +00:00
Douglas Gregor b8389976b2 In the conflict between C++11 [expr.prim.general]p4, which declares
that 'this' can be used in the brace-or-equal-initializer of a
non-static data member, and C++11 [expr.prim.lambda]p9, which says
that lambda expressions not in block scope can have no captures, side
fully with C++11 [expr.prim.general]p4 by allowing 'this' to be
captured within these initializers. This seems to be the intent of
non-static data member initializers.

llvm-svn: 151101
2012-02-21 22:51:27 +00:00
Douglas Gregor 680e9e018d Improve our handling of lambda expressions that occur within default
arguments. There are two aspects to this:

  - Make sure that when marking the declarations referenced in a
  default argument, we don't try to mark local variables, both because
  it's a waste of time and because the semantics are wrong: we're not
  in a place where we could capture these variables again even if it
  did make sense.
  - When a lambda expression occurs in a default argument of a
  function template, make sure that the corresponding closure type is
  considered dependent, so that it will get properly instantiated. The
  second bit is a bit of a hack; to fix it properly, we may have to
  rearchitect our handling of default arguments, parsing them only
  after creating the function definition. However, I'd like to
  separate that work from the lambdas work.

llvm-svn: 151076
2012-02-21 19:11:17 +00:00
Douglas Gregor fdf598eaf3 Rewrite variable capture within lambda expressions and blocks,
eliminating a bunch of redundant code and properly modeling how the
captures of outside blocks/lambdas affect the types seen by inner
captures.

This new scheme makes two passes over the capturing scope stack. The
first pass goes up the stack (from innermost to outermost), assessing
whether the capture looks feasible and stopping when it either hits
the scope where the variable is declared or when it finds an existing
capture. The second pass then walks down the stack (from outermost to
innermost), capturing the variable at each step and updating the
captured type and the type that an expression referring to that
captured variable would see. It also checks type-specific
restrictions, such as the inability to capture an array within a
block. Note that only the first odr-use of each
variable needs to do the full walk; subsequent uses will find the
capture immediately, so multiple walks need not occur.

The same routine that builds the captures can also compute the type of
the captures without signaling errors and without actually performing
the capture. This functionality is used to determine the type of
declaration references as well as implementing the weird decltype((x))
rule within lambda expressions.

The capture code now explicitly takes sides in the debate over C++
core issue 1249, which concerns the type of captures within nested
lambdas. We opt to use the more permissive, more useful definition
implemented by GCC rather than the one implemented by EDG.

llvm-svn: 150875
2012-02-18 09:37:24 +00:00
Douglas Gregor 812d8f6387 Unify our computation of the type of a captured reference to a
variable; it was previously duplicated, and one of the copies failed
to account for outer non-mutable lambda captures.

llvm-svn: 150872
2012-02-18 05:51:20 +00:00
Douglas Gregor 7ae3c75d97 Only add 'const' to the type of variables captured in a lambda when
we're capturing it by value in a non-mutable lambda.

llvm-svn: 150791
2012-02-17 04:02:59 +00:00
Douglas Gregor a86bc00d3e Lambda closure types are always considered to be like "local" classes,
even if they are not within a function scope. Teach template
instantiation to treat them as such, and make sure that we have a
local instantiation scope when instantiating default arguments and
static data members.

llvm-svn: 150725
2012-02-16 21:36:18 +00:00
Douglas Gregor d3b672c385 Implicitly define a lambda's conversion functions (to function
pointers and block pointers). We use dummy definitions to keep the
invariant that an implicit, used definition has a body; IR generation
will substitute the actual contents, since they can't be represented
as C++. 

For the block pointer case, compute the copy-initialization needed to
capture the lambda object in the block, which IR generation will need
later.

llvm-svn: 150645
2012-02-16 01:06:16 +00:00
Douglas Gregor 33e863cfc8 Lambda closure types have a conversion function to a block pointer
with the same parameter types and return type as the function call
operator. This is the real answer to

  http://stackoverflow.com/questions/4148242/is-it-possible-to-convert-a-c0x-lambda-to-a-clang-block

:)

llvm-svn: 150620
2012-02-15 22:08:38 +00:00
Douglas Gregor 74f7d50f6a When overload resolution picks an implicitly-deleted special member
function, provide a specialized diagnostic that indicates the kind of
special member function (default constructor, copy assignment
operator, etc.) and that it was implicitly deleted. Add a hook where
we can provide more detailed information later.

llvm-svn: 150611
2012-02-15 19:33:52 +00:00
Douglas Gregor 02267a8e48 A little more lambda capture initialization diagnostics cleanup
llvm-svn: 150589
2012-02-15 17:05:32 +00:00
Douglas Gregor 19666fb1aa Introduce a new initialization entity for lambda captures, and
specialize location information and diagnostics for this entity.

llvm-svn: 150588
2012-02-15 16:57:26 +00:00
Douglas Gregor cf11eb76ed Specialize noreturn diagnostics for lambda expressions.
llvm-svn: 150586
2012-02-15 16:20:15 +00:00
Douglas Gregor b9e38f19f7 Specialize the diagnostic complaining about conflicting types of
return statements within a lambda; this diagnostic previously referred
to blocks.

llvm-svn: 150584
2012-02-15 15:57:22 +00:00
Richard Smith b228a86fcf Implement DR1454. This allows all intermediate results in constant expressions
to be core constant expressions (including pointers and references to
temporaries), and makes constexpr calculations Turing-complete. A Turing machine
simulator is included as a testcase.

This opens up the possibilty of removing CCValue entirely, and removing some
copies from the constant evaluator in the process, but that cleanup is not part
of this change.

llvm-svn: 150557
2012-02-15 02:18:13 +00:00
Douglas Gregor adb376ec33 Implement C++ core issue 974, which permits default arguments for
lambda expressions. Because these issue was pulled back from Ready
status at the Kona meeting, we still emit an ExtWarn when using
default arguments for lambda expressions.

llvm-svn: 150519
2012-02-14 22:28:59 +00:00
Douglas Gregor 621003e7b9 Check the return type of lambda expressions.
llvm-svn: 150503
2012-02-14 21:20:44 +00:00
Douglas Gregor 3e308b1fba Implement support for lambda capture pack expansions, e.g.,
[&values...] { print(values...); }

llvm-svn: 150497
2012-02-14 19:27:52 +00:00
Douglas Gregor 1b66d8c287 Simple test ensuring that we perform direct initialization when copy-capturing in lambdas
llvm-svn: 150442
2012-02-14 00:03:38 +00:00
Douglas Gregor b4328233c1 Link together the call operator produced from transforming a lambda
expression with the original call operator, so that we don't try to
separately instantiate the call operator. Test and tweak a few more
bits for template instantiation of lambda expressions.

llvm-svn: 150440
2012-02-14 00:00:48 +00:00
Douglas Gregor 0c46b2b7ce Introduce support for template instantiation of lambda
expressions. This is mostly a simple refact, splitting the main "start
a lambda expression" function into smaller chunks that are driven
either from the parser (Sema::ActOnLambdaExpr) or during AST
transformation (TreeTransform::TransformLambdaExpr). A few minor
interesting points:

  - Added new entry points for TreeTransform, so that we can
  explicitly establish the link between the lambda closure type in the
  template and the lambda closure type in the instantiation.
  - Added a bit into LambdaExpr specifying whether it had an explicit
  result type or not. We should have had this anyway.

This code is 'lightly' tested.

llvm-svn: 150417
2012-02-13 22:00:16 +00:00
Richard Smith 3607ffee5c Update constexpr implementation to match CWG's chosen approach for core issues
1358, 1360, 1452 and 1453.
 - Instantiations of constexpr functions are always constexpr. This removes the
   need for separate declaration/definition checking, which is now gone.
 - This makes it possible for a constexpr function to be virtual, if they are
   only dependently virtual. Virtual calls to such functions are not constant
   expressions.
 - Likewise, it's now possible for a literal type to have virtual base classes.
   A constexpr constructor for such a type cannot actually produce a constant
   expression, though, so add a special-case diagnostic for a constructor call
   to such a type rather than trying to evaluate it.
 - Classes with trivial default constructors (for which value initialization can
   produce a fully-initialized value) are considered literal types.
 - Classes with volatile members are not literal types.
 - constexpr constructors can be members of non-literal types. We do not yet use
   static initialization for global objects constructed in this way.

llvm-svn: 150359
2012-02-13 03:54:03 +00:00
Douglas Gregor 81495f341d Within the body of a lambda expression, decltype((x)) for an
id-expression 'x' will compute the type based on the assumption that
'x' will be captured, even if it isn't captured, per C++11
[expr.prim.lambda]p18. There are two related refactors that go into
implementing this:

  1) Split out the check that determines whether we should capture a
  particular variable reference, along with the computation of the
  type of the field, from the actual act of capturing the
  variable. 
  2) Always compute the result of decltype() within Sema, rather than
  AST, because the decltype() computation is now context-sensitive.

llvm-svn: 150347
2012-02-12 18:42:33 +00:00
Douglas Gregor 1a22d2889b Lambdas have a deleted default constructor and a deleted copy
assignment operator, per C++ [expr.prim.lambda]p19. Make it so.

llvm-svn: 150345
2012-02-12 17:34:23 +00:00
Eli Friedman c9751069f5 Make sure Sema creates a field for 'this' captures. (Doug, please double-check that this is correct.)
llvm-svn: 150292
2012-02-11 02:51:16 +00:00
Douglas Gregor 28be7c5e20 Add simple semantic test for C++11 [expr.prim.lambda]p16, which covers recursive capture. This is far more interesting for IRgen.
llvm-svn: 150283
2012-02-10 23:38:02 +00:00
Douglas Gregor f0d495100c Implement C++11 [expr.lambda.prim]p13, which prohibits lambdas in
default arguments if in fact those lambdas capture any entity.

llvm-svn: 150282
2012-02-10 23:30:22 +00:00
Douglas Gregor a1bffa26ca Allow implicit capture of 'this' in a lambda even when the capture
default is '=', and reword the warning about explicitly capturing
'this' in such lambdas to indicate that only explicit capture is
banned. 

Introduce Fix-Its for this and other "save the programmer from
themself" rules regarding what can be explicitly captured and what
must be implicitly captured.

llvm-svn: 150256
2012-02-10 17:46:20 +00:00
Douglas Gregor 01db64f0ac Add test from [expr.prim.lambda]p12, which deals with odr-use and
nested captures. We currently don't get odr-use correct in array
bounds, so that bit is commented out while we sort out what we need to
do.

llvm-svn: 150255
2012-02-10 16:48:36 +00:00
Douglas Gregor 04bbab586b Don't introduce a lambda's operator() into the class until after we
have finished parsing the body, so that name lookup will never find
anything within the closure type. Then, add this operator() and the
conversion function (if available) before completing the class.

llvm-svn: 150252
2012-02-10 16:13:20 +00:00
Douglas Gregor 136b2f24f0 Add a lambda example from the working draft.
llvm-svn: 150239
2012-02-10 09:37:05 +00:00
Douglas Gregor f02455e5ee Add various tests for captures and the reaching scope of the lambda
expression. Implement C++11 [expr.prim.lambda]p12's requirement that
capturing a variable will odr-use it.

llvm-svn: 150237
2012-02-10 09:26:04 +00:00
Douglas Gregor 12695101ec Implement the conversion to a function pointer for lambda expressions,
per C++ [expr.prim.lambda]p6.

llvm-svn: 150236
2012-02-10 08:36:38 +00:00
Richard Smith 0cd4ab194b Update to new resolution for DR1458. When taking the address of an object of
incomplete class type which has an overloaded operator&, it's now just
unspecified whether the overloaded operator or the builtin is used.

llvm-svn: 150234
2012-02-10 07:41:06 +00:00
Douglas Gregor 940a550f0d Don't allow deduction of a lambda result type from an initializer
list; it is not an expression.

llvm-svn: 150194
2012-02-09 18:40:39 +00:00
Douglas Gregor a30a0bc1db Tests for C++ [expr.prim.lambda]p5. We already implement all of these
semantics. 

llvm-svn: 150190
2012-02-09 18:19:44 +00:00
Douglas Gregor 7345626a5b Implement return type deduction for lambdas per C++11
[expr.prim.lambda]p4, including the current suggested resolution of
core isue 975, which allows multiple return statements so long as the
types match. ExtWarn when user code is actually making use of this
extension.

llvm-svn: 150168
2012-02-09 10:18:50 +00:00
Douglas Gregor 656bc62a73 Remove the "unsupported" error for lambda expressions. It's annoying,
and rapidly becoming untrue.

llvm-svn: 150165
2012-02-09 08:26:42 +00:00
Douglas Gregor 291af2b7e6 Add a test for the non-aggregaticity of lambda types per C++11
[expr.prim.lambda].

llvm-svn: 150164
2012-02-09 08:15:36 +00:00
Douglas Gregor 8962549164 Implement C++ [expr.prim.lambda]p2, which bans lambda expressions in
unevaluated operands. Be certain that we're marking everything
referenced within a capture initializer as odr-used.

llvm-svn: 150163
2012-02-09 08:14:43 +00:00
Douglas Gregor 199cec7673 Implement capture-by-copy for arrays in lambdas.
llvm-svn: 150138
2012-02-09 02:45:47 +00:00
Douglas Gregor 3d23f78852 When we create a non-static data member in the closure object for a
capture, make sure we actually add the field.

llvm-svn: 150135
2012-02-09 02:12:34 +00:00
Douglas Gregor 8c50e7c5e3 Various interrelated cleanups for lambdas:
- Complete the lambda class when we finish the lambda expression
    (previously, it was left in the "being completed" state)
  - Actually return the LambdaExpr object and bind to the resulting
  temporary when needed.
  - Detect when cleanups are needed while capturing a variable into a
  lambda (e.g., due to default arguments in the copy constructor), and
  make sure those cleanups apply for the whole of the lambda
  expression.
    

llvm-svn: 150123
2012-02-09 00:47:04 +00:00
Douglas Gregor 8390afde4e Minor comment fix
llvm-svn: 150090
2012-02-08 21:19:04 +00:00
Douglas Gregor c70fe353ea When computing the type of a local variable reference within a lambda,
only add 'const' for variables captured by copy in potentially
evaluated expressions of non-mutable lambdas. (The "by copy" part was
missing).

llvm-svn: 150088
2012-02-08 20:56:50 +00:00
Douglas Gregor 21f4692c62 When completing a lambda expression, make sure to check and attach the
body of the lambda to the function call operator.

llvm-svn: 150087
2012-02-08 20:17:14 +00:00
Richard Smith 90cacbbf3e Implement DR1458: Taking the address of an object of incomplete class type is
not a constant expression, because we can't tell whether the complete class type
will have an overloaded operator&.

llvm-svn: 150066
2012-02-08 08:11:33 +00:00
Richard Smith 3c7ad4e01b Add more testing for r149776.
llvm-svn: 150061
2012-02-08 06:41:34 +00:00
Richard Smith da7c4ba1af Implement the agreed resolution to DR1457: a signed left shift of a 1 bit into
the sign bit doesn't have undefined behavior, but a signed left shift of a 1 bit
out of the sign bit still does. As promised to Howard :)

The suppression of the potential constant expression checking in system headers
is also removed, since the problem it was working around is gone.

llvm-svn: 150059
2012-02-08 06:14:53 +00:00
Douglas Gregor e31e606ff3 Introduce basic ASTs for lambda expressions. This covers:
- Capturing variables by-reference and by-copy within a lambda
  - The representation of lambda captures
  - The creation of the non-static data members in the lambda class
  that store the captured variables
  - The initialization of the non-static data members from the
  captured variables
  - Pretty-printing lambda expressions

There are a number of FIXMEs, both explicit and implied, including:
  - Creating a field for a capture of 'this'
  - Improved diagnostics for initialization failures when capturing
  variables by copy
  - Dealing with temporaries created during said initialization
  - Template instantiation
  - AST (de-)serialization
  - Binding and returning the lambda expression; turning it into a
  proper temporary
  - Lots and lots of semantic constraints
  - Parameter pack captures

llvm-svn: 149977
2012-02-07 10:09:13 +00:00
Richard Smith c82fae6070 constexpr: Fix implementation of DR1311: check for volatile qualifiers in
lvalue-to-rvalue conversions on the source type of the conversion, not the
target type (which has them removed for non-class types).

llvm-svn: 149796
2012-02-05 01:23:16 +00:00
Richard Smith f4c51d9d76 In C++11 mode, when an integral constant expression is desired and we have a
value of class type, look for a unique conversion operator converting to
integral or unscoped enumeration type and use that. Implements [expr.const]p5.

Sema::VerifyIntegerConstantExpression now performs the conversion and returns
the converted result. Some important callers of Expr::isIntegralConstantExpr
have been switched over to using it (including all of those required for C++11
conformance); this switch brings a side-benefit of improved diagnostics and, in
several cases, simpler code. However, some language extensions and attributes
have not been moved across and will not perform implicit conversions on
constant expressions of literal class type where an ICE is required.

In passing, fix static_assert to perform a contextual conversion to bool on its
argument.

llvm-svn: 149776
2012-02-04 09:53:13 +00:00
Eli Friedman 24af850470 Implement implicit capture for lambda expressions.
Still left: explicit captures in lambdas need to cause implicit capture, and I need to take a look at the diagnostics for some cases.

llvm-svn: 149718
2012-02-03 22:47:37 +00:00
Richard Smith 84f6dcf2b5 constexpr:
* support the gcc __builtin_constant_p() ? ... : ... folding hack in C++11
  * check for unspecified values in pointer comparisons and pointer subtractions

llvm-svn: 149578
2012-02-02 01:16:57 +00:00
Douglas Gregor 6b8ef34f84 Diagnose the restriction on default arguments in C++11 [expr.prim.lambda]p5.
llvm-svn: 149517
2012-02-01 17:18:19 +00:00
Richard Smith 1b470417e4 constexpr: check for overflow in pointer subtraction.
This is a mess. According to the C++11 standard, pointer subtraction only has
undefined behavior if the difference of the array indices does not fit into a
ptrdiff_t.

However, common implementations effectively perform a char* subtraction first,
and then divide the result by the element size, which can cause overflows in
some cases. Those cases are not considered to be undefined behavior by this
change; perhaps they should be.

llvm-svn: 149490
2012-02-01 08:10:20 +00:00
Richard Smith c8042323e1 constexpr: overflow checking for integral and floating-point arithmetic.
llvm-svn: 149473
2012-02-01 05:53:12 +00:00
Richard Smith 47b349328b constexpr: require 'this' to point to an object in a constexpr method call.
llvm-svn: 149467
2012-02-01 02:39:43 +00:00
Douglas Gregor 53a9bdf17e Improve checking of explicit captures in a C++11 lambda expression:
- Actually building the var -> capture mapping properly (there was an off-by-one error)
  - Keeping track of the source location of each capture
  - Minor QoI improvements, e.g, highlighing the prior capture if
  there are multiple captures, pointing at the variable declaration we
  found if we reject it.

As part of this, add standard citations for the various semantic
checks we perform, and note where we're not performing those checks as
we should.

llvm-svn: 149462
2012-02-01 01:18:43 +00:00
Douglas Gregor 0e8ff39dc7 Diagnose attempts to explicitly capture a __block variable in a lambda.
llvm-svn: 149458
2012-02-01 00:09:55 +00:00
Richard Smith 000e9aa7ed constexpr: Treat INT_MIN % -1 as undefined behavior in C++11. Technically, it
isn't, but this is just a (reported) defect in the wording.

llvm-svn: 149448
2012-01-31 23:24:19 +00:00
Richard Smith de21b245c6 constexpr: the result of a relational operator between pointers to void is
unspecified unless the pointers are equal; therefore, such a comparison is not
a constant expression unless the pointers are equal.

llvm-svn: 149366
2012-01-31 06:41:30 +00:00
Richard Smith fe800031ec constexpr: catch a collection of integral undefined behaviors:
-INT_MIN and INT_MIN / -1
  Shift by a negative or too large quantity
  Left shift of negative value
  Overflow in left shift

llvm-svn: 149344
2012-01-31 04:08:20 +00:00
Richard Smith 006bfc91e8 constexpr: remove integral conversion overflow checking introduced in r149286.
As Eli points out, this is implementation-defined, and the way we define it
makes this fine.

llvm-svn: 149327
2012-01-31 01:47:46 +00:00
Richard Smith 911e142f03 constexpr: disallow signed integer overflow in integral conversions in constant
expressions in C++11.

llvm-svn: 149286
2012-01-30 22:27:01 +00:00
David Blaikie 25896afbe5 Support decltype as a simple-type-specifier.
This makes all sorts of fun examples work with decltype.
Reviewed by Richard Smith.

llvm-svn: 148787
2012-01-24 05:47:35 +00:00
Eli Friedman 456f01833b Remove PotentiallyPotentiallyEvaluated, and replace it with a much simpler and less error-prone way of handling the relevant cases. Towards marking of whether a declaration is used more accurately.
llvm-svn: 148522
2012-01-20 01:26:23 +00:00
Richard Smith f8379a0fc3 constexpr: converted constant expression handling for enumerator values, case
values and non-type template arguments of integral and enumeration types.

This change causes some legal C++98 code to no longer compile in C++11 mode, by
enforcing the C++11 rule that narrowing integral conversions are not permitted
in the final implicit conversion sequence for the above cases.

llvm-svn: 148439
2012-01-18 23:55:52 +00:00
Eli Friedman 7bda7f7533 Make PotentiallyPotentiallyEvaluated contexts work correctly when referencing a class field from outside an instance method.
llvm-svn: 148376
2012-01-18 03:53:45 +00:00
Richard Smith 2ec4061e39 Pedantic diagnostic correction: in C++, we have integral constant expressions,
not integer constant expressions. In passing, fix the 'folding is an extension'
diagnostic to not claim we're accepting the code, since that's not true in
-pedantic-errors mode, and add this diagnostic to -Wgnu.

llvm-svn: 148209
2012-01-15 03:51:30 +00:00
Richard Smith 02ab9c2940 Allow constant-folding of references which were formed in a manner not permitted
in a constant expression, for compatibility with g++.

llvm-svn: 148020
2012-01-12 06:08:57 +00:00
Richard Smith a8105bc9ce C++11 generalized constant expressions: implement checking and diagnostics for
pointer-arithmetic-related undefined behavior and unspecified results. We
continue to fold such values, but now notice they aren't constant expressions.

llvm-svn: 147659
2012-01-06 16:39:00 +00:00
Richard Smith fddd384b73 Unrevert r147271, reverted in r147361.
Also temporarily remove the assumption from IR gen that we can emit IR for every
constant we can fold, since it isn't currently true in C++11, to fix PR11676.

Original comment from r147271:

constexpr: perform zero-initialization prior to / instead of performing a
constructor call when appropriate. Thanks to Eli for spotting this.

llvm-svn: 147384
2011-12-30 21:15:51 +00:00
Rafael Espindola fafe4b7572 Revert r147271. This fixes PR11676.
llvm-svn: 147362
2011-12-30 03:11:50 +00:00
Richard Smith 6e52514c5d constexpr: support for evaluation and codegen of typeid constants.
llvm-svn: 147290
2011-12-27 12:18:28 +00:00
Richard Smith 771c4a1775 constexpr: perform zero-initialization prior to / instead of performing a
constructor call when appropriate. Thanks to Eli for spotting this.

llvm-svn: 147271
2011-12-25 20:00:17 +00:00
Richard Smith f2b681b4d8 constexpr: diagnostic improvements for invalid lvalue-to-rvalue conversions in
constant expressions.

llvm-svn: 147035
2011-12-21 05:04:46 +00:00
Richard Smith 764d2fe666 Unlike in C++03, a constant-expression is not an unevaluated operand in C++11.
Split out a new ExpressionEvaluationContext flag for this case, and don't treat
it as unevaluated in C++11. This fixes some crash-on-invalids where we would
allow references to class members in potentially-evaluated constant expressions
in static member functions, and also fixes half of PR10177.

The fix to PR10177 exposed a case where template instantiation failed to provide
a source location for a diagnostic, so TreeTransform has been tweaked to supply
source locations when transforming a type. The source location is still not very
good, but MarkDeclarationsReferencedInType would need to operate on a TypeLoc to
improve it further.

Also fix MarkDeclarationReferenced in C++98 mode to trigger instantiation for
static data members of class templates which are used in constant expressions.
This fixes a link-time problem, but we still incorrectly treat the member as
non-constant. The rest of the fix for that issue is blocked on PCH support for
early-instantiated static data members, which will be added in a subsequent
patch.

llvm-svn: 146955
2011-12-20 02:08:33 +00:00
Richard Smith d0b4dd656d constexpr handling improvements. Produce detailed diagnostics when a 'constexpr'
variable is initialized by a non-constant expression, and pass in the variable
being declared so that earlier-initialized fields' values can be used.

Rearrange VarDecl init evaluation to make this possible, and in so doing fix a
long-standing issue in our C++ constant expression handling, where we would
mishandle cases like:

  extern const int a;
  const int n = a;
  const int a = 5;
  int arr[n];

Here, n is not initialized by a constant expression, so can't be used in an ICE,
even though the initialization expression would be an ICE if it appeared later
in the TU. This requires computing whether the initializer is an ICE eagerly,
and saving that information in PCH files.

llvm-svn: 146856
2011-12-19 06:19:21 +00:00
Richard Smith 864711ee9f Reduce recursion limit on this test further to try to make the msys bot green.
llvm-svn: 146759
2011-12-16 21:59:02 +00:00
Richard Smith f6f003af6a C++11 constexpr: Add note stacks containing backtraces if constant evaluation
fails within a call to a constexpr function. Add -fconstexpr-backtrace-limit
argument to driver and frontend, to control the maximum number of notes so
produced (default 10). Fix APValue printing to be able to pretty-print all
APValue types, and move the testing for this functionality from a unittest to
a -verify test now that it's visible in clang's output.

llvm-svn: 146749
2011-12-16 19:06:07 +00:00
Richard Smith 9af58d4426 Halve the constexpr recursion depth in this test in an attempt to make the
freebsd bots happy. In the longer term, we should have a mechanism for moving
constexpr recursion off the call stack, to support the default limit of 512
suggested by the standard.

llvm-svn: 146596
2011-12-14 21:55:23 +00:00
Douglas Gregor 7234103225 Don't consider an overloaded operator& when the expression is actually
going to be a pointer-to-member constant. Fixes <rdar://problem/10544564>.

llvm-svn: 146587
2011-12-14 21:23:13 +00:00
David Blaikie 42b99e08e6 Disallow decltype in qualified declarator-ids.
llvm-svn: 146480
2011-12-13 08:03:36 +00:00
Richard Smith 357362d00b Add checks and diagnostics for many of the cases which C++11 considers to not
be constant expressions.

llvm-svn: 146479
2011-12-13 06:39:58 +00:00
David Blaikie 15a430a368 Support decltype in nested-name-specifiers.
llvm-svn: 145785
2011-12-04 05:04:18 +00:00
David Blaikie b06c68d320 Move tests to the appropriate directory to match the spec hierarchy.
llvm-svn: 143906
2011-11-07 03:27:58 +00:00
Douglas Gregor 7e1aa5b7ac Don't try to diagnose anything when we're passing incomplete types
through varargs. This only happens when we're in an unevaluated
context, where we don't want to trigger an error anyway. Fixes PR11131
/ <rdar://problem/10288375>.

llvm-svn: 141986
2011-10-14 20:34:19 +00:00
Richard Smith 9ca5c42582 Update all tests other than Driver/std.cpp to use -std=c++11 rather than
-std=c++0x. Patch by Ahmed Charles!

llvm-svn: 141900
2011-10-13 22:29:44 +00:00
Douglas Gregor cda2270217 Allow calling an overloaded function set by taking the address of the
functions, e.g., (&f)(0). Fixes <rdar://problem/9803316>.

llvm-svn: 141877
2011-10-13 18:10:35 +00:00
John McCall 50a2c2c19d Catch placeholder types in DefaultLvalueConversion
and DefaultFunctionArrayLvalueConversion.  To prevent
significant regression for should-this-be-a-call fixits,
and to repair some such regression from the introduction of
bound member placeholders, make those placeholder checks
try to build calls appropriately.  Harden the build-a-call
logic while we're at it.

llvm-svn: 141738
2011-10-11 23:14:30 +00:00
Richard Smith 938f40b5aa Implement support for C++11 in-class initialization of non-static data members.
llvm-svn: 132878
2011-06-11 17:19:42 +00:00
Douglas Gregor fcafc6e3de Implement the initial part of C++0x [expr.const]p2, which specifies
that the unevaluated subexpressions of &&, ||, and ? : are not
considered when determining whether the expression is a constant
expression. Also, turn the "used in its own initializer" warning into
a runtime-behavior warning, so that it doesn't fire when a variable is
used as part of an unevaluated subexpression of its own initializer.

Fixes PR9999.

llvm-svn: 131968
2011-05-24 16:02:01 +00:00
Douglas Gregor 253cadfe68 Implement C++0x semantics for passing non-POD classes through varargs.
llvm-svn: 131792
2011-05-21 16:27:21 +00:00
Eli Friedman 622e4fcac6 PR9899: handle pseudo-destructors correctly in noexcept() expressions.
llvm-svn: 131220
2011-05-12 02:11:32 +00:00
Eli Friedman c6587cc550 PR9882: Fix noexcept to deal with dependent new, delete, calls, and
dynamic_cast correctly.

llvm-svn: 131177
2011-05-11 05:22:44 +00:00
Douglas Gregor 89f3cd5c15 Clean up our handling of template-ids that resolve down to a single
overload, so that we actually do the resolution for full expressions
and emit more consistent, useful diagnostics. Also fixes an IRGen
crasher, where Sema wouldn't diagnose a resolvable bound member
function template-id used in a full-expression (<rdar://problem/9108698>).

llvm-svn: 127747
2011-03-16 19:16:25 +00:00
Sebastian Redl 52ddca23a7 Extend the noexcept expression test to test noexcept specification functions.
llvm-svn: 127693
2011-03-15 20:45:42 +00:00
Anders Carlsson 6774b1f1c1 Add -fcxx-exceptions to all tests that use C++ exceptions.
llvm-svn: 126599
2011-02-28 00:40:07 +00:00
Richard Smith 30482bc786 Implement the C++0x deduced 'auto' feature.
This fixes PR 8738, 9060 and 9132.

llvm-svn: 126069
2011-02-20 03:19:35 +00:00
Anders Carlsson 479d6f51e3 Pass -fexceptions to all tests that use try/catch/throw.
llvm-svn: 126037
2011-02-19 19:23:03 +00:00
Douglas Gregor b327eac117 Implement C++0x [expr.static.cast]p9, which permits explicitly casting
a scoped enumeration type to an integral or floating type,
properly. There was an over-eager assertion, and it was missing the
floating-point case.

Fixes PR9107/<rdar://problem/8937402>. 

llvm-svn: 125825
2011-02-18 03:01:41 +00:00
John McCall 351762cda2 A few more tweaks to the blocks AST representation:
- BlockDeclRefExprs always store VarDecls
  - BDREs no longer store copy expressions
  - BlockDecls now store a list of captured variables, information about
    how they're captured, and a copy expression if necessary
    
With that in hand, change IR generation to use the captures data in       
blocks instead of walking the block independently.        

Additionally, optimize block layout by emitting fields in descending
alignment order, with a heuristic for filling in words when alignment
of the end of the block header is insufficient for the most aligned
field.

llvm-svn: 125005
2011-02-07 10:33:21 +00:00
Douglas Gregor 125fa40c34 When calling a bound pointer to member function, check the
cv-qualifiers on the object against the cv-qualifiers on the member
function. Fixes PR8315.

llvm-svn: 124865
2011-02-04 12:57:49 +00:00