Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 1 | //===--- SemaLambda.cpp - Semantic Analysis for C++11 Lambdas -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ lambda expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "clang/Sema/DeclSpec.h" |
| 14 | #include "clang/Sema/Initialization.h" |
| 15 | #include "clang/Sema/Lookup.h" |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Scope.h" |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ScopeInfo.h" |
| 18 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
| 21 | using namespace clang; |
| 22 | using namespace sema; |
| 23 | |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame^] | 24 | CXXRecordDecl *Sema::createLambdaClosureType(SourceRange IntroducerRange, |
| 25 | bool KnownDependent) { |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 26 | DeclContext *DC = CurContext; |
| 27 | while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext())) |
| 28 | DC = DC->getParent(); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 29 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 30 | // Start constructing the lambda class. |
Douglas Gregor | da8962a | 2012-02-13 15:44:47 +0000 | [diff] [blame] | 31 | CXXRecordDecl *Class = CXXRecordDecl::CreateLambda(Context, DC, |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame^] | 32 | IntroducerRange.getBegin(), |
| 33 | KnownDependent); |
Douglas Gregor | fa07ab5 | 2012-02-20 20:47:06 +0000 | [diff] [blame] | 34 | DC->addDecl(Class); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 35 | |
| 36 | return Class; |
| 37 | } |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 38 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 39 | CXXMethodDecl *Sema::startLambdaDefinition(CXXRecordDecl *Class, |
| 40 | SourceRange IntroducerRange, |
| 41 | TypeSourceInfo *MethodType, |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 42 | SourceLocation EndLoc, |
| 43 | llvm::ArrayRef<ParmVarDecl *> Params) { |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 44 | // C++11 [expr.prim.lambda]p5: |
| 45 | // The closure type for a lambda-expression has a public inline function |
| 46 | // call operator (13.5.4) whose parameters and return type are described by |
| 47 | // the lambda-expression's parameter-declaration-clause and |
| 48 | // trailing-return-type respectively. |
| 49 | DeclarationName MethodName |
| 50 | = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
| 51 | DeclarationNameLoc MethodNameLoc; |
| 52 | MethodNameLoc.CXXOperatorName.BeginOpNameLoc |
| 53 | = IntroducerRange.getBegin().getRawEncoding(); |
| 54 | MethodNameLoc.CXXOperatorName.EndOpNameLoc |
| 55 | = IntroducerRange.getEnd().getRawEncoding(); |
| 56 | CXXMethodDecl *Method |
| 57 | = CXXMethodDecl::Create(Context, Class, EndLoc, |
| 58 | DeclarationNameInfo(MethodName, |
| 59 | IntroducerRange.getBegin(), |
| 60 | MethodNameLoc), |
| 61 | MethodType->getType(), MethodType, |
| 62 | /*isStatic=*/false, |
| 63 | SC_None, |
| 64 | /*isInline=*/true, |
| 65 | /*isConstExpr=*/false, |
| 66 | EndLoc); |
| 67 | Method->setAccess(AS_public); |
| 68 | |
| 69 | // Temporarily set the lexical declaration context to the current |
| 70 | // context, so that the Scope stack matches the lexical nesting. |
Douglas Gregor | fa07ab5 | 2012-02-20 20:47:06 +0000 | [diff] [blame] | 71 | Method->setLexicalDeclContext(CurContext); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 73 | // Add parameters. |
| 74 | if (!Params.empty()) { |
| 75 | Method->setParams(Params); |
| 76 | CheckParmsForFunctionDef(const_cast<ParmVarDecl **>(Params.begin()), |
| 77 | const_cast<ParmVarDecl **>(Params.end()), |
| 78 | /*CheckParameterNames=*/false); |
| 79 | |
| 80 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 81 | PEnd = Method->param_end(); |
| 82 | P != PEnd; ++P) |
| 83 | (*P)->setOwningFunction(Method); |
| 84 | } |
| 85 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 86 | return Method; |
| 87 | } |
| 88 | |
| 89 | LambdaScopeInfo *Sema::enterLambdaScope(CXXMethodDecl *CallOperator, |
| 90 | SourceRange IntroducerRange, |
| 91 | LambdaCaptureDefault CaptureDefault, |
| 92 | bool ExplicitParams, |
| 93 | bool ExplicitResultType, |
| 94 | bool Mutable) { |
| 95 | PushLambdaScope(CallOperator->getParent(), CallOperator); |
| 96 | LambdaScopeInfo *LSI = getCurLambda(); |
| 97 | if (CaptureDefault == LCD_ByCopy) |
| 98 | LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval; |
| 99 | else if (CaptureDefault == LCD_ByRef) |
| 100 | LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref; |
| 101 | LSI->IntroducerRange = IntroducerRange; |
| 102 | LSI->ExplicitParams = ExplicitParams; |
| 103 | LSI->Mutable = Mutable; |
| 104 | |
| 105 | if (ExplicitResultType) { |
| 106 | LSI->ReturnType = CallOperator->getResultType(); |
Douglas Gregor | 53393f2 | 2012-02-14 21:20:44 +0000 | [diff] [blame] | 107 | |
| 108 | if (!LSI->ReturnType->isDependentType() && |
| 109 | !LSI->ReturnType->isVoidType()) { |
| 110 | if (RequireCompleteType(CallOperator->getLocStart(), LSI->ReturnType, |
| 111 | diag::err_lambda_incomplete_result)) { |
| 112 | // Do nothing. |
| 113 | } else if (LSI->ReturnType->isObjCObjectOrInterfaceType()) { |
| 114 | Diag(CallOperator->getLocStart(), diag::err_lambda_objc_object_result) |
| 115 | << LSI->ReturnType; |
| 116 | } |
| 117 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 118 | } else { |
| 119 | LSI->HasImplicitReturnType = true; |
| 120 | } |
| 121 | |
| 122 | return LSI; |
| 123 | } |
| 124 | |
| 125 | void Sema::finishLambdaExplicitCaptures(LambdaScopeInfo *LSI) { |
| 126 | LSI->finishedExplicitCaptures(); |
| 127 | } |
| 128 | |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 129 | void Sema::addLambdaParameters(CXXMethodDecl *CallOperator, Scope *CurScope) { |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 130 | // Introduce our parameters into the function scope |
| 131 | for (unsigned p = 0, NumParams = CallOperator->getNumParams(); |
| 132 | p < NumParams; ++p) { |
| 133 | ParmVarDecl *Param = CallOperator->getParamDecl(p); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 134 | |
| 135 | // If this has an identifier, add it to the scope stack. |
| 136 | if (CurScope && Param->getIdentifier()) { |
| 137 | CheckShadow(CurScope, Param); |
| 138 | |
| 139 | PushOnScopeChains(Param, CurScope); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, |
| 145 | Declarator &ParamInfo, |
| 146 | Scope *CurScope) { |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame^] | 147 | // Determine if we're within a context where we know that the lambda will |
| 148 | // be dependent, because there are template parameters in scope. |
| 149 | bool KnownDependent = false; |
| 150 | if (Scope *TmplScope = CurScope->getTemplateParamParent()) |
| 151 | if (!TmplScope->decl_empty()) |
| 152 | KnownDependent = true; |
| 153 | |
| 154 | CXXRecordDecl *Class = createLambdaClosureType(Intro.Range, KnownDependent); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 155 | |
| 156 | // Determine the signature of the call operator. |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 157 | TypeSourceInfo *MethodTyInfo; |
| 158 | bool ExplicitParams = true; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 159 | bool ExplicitResultType = true; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 160 | SourceLocation EndLoc; |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 161 | llvm::ArrayRef<ParmVarDecl *> Params; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 162 | if (ParamInfo.getNumTypeObjects() == 0) { |
| 163 | // C++11 [expr.prim.lambda]p4: |
| 164 | // If a lambda-expression does not include a lambda-declarator, it is as |
| 165 | // if the lambda-declarator were (). |
| 166 | FunctionProtoType::ExtProtoInfo EPI; |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 167 | EPI.HasTrailingReturn = true; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 168 | EPI.TypeQuals |= DeclSpec::TQ_const; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 169 | QualType MethodTy = Context.getFunctionType(Context.DependentTy, |
| 170 | /*Args=*/0, /*NumArgs=*/0, EPI); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 171 | MethodTyInfo = Context.getTrivialTypeSourceInfo(MethodTy); |
| 172 | ExplicitParams = false; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 173 | ExplicitResultType = false; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 174 | EndLoc = Intro.Range.getEnd(); |
| 175 | } else { |
| 176 | assert(ParamInfo.isFunctionDeclarator() && |
| 177 | "lambda-declarator is a function"); |
| 178 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getFunctionTypeInfo(); |
| 179 | |
| 180 | // C++11 [expr.prim.lambda]p5: |
| 181 | // This function call operator is declared const (9.3.1) if and only if |
| 182 | // the lambda-expression's parameter-declaration-clause is not followed |
| 183 | // by mutable. It is neither virtual nor declared volatile. [...] |
| 184 | if (!FTI.hasMutableQualifier()) |
| 185 | FTI.TypeQuals |= DeclSpec::TQ_const; |
| 186 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 187 | MethodTyInfo = GetTypeForDeclarator(ParamInfo, CurScope); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 188 | assert(MethodTyInfo && "no type from lambda-declarator"); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 189 | EndLoc = ParamInfo.getSourceRange().getEnd(); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 190 | |
| 191 | ExplicitResultType |
| 192 | = MethodTyInfo->getType()->getAs<FunctionType>()->getResultType() |
| 193 | != Context.DependentTy; |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 194 | |
| 195 | TypeLoc TL = MethodTyInfo->getTypeLoc(); |
| 196 | FunctionProtoTypeLoc Proto = cast<FunctionProtoTypeLoc>(TL); |
| 197 | Params = llvm::ArrayRef<ParmVarDecl *>(Proto.getParmArray(), |
| 198 | Proto.getNumArgs()); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 201 | CXXMethodDecl *Method = startLambdaDefinition(Class, Intro.Range, |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 202 | MethodTyInfo, EndLoc, Params); |
| 203 | |
| 204 | if (ExplicitParams) |
| 205 | CheckCXXDefaultArguments(Method); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 206 | |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 207 | // Attributes on the lambda apply to the method. |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 208 | ProcessDeclAttributes(CurScope, Method, ParamInfo); |
| 209 | |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 210 | // Introduce the function call operator as the current declaration context. |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 211 | PushDeclContext(CurScope, Method); |
| 212 | |
| 213 | // Introduce the lambda scope. |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 214 | LambdaScopeInfo *LSI |
| 215 | = enterLambdaScope(Method, Intro.Range, Intro.Default, ExplicitParams, |
| 216 | ExplicitResultType, |
| 217 | (Method->getTypeQualifiers() & Qualifiers::Const) == 0); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 218 | |
| 219 | // Handle explicit captures. |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 220 | SourceLocation PrevCaptureLoc |
| 221 | = Intro.Default == LCD_None? Intro.Range.getBegin() : Intro.DefaultLoc; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 222 | for (llvm::SmallVector<LambdaCapture, 4>::const_iterator |
| 223 | C = Intro.Captures.begin(), |
| 224 | E = Intro.Captures.end(); |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 225 | C != E; |
| 226 | PrevCaptureLoc = C->Loc, ++C) { |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 227 | if (C->Kind == LCK_This) { |
| 228 | // C++11 [expr.prim.lambda]p8: |
| 229 | // An identifier or this shall not appear more than once in a |
| 230 | // lambda-capture. |
| 231 | if (LSI->isCXXThisCaptured()) { |
| 232 | Diag(C->Loc, diag::err_capture_more_than_once) |
| 233 | << "'this'" |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 234 | << SourceRange(LSI->getCXXThisCapture().getLocation()) |
| 235 | << FixItHint::CreateRemoval( |
| 236 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 237 | continue; |
| 238 | } |
| 239 | |
| 240 | // C++11 [expr.prim.lambda]p8: |
| 241 | // If a lambda-capture includes a capture-default that is =, the |
| 242 | // lambda-capture shall not contain this [...]. |
| 243 | if (Intro.Default == LCD_ByCopy) { |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 244 | Diag(C->Loc, diag::err_this_capture_with_copy_default) |
| 245 | << FixItHint::CreateRemoval( |
| 246 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 247 | continue; |
| 248 | } |
| 249 | |
| 250 | // C++11 [expr.prim.lambda]p12: |
| 251 | // If this is captured by a local lambda expression, its nearest |
| 252 | // enclosing function shall be a non-static member function. |
| 253 | QualType ThisCaptureType = getCurrentThisType(); |
| 254 | if (ThisCaptureType.isNull()) { |
| 255 | Diag(C->Loc, diag::err_this_capture) << true; |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | CheckCXXThisCapture(C->Loc, /*Explicit=*/true); |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | assert(C->Id && "missing identifier for capture"); |
| 264 | |
| 265 | // C++11 [expr.prim.lambda]p8: |
| 266 | // If a lambda-capture includes a capture-default that is &, the |
| 267 | // identifiers in the lambda-capture shall not be preceded by &. |
| 268 | // If a lambda-capture includes a capture-default that is =, [...] |
| 269 | // each identifier it contains shall be preceded by &. |
| 270 | if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef) { |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 271 | Diag(C->Loc, diag::err_reference_capture_with_reference_default) |
| 272 | << FixItHint::CreateRemoval( |
| 273 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 274 | continue; |
| 275 | } else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy) { |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 276 | Diag(C->Loc, diag::err_copy_capture_with_copy_default) |
| 277 | << FixItHint::CreateRemoval( |
| 278 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 279 | continue; |
| 280 | } |
| 281 | |
| 282 | DeclarationNameInfo Name(C->Id, C->Loc); |
| 283 | LookupResult R(*this, Name, LookupOrdinaryName); |
| 284 | LookupName(R, CurScope); |
| 285 | if (R.isAmbiguous()) |
| 286 | continue; |
| 287 | if (R.empty()) { |
| 288 | // FIXME: Disable corrections that would add qualification? |
| 289 | CXXScopeSpec ScopeSpec; |
| 290 | DeclFilterCCC<VarDecl> Validator; |
| 291 | if (DiagnoseEmptyLookup(CurScope, ScopeSpec, R, Validator)) |
| 292 | continue; |
| 293 | } |
| 294 | |
| 295 | // C++11 [expr.prim.lambda]p10: |
| 296 | // The identifiers in a capture-list are looked up using the usual rules |
| 297 | // for unqualified name lookup (3.4.1); each such lookup shall find a |
| 298 | // variable with automatic storage duration declared in the reaching |
| 299 | // scope of the local lambda expression. |
Douglas Gregor | 53393f2 | 2012-02-14 21:20:44 +0000 | [diff] [blame] | 300 | // |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 301 | // Note that the 'reaching scope' check happens in tryCaptureVariable(). |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 302 | VarDecl *Var = R.getAsSingle<VarDecl>(); |
| 303 | if (!Var) { |
| 304 | Diag(C->Loc, diag::err_capture_does_not_name_variable) << C->Id; |
| 305 | continue; |
| 306 | } |
| 307 | |
| 308 | if (!Var->hasLocalStorage()) { |
| 309 | Diag(C->Loc, diag::err_capture_non_automatic_variable) << C->Id; |
| 310 | Diag(Var->getLocation(), diag::note_previous_decl) << C->Id; |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | // C++11 [expr.prim.lambda]p8: |
| 315 | // An identifier or this shall not appear more than once in a |
| 316 | // lambda-capture. |
| 317 | if (LSI->isCaptured(Var)) { |
| 318 | Diag(C->Loc, diag::err_capture_more_than_once) |
| 319 | << C->Id |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 320 | << SourceRange(LSI->getCapture(Var).getLocation()) |
| 321 | << FixItHint::CreateRemoval( |
| 322 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 323 | continue; |
| 324 | } |
| 325 | |
Douglas Gregor | a736524 | 2012-02-14 19:27:52 +0000 | [diff] [blame] | 326 | // C++11 [expr.prim.lambda]p23: |
| 327 | // A capture followed by an ellipsis is a pack expansion (14.5.3). |
| 328 | SourceLocation EllipsisLoc; |
| 329 | if (C->EllipsisLoc.isValid()) { |
| 330 | if (Var->isParameterPack()) { |
| 331 | EllipsisLoc = C->EllipsisLoc; |
| 332 | } else { |
| 333 | Diag(C->EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 334 | << SourceRange(C->Loc); |
| 335 | |
| 336 | // Just ignore the ellipsis. |
| 337 | } |
| 338 | } else if (Var->isParameterPack()) { |
| 339 | Diag(C->Loc, diag::err_lambda_unexpanded_pack); |
| 340 | continue; |
| 341 | } |
| 342 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 343 | TryCaptureKind Kind = C->Kind == LCK_ByRef ? TryCapture_ExplicitByRef : |
| 344 | TryCapture_ExplicitByVal; |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 345 | tryCaptureVariable(Var, C->Loc, Kind, EllipsisLoc); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 346 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 347 | finishLambdaExplicitCaptures(LSI); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 348 | |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 349 | // Add lambda parameters into scope. |
| 350 | addLambdaParameters(Method, CurScope); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 351 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 352 | // Enter a new evaluation context to insulate the lambda from any |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 353 | // cleanups from the enclosing full-expression. |
| 354 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 357 | void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope, |
| 358 | bool IsInstantiation) { |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 359 | // Leave the expression-evaluation context. |
| 360 | DiscardCleanupsInEvaluationContext(); |
| 361 | PopExpressionEvaluationContext(); |
| 362 | |
| 363 | // Leave the context of the lambda. |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 364 | if (!IsInstantiation) |
| 365 | PopDeclContext(); |
Douglas Gregor | 630d5ff | 2012-02-09 01:28:42 +0000 | [diff] [blame] | 366 | |
| 367 | // Finalize the lambda. |
| 368 | LambdaScopeInfo *LSI = getCurLambda(); |
| 369 | CXXRecordDecl *Class = LSI->Lambda; |
| 370 | Class->setInvalidDecl(); |
| 371 | SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end()); |
| 372 | ActOnFields(0, Class->getLocation(), Class, Fields, |
| 373 | SourceLocation(), SourceLocation(), 0); |
| 374 | CheckCompletedCXXClass(Class); |
| 375 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 376 | PopFunctionScopeInfo(); |
| 377 | } |
| 378 | |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 379 | /// \brief Add a lambda's conversion to function pointer, as described in |
| 380 | /// C++11 [expr.prim.lambda]p6. |
| 381 | static void addFunctionPointerConversion(Sema &S, |
| 382 | SourceRange IntroducerRange, |
| 383 | CXXRecordDecl *Class, |
| 384 | CXXMethodDecl *CallOperator) { |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 385 | // Add the conversion to function pointer. |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 386 | const FunctionProtoType *Proto |
| 387 | = CallOperator->getType()->getAs<FunctionProtoType>(); |
| 388 | QualType FunctionPtrTy; |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 389 | QualType FunctionTy; |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 390 | { |
| 391 | FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); |
| 392 | ExtInfo.TypeQuals = 0; |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 393 | FunctionTy = S.Context.getFunctionType(Proto->getResultType(), |
| 394 | Proto->arg_type_begin(), |
| 395 | Proto->getNumArgs(), |
| 396 | ExtInfo); |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 397 | FunctionPtrTy = S.Context.getPointerType(FunctionTy); |
| 398 | } |
| 399 | |
| 400 | FunctionProtoType::ExtProtoInfo ExtInfo; |
| 401 | ExtInfo.TypeQuals = Qualifiers::Const; |
| 402 | QualType ConvTy = S.Context.getFunctionType(FunctionPtrTy, 0, 0, ExtInfo); |
| 403 | |
| 404 | SourceLocation Loc = IntroducerRange.getBegin(); |
| 405 | DeclarationName Name |
| 406 | = S.Context.DeclarationNames.getCXXConversionFunctionName( |
| 407 | S.Context.getCanonicalType(FunctionPtrTy)); |
| 408 | DeclarationNameLoc NameLoc; |
| 409 | NameLoc.NamedType.TInfo = S.Context.getTrivialTypeSourceInfo(FunctionPtrTy, |
| 410 | Loc); |
| 411 | CXXConversionDecl *Conversion |
| 412 | = CXXConversionDecl::Create(S.Context, Class, Loc, |
| 413 | DeclarationNameInfo(Name, Loc, NameLoc), |
| 414 | ConvTy, |
| 415 | S.Context.getTrivialTypeSourceInfo(ConvTy, |
| 416 | Loc), |
| 417 | /*isInline=*/false, /*isExplicit=*/false, |
| 418 | /*isConstexpr=*/false, |
| 419 | CallOperator->getBody()->getLocEnd()); |
| 420 | Conversion->setAccess(AS_public); |
| 421 | Conversion->setImplicit(true); |
| 422 | Class->addDecl(Conversion); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 423 | |
| 424 | // Add a non-static member function "__invoke" that will be the result of |
| 425 | // the conversion. |
| 426 | Name = &S.Context.Idents.get("__invoke"); |
| 427 | CXXMethodDecl *Invoke |
| 428 | = CXXMethodDecl::Create(S.Context, Class, Loc, |
| 429 | DeclarationNameInfo(Name, Loc), FunctionTy, |
| 430 | CallOperator->getTypeSourceInfo(), |
| 431 | /*IsStatic=*/true, SC_Static, /*IsInline=*/true, |
| 432 | /*IsConstexpr=*/false, |
| 433 | CallOperator->getBody()->getLocEnd()); |
| 434 | SmallVector<ParmVarDecl *, 4> InvokeParams; |
| 435 | for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) { |
| 436 | ParmVarDecl *From = CallOperator->getParamDecl(I); |
| 437 | InvokeParams.push_back(ParmVarDecl::Create(S.Context, Invoke, |
| 438 | From->getLocStart(), |
| 439 | From->getLocation(), |
| 440 | From->getIdentifier(), |
| 441 | From->getType(), |
| 442 | From->getTypeSourceInfo(), |
| 443 | From->getStorageClass(), |
| 444 | From->getStorageClassAsWritten(), |
| 445 | /*DefaultArg=*/0)); |
| 446 | } |
| 447 | Invoke->setParams(InvokeParams); |
| 448 | Invoke->setAccess(AS_private); |
| 449 | Invoke->setImplicit(true); |
| 450 | Class->addDecl(Invoke); |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Douglas Gregor | c2956e5 | 2012-02-15 22:08:38 +0000 | [diff] [blame] | 453 | /// \brief Add a lambda's conversion to block pointer. |
| 454 | static void addBlockPointerConversion(Sema &S, |
| 455 | SourceRange IntroducerRange, |
| 456 | CXXRecordDecl *Class, |
| 457 | CXXMethodDecl *CallOperator) { |
| 458 | const FunctionProtoType *Proto |
| 459 | = CallOperator->getType()->getAs<FunctionProtoType>(); |
| 460 | QualType BlockPtrTy; |
| 461 | { |
| 462 | FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); |
| 463 | ExtInfo.TypeQuals = 0; |
| 464 | QualType FunctionTy |
| 465 | = S.Context.getFunctionType(Proto->getResultType(), |
| 466 | Proto->arg_type_begin(), |
| 467 | Proto->getNumArgs(), |
| 468 | ExtInfo); |
| 469 | BlockPtrTy = S.Context.getBlockPointerType(FunctionTy); |
| 470 | } |
| 471 | |
| 472 | FunctionProtoType::ExtProtoInfo ExtInfo; |
| 473 | ExtInfo.TypeQuals = Qualifiers::Const; |
| 474 | QualType ConvTy = S.Context.getFunctionType(BlockPtrTy, 0, 0, ExtInfo); |
| 475 | |
| 476 | SourceLocation Loc = IntroducerRange.getBegin(); |
| 477 | DeclarationName Name |
| 478 | = S.Context.DeclarationNames.getCXXConversionFunctionName( |
| 479 | S.Context.getCanonicalType(BlockPtrTy)); |
| 480 | DeclarationNameLoc NameLoc; |
| 481 | NameLoc.NamedType.TInfo = S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc); |
| 482 | CXXConversionDecl *Conversion |
| 483 | = CXXConversionDecl::Create(S.Context, Class, Loc, |
| 484 | DeclarationNameInfo(Name, Loc, NameLoc), |
| 485 | ConvTy, |
| 486 | S.Context.getTrivialTypeSourceInfo(ConvTy, Loc), |
| 487 | /*isInline=*/false, /*isExplicit=*/false, |
| 488 | /*isConstexpr=*/false, |
| 489 | CallOperator->getBody()->getLocEnd()); |
| 490 | Conversion->setAccess(AS_public); |
| 491 | Conversion->setImplicit(true); |
| 492 | Class->addDecl(Conversion); |
| 493 | } |
| 494 | |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 495 | /// \brief Determine whether the given context is or is enclosed in an inline |
| 496 | /// function. |
| 497 | static bool isInInlineFunction(const DeclContext *DC) { |
| 498 | while (!DC->isFileContext()) { |
| 499 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 500 | if (FD->isInlined()) |
| 501 | return true; |
| 502 | |
| 503 | DC = DC->getLexicalParent(); |
| 504 | } |
| 505 | |
| 506 | return false; |
| 507 | } |
| 508 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 509 | ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body, |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 510 | Scope *CurScope, |
| 511 | llvm::Optional<unsigned> ManglingNumber, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 512 | Decl *ContextDecl, |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 513 | bool IsInstantiation) { |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 514 | // Leave the expression-evaluation context. |
| 515 | DiscardCleanupsInEvaluationContext(); |
| 516 | PopExpressionEvaluationContext(); |
| 517 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 518 | // Collect information from the lambda scope. |
| 519 | llvm::SmallVector<LambdaExpr::Capture, 4> Captures; |
| 520 | llvm::SmallVector<Expr *, 4> CaptureInits; |
| 521 | LambdaCaptureDefault CaptureDefault; |
| 522 | CXXRecordDecl *Class; |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 523 | CXXMethodDecl *CallOperator; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 524 | SourceRange IntroducerRange; |
| 525 | bool ExplicitParams; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 526 | bool ExplicitResultType; |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 527 | bool LambdaExprNeedsCleanups; |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 528 | llvm::SmallVector<VarDecl *, 4> ArrayIndexVars; |
| 529 | llvm::SmallVector<unsigned, 4> ArrayIndexStarts; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 530 | { |
| 531 | LambdaScopeInfo *LSI = getCurLambda(); |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 532 | CallOperator = LSI->CallOperator; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 533 | Class = LSI->Lambda; |
| 534 | IntroducerRange = LSI->IntroducerRange; |
| 535 | ExplicitParams = LSI->ExplicitParams; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 536 | ExplicitResultType = !LSI->HasImplicitReturnType; |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 537 | LambdaExprNeedsCleanups = LSI->ExprNeedsCleanups; |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 538 | ArrayIndexVars.swap(LSI->ArrayIndexVars); |
| 539 | ArrayIndexStarts.swap(LSI->ArrayIndexStarts); |
| 540 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 541 | // Translate captures. |
| 542 | for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) { |
| 543 | LambdaScopeInfo::Capture From = LSI->Captures[I]; |
| 544 | assert(!From.isBlockCapture() && "Cannot capture __block variables"); |
| 545 | bool IsImplicit = I >= LSI->NumExplicitCaptures; |
| 546 | |
| 547 | // Handle 'this' capture. |
| 548 | if (From.isThisCapture()) { |
| 549 | Captures.push_back(LambdaExpr::Capture(From.getLocation(), |
| 550 | IsImplicit, |
| 551 | LCK_This)); |
| 552 | CaptureInits.push_back(new (Context) CXXThisExpr(From.getLocation(), |
| 553 | getCurrentThisType(), |
| 554 | /*isImplicit=*/true)); |
| 555 | continue; |
| 556 | } |
| 557 | |
| 558 | VarDecl *Var = From.getVariable(); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 559 | LambdaCaptureKind Kind = From.isCopyCapture()? LCK_ByCopy : LCK_ByRef; |
| 560 | Captures.push_back(LambdaExpr::Capture(From.getLocation(), IsImplicit, |
Douglas Gregor | a736524 | 2012-02-14 19:27:52 +0000 | [diff] [blame] | 561 | Kind, Var, From.getEllipsisLoc())); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 562 | CaptureInits.push_back(From.getCopyExpr()); |
| 563 | } |
| 564 | |
| 565 | switch (LSI->ImpCaptureStyle) { |
| 566 | case CapturingScopeInfo::ImpCap_None: |
| 567 | CaptureDefault = LCD_None; |
| 568 | break; |
| 569 | |
| 570 | case CapturingScopeInfo::ImpCap_LambdaByval: |
| 571 | CaptureDefault = LCD_ByCopy; |
| 572 | break; |
| 573 | |
| 574 | case CapturingScopeInfo::ImpCap_LambdaByref: |
| 575 | CaptureDefault = LCD_ByRef; |
| 576 | break; |
| 577 | |
| 578 | case CapturingScopeInfo::ImpCap_Block: |
| 579 | llvm_unreachable("block capture in lambda"); |
| 580 | break; |
| 581 | } |
| 582 | |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 583 | // C++11 [expr.prim.lambda]p4: |
| 584 | // If a lambda-expression does not include a |
| 585 | // trailing-return-type, it is as if the trailing-return-type |
| 586 | // denotes the following type: |
| 587 | // FIXME: Assumes current resolution to core issue 975. |
| 588 | if (LSI->HasImplicitReturnType) { |
| 589 | // - if there are no return statements in the |
| 590 | // compound-statement, or all return statements return |
| 591 | // either an expression of type void or no expression or |
| 592 | // braced-init-list, the type void; |
| 593 | if (LSI->ReturnType.isNull()) { |
| 594 | LSI->ReturnType = Context.VoidTy; |
| 595 | } else { |
| 596 | // C++11 [expr.prim.lambda]p4: |
| 597 | // - if the compound-statement is of the form |
| 598 | // |
| 599 | // { attribute-specifier-seq[opt] return expression ; } |
| 600 | // |
| 601 | // the type of the returned expression after |
| 602 | // lvalue-to-rvalue conversion (4.1), array-to-pointer |
| 603 | // conver- sion (4.2), and function-to-pointer conversion |
| 604 | // (4.3); |
| 605 | // |
| 606 | // Since we're accepting the resolution to a post-C++11 core |
| 607 | // issue with a non-trivial extension, provide a warning (by |
| 608 | // default). |
| 609 | CompoundStmt *CompoundBody = cast<CompoundStmt>(Body); |
| 610 | if (!(CompoundBody->size() == 1 && |
| 611 | isa<ReturnStmt>(*CompoundBody->body_begin())) && |
| 612 | !Context.hasSameType(LSI->ReturnType, Context.VoidTy)) |
| 613 | Diag(IntroducerRange.getBegin(), |
| 614 | diag::ext_lambda_implies_void_return); |
| 615 | } |
| 616 | |
| 617 | // Create a function type with the inferred return type. |
| 618 | const FunctionProtoType *Proto |
| 619 | = CallOperator->getType()->getAs<FunctionProtoType>(); |
| 620 | QualType FunctionTy |
| 621 | = Context.getFunctionType(LSI->ReturnType, |
| 622 | Proto->arg_type_begin(), |
| 623 | Proto->getNumArgs(), |
| 624 | Proto->getExtProtoInfo()); |
| 625 | CallOperator->setType(FunctionTy); |
| 626 | } |
| 627 | |
Douglas Gregor | 215e4e1 | 2012-02-12 17:34:23 +0000 | [diff] [blame] | 628 | // C++ [expr.prim.lambda]p7: |
| 629 | // The lambda-expression's compound-statement yields the |
| 630 | // function-body (8.4) of the function call operator [...]. |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 631 | ActOnFinishFunctionBody(CallOperator, Body, IsInstantiation); |
Douglas Gregor | 215e4e1 | 2012-02-12 17:34:23 +0000 | [diff] [blame] | 632 | CallOperator->setLexicalDeclContext(Class); |
| 633 | Class->addDecl(CallOperator); |
| 634 | |
Douglas Gregor | b555971 | 2012-02-10 16:13:20 +0000 | [diff] [blame] | 635 | // C++11 [expr.prim.lambda]p6: |
| 636 | // The closure type for a lambda-expression with no lambda-capture |
| 637 | // has a public non-virtual non-explicit const conversion function |
| 638 | // to pointer to function having the same parameter and return |
| 639 | // types as the closure type's function call operator. |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 640 | if (Captures.empty() && CaptureDefault == LCD_None) |
| 641 | addFunctionPointerConversion(*this, IntroducerRange, Class, |
| 642 | CallOperator); |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 643 | |
Douglas Gregor | c2956e5 | 2012-02-15 22:08:38 +0000 | [diff] [blame] | 644 | // Objective-C++: |
| 645 | // The closure type for a lambda-expression has a public non-virtual |
| 646 | // non-explicit const conversion function to a block pointer having the |
| 647 | // same parameter and return types as the closure type's function call |
| 648 | // operator. |
| 649 | if (getLangOptions().Blocks) |
| 650 | addBlockPointerConversion(*this, IntroducerRange, Class, CallOperator); |
| 651 | |
Douglas Gregor | b555971 | 2012-02-10 16:13:20 +0000 | [diff] [blame] | 652 | // Finalize the lambda class. |
| 653 | SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end()); |
Douglas Gregor | b555971 | 2012-02-10 16:13:20 +0000 | [diff] [blame] | 654 | ActOnFields(0, Class->getLocation(), Class, Fields, |
| 655 | SourceLocation(), SourceLocation(), 0); |
| 656 | CheckCompletedCXXClass(Class); |
| 657 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 660 | if (LambdaExprNeedsCleanups) |
| 661 | ExprNeedsCleanups = true; |
| 662 | |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 663 | // If we don't already have a mangling number for this lambda expression, |
| 664 | // allocate one now. |
| 665 | if (!ManglingNumber) { |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 666 | ContextDecl = ExprEvalContexts.back().LambdaContextDecl; |
| 667 | |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 668 | enum ContextKind { |
| 669 | Normal, |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 670 | DefaultArgument, |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 671 | DataMember, |
Craig Topper | a7b07fd | 2012-02-21 05:39:57 +0000 | [diff] [blame] | 672 | StaticDataMember |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 673 | } Kind = Normal; |
| 674 | |
| 675 | // Default arguments of member function parameters that appear in a class |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 676 | // definition, as well as the initializers of data members, receive special |
| 677 | // treatment. Identify them. |
| 678 | if (ContextDecl) { |
| 679 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(ContextDecl)) { |
| 680 | if (const DeclContext *LexicalDC |
| 681 | = Param->getDeclContext()->getLexicalParent()) |
| 682 | if (LexicalDC->isRecord()) |
| 683 | Kind = DefaultArgument; |
| 684 | } else if (VarDecl *Var = dyn_cast<VarDecl>(ContextDecl)) { |
| 685 | if (Var->getDeclContext()->isRecord()) |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 686 | Kind = StaticDataMember; |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 687 | } else if (isa<FieldDecl>(ContextDecl)) { |
| 688 | Kind = DataMember; |
| 689 | } |
| 690 | } |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 691 | |
| 692 | switch (Kind) { |
| 693 | case Normal: |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 694 | if (CurContext->isDependentContext() || isInInlineFunction(CurContext)) |
| 695 | ManglingNumber = Context.getLambdaManglingNumber(CallOperator); |
| 696 | else |
| 697 | ManglingNumber = 0; |
| 698 | |
| 699 | // There is no special context for this lambda. |
| 700 | ContextDecl = 0; |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 701 | break; |
| 702 | |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 703 | case StaticDataMember: |
| 704 | if (!CurContext->isDependentContext()) { |
| 705 | ManglingNumber = 0; |
| 706 | ContextDecl = 0; |
| 707 | break; |
| 708 | } |
| 709 | // Fall through to assign a mangling number. |
| 710 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 711 | case DataMember: |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 712 | case DefaultArgument: |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 713 | ManglingNumber = ExprEvalContexts.back().getLambdaMangleContext() |
| 714 | .getManglingNumber(CallOperator); |
| 715 | break; |
| 716 | } |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 719 | LambdaExpr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange, |
| 720 | CaptureDefault, Captures, |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 721 | ExplicitParams, ExplicitResultType, |
| 722 | CaptureInits, ArrayIndexVars, |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 723 | ArrayIndexStarts, Body->getLocEnd(), |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 724 | *ManglingNumber, ContextDecl); |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 725 | |
| 726 | // C++11 [expr.prim.lambda]p2: |
| 727 | // A lambda-expression shall not appear in an unevaluated operand |
| 728 | // (Clause 5). |
Douglas Gregor | d5387e8 | 2012-02-14 00:00:48 +0000 | [diff] [blame] | 729 | if (!CurContext->isDependentContext()) { |
| 730 | switch (ExprEvalContexts.back().Context) { |
| 731 | case Unevaluated: |
| 732 | // We don't actually diagnose this case immediately, because we |
| 733 | // could be within a context where we might find out later that |
| 734 | // the expression is potentially evaluated (e.g., for typeid). |
| 735 | ExprEvalContexts.back().Lambdas.push_back(Lambda); |
| 736 | break; |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 737 | |
Douglas Gregor | d5387e8 | 2012-02-14 00:00:48 +0000 | [diff] [blame] | 738 | case ConstantEvaluated: |
| 739 | case PotentiallyEvaluated: |
| 740 | case PotentiallyEvaluatedIfUsed: |
| 741 | break; |
| 742 | } |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 743 | } |
Douglas Gregor | d5387e8 | 2012-02-14 00:00:48 +0000 | [diff] [blame] | 744 | |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 745 | return MaybeBindToTemporary(Lambda); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 746 | } |