1 //===--- ASTLambda.h - Lambda Helper Functions --------------*- C++ -*-===//
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 /// \file
11 /// \brief This file provides some common utility functions for processing
12 /// Lambda related AST Constructs.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_AST_ASTLAMBDA_H
17 #define LLVM_CLANG_AST_ASTLAMBDA_H
18 
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/DeclTemplate.h"
21 
22 namespace clang {
getLambdaStaticInvokerName()23 inline StringRef getLambdaStaticInvokerName() {
24   return "__invoke";
25 }
26 // This function returns true if M is a specialization, a template,
27 // or a non-generic lambda call operator.
isLambdaCallOperator(const CXXMethodDecl * MD)28 inline bool isLambdaCallOperator(const CXXMethodDecl *MD) {
29   const CXXRecordDecl *LambdaClass = MD->getParent();
30   if (!LambdaClass || !LambdaClass->isLambda()) return false;
31   return MD->getOverloadedOperator() == OO_Call;
32 }
33 
isLambdaCallOperator(const DeclContext * DC)34 inline bool isLambdaCallOperator(const DeclContext *DC) {
35   if (!DC || !isa<CXXMethodDecl>(DC)) return false;
36   return isLambdaCallOperator(cast<CXXMethodDecl>(DC));
37 }
38 
isGenericLambdaCallOperatorSpecialization(const CXXMethodDecl * MD)39 inline bool isGenericLambdaCallOperatorSpecialization(const CXXMethodDecl *MD) {
40   if (!MD) return false;
41   const CXXRecordDecl *LambdaClass = MD->getParent();
42   if (LambdaClass && LambdaClass->isGenericLambda())
43     return isLambdaCallOperator(MD) &&
44                     MD->isFunctionTemplateSpecialization();
45   return false;
46 }
47 
isLambdaConversionOperator(CXXConversionDecl * C)48 inline bool isLambdaConversionOperator(CXXConversionDecl *C) {
49   return C ? C->getParent()->isLambda() : false;
50 }
51 
isLambdaConversionOperator(Decl * D)52 inline bool isLambdaConversionOperator(Decl *D) {
53   if (!D) return false;
54   if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D))
55     return isLambdaConversionOperator(Conv);
56   if (FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(D))
57     if (CXXConversionDecl *Conv =
58         dyn_cast_or_null<CXXConversionDecl>(F->getTemplatedDecl()))
59       return isLambdaConversionOperator(Conv);
60   return false;
61 }
62 
isGenericLambdaCallOperatorSpecialization(DeclContext * DC)63 inline bool isGenericLambdaCallOperatorSpecialization(DeclContext *DC) {
64   return isGenericLambdaCallOperatorSpecialization(
65                                           dyn_cast<CXXMethodDecl>(DC));
66 }
67 
68 
69 // This returns the parent DeclContext ensuring that the correct
70 // parent DeclContext is returned for Lambdas
getLambdaAwareParentOfDeclContext(DeclContext * DC)71 inline DeclContext *getLambdaAwareParentOfDeclContext(DeclContext *DC) {
72   if (isLambdaCallOperator(DC))
73     return DC->getParent()->getParent();
74   else
75     return DC->getParent();
76 }
77 
78 } // clang
79 
80 #endif
81