friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
};
-/// NamedDecl - This represents a decl with a name. Many
-/// decls have names, but not ObjCMethodDecl, @class, etc.
+/// NamedDecl - This represents a decl with a name. Many decls have names such
+/// as ObjCMethodDecl, but not @class, etc.
class NamedDecl : public Decl {
/// Name - The name of this declaration, which is typically a normal
/// identifier but may also be a special kind of name (C++
return PP.getLangOptions();
}
+/// getCurFunctionDecl - If inside of a function body, this returns a pointer
+/// to the function decl for the function being parsed. If we're currently
+/// in a 'block', this returns the containing context.
+FunctionDecl *Sema::getCurFunctionDecl() {
+ DeclContext *DC = CurContext;
+ while (isa<BlockDecl>(DC))
+ DC = DC->getParent();
+ return dyn_cast<FunctionDecl>(DC);
+}
+
ObjCMethodDecl *Sema::getCurMethodDecl() {
DeclContext *DC = CurContext;
while (isa<BlockDecl>(DC))
DC = DC->getParent();
return dyn_cast<ObjCMethodDecl>(DC);
}
+
+NamedDecl *Sema::getCurFunctionOrMethodDecl() {
+ DeclContext *DC = CurContext;
+ while (isa<BlockDecl>(DC))
+ DC = DC->getParent();
+ if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
+ return cast<NamedDecl>(DC);
+ return 0;
+}
+
void PushDeclContext(DeclContext *DC);
void PopDeclContext();
- /// CurFunctionDecl - If inside of a function body, this returns a pointer to
- /// the function decl for the function being parsed.
- FunctionDecl *getCurFunctionDecl() {
- return dyn_cast<FunctionDecl>(CurContext);
- }
-
- /// CurMethodDecl - If inside of a method body, this returns a pointer to
- /// the method decl for the method being parsed.
+ /// getCurFunctionDecl - If inside of a function body, this returns a pointer
+ /// to the function decl for the function being parsed. If we're currently
+ /// in a 'block', this returns the containing context.
+ FunctionDecl *getCurFunctionDecl();
+
+ /// getCurMethodDecl - If inside of a method body, this returns a pointer to
+ /// the method decl for the method being parsed. If we're currently
+ /// in a 'block', this returns the containing context.
ObjCMethodDecl *getCurMethodDecl();
+ /// getCurFunctionOrMethodDecl - Return the Decl for the current ObjC method
+ /// or C function we're in, otherwise return null. If we're currently
+ /// in a 'block', this returns the containing context.
+ NamedDecl *getCurFunctionOrMethodDecl();
+
/// Add this decl to the scope shadowed decl chains.
void PushOnScopeChains(NamedDecl *D, Scope *S);
// FIXME: This isn't correct for methods (results in bogus warning).
// Get the last formal in the current function.
const ParmVarDecl *LastArg;
- if (getCurFunctionDecl())
- LastArg = *(getCurFunctionDecl()->param_end()-1);
+ if (FunctionDecl *FD = getCurFunctionDecl())
+ LastArg = *(FD->param_end()-1);
else
LastArg = *(getCurMethodDecl()->param_end()-1);
SecondArgIsLastNamedArgument = PV == LastArg;
}
// Verify that this is in a function context.
- if (getCurFunctionDecl() == 0 && getCurMethodDecl() == 0)
+ if (getCurFunctionOrMethodDecl() == 0)
return Diag(Loc, diag::err_predef_outside_function);
// Pre-defined identifiers are of type char[x], where x is the length of the
// string.
unsigned Length;
- if (getCurFunctionDecl())
- Length = getCurFunctionDecl()->getIdentifier()->getLength();
+ if (FunctionDecl *FD = getCurFunctionDecl())
+ Length = FD->getIdentifier()->getLength();
else
Length = getCurMethodDecl()->getSynthesizedMethodSize();
DeclarationName()))
return true;
- bool isFileScope = !getCurFunctionDecl() && !getCurMethodDecl();
+ bool isFileScope = getCurFunctionOrMethodDecl() == 0;
if (isFileScope) { // 6.5.2.5p3
if (CheckForConstantInitializer(literalExpr, literalType))
return true;
Expr *RetValExp = static_cast<Expr *>(rex);
if (CurBlock)
return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
- QualType FnRetType =
- getCurFunctionDecl() ? getCurFunctionDecl()->getResultType() :
- getCurMethodDecl()->getResultType();
+
+ QualType FnRetType;
+ if (FunctionDecl *FD = getCurFunctionDecl())
+ FnRetType = FD->getResultType();
+ else
+ FnRetType = getCurMethodDecl()->getResultType();
if (FnRetType->isVoidType()) {
if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns)
bar(^{ test5g = 1; });
}
+// rdar://6405429 - __func__ in a block refers to the containing function name.
+const char*test6() {
+ return ^{
+ return __func__;
+ } ();
+}
+