2007-07-11 21:01:13 +04:00
|
|
|
//===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for declarations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-26 02:03:47 +04:00
|
|
|
#include "clang/Sema/SemaInternal.h"
|
2010-08-13 00:07:10 +04:00
|
|
|
#include "clang/Sema/Initialization.h"
|
|
|
|
#include "clang/Sema/Lookup.h"
|
2010-08-24 12:50:51 +04:00
|
|
|
#include "clang/Sema/CXXFieldCollector.h"
|
|
|
|
#include "clang/Sema/Scope.h"
|
2010-08-25 12:40:02 +04:00
|
|
|
#include "clang/Sema/ScopeInfo.h"
|
2008-07-03 08:20:39 +04:00
|
|
|
#include "clang/AST/APValue.h"
|
2008-02-06 03:51:33 +03:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-10-06 21:59:45 +04:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2010-08-25 11:42:41 +04:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2010-08-24 11:21:54 +04:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-02-04 22:02:06 +03:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2008-06-26 22:38:35 +04:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2009-04-27 00:35:05 +04:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2010-08-20 22:27:03 +04:00
|
|
|
#include "clang/Sema/DeclSpec.h"
|
|
|
|
#include "clang/Sema/ParsedTemplate.h"
|
2009-10-14 03:27:22 +04:00
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
2009-08-27 03:45:07 +04:00
|
|
|
#include "clang/Basic/PartialDiagnostic.h"
|
2008-01-31 02:46:05 +03:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2009-08-27 03:45:07 +04:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2008-01-31 02:46:05 +03:00
|
|
|
// FIXME: layering (ideally, Sema shouldn't be dependent on Lex API's)
|
2008-02-06 03:51:33 +03:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-09-09 19:08:12 +04:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2009-12-24 12:58:38 +03:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2008-12-24 00:05:05 +03:00
|
|
|
#include <algorithm>
|
2009-09-29 01:14:19 +04:00
|
|
|
#include <cstring>
|
2008-12-24 00:05:05 +03:00
|
|
|
#include <functional>
|
2007-07-11 21:01:13 +04:00
|
|
|
using namespace clang;
|
2010-08-25 12:40:02 +04:00
|
|
|
using namespace sema;
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
Sema::DeclGroupPtrTy Sema::ConvertDeclToDeclGroup(Decl *Ptr) {
|
|
|
|
return DeclGroupPtrTy::make(DeclGroupRef(Ptr));
|
2009-03-29 20:50:03 +04:00
|
|
|
}
|
|
|
|
|
2009-02-04 22:16:12 +03:00
|
|
|
/// \brief If the identifier refers to a type name within this scope,
|
|
|
|
/// return the declaration of that type.
|
|
|
|
///
|
|
|
|
/// This routine performs ordinary name lookup of the identifier II
|
|
|
|
/// within the given scope, with optional C++ scope specifier SS, to
|
2009-02-09 18:09:02 +03:00
|
|
|
/// determine whether the name refers to a type. If so, returns an
|
|
|
|
/// opaque pointer (actually a QualType) corresponding to that
|
|
|
|
/// type. Otherwise, returns NULL.
|
2009-02-04 22:16:12 +03:00
|
|
|
///
|
|
|
|
/// If name lookup results in an ambiguity, this routine will complain
|
|
|
|
/// and then return NULL.
|
2010-08-24 09:47:05 +04:00
|
|
|
ParsedType Sema::getTypeName(IdentifierInfo &II, SourceLocation NameLoc,
|
|
|
|
Scope *S, CXXScopeSpec *SS,
|
|
|
|
bool isClassName,
|
|
|
|
ParsedType ObjectTypePtr) {
|
2009-11-21 01:03:38 +03:00
|
|
|
// Determine where we will perform name lookup.
|
|
|
|
DeclContext *LookupCtx = 0;
|
|
|
|
if (ObjectTypePtr) {
|
2010-08-24 09:47:05 +04:00
|
|
|
QualType ObjectType = ObjectTypePtr.get();
|
2009-11-21 01:03:38 +03:00
|
|
|
if (ObjectType->isRecordType())
|
|
|
|
LookupCtx = computeDeclContext(ObjectType);
|
2010-04-08 03:29:58 +04:00
|
|
|
} else if (SS && SS->isNotEmpty()) {
|
2009-11-21 01:03:38 +03:00
|
|
|
LookupCtx = computeDeclContext(*SS, false);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-11-21 01:03:38 +03:00
|
|
|
if (!LookupCtx) {
|
|
|
|
if (isDependentScopeSpecifier(*SS)) {
|
|
|
|
// C++ [temp.res]p3:
|
|
|
|
// A qualified-id that refers to a type and in which the
|
|
|
|
// nested-name-specifier depends on a template-parameter (14.6.2)
|
|
|
|
// shall be prefixed by the keyword typename to indicate that the
|
|
|
|
// qualified-id denotes a type, forming an
|
|
|
|
// elaborated-type-specifier (7.1.5.3).
|
|
|
|
//
|
|
|
|
// We therefore do not perform any name lookup if the result would
|
|
|
|
// refer to a member of an unknown specialization.
|
|
|
|
if (!isClassName)
|
2010-08-24 09:47:05 +04:00
|
|
|
return ParsedType();
|
2009-11-21 01:03:38 +03:00
|
|
|
|
2010-06-11 04:33:02 +04:00
|
|
|
// We know from the grammar that this name refers to a type,
|
|
|
|
// so build a dependent node to describe the type.
|
2010-08-24 09:47:05 +04:00
|
|
|
QualType T =
|
|
|
|
CheckTypenameType(ETK_None, SS->getScopeRep(), II,
|
|
|
|
SourceLocation(), SS->getRange(), NameLoc);
|
|
|
|
return ParsedType::make(T);
|
2009-11-21 01:03:38 +03:00
|
|
|
}
|
|
|
|
|
2010-08-24 09:47:05 +04:00
|
|
|
return ParsedType();
|
2009-11-21 01:03:38 +03:00
|
|
|
}
|
|
|
|
|
2010-05-01 04:40:08 +04:00
|
|
|
if (!LookupCtx->isDependentContext() &&
|
|
|
|
RequireCompleteDeclContext(*SS, LookupCtx))
|
2010-08-24 09:47:05 +04:00
|
|
|
return ParsedType();
|
2009-08-26 22:27:52 +04:00
|
|
|
}
|
2009-12-21 04:42:38 +03:00
|
|
|
|
|
|
|
// FIXME: LookupNestedNameSpecifierName isn't the right kind of
|
|
|
|
// lookup for class-names.
|
|
|
|
LookupNameKind Kind = isClassName ? LookupNestedNameSpecifierName :
|
|
|
|
LookupOrdinaryName;
|
|
|
|
LookupResult Result(*this, &II, NameLoc, Kind);
|
2009-11-21 01:03:38 +03:00
|
|
|
if (LookupCtx) {
|
|
|
|
// Perform "qualified" name lookup into the declaration context we
|
|
|
|
// computed, which is either the type of the base of a member access
|
|
|
|
// expression or the declaration context associated with a prior
|
|
|
|
// nested-name-specifier.
|
|
|
|
LookupQualifiedName(Result, LookupCtx);
|
2009-05-11 23:58:34 +04:00
|
|
|
|
2009-11-21 01:03:38 +03:00
|
|
|
if (ObjectTypePtr && Result.empty()) {
|
|
|
|
// C++ [basic.lookup.classref]p3:
|
|
|
|
// If the unqualified-id is ~type-name, the type-name is looked up
|
|
|
|
// in the context of the entire postfix-expression. If the type T of
|
|
|
|
// the object expression is of a class type C, the type-name is also
|
|
|
|
// looked up in the scope of class C. At least one of the lookups shall
|
|
|
|
// find a name that refers to (possibly cv-qualified) T.
|
|
|
|
LookupName(Result, S);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Perform unqualified name lookup.
|
|
|
|
LookupName(Result, S);
|
|
|
|
}
|
|
|
|
|
2009-02-17 01:07:16 +03:00
|
|
|
NamedDecl *IIDecl = 0;
|
2009-11-17 05:14:36 +03:00
|
|
|
switch (Result.getResultKind()) {
|
2009-02-17 01:07:16 +03:00
|
|
|
case LookupResult::NotFound:
|
2010-01-15 04:44:47 +03:00
|
|
|
case LookupResult::NotFoundInCurrentInstantiation:
|
2009-02-17 01:07:16 +03:00
|
|
|
case LookupResult::FoundOverloaded:
|
2009-11-18 05:36:19 +03:00
|
|
|
case LookupResult::FoundUnresolvedValue:
|
2010-01-27 04:50:18 +03:00
|
|
|
Result.suppressDiagnostics();
|
2010-08-24 09:47:05 +04:00
|
|
|
return ParsedType();
|
2009-02-04 20:00:24 +03:00
|
|
|
|
2009-10-26 01:09:09 +03:00
|
|
|
case LookupResult::Ambiguous:
|
2009-10-10 09:48:19 +04:00
|
|
|
// Recover from type-hiding ambiguities by hiding the type. We'll
|
|
|
|
// do the lookup again when looking for an object, and we can
|
|
|
|
// diagnose the error then. If we don't do this, then the error
|
|
|
|
// about hiding the type will be immediately followed by an error
|
|
|
|
// that only makes sense if the identifier was treated like a type.
|
2009-11-17 05:14:36 +03:00
|
|
|
if (Result.getAmbiguityKind() == LookupResult::AmbiguousTagHiding) {
|
|
|
|
Result.suppressDiagnostics();
|
2010-08-24 09:47:05 +04:00
|
|
|
return ParsedType();
|
2009-11-17 05:14:36 +03:00
|
|
|
}
|
2009-10-10 09:48:19 +04:00
|
|
|
|
2009-04-02 01:51:26 +04:00
|
|
|
// Look to see if we have a type anywhere in the list of results.
|
|
|
|
for (LookupResult::iterator Res = Result.begin(), ResEnd = Result.end();
|
|
|
|
Res != ResEnd; ++Res) {
|
|
|
|
if (isa<TypeDecl>(*Res) || isa<ObjCInterfaceDecl>(*Res)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
if (!IIDecl ||
|
|
|
|
(*Res)->getLocation().getRawEncoding() <
|
2009-04-13 19:14:38 +04:00
|
|
|
IIDecl->getLocation().getRawEncoding())
|
|
|
|
IIDecl = *Res;
|
2009-04-02 01:51:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IIDecl) {
|
|
|
|
// None of the entities we found is a type, so there is no way
|
|
|
|
// to even assume that the result is a type. In this case, don't
|
|
|
|
// complain about the ambiguity. The parser will either try to
|
|
|
|
// perform this lookup again (e.g., as an object name), which
|
|
|
|
// will produce the ambiguity, or will complain that it expected
|
|
|
|
// a type name.
|
2009-11-17 05:14:36 +03:00
|
|
|
Result.suppressDiagnostics();
|
2010-08-24 09:47:05 +04:00
|
|
|
return ParsedType();
|
2009-04-02 01:51:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// We found a type within the ambiguous lookup; diagnose the
|
|
|
|
// ambiguity and then return that type. This might be the right
|
|
|
|
// answer, or it might not be, but it suppresses any attempt to
|
|
|
|
// perform the name lookup again.
|
|
|
|
break;
|
2009-02-04 20:00:24 +03:00
|
|
|
|
2009-02-17 01:07:16 +03:00
|
|
|
case LookupResult::Found:
|
2009-10-10 01:13:30 +04:00
|
|
|
IIDecl = Result.getFoundDecl();
|
2009-02-17 01:07:16 +03:00
|
|
|
break;
|
2009-01-15 03:26:24 +03:00
|
|
|
}
|
|
|
|
|
2009-10-25 20:16:46 +03:00
|
|
|
assert(IIDecl && "Didn't find decl");
|
2009-11-04 05:18:39 +03:00
|
|
|
|
2009-10-25 20:16:46 +03:00
|
|
|
QualType T;
|
|
|
|
if (TypeDecl *TD = dyn_cast<TypeDecl>(IIDecl)) {
|
2009-11-04 05:18:39 +03:00
|
|
|
DiagnoseUseOfDecl(IIDecl, NameLoc);
|
2009-11-17 05:14:36 +03:00
|
|
|
|
2009-10-25 20:16:46 +03:00
|
|
|
if (T.isNull())
|
|
|
|
T = Context.getTypeDeclType(TD);
|
|
|
|
|
2009-03-19 03:39:20 +03:00
|
|
|
if (SS)
|
2010-05-12 01:36:43 +04:00
|
|
|
T = getElaboratedType(ETK_None, *SS, T);
|
2009-10-25 20:16:46 +03:00
|
|
|
|
|
|
|
} else if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(IIDecl)) {
|
|
|
|
T = Context.getObjCInterfaceType(IDecl);
|
2009-11-17 05:14:36 +03:00
|
|
|
} else {
|
|
|
|
// If it's not plausibly a type, suppress diagnostics.
|
|
|
|
Result.suppressDiagnostics();
|
2010-08-24 09:47:05 +04:00
|
|
|
return ParsedType();
|
2009-11-17 05:14:36 +03:00
|
|
|
}
|
Introduce a representation for types that we referred to via a
qualified name, e.g.,
foo::x
so that we retain the nested-name-specifier as written in the source
code and can reproduce that qualified name when printing the types
back (e.g., in diagnostics). This is PR3493, which won't be complete
until finished the other tasks mentioned near the end of this commit.
The parser's representation of nested-name-specifiers, CXXScopeSpec,
is now a bit fatter, because it needs to contain the scopes that
precede each '::' and keep track of whether the global scoping
operator '::' was at the beginning. For example, we need to keep track
of the leading '::', 'foo', and 'bar' in
::foo::bar::x
The Action's CXXScopeTy * is no longer a DeclContext *. It's now the
opaque version of the new NestedNameSpecifier, which contains a single
component of a nested-name-specifier (either a DeclContext * or a Type
*, bitmangled).
The new sugar type QualifiedNameType composes a sequence of
NestedNameSpecifiers with a representation of the type we're actually
referring to. At present, we only build QualifiedNameType nodes within
Sema::getTypeName. This will be extended to other type-constructing
actions (e.g., ActOnClassTemplateId).
Also on the way: QualifiedDeclRefExprs will also store a sequence of
NestedNameSpecifiers, so that we can print out the property
nested-name-specifier. I expect to also use this for handling
dependent names like Fibonacci<I - 1>::value.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67265 91177308-0d34-0410-b5e6-96231b3b80d8
2009-03-19 03:18:19 +03:00
|
|
|
|
2010-08-24 09:47:05 +04:00
|
|
|
return ParsedType::make(T);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-04-13 01:49:30 +04:00
|
|
|
/// isTagName() - This method is called *for error recovery purposes only*
|
|
|
|
/// to determine if the specified name is a valid tag name ("struct foo"). If
|
|
|
|
/// so, this returns the TST for the tag corresponding to it (TST_enum,
|
|
|
|
/// TST_union, TST_struct, TST_class). This is used to diagnose cases in C
|
|
|
|
/// where the user forgot to specify the tag.
|
|
|
|
DeclSpec::TST Sema::isTagName(IdentifierInfo &II, Scope *S) {
|
|
|
|
// Do a tag name lookup in this scope.
|
2009-11-17 05:14:36 +03:00
|
|
|
LookupResult R(*this, &II, SourceLocation(), LookupTagName);
|
|
|
|
LookupName(R, S, false);
|
|
|
|
R.suppressDiagnostics();
|
|
|
|
if (R.getResultKind() == LookupResult::Found)
|
2009-12-02 11:25:40 +03:00
|
|
|
if (const TagDecl *TD = R.getAsSingle<TagDecl>()) {
|
2009-04-13 01:49:30 +04:00
|
|
|
switch (TD->getTagKind()) {
|
2010-05-12 01:36:43 +04:00
|
|
|
default: return DeclSpec::TST_unspecified;
|
|
|
|
case TTK_Struct: return DeclSpec::TST_struct;
|
|
|
|
case TTK_Union: return DeclSpec::TST_union;
|
|
|
|
case TTK_Class: return DeclSpec::TST_class;
|
|
|
|
case TTK_Enum: return DeclSpec::TST_enum;
|
2009-04-13 01:49:30 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-13 01:49:30 +04:00
|
|
|
return DeclSpec::TST_unspecified;
|
|
|
|
}
|
|
|
|
|
2009-10-14 03:27:22 +04:00
|
|
|
bool Sema::DiagnoseUnknownTypeName(const IdentifierInfo &II,
|
|
|
|
SourceLocation IILoc,
|
|
|
|
Scope *S,
|
2010-04-08 20:38:48 +04:00
|
|
|
CXXScopeSpec *SS,
|
2010-08-24 09:47:05 +04:00
|
|
|
ParsedType &SuggestedType) {
|
2009-10-14 03:27:22 +04:00
|
|
|
// We don't have anything to suggest (yet).
|
2010-08-24 09:47:05 +04:00
|
|
|
SuggestedType = ParsedType();
|
2009-10-14 03:27:22 +04:00
|
|
|
|
2009-12-30 20:04:44 +03:00
|
|
|
// There may have been a typo in the name of the type. Look up typo
|
|
|
|
// results, in case we have something that we can suggest.
|
|
|
|
LookupResult Lookup(*this, &II, IILoc, LookupOrdinaryName,
|
|
|
|
NotForRedeclaration);
|
|
|
|
|
2010-04-15 00:04:41 +04:00
|
|
|
if (DeclarationName Corrected = CorrectTypo(Lookup, S, SS, 0, 0, CTC_Type)) {
|
|
|
|
if (NamedDecl *Result = Lookup.getAsSingle<NamedDecl>()) {
|
|
|
|
if ((isa<TypeDecl>(Result) || isa<ObjCInterfaceDecl>(Result)) &&
|
|
|
|
!Result->isInvalidDecl()) {
|
|
|
|
// We found a similarly-named type or interface; suggest that.
|
|
|
|
if (!SS || !SS->isSet())
|
|
|
|
Diag(IILoc, diag::err_unknown_typename_suggest)
|
|
|
|
<< &II << Lookup.getLookupName()
|
|
|
|
<< FixItHint::CreateReplacement(SourceRange(IILoc),
|
|
|
|
Result->getNameAsString());
|
|
|
|
else if (DeclContext *DC = computeDeclContext(*SS, false))
|
|
|
|
Diag(IILoc, diag::err_unknown_nested_typename_suggest)
|
|
|
|
<< &II << DC << Lookup.getLookupName() << SS->getRange()
|
|
|
|
<< FixItHint::CreateReplacement(SourceRange(IILoc),
|
|
|
|
Result->getNameAsString());
|
|
|
|
else
|
|
|
|
llvm_unreachable("could not have corrected a typo here");
|
|
|
|
|
|
|
|
Diag(Result->getLocation(), diag::note_previous_decl)
|
|
|
|
<< Result->getDeclName();
|
|
|
|
|
|
|
|
SuggestedType = getTypeName(*Result->getIdentifier(), IILoc, S, SS);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (Lookup.empty()) {
|
|
|
|
// We corrected to a keyword.
|
|
|
|
// FIXME: Actually recover with the keyword we suggest, and emit a fix-it.
|
|
|
|
Diag(IILoc, diag::err_unknown_typename_suggest)
|
|
|
|
<< &II << Corrected;
|
|
|
|
return true;
|
2009-12-30 20:04:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-09 01:04:54 +04:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
// See if II is a class template that the user forgot to pass arguments to.
|
|
|
|
UnqualifiedId Name;
|
|
|
|
Name.setIdentifier(&II, IILoc);
|
|
|
|
CXXScopeSpec EmptySS;
|
|
|
|
TemplateTy TemplateResult;
|
2010-05-22 03:18:07 +04:00
|
|
|
bool MemberOfUnknownSpecialization;
|
2010-08-06 16:11:11 +04:00
|
|
|
if (isTemplateName(S, SS ? *SS : EmptySS, /*hasTemplateKeyword=*/false,
|
2010-08-24 09:47:05 +04:00
|
|
|
Name, ParsedType(), true, TemplateResult,
|
2010-05-22 03:18:07 +04:00
|
|
|
MemberOfUnknownSpecialization) == TNK_Type_template) {
|
2010-04-09 01:04:54 +04:00
|
|
|
TemplateName TplName = TemplateResult.getAsVal<TemplateName>();
|
|
|
|
Diag(IILoc, diag::err_template_missing_args) << TplName;
|
|
|
|
if (TemplateDecl *TplDecl = TplName.getAsTemplateDecl()) {
|
|
|
|
Diag(TplDecl->getLocation(), diag::note_template_decl_here)
|
|
|
|
<< TplDecl->getTemplateParameters()->getSourceRange();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-14 03:27:22 +04:00
|
|
|
// FIXME: Should we move the logic that tries to recover from a missing tag
|
|
|
|
// (struct, union, enum) from Parser::ParseImplicitInt here, instead?
|
|
|
|
|
2009-12-30 20:04:44 +03:00
|
|
|
if (!SS || (!SS->isSet() && !SS->isInvalid()))
|
2009-10-14 03:27:22 +04:00
|
|
|
Diag(IILoc, diag::err_unknown_typename) << &II;
|
|
|
|
else if (DeclContext *DC = computeDeclContext(*SS, false))
|
|
|
|
Diag(IILoc, diag::err_typename_nested_not_found)
|
|
|
|
<< &II << DC << SS->getRange();
|
|
|
|
else if (isDependentScopeSpecifier(*SS)) {
|
|
|
|
Diag(SS->getRange().getBegin(), diag::err_typename_missing)
|
2009-10-19 01:17:35 +04:00
|
|
|
<< (NestedNameSpecifier *)SS->getScopeRep() << II.getName()
|
2009-10-14 03:27:22 +04:00
|
|
|
<< SourceRange(SS->getRange().getBegin(), IILoc)
|
2010-03-31 21:46:05 +04:00
|
|
|
<< FixItHint::CreateInsertion(SS->getRange().getBegin(), "typename ");
|
2010-06-17 02:31:08 +04:00
|
|
|
SuggestedType = ActOnTypenameType(S, SourceLocation(), *SS, II, IILoc).get();
|
2009-10-14 03:27:22 +04:00
|
|
|
} else {
|
|
|
|
assert(SS && SS->isInvalid() &&
|
|
|
|
"Invalid scope specifier has already been diagnosed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-04-13 01:49:30 +04:00
|
|
|
|
2009-08-18 04:00:49 +04:00
|
|
|
// Determines the context to return to after temporarily entering a
|
|
|
|
// context. This depends in an unnecessarily complicated way on the
|
|
|
|
// exact ordering of callbacks from the parser.
|
2008-11-08 20:17:31 +03:00
|
|
|
DeclContext *Sema::getContainingDC(DeclContext *DC) {
|
2009-08-18 04:00:49 +04:00
|
|
|
|
|
|
|
// Functions defined inline within classes aren't parsed until we've
|
|
|
|
// finished parsing the top-level class, so the top-level class is
|
|
|
|
// the context we'll need to return to.
|
|
|
|
if (isa<FunctionDecl>(DC)) {
|
|
|
|
DC = DC->getLexicalParent();
|
|
|
|
|
|
|
|
// A function not defined within a class will always return to its
|
|
|
|
// lexical context.
|
|
|
|
if (!isa<CXXRecordDecl>(DC))
|
|
|
|
return DC;
|
|
|
|
|
|
|
|
// A C++ inline method/friend is parsed *after* the topmost class
|
|
|
|
// it was declared in is fully parsed ("complete"); the topmost
|
|
|
|
// class is the context we need to return to.
|
2008-11-19 21:01:13 +03:00
|
|
|
while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getLexicalParent()))
|
2008-07-01 14:37:29 +04:00
|
|
|
DC = RD;
|
|
|
|
|
|
|
|
// Return the declaration context of the topmost class the inline method is
|
|
|
|
// declared in.
|
|
|
|
return DC;
|
|
|
|
}
|
|
|
|
|
2010-08-06 04:46:05 +04:00
|
|
|
// ObjCMethodDecls are parsed (for some reason) outside the context
|
|
|
|
// of the class.
|
2008-11-10 02:41:00 +03:00
|
|
|
if (isa<ObjCMethodDecl>(DC))
|
2010-08-06 04:46:05 +04:00
|
|
|
return DC->getLexicalParent()->getLexicalParent();
|
2008-11-10 02:41:00 +03:00
|
|
|
|
2008-11-19 21:01:13 +03:00
|
|
|
return DC->getLexicalParent();
|
2008-07-01 14:37:29 +04:00
|
|
|
}
|
|
|
|
|
2008-12-11 19:49:14 +03:00
|
|
|
void Sema::PushDeclContext(Scope *S, DeclContext *DC) {
|
2008-11-08 20:17:31 +03:00
|
|
|
assert(getContainingDC(DC) == CurContext &&
|
2008-12-08 10:14:51 +03:00
|
|
|
"The next DeclContext should be lexically contained in the current one.");
|
2008-04-22 22:39:57 +04:00
|
|
|
CurContext = DC;
|
2008-12-11 19:49:14 +03:00
|
|
|
S->setEntity(DC);
|
2008-04-04 10:12:32 +04:00
|
|
|
}
|
|
|
|
|
2008-04-06 08:47:34 +04:00
|
|
|
void Sema::PopDeclContext() {
|
|
|
|
assert(CurContext && "DeclContext imbalance!");
|
2008-12-11 19:49:14 +03:00
|
|
|
|
2008-11-08 20:17:31 +03:00
|
|
|
CurContext = getContainingDC(CurContext);
|
2010-07-24 02:45:07 +04:00
|
|
|
assert(CurContext && "Popped translation unit!");
|
2008-04-04 10:12:32 +04:00
|
|
|
}
|
|
|
|
|
2009-06-18 03:19:02 +04:00
|
|
|
/// EnterDeclaratorContext - Used when we must lookup names in the context
|
|
|
|
/// of a declarator's nested name specifier.
|
2009-12-19 13:49:29 +03:00
|
|
|
///
|
2009-06-18 03:15:40 +04:00
|
|
|
void Sema::EnterDeclaratorContext(Scope *S, DeclContext *DC) {
|
2009-12-19 13:49:29 +03:00
|
|
|
// C++0x [basic.lookup.unqual]p13:
|
|
|
|
// A name used in the definition of a static data member of class
|
|
|
|
// X (after the qualified-id of the static member) is looked up as
|
|
|
|
// if the name was used in a member function of X.
|
|
|
|
// C++0x [basic.lookup.unqual]p14:
|
|
|
|
// If a variable member of a namespace is defined outside of the
|
|
|
|
// scope of its namespace then any name used in the definition of
|
|
|
|
// the variable member (after the declarator-id) is looked up as
|
|
|
|
// if the definition of the variable member occurred in its
|
|
|
|
// namespace.
|
|
|
|
// Both of these imply that we should push a scope whose context
|
|
|
|
// is the semantic context of the declaration. We can't use
|
|
|
|
// PushDeclContext here because that context is not necessarily
|
|
|
|
// lexically contained in the current context. Fortunately,
|
|
|
|
// the containing scope should have the appropriate information.
|
|
|
|
|
|
|
|
assert(!S->getEntity() && "scope already has entity");
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
Scope *Ancestor = S->getParent();
|
|
|
|
while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent();
|
|
|
|
assert(Ancestor->getEntity() == CurContext && "ancestor context mismatch");
|
|
|
|
#endif
|
|
|
|
|
2009-06-18 03:15:40 +04:00
|
|
|
CurContext = DC;
|
2009-12-19 13:49:29 +03:00
|
|
|
S->setEntity(DC);
|
2009-06-18 03:15:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::ExitDeclaratorContext(Scope *S) {
|
2009-12-19 13:49:29 +03:00
|
|
|
assert(S->getEntity() == CurContext && "Context imbalance!");
|
|
|
|
|
|
|
|
// Switch back to the lexical context. The safety of this is
|
|
|
|
// enforced by an assert in EnterDeclaratorContext.
|
|
|
|
Scope *Ancestor = S->getParent();
|
|
|
|
while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent();
|
|
|
|
CurContext = (DeclContext*) Ancestor->getEntity();
|
|
|
|
|
|
|
|
// We don't need to do anything with the scope, which is going to
|
|
|
|
// disappear.
|
2009-06-18 03:15:40 +04:00
|
|
|
}
|
|
|
|
|
Initial implementation of function overloading in C.
This commit adds a new attribute, "overloadable", that enables C++
function overloading in C. The attribute can only be added to function
declarations, e.g.,
int *f(int) __attribute__((overloadable));
If the "overloadable" attribute exists on a function with a given
name, *all* functions with that name (and in that scope) must have the
"overloadable" attribute. Sets of overloaded functions with the
"overloadable" attribute then follow the normal C++ rules for
overloaded functions, e.g., overloads must have different
parameter-type-lists from each other.
When calling an overloaded function in C, we follow the same
overloading rules as C++, with three extensions to the set of standard
conversions:
- A value of a given struct or union type T can be converted to the
type T. This is just the identity conversion. (In C++, this would
go through a copy constructor).
- A value of pointer type T* can be converted to a value of type U*
if T and U are compatible types. This conversion has Conversion
rank (it's considered a pointer conversion in C).
- A value of type T can be converted to a value of type U if T and U
are compatible (and are not both pointer types). This conversion
has Conversion rank (it's considered to be a new kind of
conversion unique to C, a "compatible" conversion).
Known defects (and, therefore, next steps):
1) The standard-conversion handling does not understand conversions
involving _Complex or vector extensions, so it is likely to get
these wrong. We need to add these conversions.
2) All overloadable functions with the same name will have the same
linkage name, which means we'll get a collision in the linker (if
not sooner). We'll need to mangle the names of these functions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64336 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-12 02:02:49 +03:00
|
|
|
/// \brief Determine whether we allow overloading of the function
|
|
|
|
/// PrevDecl with another declaration.
|
|
|
|
///
|
|
|
|
/// This routine determines whether overloading is possible, not
|
|
|
|
/// whether some new function is actually an overload. It will return
|
|
|
|
/// true in C++ (where we can always provide overloads) or, as an
|
|
|
|
/// extension, in C when the previous function is already an
|
|
|
|
/// overloaded function declaration or has the "overloadable"
|
|
|
|
/// attribute.
|
2009-11-19 01:49:29 +03:00
|
|
|
static bool AllowOverloadingOfFunction(LookupResult &Previous,
|
|
|
|
ASTContext &Context) {
|
Initial implementation of function overloading in C.
This commit adds a new attribute, "overloadable", that enables C++
function overloading in C. The attribute can only be added to function
declarations, e.g.,
int *f(int) __attribute__((overloadable));
If the "overloadable" attribute exists on a function with a given
name, *all* functions with that name (and in that scope) must have the
"overloadable" attribute. Sets of overloaded functions with the
"overloadable" attribute then follow the normal C++ rules for
overloaded functions, e.g., overloads must have different
parameter-type-lists from each other.
When calling an overloaded function in C, we follow the same
overloading rules as C++, with three extensions to the set of standard
conversions:
- A value of a given struct or union type T can be converted to the
type T. This is just the identity conversion. (In C++, this would
go through a copy constructor).
- A value of pointer type T* can be converted to a value of type U*
if T and U are compatible types. This conversion has Conversion
rank (it's considered a pointer conversion in C).
- A value of type T can be converted to a value of type U if T and U
are compatible (and are not both pointer types). This conversion
has Conversion rank (it's considered to be a new kind of
conversion unique to C, a "compatible" conversion).
Known defects (and, therefore, next steps):
1) The standard-conversion handling does not understand conversions
involving _Complex or vector extensions, so it is likely to get
these wrong. We need to add these conversions.
2) All overloadable functions with the same name will have the same
linkage name, which means we'll get a collision in the linker (if
not sooner). We'll need to mangle the names of these functions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64336 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-12 02:02:49 +03:00
|
|
|
if (Context.getLangOptions().CPlusPlus)
|
|
|
|
return true;
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Previous.getResultKind() == LookupResult::FoundOverloaded)
|
Initial implementation of function overloading in C.
This commit adds a new attribute, "overloadable", that enables C++
function overloading in C. The attribute can only be added to function
declarations, e.g.,
int *f(int) __attribute__((overloadable));
If the "overloadable" attribute exists on a function with a given
name, *all* functions with that name (and in that scope) must have the
"overloadable" attribute. Sets of overloaded functions with the
"overloadable" attribute then follow the normal C++ rules for
overloaded functions, e.g., overloads must have different
parameter-type-lists from each other.
When calling an overloaded function in C, we follow the same
overloading rules as C++, with three extensions to the set of standard
conversions:
- A value of a given struct or union type T can be converted to the
type T. This is just the identity conversion. (In C++, this would
go through a copy constructor).
- A value of pointer type T* can be converted to a value of type U*
if T and U are compatible types. This conversion has Conversion
rank (it's considered a pointer conversion in C).
- A value of type T can be converted to a value of type U if T and U
are compatible (and are not both pointer types). This conversion
has Conversion rank (it's considered to be a new kind of
conversion unique to C, a "compatible" conversion).
Known defects (and, therefore, next steps):
1) The standard-conversion handling does not understand conversions
involving _Complex or vector extensions, so it is likely to get
these wrong. We need to add these conversions.
2) All overloadable functions with the same name will have the same
linkage name, which means we'll get a collision in the linker (if
not sooner). We'll need to mangle the names of these functions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64336 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-12 02:02:49 +03:00
|
|
|
return true;
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
return (Previous.getResultKind() == LookupResult::Found
|
|
|
|
&& Previous.getFoundDecl()->hasAttr<OverloadableAttr>());
|
Initial implementation of function overloading in C.
This commit adds a new attribute, "overloadable", that enables C++
function overloading in C. The attribute can only be added to function
declarations, e.g.,
int *f(int) __attribute__((overloadable));
If the "overloadable" attribute exists on a function with a given
name, *all* functions with that name (and in that scope) must have the
"overloadable" attribute. Sets of overloaded functions with the
"overloadable" attribute then follow the normal C++ rules for
overloaded functions, e.g., overloads must have different
parameter-type-lists from each other.
When calling an overloaded function in C, we follow the same
overloading rules as C++, with three extensions to the set of standard
conversions:
- A value of a given struct or union type T can be converted to the
type T. This is just the identity conversion. (In C++, this would
go through a copy constructor).
- A value of pointer type T* can be converted to a value of type U*
if T and U are compatible types. This conversion has Conversion
rank (it's considered a pointer conversion in C).
- A value of type T can be converted to a value of type U if T and U
are compatible (and are not both pointer types). This conversion
has Conversion rank (it's considered to be a new kind of
conversion unique to C, a "compatible" conversion).
Known defects (and, therefore, next steps):
1) The standard-conversion handling does not understand conversions
involving _Complex or vector extensions, so it is likely to get
these wrong. We need to add these conversions.
2) All overloadable functions with the same name will have the same
linkage name, which means we'll get a collision in the linker (if
not sooner). We'll need to mangle the names of these functions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64336 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-12 02:02:49 +03:00
|
|
|
}
|
|
|
|
|
2008-04-12 04:47:19 +04:00
|
|
|
/// Add this decl to the scope shadowed decl chains.
|
2009-09-01 02:39:49 +04:00
|
|
|
void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, bool AddToContext) {
|
2009-01-05 22:45:36 +03:00
|
|
|
// Move up the scope chain until we find the nearest enclosing
|
|
|
|
// non-transparent context. The declaration will be introduced into this
|
|
|
|
// scope.
|
2009-09-09 19:08:12 +04:00
|
|
|
while (S->getEntity() &&
|
2009-01-05 22:45:36 +03:00
|
|
|
((DeclContext *)S->getEntity())->isTransparentContext())
|
|
|
|
S = S->getParent();
|
|
|
|
|
2008-12-24 00:05:05 +03:00
|
|
|
// Add scoped declarations into their context, so that they can be
|
|
|
|
// found later. Declarations without a context won't be inserted
|
|
|
|
// into any context.
|
2009-09-01 02:39:49 +04:00
|
|
|
if (AddToContext)
|
|
|
|
CurContext->addDecl(D);
|
2008-12-24 00:05:05 +03:00
|
|
|
|
2010-02-21 10:08:09 +03:00
|
|
|
// Out-of-line definitions shouldn't be pushed into scope in C++.
|
|
|
|
// Out-of-line variable and function definitions shouldn't even in C.
|
|
|
|
if ((getLangOptions().CPlusPlus || isa<VarDecl>(D) || isa<FunctionDecl>(D)) &&
|
|
|
|
D->isOutOfLine())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Template instantiations should also not be pushed into scope.
|
|
|
|
if (isa<FunctionDecl>(D) &&
|
|
|
|
cast<FunctionDecl>(D)->isFunctionTemplateSpecialization())
|
2009-09-28 22:41:37 +04:00
|
|
|
return;
|
|
|
|
|
2009-10-10 01:13:30 +04:00
|
|
|
// If this replaces anything in the current scope,
|
|
|
|
IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()),
|
|
|
|
IEnd = IdResolver.end();
|
|
|
|
for (; I != IEnd; ++I) {
|
2010-08-21 13:40:31 +04:00
|
|
|
if (S->isDeclScope(*I) && D->declarationReplaces(*I)) {
|
|
|
|
S->RemoveDecl(*I);
|
2009-10-10 01:13:30 +04:00
|
|
|
IdResolver.RemoveDecl(*I);
|
2008-12-24 00:05:05 +03:00
|
|
|
|
2009-10-10 01:13:30 +04:00
|
|
|
// Should only need to replace one decl.
|
|
|
|
break;
|
2009-04-24 06:57:34 +04:00
|
|
|
}
|
2008-05-10 03:39:43 +04:00
|
|
|
}
|
2008-10-21 20:13:35 +04:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
S->AddDecl(D);
|
2008-05-10 03:39:43 +04:00
|
|
|
IdResolver.AddDecl(D);
|
2008-04-12 04:47:19 +04:00
|
|
|
}
|
|
|
|
|
2009-09-28 04:47:05 +04:00
|
|
|
bool Sema::isDeclInScope(NamedDecl *&D, DeclContext *Ctx, Scope *S) {
|
|
|
|
return IdResolver.isDeclInScope(D, Ctx, Context, S);
|
|
|
|
}
|
|
|
|
|
2010-08-24 12:50:51 +04:00
|
|
|
Scope *Sema::getScopeForDeclContext(Scope *S, DeclContext *DC) {
|
|
|
|
DeclContext *TargetDC = DC->getPrimaryContext();
|
|
|
|
do {
|
|
|
|
if (DeclContext *ScopeDC = (DeclContext*) S->getEntity())
|
|
|
|
if (ScopeDC->getPrimaryContext() == TargetDC)
|
|
|
|
return S;
|
|
|
|
} while ((S = S->getParent()));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
static bool isOutOfScopePreviousDeclaration(NamedDecl *,
|
|
|
|
DeclContext*,
|
|
|
|
ASTContext&);
|
|
|
|
|
|
|
|
/// Filters out lookup results that don't fall within the given scope
|
|
|
|
/// as determined by isDeclInScope.
|
|
|
|
static void FilterLookupForScope(Sema &SemaRef, LookupResult &R,
|
|
|
|
DeclContext *Ctx, Scope *S,
|
|
|
|
bool ConsiderLinkage) {
|
|
|
|
LookupResult::Filter F = R.makeFilter();
|
|
|
|
while (F.hasNext()) {
|
|
|
|
NamedDecl *D = F.next();
|
|
|
|
|
|
|
|
if (SemaRef.isDeclInScope(D, Ctx, S))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ConsiderLinkage &&
|
|
|
|
isOutOfScopePreviousDeclaration(D, Ctx, SemaRef.Context))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
F.erase();
|
|
|
|
}
|
|
|
|
|
|
|
|
F.done();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isUsingDecl(NamedDecl *D) {
|
|
|
|
return isa<UsingShadowDecl>(D) ||
|
|
|
|
isa<UnresolvedUsingTypenameDecl>(D) ||
|
|
|
|
isa<UnresolvedUsingValueDecl>(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes using shadow declarations from the lookup results.
|
|
|
|
static void RemoveUsingDecls(LookupResult &R) {
|
|
|
|
LookupResult::Filter F = R.makeFilter();
|
|
|
|
while (F.hasNext())
|
|
|
|
if (isUsingDecl(F.next()))
|
|
|
|
F.erase();
|
|
|
|
|
|
|
|
F.done();
|
|
|
|
}
|
|
|
|
|
2010-08-15 14:17:33 +04:00
|
|
|
/// \brief Check for this common pattern:
|
|
|
|
/// @code
|
|
|
|
/// class S {
|
|
|
|
/// S(const S&); // DO NOT IMPLEMENT
|
|
|
|
/// void operator=(const S&); // DO NOT IMPLEMENT
|
|
|
|
/// };
|
|
|
|
/// @endcode
|
|
|
|
static bool IsDisallowedCopyOrAssign(const CXXMethodDecl *D) {
|
|
|
|
// FIXME: Should check for private access too but access is set after we get
|
|
|
|
// the decl here.
|
|
|
|
if (D->isThisDeclarationADefinition())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D))
|
|
|
|
return CD->isCopyConstructor();
|
2010-09-28 02:06:20 +04:00
|
|
|
if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
|
|
|
|
return Method->isCopyAssignmentOperator();
|
|
|
|
return false;
|
2010-08-15 14:17:33 +04:00
|
|
|
}
|
|
|
|
|
2010-08-15 05:15:20 +04:00
|
|
|
bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
|
|
|
|
assert(D);
|
|
|
|
|
|
|
|
if (D->isInvalidDecl() || D->isUsed() || D->hasAttr<UnusedAttr>())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Ignore class templates.
|
|
|
|
if (D->getDeclContext()->isDependentContext())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We warn for unused decls internal to the translation unit.
|
|
|
|
if (D->getLinkage() == ExternalLinkage)
|
|
|
|
return false;
|
|
|
|
|
2010-08-13 22:42:17 +04:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2010-08-15 14:17:33 +04:00
|
|
|
if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
|
|
|
|
return false;
|
2010-08-15 05:15:20 +04:00
|
|
|
|
2010-08-15 14:17:33 +04:00
|
|
|
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
|
|
|
|
if (MD->isVirtual() || IsDisallowedCopyOrAssign(MD))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// 'static inline' functions are used in headers; don't warn.
|
2010-08-26 07:08:43 +04:00
|
|
|
if (FD->getStorageClass() == SC_Static &&
|
2010-08-15 14:17:33 +04:00
|
|
|
FD->isInlineSpecified())
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-15 05:15:20 +04:00
|
|
|
|
|
|
|
if (FD->isThisDeclarationADefinition())
|
|
|
|
return !Context.DeclMustBeEmitted(FD);
|
|
|
|
return true;
|
2010-08-15 14:17:33 +04:00
|
|
|
}
|
2010-08-15 05:15:20 +04:00
|
|
|
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
if (VD->isStaticDataMember() &&
|
|
|
|
VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( VD->isFileVarDecl() &&
|
|
|
|
!VD->getType().isConstant(Context))
|
|
|
|
return !Context.DeclMustBeEmitted(VD);
|
2010-08-13 22:42:40 +04:00
|
|
|
}
|
|
|
|
|
2010-08-15 05:15:20 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::MarkUnusedFileScopedDecl(const DeclaratorDecl *D) {
|
|
|
|
if (!D)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
const FunctionDecl *First = FD->getFirstDeclaration();
|
|
|
|
if (FD != First && ShouldWarnIfUnusedFileScopedDecl(First))
|
|
|
|
return; // First should already be in the vector.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
const VarDecl *First = VD->getFirstDeclaration();
|
|
|
|
if (VD != First && ShouldWarnIfUnusedFileScopedDecl(First))
|
|
|
|
return; // First should already be in the vector.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ShouldWarnIfUnusedFileScopedDecl(D))
|
|
|
|
UnusedFileScopedDecls.push_back(D);
|
|
|
|
}
|
2010-08-13 22:42:17 +04:00
|
|
|
|
2009-11-07 10:18:14 +03:00
|
|
|
static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) {
|
2010-02-05 01:26:26 +03:00
|
|
|
if (D->isInvalidDecl())
|
|
|
|
return false;
|
|
|
|
|
2009-11-07 10:26:56 +03:00
|
|
|
if (D->isUsed() || D->hasAttr<UnusedAttr>())
|
|
|
|
return false;
|
2010-02-05 01:26:26 +03:00
|
|
|
|
|
|
|
// White-list anything that isn't a local variable.
|
|
|
|
if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D) ||
|
|
|
|
!D->getDeclContext()->isFunctionOrMethod())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Types of valid local variables should be complete, so this should succeed.
|
2009-11-07 10:26:56 +03:00
|
|
|
if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
|
2010-03-31 06:47:45 +04:00
|
|
|
|
|
|
|
// White-list anything with an __attribute__((unused)) type.
|
|
|
|
QualType Ty = VD->getType();
|
|
|
|
|
|
|
|
// Only look at the outermost level of typedef.
|
|
|
|
if (const TypedefType *TT = dyn_cast<TypedefType>(Ty)) {
|
|
|
|
if (TT->getDecl()->hasAttr<UnusedAttr>())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-09 03:05:03 +04:00
|
|
|
// If we failed to complete the type for some reason, or if the type is
|
|
|
|
// dependent, don't diagnose the variable.
|
|
|
|
if (Ty->isIncompleteType() || Ty->isDependentType())
|
2010-04-27 20:20:13 +04:00
|
|
|
return false;
|
|
|
|
|
2010-03-31 06:47:45 +04:00
|
|
|
if (const TagType *TT = Ty->getAs<TagType>()) {
|
|
|
|
const TagDecl *Tag = TT->getDecl();
|
|
|
|
if (Tag->hasAttr<UnusedAttr>())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) {
|
2010-05-09 03:05:03 +04:00
|
|
|
// FIXME: Checking for the presence of a user-declared constructor
|
|
|
|
// isn't completely accurate; we'd prefer to check that the initializer
|
|
|
|
// has no side effects.
|
|
|
|
if (RD->hasUserDeclaredConstructor() || !RD->hasTrivialDestructor())
|
2009-11-07 10:26:56 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-03-31 06:47:45 +04:00
|
|
|
|
|
|
|
// TODO: __attribute__((unused)) templates?
|
2009-11-07 10:26:56 +03:00
|
|
|
}
|
|
|
|
|
2010-02-05 01:26:26 +03:00
|
|
|
return true;
|
2009-11-07 10:18:14 +03:00
|
|
|
}
|
|
|
|
|
2010-05-09 03:05:03 +04:00
|
|
|
void Sema::DiagnoseUnusedDecl(const NamedDecl *D) {
|
|
|
|
if (!ShouldDiagnoseUnusedDecl(D))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (isa<VarDecl>(D) && cast<VarDecl>(D)->isExceptionVariable())
|
|
|
|
Diag(D->getLocation(), diag::warn_unused_exception_param)
|
|
|
|
<< D->getDeclName();
|
|
|
|
else
|
|
|
|
Diag(D->getLocation(), diag::warn_unused_variable)
|
|
|
|
<< D->getDeclName();
|
|
|
|
}
|
|
|
|
|
2007-10-10 02:01:59 +04:00
|
|
|
void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
|
2007-08-26 10:24:45 +04:00
|
|
|
if (S->decl_empty()) return;
|
2008-12-05 21:15:24 +03:00
|
|
|
assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) &&
|
2009-09-09 19:08:12 +04:00
|
|
|
"Scope shouldn't contain decls!");
|
2008-05-10 03:39:43 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
|
|
|
|
I != E; ++I) {
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *TmpD = (*I);
|
2007-09-13 22:10:37 +04:00
|
|
|
assert(TmpD && "This decl didn't get pushed??");
|
2008-06-10 05:32:09 +04:00
|
|
|
|
2008-12-11 19:49:14 +03:00
|
|
|
assert(isa<NamedDecl>(TmpD) && "Decl isn't NamedDecl?");
|
|
|
|
NamedDecl *D = cast<NamedDecl>(TmpD);
|
2008-06-10 05:32:09 +04:00
|
|
|
|
2008-12-11 19:49:14 +03:00
|
|
|
if (!D->getDeclName()) continue;
|
|
|
|
|
2009-10-09 01:35:42 +04:00
|
|
|
// Diagnose unused variables in this scope.
|
2010-05-09 03:05:03 +04:00
|
|
|
if (S->getNumErrorsAtStart() == getDiagnostics().getNumErrors())
|
|
|
|
DiagnoseUnusedDecl(D);
|
|
|
|
|
2008-12-11 19:49:14 +03:00
|
|
|
// Remove this name from our lexical scope.
|
|
|
|
IdResolver.RemoveDecl(D);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-11 16:19:30 +04:00
|
|
|
/// \brief Look for an Objective-C class in the translation unit.
|
|
|
|
///
|
|
|
|
/// \param Id The name of the Objective-C class we're looking for. If
|
|
|
|
/// typo-correction fixes this name, the Id will be updated
|
|
|
|
/// to the fixed name.
|
|
|
|
///
|
|
|
|
/// \param IdLoc The location of the name in the translation unit.
|
|
|
|
///
|
|
|
|
/// \param TypoCorrection If true, this routine will attempt typo correction
|
|
|
|
/// if there is no class with the given name.
|
|
|
|
///
|
|
|
|
/// \returns The declaration of the named Objective-C class, or NULL if the
|
|
|
|
/// class could not be found.
|
|
|
|
ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *&Id,
|
|
|
|
SourceLocation IdLoc,
|
|
|
|
bool TypoCorrection) {
|
|
|
|
// The third "scope" argument is 0 since we aren't enabling lazy built-in
|
|
|
|
// creation from this context.
|
|
|
|
NamedDecl *IDecl = LookupSingleName(TUScope, Id, IdLoc, LookupOrdinaryName);
|
|
|
|
|
|
|
|
if (!IDecl && TypoCorrection) {
|
|
|
|
// Perform typo correction at the given location, but only if we
|
|
|
|
// find an Objective-C class name.
|
|
|
|
LookupResult R(*this, Id, IdLoc, LookupOrdinaryName);
|
|
|
|
if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
|
|
|
|
(IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
|
|
|
|
Diag(IdLoc, diag::err_undef_interface_suggest)
|
|
|
|
<< Id << IDecl->getDeclName()
|
|
|
|
<< FixItHint::CreateReplacement(IdLoc, IDecl->getNameAsString());
|
|
|
|
Diag(IDecl->getLocation(), diag::note_previous_decl)
|
|
|
|
<< IDecl->getDeclName();
|
|
|
|
|
|
|
|
Id = IDecl->getIdentifier();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dyn_cast_or_null<ObjCInterfaceDecl>(IDecl);
|
|
|
|
}
|
|
|
|
|
2009-01-12 21:45:55 +03:00
|
|
|
/// getNonFieldDeclScope - Retrieves the innermost scope, starting
|
|
|
|
/// from S, where a non-field would be declared. This routine copes
|
|
|
|
/// with the difference between C and C++ scoping rules in structs and
|
|
|
|
/// unions. For example, the following code is well-formed in C but
|
|
|
|
/// ill-formed in C++:
|
|
|
|
/// @code
|
|
|
|
/// struct S6 {
|
|
|
|
/// enum { BAR } e;
|
|
|
|
/// };
|
2009-09-09 19:08:12 +04:00
|
|
|
///
|
2009-01-12 21:45:55 +03:00
|
|
|
/// void test_S6() {
|
|
|
|
/// struct S6 a;
|
|
|
|
/// a.e = BAR;
|
|
|
|
/// }
|
|
|
|
/// @endcode
|
|
|
|
/// For the declaration of BAR, this routine will return a different
|
|
|
|
/// scope. The scope S will be the scope of the unnamed enumeration
|
|
|
|
/// within S6. In C++, this routine will return the scope associated
|
|
|
|
/// with S6, because the enumeration's scope is a transparent
|
|
|
|
/// context but structures can contain non-field names. In C, this
|
|
|
|
/// routine will return the translation unit scope, since the
|
|
|
|
/// enumeration's scope is a transparent context and structures cannot
|
|
|
|
/// contain non-field names.
|
|
|
|
Scope *Sema::getNonFieldDeclScope(Scope *S) {
|
|
|
|
while (((S->getFlags() & Scope::DeclScope) == 0) ||
|
2009-09-09 19:08:12 +04:00
|
|
|
(S->getEntity() &&
|
2009-01-12 21:45:55 +03:00
|
|
|
((DeclContext *)S->getEntity())->isTransparentContext()) ||
|
|
|
|
(S->isClassScope() && !getLangOptions().CPlusPlus))
|
|
|
|
S = S->getParent();
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64504 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-14 02:20:09 +03:00
|
|
|
/// LazilyCreateBuiltin - The specified Builtin-ID was first used at
|
|
|
|
/// file scope. lazily create a decl for it. ForRedeclaration is true
|
|
|
|
/// if we're creating this built-in in anticipation of redeclaring the
|
|
|
|
/// built-in.
|
2009-01-20 04:17:11 +03:00
|
|
|
NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64504 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-14 02:20:09 +03:00
|
|
|
Scope *S, bool ForRedeclaration,
|
|
|
|
SourceLocation Loc) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Builtin::ID BID = (Builtin::ID)bid;
|
|
|
|
|
2009-06-14 04:45:47 +04:00
|
|
|
ASTContext::GetBuiltinTypeError Error;
|
2009-09-09 19:08:12 +04:00
|
|
|
QualType R = Context.GetBuiltinType(BID, Error);
|
2009-02-14 04:52:53 +03:00
|
|
|
switch (Error) {
|
2009-06-14 04:45:47 +04:00
|
|
|
case ASTContext::GE_None:
|
2009-02-14 04:52:53 +03:00
|
|
|
// Okay
|
|
|
|
break;
|
|
|
|
|
2009-07-29 03:57:15 +04:00
|
|
|
case ASTContext::GE_Missing_stdio:
|
2009-02-14 04:52:53 +03:00
|
|
|
if (ForRedeclaration)
|
|
|
|
Diag(Loc, diag::err_implicit_decl_requires_stdio)
|
|
|
|
<< Context.BuiltinInfo.GetName(BID);
|
|
|
|
return 0;
|
2009-07-28 06:25:19 +04:00
|
|
|
|
2009-07-29 03:57:15 +04:00
|
|
|
case ASTContext::GE_Missing_setjmp:
|
2009-07-28 06:25:19 +04:00
|
|
|
if (ForRedeclaration)
|
|
|
|
Diag(Loc, diag::err_implicit_decl_requires_setjmp)
|
|
|
|
<< Context.BuiltinInfo.GetName(BID);
|
|
|
|
return 0;
|
2009-02-14 04:52:53 +03:00
|
|
|
}
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64504 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-14 02:20:09 +03:00
|
|
|
|
|
|
|
if (!ForRedeclaration && Context.BuiltinInfo.isPredefinedLibFunction(BID)) {
|
|
|
|
Diag(Loc, diag::ext_implicit_lib_function_decl)
|
|
|
|
<< Context.BuiltinInfo.GetName(BID)
|
|
|
|
<< R;
|
2009-02-17 00:58:21 +03:00
|
|
|
if (Context.BuiltinInfo.getHeaderName(BID) &&
|
2009-04-16 07:59:32 +04:00
|
|
|
Diags.getDiagnosticLevel(diag::ext_implicit_lib_function_decl)
|
|
|
|
!= Diagnostic::Ignored)
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64504 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-14 02:20:09 +03:00
|
|
|
Diag(Loc, diag::note_please_include_header)
|
|
|
|
<< Context.BuiltinInfo.getHeaderName(BID)
|
|
|
|
<< Context.BuiltinInfo.GetName(BID);
|
|
|
|
}
|
|
|
|
|
2008-04-17 18:47:13 +04:00
|
|
|
FunctionDecl *New = FunctionDecl::Create(Context,
|
|
|
|
Context.getTranslationUnitDecl(),
|
2009-12-07 05:54:59 +03:00
|
|
|
Loc, II, R, /*TInfo=*/0,
|
2010-08-26 07:08:43 +04:00
|
|
|
SC_Extern,
|
|
|
|
SC_None, false,
|
2009-02-25 19:33:18 +03:00
|
|
|
/*hasPrototype=*/true);
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64504 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-14 02:20:09 +03:00
|
|
|
New->setImplicit();
|
|
|
|
|
2008-05-06 02:18:14 +04:00
|
|
|
// Create Decl objects for each parameter, adding them to the
|
|
|
|
// FunctionDecl.
|
2009-02-27 02:50:07 +03:00
|
|
|
if (FunctionProtoType *FT = dyn_cast<FunctionProtoType>(R)) {
|
2008-05-06 02:18:14 +04:00
|
|
|
llvm::SmallVector<ParmVarDecl*, 16> Params;
|
|
|
|
for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i)
|
|
|
|
Params.push_back(ParmVarDecl::Create(Context, New, SourceLocation(), 0,
|
2009-12-07 05:54:59 +03:00
|
|
|
FT->getArgType(i), /*TInfo=*/0,
|
2010-08-26 07:08:43 +04:00
|
|
|
SC_None, SC_None, 0));
|
2010-02-11 04:19:42 +03:00
|
|
|
New->setParams(Params.data(), Params.size());
|
2008-05-06 02:18:14 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
AddKnownFunctionAttributes(New);
|
|
|
|
|
2008-04-11 11:00:53 +04:00
|
|
|
// TUScope is the translation-unit scope to insert this function into.
|
2009-01-09 21:51:29 +03:00
|
|
|
// FIXME: This is hideous. We need to teach PushOnScopeChains to
|
|
|
|
// relate Scopes to DeclContexts, and probably eliminate CurContext
|
|
|
|
// entirely, but we're not there yet.
|
|
|
|
DeclContext *SavedContext = CurContext;
|
|
|
|
CurContext = Context.getTranslationUnitDecl();
|
2008-05-10 03:39:43 +04:00
|
|
|
PushOnScopeChains(New, TUScope);
|
2009-01-09 21:51:29 +03:00
|
|
|
CurContext = SavedContext;
|
2007-07-11 21:01:13 +04:00
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2009-02-16 20:45:42 +03:00
|
|
|
/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the
|
|
|
|
/// same name and scope as a previous declaration 'Old'. Figure out
|
|
|
|
/// how to resolve this situation, merging decls or emitting
|
2009-04-25 12:06:05 +04:00
|
|
|
/// diagnostics as appropriate. If there was an error, set New to be invalid.
|
2007-07-11 21:01:13 +04:00
|
|
|
///
|
2009-11-19 01:49:29 +03:00
|
|
|
void Sema::MergeTypeDefDecl(TypedefDecl *New, LookupResult &OldDecls) {
|
|
|
|
// If the new decl is known invalid already, don't bother doing any
|
|
|
|
// merging checks.
|
|
|
|
if (New->isInvalidDecl()) return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-09-09 18:32:20 +04:00
|
|
|
// Allow multiple definitions for ObjC built-in typedefs.
|
|
|
|
// FIXME: Verify the underlying types are equivalent!
|
|
|
|
if (getLangOptions().ObjC1) {
|
2008-11-20 08:41:43 +03:00
|
|
|
const IdentifierInfo *TypeID = New->getIdentifier();
|
|
|
|
switch (TypeID->getLength()) {
|
|
|
|
default: break;
|
2009-09-09 19:08:12 +04:00
|
|
|
case 2:
|
2008-11-20 08:41:43 +03:00
|
|
|
if (!TypeID->isStr("id"))
|
|
|
|
break;
|
2009-08-17 20:35:33 +04:00
|
|
|
Context.ObjCIdRedefinitionType = New->getUnderlyingType();
|
2009-07-11 03:34:53 +04:00
|
|
|
// Install the built-in type for 'id', ignoring the current definition.
|
|
|
|
New->setTypeForDecl(Context.getObjCIdType().getTypePtr());
|
|
|
|
return;
|
2008-11-20 08:41:43 +03:00
|
|
|
case 5:
|
|
|
|
if (!TypeID->isStr("Class"))
|
|
|
|
break;
|
2009-08-17 20:35:33 +04:00
|
|
|
Context.ObjCClassRedefinitionType = New->getUnderlyingType();
|
2009-07-11 03:34:53 +04:00
|
|
|
// Install the built-in type for 'Class', ignoring the current definition.
|
|
|
|
New->setTypeForDecl(Context.getObjCClassType().getTypePtr());
|
2009-04-25 12:06:05 +04:00
|
|
|
return;
|
2008-11-20 08:41:43 +03:00
|
|
|
case 3:
|
|
|
|
if (!TypeID->isStr("SEL"))
|
|
|
|
break;
|
2009-11-26 02:07:42 +03:00
|
|
|
Context.ObjCSelRedefinitionType = New->getUnderlyingType();
|
2009-11-21 22:53:08 +03:00
|
|
|
// Install the built-in type for 'SEL', ignoring the current definition.
|
|
|
|
New->setTypeForDecl(Context.getObjCSelType().getTypePtr());
|
2009-04-25 12:06:05 +04:00
|
|
|
return;
|
2008-11-20 08:41:43 +03:00
|
|
|
case 8:
|
|
|
|
if (!TypeID->isStr("Protocol"))
|
|
|
|
break;
|
2008-09-09 18:32:20 +04:00
|
|
|
Context.setObjCProtoType(New->getUnderlyingType());
|
2009-04-25 12:06:05 +04:00
|
|
|
return;
|
2008-09-09 18:32:20 +04:00
|
|
|
}
|
|
|
|
// Fall through - the typedef name was not a builtin type.
|
|
|
|
}
|
2009-11-19 01:49:29 +03:00
|
|
|
|
2009-01-28 20:15:10 +03:00
|
|
|
// Verify the old decl was also a type.
|
2009-12-30 03:31:22 +03:00
|
|
|
TypeDecl *Old = OldDecls.getAsSingle<TypeDecl>();
|
|
|
|
if (!Old) {
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(New->getLocation(), diag::err_redefinition_different_kind)
|
2008-11-24 00:45:46 +03:00
|
|
|
<< New->getDeclName();
|
2009-11-19 01:49:29 +03:00
|
|
|
|
|
|
|
NamedDecl *OldD = OldDecls.getRepresentativeDecl();
|
2009-04-25 12:06:05 +04:00
|
|
|
if (OldD->getLocation().isValid())
|
2009-01-16 22:58:32 +03:00
|
|
|
Diag(OldD->getLocation(), diag::note_previous_definition);
|
2009-11-19 01:49:29 +03:00
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
return New->setInvalidDecl();
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2009-01-28 20:15:10 +03:00
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
// If the old declaration is invalid, just give up here.
|
|
|
|
if (Old->isInvalidDecl())
|
|
|
|
return New->setInvalidDecl();
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
// Determine the "old" type we'll use for checking and diagnostics.
|
2009-01-28 20:15:10 +03:00
|
|
|
QualType OldType;
|
|
|
|
if (TypedefDecl *OldTypedef = dyn_cast<TypedefDecl>(Old))
|
|
|
|
OldType = OldTypedef->getUnderlyingType();
|
|
|
|
else
|
|
|
|
OldType = Context.getTypeDeclType(Old);
|
|
|
|
|
2008-07-25 22:44:27 +04:00
|
|
|
// If the typedef types are not identical, reject them in all languages and
|
|
|
|
// with any extensions enabled.
|
2009-01-28 20:15:10 +03:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
if (OldType != New->getUnderlyingType() &&
|
|
|
|
Context.getCanonicalType(OldType) !=
|
2008-07-25 22:44:27 +04:00
|
|
|
Context.getCanonicalType(New->getUnderlyingType())) {
|
2008-11-20 09:13:02 +03:00
|
|
|
Diag(New->getLocation(), diag::err_redefinition_different_typedef)
|
2009-01-28 20:15:10 +03:00
|
|
|
<< New->getUnderlyingType() << OldType;
|
2009-04-25 12:06:05 +04:00
|
|
|
if (Old->getLocation().isValid())
|
2009-01-16 22:58:32 +03:00
|
|
|
Diag(Old->getLocation(), diag::note_previous_definition);
|
2009-04-25 12:06:05 +04:00
|
|
|
return New->setInvalidDecl();
|
2008-07-25 22:44:27 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-12-30 03:31:22 +03:00
|
|
|
// The types match. Link up the redeclaration chain if the old
|
|
|
|
// declaration was a typedef.
|
|
|
|
// FIXME: this is a potential source of wierdness if the type
|
|
|
|
// spellings don't match exactly.
|
|
|
|
if (isa<TypedefDecl>(Old))
|
|
|
|
New->setPreviousDeclaration(cast<TypedefDecl>(Old));
|
|
|
|
|
2009-07-11 03:34:53 +04:00
|
|
|
if (getLangOptions().Microsoft)
|
2009-04-25 12:06:05 +04:00
|
|
|
return;
|
2008-06-11 10:20:39 +04:00
|
|
|
|
2009-04-18 02:04:20 +04:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
2010-01-12 00:54:40 +03:00
|
|
|
// C++ [dcl.typedef]p2:
|
|
|
|
// In a given non-class scope, a typedef specifier can be used to
|
|
|
|
// redefine the name of any type declared in that scope to refer
|
|
|
|
// to the type to which it already refers.
|
2009-04-18 02:04:20 +04:00
|
|
|
if (!isa<CXXRecordDecl>(CurContext))
|
2009-04-25 12:06:05 +04:00
|
|
|
return;
|
2010-01-12 00:54:40 +03:00
|
|
|
|
|
|
|
// C++0x [dcl.typedef]p4:
|
|
|
|
// In a given class scope, a typedef specifier can be used to redefine
|
|
|
|
// any class-name declared in that scope that is not also a typedef-name
|
|
|
|
// to refer to the type to which it already refers.
|
|
|
|
//
|
|
|
|
// This wording came in via DR424, which was a correction to the
|
|
|
|
// wording in DR56, which accidentally banned code like:
|
|
|
|
//
|
|
|
|
// struct S {
|
|
|
|
// typedef struct A { } A;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// in the C++03 standard. We implement the C++0x semantics, which
|
|
|
|
// allow the above but disallow
|
|
|
|
//
|
|
|
|
// struct S {
|
|
|
|
// typedef int I;
|
|
|
|
// typedef int I;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// since that was the intent of DR56.
|
2010-01-12 01:30:10 +03:00
|
|
|
if (!isa<TypedefDecl >(Old))
|
2010-01-12 00:54:40 +03:00
|
|
|
return;
|
|
|
|
|
2009-04-18 02:04:20 +04:00
|
|
|
Diag(New->getLocation(), diag::err_redefinition)
|
|
|
|
<< New->getDeclName();
|
|
|
|
Diag(Old->getLocation(), diag::note_previous_definition);
|
2009-04-25 12:06:05 +04:00
|
|
|
return New->setInvalidDecl();
|
2008-09-12 22:10:20 +04:00
|
|
|
}
|
2008-06-11 10:20:39 +04:00
|
|
|
|
2009-04-18 02:04:20 +04:00
|
|
|
// If we have a redefinition of a typedef in C, emit a warning. This warning
|
|
|
|
// is normally mapped to an error, but can be controlled with
|
2009-06-05 03:03:07 +04:00
|
|
|
// -Wtypedef-redefinition. If either the original or the redefinition is
|
|
|
|
// in a system header, don't emit this for compatibility with GCC.
|
2010-03-01 23:59:53 +03:00
|
|
|
if (getDiagnostics().getSuppressSystemWarnings() &&
|
2009-06-05 03:03:07 +04:00
|
|
|
(Context.getSourceManager().isInSystemHeader(Old->getLocation()) ||
|
|
|
|
Context.getSourceManager().isInSystemHeader(New->getLocation())))
|
2009-04-27 05:46:12 +04:00
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-18 02:04:20 +04:00
|
|
|
Diag(New->getLocation(), diag::warn_redefinition_of_typedef)
|
|
|
|
<< New->getDeclName();
|
2008-11-24 02:12:31 +03:00
|
|
|
Diag(Old->getLocation(), diag::note_previous_definition);
|
2009-04-25 12:06:05 +04:00
|
|
|
return;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-06-26 22:38:35 +04:00
|
|
|
/// DeclhasAttr - returns true if decl Declaration already has the target
|
|
|
|
/// attribute.
|
2009-09-09 19:08:12 +04:00
|
|
|
static bool
|
2010-08-19 03:23:40 +04:00
|
|
|
DeclHasAttr(const Decl *D, const Attr *A) {
|
|
|
|
const OwnershipAttr *OA = dyn_cast<OwnershipAttr>(A);
|
|
|
|
for (Decl::attr_iterator i = D->attr_begin(), e = D->attr_end(); i != e; ++i)
|
|
|
|
if ((*i)->getKind() == A->getKind()) {
|
|
|
|
// FIXME: Don't hardcode this check
|
|
|
|
if (OA && isa<OwnershipAttr>(*i))
|
|
|
|
return OA->getOwnKind() == cast<OwnershipAttr>(*i)->getOwnKind();
|
2008-03-03 06:28:21 +03:00
|
|
|
return true;
|
2010-08-19 03:23:40 +04:00
|
|
|
}
|
2008-03-03 06:28:21 +03:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-19 03:23:40 +04:00
|
|
|
/// MergeDeclAttributes - append attributes from the Old decl to the New one.
|
|
|
|
static void MergeDeclAttributes(Decl *New, Decl *Old, ASTContext &C) {
|
|
|
|
if (!Old->hasAttrs())
|
|
|
|
return;
|
|
|
|
// Ensure that any moving of objects within the allocated map is done before
|
|
|
|
// we process them.
|
|
|
|
if (!New->hasAttrs())
|
|
|
|
New->setAttrs(AttrVec());
|
|
|
|
for (Decl::attr_iterator i = Old->attr_begin(), e = Old->attr_end(); i != e;
|
|
|
|
++i) {
|
|
|
|
// FIXME: Make this more general than just checking for Overloadable.
|
|
|
|
if (!DeclHasAttr(New, *i) && (*i)->getKind() != attr::Overloadable) {
|
|
|
|
Attr *NewAttr = (*i)->clone(C);
|
2009-04-28 10:37:30 +04:00
|
|
|
NewAttr->setInherited(true);
|
2009-06-30 06:34:44 +04:00
|
|
|
New->addAttr(NewAttr);
|
2008-03-03 06:28:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-27 01:25:24 +04:00
|
|
|
namespace {
|
|
|
|
|
2009-03-07 01:43:54 +03:00
|
|
|
/// Used in MergeFunctionDecl to keep track of function parameters in
|
|
|
|
/// C.
|
|
|
|
struct GNUCompatibleParamWarning {
|
|
|
|
ParmVarDecl *OldParm;
|
|
|
|
ParmVarDecl *NewParm;
|
|
|
|
QualType PromotedType;
|
|
|
|
};
|
|
|
|
|
2010-07-27 01:25:24 +04:00
|
|
|
}
|
2009-12-05 01:33:25 +03:00
|
|
|
|
|
|
|
/// getSpecialMember - get the special member enum for a method.
|
2010-04-22 09:40:53 +04:00
|
|
|
Sema::CXXSpecialMember Sema::getSpecialMember(const CXXMethodDecl *MD) {
|
2009-12-05 01:33:25 +03:00
|
|
|
if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) {
|
2009-12-22 03:34:07 +03:00
|
|
|
if (Ctor->isCopyConstructor())
|
2009-12-05 01:33:25 +03:00
|
|
|
return Sema::CXXCopyConstructor;
|
2010-04-22 09:40:53 +04:00
|
|
|
|
|
|
|
return Sema::CXXConstructor;
|
2009-12-05 01:33:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<CXXDestructorDecl>(MD))
|
|
|
|
return Sema::CXXDestructor;
|
|
|
|
|
2010-09-28 02:37:28 +04:00
|
|
|
assert(MD->isCopyAssignmentOperator() &&
|
|
|
|
"Must have copy assignment operator");
|
2009-12-05 01:33:25 +03:00
|
|
|
return Sema::CXXCopyAssignment;
|
|
|
|
}
|
|
|
|
|
2010-06-10 01:17:41 +04:00
|
|
|
/// canRedefineFunction - checks if a function can be redefined. Currently,
|
2010-02-18 05:00:42 +03:00
|
|
|
/// only extern inline functions can be redefined, and even then only in
|
|
|
|
/// GNU89 mode.
|
|
|
|
static bool canRedefineFunction(const FunctionDecl *FD,
|
|
|
|
const LangOptions& LangOpts) {
|
|
|
|
return (LangOpts.GNUMode && !LangOpts.C99 && !LangOpts.CPlusPlus &&
|
|
|
|
FD->isInlineSpecified() &&
|
2010-08-26 07:08:43 +04:00
|
|
|
FD->getStorageClass() == SC_Extern);
|
2010-02-18 05:00:42 +03:00
|
|
|
}
|
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
/// MergeFunctionDecl - We just parsed a function 'New' from
|
|
|
|
/// declarator D which has the same name and scope as a previous
|
|
|
|
/// declaration 'Old'. Figure out how to resolve this situation,
|
|
|
|
/// merging decls or emitting diagnostics as appropriate.
|
2008-10-21 20:13:35 +04:00
|
|
|
///
|
|
|
|
/// In C++, New and Old must be declarations that are not
|
|
|
|
/// overloaded. Use IsOverload to determine whether New and Old are
|
|
|
|
/// overloaded, and to select the Old declaration that New should be
|
|
|
|
/// merged with.
|
2009-02-16 20:45:42 +03:00
|
|
|
///
|
|
|
|
/// Returns true if there was an error, false otherwise.
|
|
|
|
bool Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// Verify the old decl was also a function.
|
2009-06-26 02:08:12 +04:00
|
|
|
FunctionDecl *Old = 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
if (FunctionTemplateDecl *OldFunctionTemplate
|
2009-06-26 02:08:12 +04:00
|
|
|
= dyn_cast<FunctionTemplateDecl>(OldD))
|
|
|
|
Old = OldFunctionTemplate->getTemplatedDecl();
|
2009-09-09 19:08:12 +04:00
|
|
|
else
|
2009-06-26 02:08:12 +04:00
|
|
|
Old = dyn_cast<FunctionDecl>(OldD);
|
2007-07-11 21:01:13 +04:00
|
|
|
if (!Old) {
|
2009-12-10 22:51:03 +03:00
|
|
|
if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(OldD)) {
|
|
|
|
Diag(New->getLocation(), diag::err_using_decl_conflict_reverse);
|
|
|
|
Diag(Shadow->getTargetDecl()->getLocation(),
|
|
|
|
diag::note_using_decl_target);
|
|
|
|
Diag(Shadow->getUsingDecl()->getLocation(),
|
|
|
|
diag::note_using_decl) << 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-20 09:13:02 +03:00
|
|
|
Diag(New->getLocation(), diag::err_redefinition_different_kind)
|
2008-11-24 00:45:46 +03:00
|
|
|
<< New->getDeclName();
|
2008-11-24 02:12:31 +03:00
|
|
|
Diag(OldD->getLocation(), diag::note_previous_definition);
|
2009-02-16 20:45:42 +03:00
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-10-21 20:13:35 +04:00
|
|
|
|
|
|
|
// Determine whether the previous declaration was a definition,
|
|
|
|
// implicit declaration, or a declaration.
|
|
|
|
diag::kind PrevDiag;
|
|
|
|
if (Old->isThisDeclarationADefinition())
|
2008-11-24 02:12:31 +03:00
|
|
|
PrevDiag = diag::note_previous_definition;
|
2009-02-16 20:45:42 +03:00
|
|
|
else if (Old->isImplicit())
|
|
|
|
PrevDiag = diag::note_previous_implicit_declaration;
|
2009-09-09 19:08:12 +04:00
|
|
|
else
|
2008-11-24 02:12:31 +03:00
|
|
|
PrevDiag = diag::note_previous_declaration;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-04-07 03:10:54 +04:00
|
|
|
QualType OldQType = Context.getCanonicalType(Old->getType());
|
|
|
|
QualType NewQType = Context.getCanonicalType(New->getType());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-02-18 05:00:42 +03:00
|
|
|
// Don't complain about this if we're in GNU89 mode and the old function
|
|
|
|
// is an extern inline function.
|
2009-02-24 04:23:02 +03:00
|
|
|
if (!isa<CXXMethodDecl>(New) && !isa<CXXMethodDecl>(Old) &&
|
2010-08-26 07:08:43 +04:00
|
|
|
New->getStorageClass() == SC_Static &&
|
|
|
|
Old->getStorageClass() != SC_Static &&
|
2010-02-18 05:00:42 +03:00
|
|
|
!canRedefineFunction(Old, getLangOptions())) {
|
2009-02-24 04:23:02 +03:00
|
|
|
Diag(New->getLocation(), diag::err_static_non_static)
|
|
|
|
<< New;
|
|
|
|
Diag(Old->getLocation(), PrevDiag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-04 08:44:44 +03:00
|
|
|
// If a function is first declared with a calling convention, but is
|
|
|
|
// later declared or defined without one, the second decl assumes the
|
|
|
|
// calling convention of the first.
|
|
|
|
//
|
|
|
|
// For the new decl, we have to look at the NON-canonical type to tell the
|
|
|
|
// difference between a function that really doesn't have a calling
|
|
|
|
// convention and one that is declared cdecl. That's because in
|
|
|
|
// canonicalization (see ASTContext.cpp), cdecl is canonicalized away
|
|
|
|
// because it is the default calling convention.
|
|
|
|
//
|
|
|
|
// Note also that we DO NOT return at this point, because we still have
|
|
|
|
// other tests to run.
|
|
|
|
const FunctionType *OldType = OldQType->getAs<FunctionType>();
|
|
|
|
const FunctionType *NewType = New->getType()->getAs<FunctionType>();
|
2010-03-31 00:24:48 +04:00
|
|
|
const FunctionType::ExtInfo OldTypeInfo = OldType->getExtInfo();
|
|
|
|
const FunctionType::ExtInfo NewTypeInfo = NewType->getExtInfo();
|
|
|
|
if (OldTypeInfo.getCC() != CC_Default &&
|
|
|
|
NewTypeInfo.getCC() == CC_Default) {
|
|
|
|
NewQType = Context.getCallConvType(NewQType, OldTypeInfo.getCC());
|
2010-02-04 08:44:44 +03:00
|
|
|
New->setType(NewQType);
|
|
|
|
NewQType = Context.getCanonicalType(NewQType);
|
2010-03-31 00:24:48 +04:00
|
|
|
} else if (!Context.isSameCallConv(OldTypeInfo.getCC(),
|
|
|
|
NewTypeInfo.getCC())) {
|
2010-02-04 08:44:44 +03:00
|
|
|
// Calling conventions really aren't compatible, so complain.
|
2010-02-06 00:31:56 +03:00
|
|
|
Diag(New->getLocation(), diag::err_cconv_change)
|
2010-03-31 00:24:48 +04:00
|
|
|
<< FunctionType::getNameForCallConv(NewTypeInfo.getCC())
|
|
|
|
<< (OldTypeInfo.getCC() == CC_Default)
|
|
|
|
<< (OldTypeInfo.getCC() == CC_Default ? "" :
|
|
|
|
FunctionType::getNameForCallConv(OldTypeInfo.getCC()));
|
2010-02-06 00:31:56 +03:00
|
|
|
Diag(Old->getLocation(), diag::note_previous_declaration);
|
2010-02-04 08:44:44 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-06 00:31:56 +03:00
|
|
|
// FIXME: diagnose the other way around?
|
2010-06-19 01:30:25 +04:00
|
|
|
if (OldType->getNoReturnAttr() && !NewType->getNoReturnAttr()) {
|
2010-02-06 00:31:56 +03:00
|
|
|
NewQType = Context.getNoReturnType(NewQType);
|
|
|
|
New->setType(NewQType);
|
|
|
|
assert(NewQType.isCanonical());
|
|
|
|
}
|
|
|
|
|
2010-06-19 01:30:25 +04:00
|
|
|
// Merge regparm attribute.
|
|
|
|
if (OldType->getRegParmType() != NewType->getRegParmType()) {
|
|
|
|
if (NewType->getRegParmType()) {
|
|
|
|
Diag(New->getLocation(), diag::err_regparm_mismatch)
|
|
|
|
<< NewType->getRegParmType()
|
|
|
|
<< OldType->getRegParmType();
|
|
|
|
Diag(Old->getLocation(), diag::note_previous_declaration);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NewQType = Context.getRegParmType(NewQType, OldType->getRegParmType());
|
|
|
|
New->setType(NewQType);
|
|
|
|
assert(NewQType.isCanonical());
|
|
|
|
}
|
|
|
|
|
2008-10-21 20:13:35 +04:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
// (C++98 13.1p2):
|
|
|
|
// Certain function declarations cannot be overloaded:
|
2009-09-09 19:08:12 +04:00
|
|
|
// -- Function declarations that differ only in the return type
|
2008-10-21 20:13:35 +04:00
|
|
|
// cannot be overloaded.
|
2009-09-09 19:08:12 +04:00
|
|
|
QualType OldReturnType
|
2008-10-21 20:13:35 +04:00
|
|
|
= cast<FunctionType>(OldQType.getTypePtr())->getResultType();
|
2009-09-09 19:08:12 +04:00
|
|
|
QualType NewReturnType
|
2008-10-21 20:13:35 +04:00
|
|
|
= cast<FunctionType>(NewQType.getTypePtr())->getResultType();
|
2010-05-20 01:37:30 +04:00
|
|
|
QualType ResQT;
|
2008-10-21 20:13:35 +04:00
|
|
|
if (OldReturnType != NewReturnType) {
|
2010-05-20 01:37:30 +04:00
|
|
|
if (NewReturnType->isObjCObjectPointerType()
|
|
|
|
&& OldReturnType->isObjCObjectPointerType())
|
|
|
|
ResQT = Context.mergeObjCGCQualifiers(NewQType, OldQType);
|
|
|
|
if (ResQT.isNull()) {
|
|
|
|
Diag(New->getLocation(), diag::err_ovl_diff_return_type);
|
|
|
|
Diag(Old->getLocation(), PrevDiag) << Old << Old->getType();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
NewQType = ResQT;
|
2008-10-21 20:13:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old);
|
2010-04-13 11:45:41 +04:00
|
|
|
CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New);
|
2009-12-05 01:33:25 +03:00
|
|
|
if (OldMethod && NewMethod) {
|
2010-04-13 11:45:41 +04:00
|
|
|
// Preserve triviality.
|
|
|
|
NewMethod->setTrivial(OldMethod->isTrivial());
|
|
|
|
|
|
|
|
bool isFriend = NewMethod->getFriendObjectKind();
|
|
|
|
|
|
|
|
if (!isFriend && NewMethod->getLexicalDeclContext()->isRecord()) {
|
2009-12-05 01:33:25 +03:00
|
|
|
// -- Member function declarations with the same name and the
|
|
|
|
// same parameter types cannot be overloaded if any of them
|
|
|
|
// is a static member function declaration.
|
|
|
|
if (OldMethod->isStatic() || NewMethod->isStatic()) {
|
|
|
|
Diag(New->getLocation(), diag::err_ovl_static_nonstatic_member);
|
|
|
|
Diag(Old->getLocation(), PrevDiag) << Old << Old->getType();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++ [class.mem]p1:
|
|
|
|
// [...] A member shall not be declared twice in the
|
|
|
|
// member-specification, except that a nested class or member
|
|
|
|
// class template can be declared and then later defined.
|
|
|
|
unsigned NewDiag;
|
|
|
|
if (isa<CXXConstructorDecl>(OldMethod))
|
|
|
|
NewDiag = diag::err_constructor_redeclared;
|
|
|
|
else if (isa<CXXDestructorDecl>(NewMethod))
|
|
|
|
NewDiag = diag::err_destructor_redeclared;
|
|
|
|
else if (isa<CXXConversionDecl>(NewMethod))
|
|
|
|
NewDiag = diag::err_conv_function_redeclared;
|
|
|
|
else
|
|
|
|
NewDiag = diag::err_member_redeclared;
|
|
|
|
|
|
|
|
Diag(New->getLocation(), NewDiag);
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64504 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-14 02:20:09 +03:00
|
|
|
Diag(Old->getLocation(), PrevDiag) << Old << Old->getType();
|
2010-04-13 11:45:41 +04:00
|
|
|
|
|
|
|
// Complain if this is an explicit declaration of a special
|
|
|
|
// member that was initially declared implicitly.
|
|
|
|
//
|
|
|
|
// As an exception, it's okay to befriend such methods in order
|
|
|
|
// to permit the implicit constructor/destructor/operator calls.
|
|
|
|
} else if (OldMethod->isImplicit()) {
|
|
|
|
if (isFriend) {
|
|
|
|
NewMethod->setImplicit();
|
|
|
|
} else {
|
2009-12-05 01:33:25 +03:00
|
|
|
Diag(NewMethod->getLocation(),
|
|
|
|
diag::err_definition_of_implicitly_declared_member)
|
2010-04-22 09:40:53 +04:00
|
|
|
<< New << getSpecialMember(OldMethod);
|
2009-12-05 01:33:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
2008-10-21 20:13:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// (C++98 8.3.5p3):
|
|
|
|
// All declarations for a function shall agree exactly in both the
|
|
|
|
// return type and the parameter-type-list.
|
2009-12-24 02:40:33 +03:00
|
|
|
// attributes should be ignored when comparing.
|
|
|
|
if (Context.getNoReturnType(OldQType, false) ==
|
|
|
|
Context.getNoReturnType(NewQType, false))
|
2009-02-24 04:23:02 +03:00
|
|
|
return MergeCompatibleFunctionDecls(New, Old);
|
2008-10-21 20:13:35 +04:00
|
|
|
|
|
|
|
// Fall through for conflicting redeclarations and redefinitions.
|
2008-04-21 06:02:58 +04:00
|
|
|
}
|
2008-04-08 08:40:51 +04:00
|
|
|
|
|
|
|
// C: Function types need to be compatible, not identical. This handles
|
2008-01-14 23:51:29 +03:00
|
|
|
// duplicate function decls like "void f(int); void f(enum X);" properly.
|
2008-04-08 08:40:51 +04:00
|
|
|
if (!getLangOptions().CPlusPlus &&
|
2008-08-22 04:56:42 +04:00
|
|
|
Context.typesAreCompatible(OldQType, NewQType)) {
|
2009-09-22 03:43:11 +04:00
|
|
|
const FunctionType *OldFuncType = OldQType->getAs<FunctionType>();
|
|
|
|
const FunctionType *NewFuncType = NewQType->getAs<FunctionType>();
|
2009-02-27 02:50:07 +03:00
|
|
|
const FunctionProtoType *OldProto = 0;
|
|
|
|
if (isa<FunctionNoProtoType>(NewFuncType) &&
|
2009-03-07 01:43:54 +03:00
|
|
|
(OldProto = dyn_cast<FunctionProtoType>(OldFuncType))) {
|
2009-02-16 21:20:44 +03:00
|
|
|
// The old declaration provided a function prototype, but the
|
|
|
|
// new declaration does not. Merge in the prototype.
|
2009-05-28 02:11:52 +04:00
|
|
|
assert(!OldProto->hasExceptionSpec() && "Exception spec in C");
|
2009-02-16 21:20:44 +03:00
|
|
|
llvm::SmallVector<QualType, 16> ParamTypes(OldProto->arg_type_begin(),
|
|
|
|
OldProto->arg_type_end());
|
|
|
|
NewQType = Context.getFunctionType(NewFuncType->getResultType(),
|
2009-05-21 13:52:38 +04:00
|
|
|
ParamTypes.data(), ParamTypes.size(),
|
2009-02-16 21:20:44 +03:00
|
|
|
OldProto->isVariadic(),
|
2010-02-22 01:15:06 +03:00
|
|
|
OldProto->getTypeQuals(),
|
|
|
|
false, false, 0, 0,
|
2010-03-31 00:24:48 +04:00
|
|
|
OldProto->getExtInfo());
|
2009-02-16 21:20:44 +03:00
|
|
|
New->setType(NewQType);
|
2009-05-15 01:46:00 +04:00
|
|
|
New->setHasInheritedPrototype();
|
2009-02-16 23:58:07 +03:00
|
|
|
|
|
|
|
// Synthesize a parameter for each argument type.
|
|
|
|
llvm::SmallVector<ParmVarDecl*, 16> Params;
|
2009-09-09 19:08:12 +04:00
|
|
|
for (FunctionProtoType::arg_type_iterator
|
|
|
|
ParamType = OldProto->arg_type_begin(),
|
2009-02-16 23:58:07 +03:00
|
|
|
ParamEnd = OldProto->arg_type_end();
|
|
|
|
ParamType != ParamEnd; ++ParamType) {
|
|
|
|
ParmVarDecl *Param = ParmVarDecl::Create(Context, New,
|
|
|
|
SourceLocation(), 0,
|
2009-12-07 05:54:59 +03:00
|
|
|
*ParamType, /*TInfo=*/0,
|
2010-08-26 07:08:43 +04:00
|
|
|
SC_None, SC_None,
|
2010-04-20 02:54:31 +04:00
|
|
|
0);
|
2009-02-16 23:58:07 +03:00
|
|
|
Param->setImplicit();
|
|
|
|
Params.push_back(Param);
|
|
|
|
}
|
|
|
|
|
2010-02-11 04:19:42 +03:00
|
|
|
New->setParams(Params.data(), Params.size());
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2009-03-07 01:43:54 +03:00
|
|
|
|
|
|
|
return MergeCompatibleFunctionDecls(New, Old);
|
|
|
|
}
|
|
|
|
|
|
|
|
// GNU C permits a K&R definition to follow a prototype declaration
|
|
|
|
// if the declared types of the parameters in the K&R definition
|
|
|
|
// match the types in the prototype declaration, even when the
|
|
|
|
// promoted types of the parameters from the K&R definition differ
|
|
|
|
// from the types in the prototype. GCC then keeps the types from
|
|
|
|
// the prototype.
|
2009-06-01 13:24:59 +04:00
|
|
|
//
|
|
|
|
// If a variadic prototype is followed by a non-variadic K&R definition,
|
|
|
|
// the K&R definition becomes variadic. This is sort of an edge case, but
|
|
|
|
// it's legal per the standard depending on how you read C99 6.7.5.3p15 and
|
|
|
|
// C99 6.9.1p8.
|
2009-03-07 01:43:54 +03:00
|
|
|
if (!getLangOptions().CPlusPlus &&
|
|
|
|
Old->hasPrototype() && !New->hasPrototype() &&
|
2009-09-22 03:43:11 +04:00
|
|
|
New->getType()->getAs<FunctionProtoType>() &&
|
2009-03-07 01:43:54 +03:00
|
|
|
Old->getNumParams() == New->getNumParams()) {
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTypes;
|
|
|
|
llvm::SmallVector<GNUCompatibleParamWarning, 16> Warnings;
|
2009-09-09 19:08:12 +04:00
|
|
|
const FunctionProtoType *OldProto
|
2009-09-22 03:43:11 +04:00
|
|
|
= Old->getType()->getAs<FunctionProtoType>();
|
2009-09-09 19:08:12 +04:00
|
|
|
const FunctionProtoType *NewProto
|
2009-09-22 03:43:11 +04:00
|
|
|
= New->getType()->getAs<FunctionProtoType>();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-07 01:43:54 +03:00
|
|
|
// Determine whether this is the GNU C extension.
|
2009-06-01 13:24:59 +04:00
|
|
|
QualType MergedReturn = Context.mergeTypes(OldProto->getResultType(),
|
|
|
|
NewProto->getResultType());
|
|
|
|
bool LooseCompatible = !MergedReturn.isNull();
|
2009-09-09 19:08:12 +04:00
|
|
|
for (unsigned Idx = 0, End = Old->getNumParams();
|
2009-06-01 13:24:59 +04:00
|
|
|
LooseCompatible && Idx != End; ++Idx) {
|
2009-03-07 01:43:54 +03:00
|
|
|
ParmVarDecl *OldParm = Old->getParamDecl(Idx);
|
|
|
|
ParmVarDecl *NewParm = New->getParamDecl(Idx);
|
2009-09-09 19:08:12 +04:00
|
|
|
if (Context.typesAreCompatible(OldParm->getType(),
|
2009-03-07 01:43:54 +03:00
|
|
|
NewProto->getArgType(Idx))) {
|
|
|
|
ArgTypes.push_back(NewParm->getType());
|
|
|
|
} else if (Context.typesAreCompatible(OldParm->getType(),
|
2010-07-29 19:18:02 +04:00
|
|
|
NewParm->getType(),
|
|
|
|
/*CompareUnqualified=*/true)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
GNUCompatibleParamWarning Warn
|
2009-03-07 01:43:54 +03:00
|
|
|
= { OldParm, NewParm, NewProto->getArgType(Idx) };
|
|
|
|
Warnings.push_back(Warn);
|
|
|
|
ArgTypes.push_back(NewParm->getType());
|
|
|
|
} else
|
2009-06-01 13:24:59 +04:00
|
|
|
LooseCompatible = false;
|
2009-03-07 01:43:54 +03:00
|
|
|
}
|
|
|
|
|
2009-06-01 13:24:59 +04:00
|
|
|
if (LooseCompatible) {
|
2009-03-07 01:43:54 +03:00
|
|
|
for (unsigned Warn = 0; Warn < Warnings.size(); ++Warn) {
|
|
|
|
Diag(Warnings[Warn].NewParm->getLocation(),
|
|
|
|
diag::ext_param_promoted_not_compatible_with_prototype)
|
|
|
|
<< Warnings[Warn].PromotedType
|
|
|
|
<< Warnings[Warn].OldParm->getType();
|
2010-07-29 19:18:02 +04:00
|
|
|
if (Warnings[Warn].OldParm->getLocation().isValid())
|
|
|
|
Diag(Warnings[Warn].OldParm->getLocation(),
|
|
|
|
diag::note_previous_declaration);
|
2009-03-07 01:43:54 +03:00
|
|
|
}
|
2009-02-16 23:58:07 +03:00
|
|
|
|
2009-06-01 13:24:59 +04:00
|
|
|
New->setType(Context.getFunctionType(MergedReturn, &ArgTypes[0],
|
|
|
|
ArgTypes.size(),
|
2010-02-22 01:15:06 +03:00
|
|
|
OldProto->isVariadic(), 0,
|
|
|
|
false, false, 0, 0,
|
2010-03-31 00:24:48 +04:00
|
|
|
OldProto->getExtInfo()));
|
2009-03-07 01:43:54 +03:00
|
|
|
return MergeCompatibleFunctionDecls(New, Old);
|
2009-02-16 21:20:44 +03:00
|
|
|
}
|
|
|
|
|
2009-03-07 01:43:54 +03:00
|
|
|
// Fall through to diagnose conflicting types.
|
2008-04-08 08:40:51 +04:00
|
|
|
}
|
2007-11-06 09:07:26 +03:00
|
|
|
|
2008-01-16 18:01:34 +03:00
|
|
|
// A function that has already been declared has been redeclared or defined
|
|
|
|
// with a different type- show appropriate diagnostic
|
2009-09-12 04:22:50 +04:00
|
|
|
if (unsigned BuiltinID = Old->getBuiltinID()) {
|
2009-02-16 20:45:42 +03:00
|
|
|
// The user has declared a builtin function with an incompatible
|
|
|
|
// signature.
|
|
|
|
if (Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) {
|
|
|
|
// The function the user is redeclaring is a library-defined
|
|
|
|
// function like 'malloc' or 'printf'. Warn about the
|
2009-03-23 20:47:24 +03:00
|
|
|
// redeclaration, then pretend that we don't know about this
|
|
|
|
// library built-in.
|
2009-02-16 20:45:42 +03:00
|
|
|
Diag(New->getLocation(), diag::warn_redecl_library_builtin) << New;
|
|
|
|
Diag(Old->getLocation(), diag::note_previous_builtin_declaration)
|
|
|
|
<< Old << Old->getType();
|
2009-03-23 20:47:24 +03:00
|
|
|
New->getIdentifier()->setBuiltinID(Builtin::NotBuiltin);
|
|
|
|
Old->setInvalidDecl();
|
|
|
|
return false;
|
2009-02-16 20:45:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PrevDiag = diag::note_previous_builtin_declaration;
|
|
|
|
}
|
2008-01-16 18:01:34 +03:00
|
|
|
|
2008-11-24 08:29:24 +03:00
|
|
|
Diag(New->getLocation(), diag::err_conflicting_types) << New->getDeclName();
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64504 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-14 02:20:09 +03:00
|
|
|
Diag(Old->getLocation(), PrevDiag) << Old << Old->getType();
|
2009-02-16 20:45:42 +03:00
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-02-24 04:23:02 +03:00
|
|
|
/// \brief Completes the merge of two function declarations that are
|
2009-09-09 19:08:12 +04:00
|
|
|
/// known to be compatible.
|
2009-02-24 04:23:02 +03:00
|
|
|
///
|
|
|
|
/// This routine handles the merging of attributes and other
|
|
|
|
/// properties of function declarations form the old declaration to
|
|
|
|
/// the new declaration, once we know that New is in fact a
|
|
|
|
/// redeclaration of Old.
|
|
|
|
///
|
|
|
|
/// \returns false
|
|
|
|
bool Sema::MergeCompatibleFunctionDecls(FunctionDecl *New, FunctionDecl *Old) {
|
|
|
|
// Merge the attributes
|
2010-08-19 03:23:40 +04:00
|
|
|
MergeDeclAttributes(New, Old, Context);
|
2009-02-24 04:23:02 +03:00
|
|
|
|
|
|
|
// Merge the storage class.
|
2010-08-26 07:08:43 +04:00
|
|
|
if (Old->getStorageClass() != SC_Extern &&
|
|
|
|
Old->getStorageClass() != SC_None)
|
2009-04-28 10:37:30 +04:00
|
|
|
New->setStorageClass(Old->getStorageClass());
|
2009-02-24 04:23:02 +03:00
|
|
|
|
|
|
|
// Merge "pure" flag.
|
|
|
|
if (Old->isPure())
|
|
|
|
New->setPure();
|
|
|
|
|
|
|
|
// Merge the "deleted" flag.
|
|
|
|
if (Old->isDeleted())
|
|
|
|
New->setDeleted();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-02-24 04:23:02 +03:00
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
return MergeCXXFunctionDecl(New, Old);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// MergeVarDecl - We just parsed a variable 'New' which has the same name
|
|
|
|
/// and scope as a previous declaration 'Old'. Figure out how to resolve this
|
|
|
|
/// situation, merging decls or emitting diagnostics as appropriate.
|
|
|
|
///
|
2009-09-09 19:08:12 +04:00
|
|
|
/// Tentative definition rules (C99 6.9.2p2) are checked by
|
|
|
|
/// FinalizeDeclaratorGroup. Unfortunately, we can't analyze tentative
|
2008-08-08 21:50:35 +04:00
|
|
|
/// definitions here, since the initializer hasn't been attached.
|
2009-09-09 19:08:12 +04:00
|
|
|
///
|
2009-11-19 01:49:29 +03:00
|
|
|
void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
|
|
|
|
// If the new decl is already invalid, don't do any other checking.
|
|
|
|
if (New->isInvalidDecl())
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Verify the old decl was also a variable.
|
2009-11-19 01:49:29 +03:00
|
|
|
VarDecl *Old = 0;
|
|
|
|
if (!Previous.isSingleResult() ||
|
|
|
|
!(Old = dyn_cast<VarDecl>(Previous.getFoundDecl()))) {
|
2008-11-20 09:38:18 +03:00
|
|
|
Diag(New->getLocation(), diag::err_redefinition_different_kind)
|
2008-11-24 00:45:46 +03:00
|
|
|
<< New->getDeclName();
|
2009-11-19 01:49:29 +03:00
|
|
|
Diag(Previous.getRepresentativeDecl()->getLocation(),
|
|
|
|
diag::note_previous_definition);
|
2009-04-25 12:06:05 +04:00
|
|
|
return New->setInvalidDecl();
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-03-03 06:28:21 +03:00
|
|
|
|
2010-08-30 18:32:14 +04:00
|
|
|
// C++ [class.mem]p1:
|
|
|
|
// A member shall not be declared twice in the member-specification [...]
|
|
|
|
//
|
|
|
|
// Here, we need only consider static data members.
|
|
|
|
if (Old->isStaticDataMember() && !New->isOutOfLine()) {
|
|
|
|
Diag(New->getLocation(), diag::err_duplicate_member)
|
|
|
|
<< New->getIdentifier();
|
|
|
|
Diag(Old->getLocation(), diag::note_previous_declaration);
|
|
|
|
New->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
2010-08-19 03:23:40 +04:00
|
|
|
MergeDeclAttributes(New, Old, Context);
|
2008-03-03 06:28:21 +03:00
|
|
|
|
2009-01-25 02:49:55 +03:00
|
|
|
// Merge the types
|
2009-05-16 17:54:38 +04:00
|
|
|
QualType MergedT;
|
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
if (Context.hasSameType(New->getType(), Old->getType()))
|
|
|
|
MergedT = New->getType();
|
2009-12-10 11:54:47 +03:00
|
|
|
// C++ [basic.link]p10:
|
|
|
|
// [...] the types specified by all declarations referring to a given
|
|
|
|
// object or function shall be identical, except that declarations for an
|
|
|
|
// array object can specify array types that differ by the presence or
|
|
|
|
// absence of a major array bound (8.3.4).
|
2009-09-09 19:08:12 +04:00
|
|
|
else if (Old->getType()->isIncompleteArrayType() &&
|
2009-09-09 10:04:29 +04:00
|
|
|
New->getType()->isArrayType()) {
|
2009-09-09 19:08:12 +04:00
|
|
|
CanQual<ArrayType> OldArray
|
2009-09-09 10:04:29 +04:00
|
|
|
= Context.getCanonicalType(Old->getType())->getAs<ArrayType>();
|
2009-09-09 19:08:12 +04:00
|
|
|
CanQual<ArrayType> NewArray
|
2009-09-09 10:04:29 +04:00
|
|
|
= Context.getCanonicalType(New->getType())->getAs<ArrayType>();
|
|
|
|
if (OldArray->getElementType() == NewArray->getElementType())
|
|
|
|
MergedT = New->getType();
|
2009-12-10 11:54:47 +03:00
|
|
|
} else if (Old->getType()->isArrayType() &&
|
|
|
|
New->getType()->isIncompleteArrayType()) {
|
|
|
|
CanQual<ArrayType> OldArray
|
|
|
|
= Context.getCanonicalType(Old->getType())->getAs<ArrayType>();
|
|
|
|
CanQual<ArrayType> NewArray
|
|
|
|
= Context.getCanonicalType(New->getType())->getAs<ArrayType>();
|
|
|
|
if (OldArray->getElementType() == NewArray->getElementType())
|
|
|
|
MergedT = Old->getType();
|
2010-05-20 01:37:30 +04:00
|
|
|
} else if (New->getType()->isObjCObjectPointerType()
|
|
|
|
&& Old->getType()->isObjCObjectPointerType()) {
|
|
|
|
MergedT = Context.mergeObjCGCQualifiers(New->getType(), Old->getType());
|
2009-09-09 10:04:29 +04:00
|
|
|
}
|
2009-05-16 17:54:38 +04:00
|
|
|
} else {
|
|
|
|
MergedT = Context.mergeTypes(New->getType(), Old->getType());
|
|
|
|
}
|
2009-01-25 02:49:55 +03:00
|
|
|
if (MergedT.isNull()) {
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(New->getLocation(), diag::err_redefinition_different_type)
|
2009-01-09 22:42:16 +03:00
|
|
|
<< New->getDeclName();
|
2008-11-24 02:12:31 +03:00
|
|
|
Diag(Old->getLocation(), diag::note_previous_definition);
|
2009-04-25 12:06:05 +04:00
|
|
|
return New->setInvalidDecl();
|
2008-01-30 03:44:01 +03:00
|
|
|
}
|
2009-01-25 02:49:55 +03:00
|
|
|
New->setType(MergedT);
|
2009-03-12 02:52:16 +03:00
|
|
|
|
2008-01-30 03:44:01 +03:00
|
|
|
// C99 6.2.2p4: Check if we have a static decl followed by a non-static.
|
2010-08-26 07:08:43 +04:00
|
|
|
if (New->getStorageClass() == SC_Static &&
|
|
|
|
(Old->getStorageClass() == SC_None || Old->hasExternalStorage())) {
|
2008-11-24 08:29:24 +03:00
|
|
|
Diag(New->getLocation(), diag::err_static_non_static) << New->getDeclName();
|
2008-11-24 02:12:31 +03:00
|
|
|
Diag(Old->getLocation(), diag::note_previous_definition);
|
2009-04-25 12:06:05 +04:00
|
|
|
return New->setInvalidDecl();
|
2008-01-30 03:44:01 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
// C99 6.2.2p4:
|
2009-03-20 01:01:50 +03:00
|
|
|
// For an identifier declared with the storage-class specifier
|
|
|
|
// extern in a scope in which a prior declaration of that
|
|
|
|
// identifier is visible,23) if the prior declaration specifies
|
|
|
|
// internal or external linkage, the linkage of the identifier at
|
|
|
|
// the later declaration is the same as the linkage specified at
|
|
|
|
// the prior declaration. If no prior declaration is visible, or
|
|
|
|
// if the prior declaration specifies no linkage, then the
|
|
|
|
// identifier has external linkage.
|
2009-03-23 19:17:01 +03:00
|
|
|
if (New->hasExternalStorage() && Old->hasLinkage())
|
2009-03-20 01:01:50 +03:00
|
|
|
/* Okay */;
|
2010-08-26 07:08:43 +04:00
|
|
|
else if (New->getStorageClass() != SC_Static &&
|
|
|
|
Old->getStorageClass() == SC_Static) {
|
2008-11-24 08:29:24 +03:00
|
|
|
Diag(New->getLocation(), diag::err_non_static_static) << New->getDeclName();
|
2008-11-24 02:12:31 +03:00
|
|
|
Diag(Old->getLocation(), diag::note_previous_definition);
|
2009-04-25 12:06:05 +04:00
|
|
|
return New->setInvalidDecl();
|
2008-01-30 03:44:01 +03:00
|
|
|
}
|
2009-04-14 06:25:56 +04:00
|
|
|
|
2008-09-17 18:05:40 +04:00
|
|
|
// Variables with external linkage are analyzed in FinalizeDeclaratorGroup.
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-14 06:25:56 +04:00
|
|
|
// FIXME: The test for external storage here seems wrong? We still
|
|
|
|
// need to check for mismatches.
|
|
|
|
if (!New->hasExternalStorage() && !New->isFileVarDecl() &&
|
2009-03-12 02:52:16 +03:00
|
|
|
// Don't complain about out-of-line definitions of static members.
|
|
|
|
!(Old->getLexicalDeclContext()->isRecord() &&
|
|
|
|
!New->getLexicalDeclContext()->isRecord())) {
|
2008-11-24 00:45:46 +03:00
|
|
|
Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName();
|
2008-11-24 02:12:31 +03:00
|
|
|
Diag(Old->getLocation(), diag::note_previous_definition);
|
2009-04-25 12:06:05 +04:00
|
|
|
return New->setInvalidDecl();
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2009-03-11 02:43:53 +03:00
|
|
|
|
2009-04-20 00:27:55 +04:00
|
|
|
if (New->isThreadSpecified() && !Old->isThreadSpecified()) {
|
|
|
|
Diag(New->getLocation(), diag::err_thread_non_thread) << New->getDeclName();
|
|
|
|
Diag(Old->getLocation(), diag::note_previous_definition);
|
|
|
|
} else if (!New->isThreadSpecified() && Old->isThreadSpecified()) {
|
|
|
|
Diag(New->getLocation(), diag::err_non_thread_thread) << New->getDeclName();
|
|
|
|
Diag(Old->getLocation(), diag::note_previous_definition);
|
|
|
|
}
|
|
|
|
|
2010-02-02 21:35:11 +03:00
|
|
|
// C++ doesn't have tentative definitions, so go right ahead and check here.
|
|
|
|
const VarDecl *Def;
|
2010-02-03 05:08:48 +03:00
|
|
|
if (getLangOptions().CPlusPlus &&
|
|
|
|
New->isThisDeclarationADefinition() == VarDecl::Definition &&
|
2010-02-02 21:35:11 +03:00
|
|
|
(Def = Old->getDefinition())) {
|
|
|
|
Diag(New->getLocation(), diag::err_redefinition)
|
|
|
|
<< New->getDeclName();
|
|
|
|
Diag(Def->getLocation(), diag::note_previous_definition);
|
|
|
|
New->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
2010-06-25 04:05:45 +04:00
|
|
|
// c99 6.2.2 P4.
|
|
|
|
// For an identifier declared with the storage-class specifier extern in a
|
|
|
|
// scope in which a prior declaration of that identifier is visible, if
|
|
|
|
// the prior declaration specifies internal or external linkage, the linkage
|
|
|
|
// of the identifier at the later declaration is the same as the linkage
|
|
|
|
// specified at the prior declaration.
|
|
|
|
// FIXME. revisit this code.
|
2010-06-21 20:08:37 +04:00
|
|
|
if (New->hasExternalStorage() &&
|
2010-06-24 22:50:41 +04:00
|
|
|
Old->getLinkage() == InternalLinkage &&
|
|
|
|
New->getDeclContext() == Old->getDeclContext())
|
2010-06-21 20:08:37 +04:00
|
|
|
New->setStorageClass(Old->getStorageClass());
|
|
|
|
|
2009-03-11 02:43:53 +03:00
|
|
|
// Keep a chain of previous declarations.
|
|
|
|
New->setPreviousDeclaration(Old);
|
2010-01-21 00:53:11 +03:00
|
|
|
|
|
|
|
// Inherit access appropriately.
|
|
|
|
New->setAccess(Old->getAccess());
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
|
|
|
|
/// no declarator (e.g. "struct foo;") is parsed.
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS,
|
|
|
|
DeclSpec &DS) {
|
2009-04-10 01:26:42 +04:00
|
|
|
// FIXME: Error on inline/virtual/explicit
|
2009-04-20 00:27:55 +04:00
|
|
|
// FIXME: Warn on useless __thread
|
2009-04-10 01:26:42 +04:00
|
|
|
// FIXME: Warn on useless const/volatile
|
|
|
|
// FIXME: Warn on useless static/extern/typedef/private_extern/mutable
|
|
|
|
// FIXME: Warn on useless attributes
|
2009-10-08 03:34:25 +04:00
|
|
|
Decl *TagD = 0;
|
2009-02-09 18:09:02 +03:00
|
|
|
TagDecl *Tag = 0;
|
|
|
|
if (DS.getTypeSpecType() == DeclSpec::TST_class ||
|
|
|
|
DS.getTypeSpecType() == DeclSpec::TST_struct ||
|
|
|
|
DS.getTypeSpecType() == DeclSpec::TST_union ||
|
2009-05-13 03:25:50 +04:00
|
|
|
DS.getTypeSpecType() == DeclSpec::TST_enum) {
|
2010-08-24 09:47:05 +04:00
|
|
|
TagD = DS.getRepAsDecl();
|
2009-10-08 03:34:25 +04:00
|
|
|
|
|
|
|
if (!TagD) // We probably had an error
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2009-05-13 03:25:50 +04:00
|
|
|
|
2009-08-06 06:15:43 +04:00
|
|
|
// Note that the above type specs guarantee that the
|
|
|
|
// type rep is a Decl, whereas in many of the others
|
|
|
|
// it's a Type.
|
2009-10-08 03:34:25 +04:00
|
|
|
Tag = dyn_cast<TagDecl>(TagD);
|
2009-05-13 03:25:50 +04:00
|
|
|
}
|
2009-02-09 18:09:02 +03:00
|
|
|
|
2009-12-17 14:35:26 +03:00
|
|
|
if (unsigned TypeQuals = DS.getTypeQualifiers()) {
|
|
|
|
// Enforce C99 6.7.3p2: "Types other than pointer types derived from object
|
|
|
|
// or incomplete types shall not be restrict-qualified."
|
|
|
|
if (TypeQuals & DeclSpec::TQ_restrict)
|
|
|
|
Diag(DS.getRestrictSpecLoc(),
|
|
|
|
diag::err_typecheck_invalid_restrict_not_pointer_noarg)
|
|
|
|
<< DS.getSourceRange();
|
|
|
|
}
|
|
|
|
|
2009-09-26 10:47:28 +04:00
|
|
|
if (DS.isFriendSpecified()) {
|
2009-10-08 03:34:25 +04:00
|
|
|
// If we're dealing with a class template decl, assume that the
|
|
|
|
// template routines are handling it.
|
|
|
|
if (TagD && isa<ClassTemplateDecl>(TagD))
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2009-10-08 03:34:25 +04:00
|
|
|
return ActOnFriendTypeDecl(S, DS, MultiTemplateParamsArg(*this, 0, 0));
|
2009-09-26 10:47:28 +04:00
|
|
|
}
|
|
|
|
|
2009-01-13 01:49:06 +03:00
|
|
|
if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) {
|
2010-08-19 03:23:40 +04:00
|
|
|
ProcessDeclAttributeList(S, Record, DS.getAttributes());
|
2009-10-26 01:21:57 +03:00
|
|
|
|
2009-01-13 01:49:06 +03:00
|
|
|
if (!Record->getDeclName() && Record->isDefinition() &&
|
2009-03-07 02:06:59 +03:00
|
|
|
DS.getStorageClassSpec() != DeclSpec::SCS_typedef) {
|
|
|
|
if (getLangOptions().CPlusPlus ||
|
|
|
|
Record->getDeclContext()->isRecord())
|
2010-05-22 00:45:30 +04:00
|
|
|
return BuildAnonymousStructOrUnion(S, DS, AS, Record);
|
2009-03-07 02:06:59 +03:00
|
|
|
|
2010-04-09 01:33:23 +04:00
|
|
|
Diag(DS.getSourceRange().getBegin(), diag::ext_no_declarators)
|
2009-03-07 02:06:59 +03:00
|
|
|
<< DS.getSourceRange();
|
|
|
|
}
|
2009-01-13 01:49:06 +03:00
|
|
|
|
|
|
|
// Microsoft allows unnamed struct/union fields. Don't complain
|
|
|
|
// about them.
|
|
|
|
// FIXME: Should we support Microsoft's extensions in this area?
|
|
|
|
if (Record->getDeclName() && getLangOptions().Microsoft)
|
2010-08-21 13:40:31 +04:00
|
|
|
return Tag;
|
2009-01-13 01:49:06 +03:00
|
|
|
}
|
2009-09-26 10:47:28 +04:00
|
|
|
|
2010-07-13 10:24:26 +04:00
|
|
|
if (getLangOptions().CPlusPlus &&
|
|
|
|
DS.getStorageClassSpec() != DeclSpec::SCS_typedef)
|
|
|
|
if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Tag))
|
|
|
|
if (Enum->enumerator_begin() == Enum->enumerator_end() &&
|
|
|
|
!Enum->getIdentifier() && !Enum->isInvalidDecl())
|
|
|
|
Diag(Enum->getLocation(), diag::ext_no_declarators)
|
|
|
|
<< DS.getSourceRange();
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
if (!DS.isMissingDeclaratorOk() &&
|
2009-02-07 01:42:48 +03:00
|
|
|
DS.getTypeSpecType() != DeclSpec::TST_error) {
|
2009-01-22 19:23:54 +03:00
|
|
|
// Warn about typedefs of enums without names, since this is an
|
2010-07-16 19:40:40 +04:00
|
|
|
// extension in both Microsoft and GNU.
|
2009-01-17 05:55:50 +03:00
|
|
|
if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef &&
|
|
|
|
Tag && isa<EnumDecl>(Tag)) {
|
2010-07-16 19:40:40 +04:00
|
|
|
Diag(DS.getSourceRange().getBegin(), diag::ext_typedef_without_a_name)
|
|
|
|
<< DS.getSourceRange();
|
2010-08-21 13:40:31 +04:00
|
|
|
return Tag;
|
2009-01-14 02:10:51 +03:00
|
|
|
}
|
|
|
|
|
2010-04-09 01:33:23 +04:00
|
|
|
Diag(DS.getSourceRange().getBegin(), diag::ext_no_declarators)
|
2008-12-28 18:28:59 +03:00
|
|
|
<< DS.getSourceRange();
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
return TagD;
|
2009-01-07 03:43:41 +03:00
|
|
|
}
|
|
|
|
|
2010-09-29 00:42:35 +04:00
|
|
|
/// ActOnVlaStmt - This rouine if finds a vla expression in a decl spec.
|
|
|
|
/// builds a statement for it and returns it so it is evaluated.
|
|
|
|
StmtResult Sema::ActOnVlaStmt(const DeclSpec &DS) {
|
|
|
|
StmtResult R;
|
|
|
|
if (DS.getTypeSpecType() == DeclSpec::TST_typeofExpr) {
|
|
|
|
Expr *Exp = DS.getRepAsExpr();
|
|
|
|
QualType Ty = Exp->getType();
|
|
|
|
if (Ty->isPointerType()) {
|
|
|
|
do
|
|
|
|
Ty = Ty->getAs<PointerType>()->getPointeeType();
|
|
|
|
while (Ty->isPointerType());
|
|
|
|
}
|
|
|
|
if (Ty->isVariableArrayType()) {
|
|
|
|
R = ActOnExprStmt(MakeFullExpr(Exp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2009-12-18 13:40:03 +03:00
|
|
|
/// We are trying to inject an anonymous member into the given scope;
|
2009-11-19 01:49:29 +03:00
|
|
|
/// check if there's an existing declaration that can't be overloaded.
|
|
|
|
///
|
|
|
|
/// \return true if this is a forbidden redeclaration
|
2009-12-18 13:40:03 +03:00
|
|
|
static bool CheckAnonMemberRedeclaration(Sema &SemaRef,
|
|
|
|
Scope *S,
|
2010-01-22 21:30:17 +03:00
|
|
|
DeclContext *Owner,
|
2009-12-18 13:40:03 +03:00
|
|
|
DeclarationName Name,
|
|
|
|
SourceLocation NameLoc,
|
|
|
|
unsigned diagnostic) {
|
|
|
|
LookupResult R(SemaRef, Name, NameLoc, Sema::LookupMemberName,
|
|
|
|
Sema::ForRedeclaration);
|
|
|
|
if (!SemaRef.LookupName(R, S)) return false;
|
|
|
|
|
|
|
|
if (R.getAsSingle<TagDecl>())
|
2009-11-19 01:49:29 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Pick a representative declaration.
|
2009-12-18 13:40:03 +03:00
|
|
|
NamedDecl *PrevDecl = R.getRepresentativeDecl()->getUnderlyingDecl();
|
2010-09-23 18:26:01 +04:00
|
|
|
assert(PrevDecl && "Expected a non-null Decl");
|
|
|
|
|
|
|
|
if (!SemaRef.isDeclInScope(PrevDecl, Owner, S))
|
|
|
|
return false;
|
2009-11-19 01:49:29 +03:00
|
|
|
|
2009-12-18 13:40:03 +03:00
|
|
|
SemaRef.Diag(NameLoc, diagnostic) << Name;
|
|
|
|
SemaRef.Diag(PrevDecl->getLocation(), diag::note_previous_declaration);
|
2009-11-19 01:49:29 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-07 03:43:41 +03:00
|
|
|
/// InjectAnonymousStructOrUnionMembers - Inject the members of the
|
|
|
|
/// anonymous struct or union AnonRecord into the owning context Owner
|
|
|
|
/// and scope S. This routine will be invoked just after we realize
|
|
|
|
/// that an unnamed union or struct is actually an anonymous union or
|
|
|
|
/// struct, e.g.,
|
|
|
|
///
|
|
|
|
/// @code
|
|
|
|
/// union {
|
|
|
|
/// int i;
|
|
|
|
/// float f;
|
|
|
|
/// }; // InjectAnonymousStructOrUnionMembers called here to inject i and
|
|
|
|
/// // f into the surrounding scope.x
|
|
|
|
/// @endcode
|
|
|
|
///
|
|
|
|
/// This routine is recursive, injecting the names of nested anonymous
|
|
|
|
/// structs/unions into the owning context and scope as well.
|
2010-05-22 00:45:30 +04:00
|
|
|
static bool InjectAnonymousStructOrUnionMembers(Sema &SemaRef, Scope *S,
|
|
|
|
DeclContext *Owner,
|
|
|
|
RecordDecl *AnonRecord,
|
|
|
|
AccessSpecifier AS) {
|
2009-11-19 01:49:29 +03:00
|
|
|
unsigned diagKind
|
|
|
|
= AnonRecord->isUnion() ? diag::err_anonymous_union_member_redecl
|
|
|
|
: diag::err_anonymous_struct_member_redecl;
|
|
|
|
|
2009-01-07 03:43:41 +03:00
|
|
|
bool Invalid = false;
|
2009-06-30 06:36:12 +04:00
|
|
|
for (RecordDecl::field_iterator F = AnonRecord->field_begin(),
|
|
|
|
FEnd = AnonRecord->field_end();
|
2009-01-07 03:43:41 +03:00
|
|
|
F != FEnd; ++F) {
|
|
|
|
if ((*F)->getDeclName()) {
|
2010-05-22 00:45:30 +04:00
|
|
|
if (CheckAnonMemberRedeclaration(SemaRef, S, Owner, (*F)->getDeclName(),
|
2009-12-18 13:40:03 +03:00
|
|
|
(*F)->getLocation(), diagKind)) {
|
2009-01-07 03:43:41 +03:00
|
|
|
// C++ [class.union]p2:
|
|
|
|
// The names of the members of an anonymous union shall be
|
|
|
|
// distinct from the names of any other entity in the
|
|
|
|
// scope in which the anonymous union is declared.
|
|
|
|
Invalid = true;
|
|
|
|
} else {
|
|
|
|
// C++ [class.union]p2:
|
|
|
|
// For the purpose of name lookup, after the anonymous union
|
|
|
|
// definition, the members of the anonymous union are
|
|
|
|
// considered to have been defined in the scope in which the
|
|
|
|
// anonymous union is declared.
|
2009-06-30 06:36:12 +04:00
|
|
|
Owner->makeDeclVisibleInContext(*F);
|
2010-08-21 13:40:31 +04:00
|
|
|
S->AddDecl(*F);
|
2010-05-22 00:45:30 +04:00
|
|
|
SemaRef.IdResolver.AddDecl(*F);
|
|
|
|
|
|
|
|
// That includes picking up the appropriate access specifier.
|
|
|
|
if (AS != AS_none) (*F)->setAccess(AS);
|
2009-01-07 03:43:41 +03:00
|
|
|
}
|
|
|
|
} else if (const RecordType *InnerRecordType
|
2009-07-30 01:53:49 +04:00
|
|
|
= (*F)->getType()->getAs<RecordType>()) {
|
2009-01-07 03:43:41 +03:00
|
|
|
RecordDecl *InnerRecord = InnerRecordType->getDecl();
|
|
|
|
if (InnerRecord->isAnonymousStructOrUnion())
|
2009-09-09 19:08:12 +04:00
|
|
|
Invalid = Invalid ||
|
2010-05-22 00:45:30 +04:00
|
|
|
InjectAnonymousStructOrUnionMembers(SemaRef, S, Owner,
|
|
|
|
InnerRecord, AS);
|
2009-01-07 03:43:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Invalid;
|
|
|
|
}
|
|
|
|
|
2010-04-20 02:54:31 +04:00
|
|
|
/// StorageClassSpecToVarDeclStorageClass - Maps a DeclSpec::SCS to
|
|
|
|
/// a VarDecl::StorageClass. Any error reporting is up to the caller:
|
2010-08-26 07:08:43 +04:00
|
|
|
/// illegal input values are mapped to SC_None.
|
|
|
|
static StorageClass
|
2010-07-30 20:47:02 +04:00
|
|
|
StorageClassSpecToVarDeclStorageClass(DeclSpec::SCS StorageClassSpec) {
|
2010-04-20 02:54:31 +04:00
|
|
|
switch (StorageClassSpec) {
|
2010-08-26 07:08:43 +04:00
|
|
|
case DeclSpec::SCS_unspecified: return SC_None;
|
|
|
|
case DeclSpec::SCS_extern: return SC_Extern;
|
|
|
|
case DeclSpec::SCS_static: return SC_Static;
|
|
|
|
case DeclSpec::SCS_auto: return SC_Auto;
|
|
|
|
case DeclSpec::SCS_register: return SC_Register;
|
|
|
|
case DeclSpec::SCS_private_extern: return SC_PrivateExtern;
|
2010-04-20 02:54:31 +04:00
|
|
|
// Illegal SCSs map to None: error reporting is up to the caller.
|
|
|
|
case DeclSpec::SCS_mutable: // Fall through.
|
2010-08-26 07:08:43 +04:00
|
|
|
case DeclSpec::SCS_typedef: return SC_None;
|
2010-04-20 02:54:31 +04:00
|
|
|
}
|
|
|
|
llvm_unreachable("unknown storage class specifier");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// StorageClassSpecToFunctionDeclStorageClass - Maps a DeclSpec::SCS to
|
2010-08-26 07:08:43 +04:00
|
|
|
/// a StorageClass. Any error reporting is up to the caller:
|
|
|
|
/// illegal input values are mapped to SC_None.
|
|
|
|
static StorageClass
|
2010-07-30 20:47:02 +04:00
|
|
|
StorageClassSpecToFunctionDeclStorageClass(DeclSpec::SCS StorageClassSpec) {
|
2010-04-20 02:54:31 +04:00
|
|
|
switch (StorageClassSpec) {
|
2010-08-26 07:08:43 +04:00
|
|
|
case DeclSpec::SCS_unspecified: return SC_None;
|
|
|
|
case DeclSpec::SCS_extern: return SC_Extern;
|
|
|
|
case DeclSpec::SCS_static: return SC_Static;
|
|
|
|
case DeclSpec::SCS_private_extern: return SC_PrivateExtern;
|
2010-04-20 02:54:31 +04:00
|
|
|
// Illegal SCSs map to None: error reporting is up to the caller.
|
|
|
|
case DeclSpec::SCS_auto: // Fall through.
|
|
|
|
case DeclSpec::SCS_mutable: // Fall through.
|
|
|
|
case DeclSpec::SCS_register: // Fall through.
|
2010-08-26 07:08:43 +04:00
|
|
|
case DeclSpec::SCS_typedef: return SC_None;
|
2010-04-20 02:54:31 +04:00
|
|
|
}
|
|
|
|
llvm_unreachable("unknown storage class specifier");
|
|
|
|
}
|
|
|
|
|
2009-01-07 03:43:41 +03:00
|
|
|
/// ActOnAnonymousStructOrUnion - Handle the declaration of an
|
|
|
|
/// anonymous structure or union. Anonymous unions are a C++ feature
|
|
|
|
/// (C++ [class.union]) and a GNU C extension; anonymous structures
|
2009-09-09 19:08:12 +04:00
|
|
|
/// are a GNU C and GNU C++ extension.
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS,
|
|
|
|
AccessSpecifier AS,
|
|
|
|
RecordDecl *Record) {
|
2009-01-07 03:43:41 +03:00
|
|
|
DeclContext *Owner = Record->getDeclContext();
|
|
|
|
|
|
|
|
// Diagnose whether this anonymous struct/union is an extension.
|
|
|
|
if (Record->isUnion() && !getLangOptions().CPlusPlus)
|
|
|
|
Diag(Record->getLocation(), diag::ext_anonymous_union);
|
|
|
|
else if (!Record->isUnion())
|
|
|
|
Diag(Record->getLocation(), diag::ext_anonymous_struct);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-01-07 03:43:41 +03:00
|
|
|
// C and C++ require different kinds of checks for anonymous
|
|
|
|
// structs/unions.
|
|
|
|
bool Invalid = false;
|
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
const char* PrevSpec = 0;
|
2009-08-04 00:12:06 +04:00
|
|
|
unsigned DiagID;
|
2009-01-07 03:43:41 +03:00
|
|
|
// C++ [class.union]p3:
|
|
|
|
// Anonymous unions declared in a named namespace or in the
|
|
|
|
// global namespace shall be declared static.
|
|
|
|
if (DS.getStorageClassSpec() != DeclSpec::SCS_static &&
|
|
|
|
(isa<TranslationUnitDecl>(Owner) ||
|
2009-09-09 19:08:12 +04:00
|
|
|
(isa<NamespaceDecl>(Owner) &&
|
2009-01-07 03:43:41 +03:00
|
|
|
cast<NamespaceDecl>(Owner)->getDeclName()))) {
|
|
|
|
Diag(Record->getLocation(), diag::err_anonymous_union_not_static);
|
|
|
|
Invalid = true;
|
|
|
|
|
|
|
|
// Recover by adding 'static'.
|
2009-08-04 00:12:06 +04:00
|
|
|
DS.SetStorageClassSpec(DeclSpec::SCS_static, SourceLocation(),
|
|
|
|
PrevSpec, DiagID);
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2009-01-07 03:43:41 +03:00
|
|
|
// C++ [class.union]p3:
|
|
|
|
// A storage class is not allowed in a declaration of an
|
|
|
|
// anonymous union in a class scope.
|
|
|
|
else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
|
|
|
|
isa<RecordDecl>(Owner)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(DS.getStorageClassSpecLoc(),
|
2009-01-07 03:43:41 +03:00
|
|
|
diag::err_anonymous_union_with_storage_spec);
|
|
|
|
Invalid = true;
|
|
|
|
|
|
|
|
// Recover by removing the storage specifier.
|
|
|
|
DS.SetStorageClassSpec(DeclSpec::SCS_unspecified, SourceLocation(),
|
2009-08-04 00:12:06 +04:00
|
|
|
PrevSpec, DiagID);
|
2009-01-07 03:43:41 +03:00
|
|
|
}
|
2009-01-07 22:46:03 +03:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
// C++ [class.union]p2:
|
2009-01-07 22:46:03 +03:00
|
|
|
// The member-specification of an anonymous union shall only
|
|
|
|
// define non-static data members. [Note: nested types and
|
|
|
|
// functions cannot be declared within an anonymous union. ]
|
2009-06-30 06:36:12 +04:00
|
|
|
for (DeclContext::decl_iterator Mem = Record->decls_begin(),
|
|
|
|
MemEnd = Record->decls_end();
|
2009-01-07 22:46:03 +03:00
|
|
|
Mem != MemEnd; ++Mem) {
|
|
|
|
if (FieldDecl *FD = dyn_cast<FieldDecl>(*Mem)) {
|
|
|
|
// C++ [class.union]p3:
|
|
|
|
// An anonymous union shall not have private or protected
|
|
|
|
// members (clause 11).
|
2010-05-22 00:45:30 +04:00
|
|
|
assert(FD->getAccess() != AS_none);
|
|
|
|
if (FD->getAccess() != AS_public) {
|
2009-01-07 22:46:03 +03:00
|
|
|
Diag(FD->getLocation(), diag::err_anonymous_record_nonpublic_member)
|
|
|
|
<< (int)Record->isUnion() << (int)(FD->getAccess() == AS_protected);
|
|
|
|
Invalid = true;
|
|
|
|
}
|
2010-08-16 21:27:08 +04:00
|
|
|
|
|
|
|
if (CheckNontrivialField(FD))
|
|
|
|
Invalid = true;
|
2009-01-07 22:46:03 +03:00
|
|
|
} else if ((*Mem)->isImplicit()) {
|
|
|
|
// Any implicit members are fine.
|
2009-02-03 03:34:39 +03:00
|
|
|
} else if (isa<TagDecl>(*Mem) && (*Mem)->getDeclContext() != Record) {
|
|
|
|
// This is a type that showed up in an
|
|
|
|
// elaborated-type-specifier inside the anonymous struct or
|
|
|
|
// union, but which actually declares a type outside of the
|
|
|
|
// anonymous struct or union. It's okay.
|
2009-01-07 22:46:03 +03:00
|
|
|
} else if (RecordDecl *MemRecord = dyn_cast<RecordDecl>(*Mem)) {
|
|
|
|
if (!MemRecord->isAnonymousStructOrUnion() &&
|
|
|
|
MemRecord->getDeclName()) {
|
2010-09-08 15:32:25 +04:00
|
|
|
// Visual C++ allows type definition in anonymous struct or union.
|
|
|
|
if (getLangOptions().Microsoft)
|
|
|
|
Diag(MemRecord->getLocation(), diag::ext_anonymous_record_with_type)
|
|
|
|
<< (int)Record->isUnion();
|
|
|
|
else {
|
|
|
|
// This is a nested type declaration.
|
|
|
|
Diag(MemRecord->getLocation(), diag::err_anonymous_record_with_type)
|
|
|
|
<< (int)Record->isUnion();
|
|
|
|
Invalid = true;
|
|
|
|
}
|
2009-01-07 22:46:03 +03:00
|
|
|
}
|
2010-06-05 09:09:32 +04:00
|
|
|
} else if (isa<AccessSpecDecl>(*Mem)) {
|
|
|
|
// Any access specifier is fine.
|
2009-01-07 22:46:03 +03:00
|
|
|
} else {
|
|
|
|
// We have something that isn't a non-static data
|
|
|
|
// member. Complain about it.
|
|
|
|
unsigned DK = diag::err_anonymous_record_bad_member;
|
|
|
|
if (isa<TypeDecl>(*Mem))
|
|
|
|
DK = diag::err_anonymous_record_with_type;
|
|
|
|
else if (isa<FunctionDecl>(*Mem))
|
|
|
|
DK = diag::err_anonymous_record_with_function;
|
|
|
|
else if (isa<VarDecl>(*Mem))
|
|
|
|
DK = diag::err_anonymous_record_with_static;
|
2010-09-08 15:32:25 +04:00
|
|
|
|
|
|
|
// Visual C++ allows type definition in anonymous struct or union.
|
|
|
|
if (getLangOptions().Microsoft &&
|
|
|
|
DK == diag::err_anonymous_record_with_type)
|
|
|
|
Diag((*Mem)->getLocation(), diag::ext_anonymous_record_with_type)
|
2009-01-07 22:46:03 +03:00
|
|
|
<< (int)Record->isUnion();
|
2010-09-08 15:32:25 +04:00
|
|
|
else {
|
|
|
|
Diag((*Mem)->getLocation(), DK)
|
|
|
|
<< (int)Record->isUnion();
|
2009-01-07 22:46:03 +03:00
|
|
|
Invalid = true;
|
2010-09-08 15:32:25 +04:00
|
|
|
}
|
2009-01-07 22:46:03 +03:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2009-01-07 03:43:41 +03:00
|
|
|
|
|
|
|
if (!Record->isUnion() && !Owner->isRecord()) {
|
2009-01-13 01:49:06 +03:00
|
|
|
Diag(Record->getLocation(), diag::err_anonymous_struct_not_member)
|
|
|
|
<< (int)getLangOptions().CPlusPlus;
|
2009-01-07 03:43:41 +03:00
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
|
2009-10-23 03:31:08 +04:00
|
|
|
// Mock up a declarator.
|
|
|
|
Declarator Dc(DS, Declarator::TypeNameContext);
|
2010-06-05 03:28:52 +04:00
|
|
|
TypeSourceInfo *TInfo = GetTypeForDeclarator(Dc, S);
|
2009-12-07 05:54:59 +03:00
|
|
|
assert(TInfo && "couldn't build declarator info for anonymous struct/union");
|
2009-10-23 03:31:08 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
// Create a declaration for this anonymous struct/union.
|
2009-01-20 04:17:11 +03:00
|
|
|
NamedDecl *Anon = 0;
|
2009-01-07 03:43:41 +03:00
|
|
|
if (RecordDecl *OwningClass = dyn_cast<RecordDecl>(Owner)) {
|
|
|
|
Anon = FieldDecl::Create(Context, OwningClass, Record->getLocation(),
|
2009-09-09 19:08:12 +04:00
|
|
|
/*IdentifierInfo=*/0,
|
2009-08-21 04:31:54 +04:00
|
|
|
Context.getTypeDeclType(Record),
|
2009-12-07 05:54:59 +03:00
|
|
|
TInfo,
|
2009-08-21 04:31:54 +04:00
|
|
|
/*BitWidth=*/0, /*Mutable=*/false);
|
2010-05-22 00:45:30 +04:00
|
|
|
Anon->setAccess(AS);
|
2010-09-29 00:38:10 +04:00
|
|
|
if (getLangOptions().CPlusPlus)
|
2009-01-07 22:46:03 +03:00
|
|
|
FieldCollector->Add(cast<FieldDecl>(Anon));
|
2009-01-07 03:43:41 +03:00
|
|
|
} else {
|
2010-04-20 02:54:31 +04:00
|
|
|
DeclSpec::SCS SCSpec = DS.getStorageClassSpec();
|
|
|
|
assert(SCSpec != DeclSpec::SCS_typedef &&
|
|
|
|
"Parser allowed 'typedef' as storage class VarDecl.");
|
2010-07-30 20:47:02 +04:00
|
|
|
VarDecl::StorageClass SC = StorageClassSpecToVarDeclStorageClass(SCSpec);
|
2010-04-20 02:54:31 +04:00
|
|
|
if (SCSpec == DeclSpec::SCS_mutable) {
|
2009-01-07 03:43:41 +03:00
|
|
|
// mutable can only appear on non-static class members, so it's always
|
|
|
|
// an error here
|
|
|
|
Diag(Record->getLocation(), diag::err_mutable_nonmember);
|
|
|
|
Invalid = true;
|
2010-08-26 07:08:43 +04:00
|
|
|
SC = SC_None;
|
2009-01-07 03:43:41 +03:00
|
|
|
}
|
2010-04-20 02:54:31 +04:00
|
|
|
SCSpec = DS.getStorageClassSpecAsWritten();
|
|
|
|
VarDecl::StorageClass SCAsWritten
|
2010-07-30 20:47:02 +04:00
|
|
|
= StorageClassSpecToVarDeclStorageClass(SCSpec);
|
2009-01-07 03:43:41 +03:00
|
|
|
|
|
|
|
Anon = VarDecl::Create(Context, Owner, Record->getLocation(),
|
2009-09-09 19:08:12 +04:00
|
|
|
/*IdentifierInfo=*/0,
|
2009-08-21 04:31:54 +04:00
|
|
|
Context.getTypeDeclType(Record),
|
2010-04-20 02:54:31 +04:00
|
|
|
TInfo, SC, SCAsWritten);
|
2009-01-07 03:43:41 +03:00
|
|
|
}
|
2009-01-07 22:46:03 +03:00
|
|
|
Anon->setImplicit();
|
2009-01-07 03:43:41 +03:00
|
|
|
|
|
|
|
// Add the anonymous struct/union object to the current
|
|
|
|
// context. We'll be referencing this object when we refer to one of
|
|
|
|
// its members.
|
2009-06-30 06:36:12 +04:00
|
|
|
Owner->addDecl(Anon);
|
2010-05-03 19:18:25 +04:00
|
|
|
|
2009-01-07 03:43:41 +03:00
|
|
|
// Inject the members of the anonymous struct/union into the owning
|
|
|
|
// context and into the identifier resolver chain for name lookup
|
|
|
|
// purposes.
|
2010-05-22 00:45:30 +04:00
|
|
|
if (InjectAnonymousStructOrUnionMembers(*this, S, Owner, Record, AS))
|
2009-01-13 01:49:06 +03:00
|
|
|
Invalid = true;
|
2009-01-07 03:43:41 +03:00
|
|
|
|
|
|
|
// Mark this as an anonymous struct/union type. Note that we do not
|
|
|
|
// do this until after we have already checked and injected the
|
|
|
|
// members of this anonymous struct/union type, because otherwise
|
|
|
|
// the members could be injected twice: once by DeclContext when it
|
|
|
|
// builds its lookup table, and once by
|
2009-09-09 19:08:12 +04:00
|
|
|
// InjectAnonymousStructOrUnionMembers.
|
2009-01-07 03:43:41 +03:00
|
|
|
Record->setAnonymousStructOrUnion(true);
|
|
|
|
|
|
|
|
if (Invalid)
|
|
|
|
Anon->setInvalidDecl();
|
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
return Anon;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2007-09-02 06:04:30 +04:00
|
|
|
|
2008-11-18 01:58:34 +03:00
|
|
|
/// GetNameForDeclarator - Determine the full declaration name for the
|
|
|
|
/// given Declarator.
|
2010-08-12 02:01:17 +04:00
|
|
|
DeclarationNameInfo Sema::GetNameForDeclarator(Declarator &D) {
|
2009-11-03 19:56:39 +03:00
|
|
|
return GetNameFromUnqualifiedId(D.getName());
|
|
|
|
}
|
2008-11-18 01:58:34 +03:00
|
|
|
|
2010-08-12 02:01:17 +04:00
|
|
|
/// \brief Retrieves the declaration name from a parsed unqualified-id.
|
|
|
|
DeclarationNameInfo
|
|
|
|
Sema::GetNameFromUnqualifiedId(const UnqualifiedId &Name) {
|
|
|
|
DeclarationNameInfo NameInfo;
|
|
|
|
NameInfo.setLoc(Name.StartLocation);
|
|
|
|
|
2009-11-03 19:56:39 +03:00
|
|
|
switch (Name.getKind()) {
|
2010-01-13 20:31:36 +03:00
|
|
|
|
2010-08-12 02:01:17 +04:00
|
|
|
case UnqualifiedId::IK_Identifier:
|
|
|
|
NameInfo.setName(Name.Identifier);
|
|
|
|
NameInfo.setLoc(Name.StartLocation);
|
|
|
|
return NameInfo;
|
|
|
|
|
|
|
|
case UnqualifiedId::IK_OperatorFunctionId:
|
|
|
|
NameInfo.setName(Context.DeclarationNames.getCXXOperatorName(
|
|
|
|
Name.OperatorFunctionId.Operator));
|
|
|
|
NameInfo.setLoc(Name.StartLocation);
|
|
|
|
NameInfo.getInfo().CXXOperatorName.BeginOpNameLoc
|
|
|
|
= Name.OperatorFunctionId.SymbolLocations[0];
|
|
|
|
NameInfo.getInfo().CXXOperatorName.EndOpNameLoc
|
|
|
|
= Name.EndLocation.getRawEncoding();
|
|
|
|
return NameInfo;
|
|
|
|
|
|
|
|
case UnqualifiedId::IK_LiteralOperatorId:
|
|
|
|
NameInfo.setName(Context.DeclarationNames.getCXXLiteralOperatorName(
|
|
|
|
Name.Identifier));
|
|
|
|
NameInfo.setLoc(Name.StartLocation);
|
|
|
|
NameInfo.setCXXLiteralOperatorNameLoc(Name.EndLocation);
|
|
|
|
return NameInfo;
|
|
|
|
|
|
|
|
case UnqualifiedId::IK_ConversionFunctionId: {
|
|
|
|
TypeSourceInfo *TInfo;
|
|
|
|
QualType Ty = GetTypeFromParser(Name.ConversionFunctionId, &TInfo);
|
|
|
|
if (Ty.isNull())
|
|
|
|
return DeclarationNameInfo();
|
|
|
|
NameInfo.setName(Context.DeclarationNames.getCXXConversionFunctionName(
|
|
|
|
Context.getCanonicalType(Ty)));
|
|
|
|
NameInfo.setLoc(Name.StartLocation);
|
|
|
|
NameInfo.setNamedTypeInfo(TInfo);
|
|
|
|
return NameInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UnqualifiedId::IK_ConstructorName: {
|
|
|
|
TypeSourceInfo *TInfo;
|
|
|
|
QualType Ty = GetTypeFromParser(Name.ConstructorName, &TInfo);
|
|
|
|
if (Ty.isNull())
|
|
|
|
return DeclarationNameInfo();
|
|
|
|
NameInfo.setName(Context.DeclarationNames.getCXXConstructorName(
|
|
|
|
Context.getCanonicalType(Ty)));
|
|
|
|
NameInfo.setLoc(Name.StartLocation);
|
|
|
|
NameInfo.setNamedTypeInfo(TInfo);
|
|
|
|
return NameInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UnqualifiedId::IK_ConstructorTemplateId: {
|
|
|
|
// In well-formed code, we can only have a constructor
|
|
|
|
// template-id that refers to the current context, so go there
|
|
|
|
// to find the actual type being constructed.
|
|
|
|
CXXRecordDecl *CurClass = dyn_cast<CXXRecordDecl>(CurContext);
|
|
|
|
if (!CurClass || CurClass->getIdentifier() != Name.TemplateId->Name)
|
|
|
|
return DeclarationNameInfo();
|
|
|
|
|
|
|
|
// Determine the type of the class being constructed.
|
|
|
|
QualType CurClassType = Context.getTypeDeclType(CurClass);
|
|
|
|
|
|
|
|
// FIXME: Check two things: that the template-id names the same type as
|
|
|
|
// CurClassType, and that the template-id does not occur when the name
|
|
|
|
// was qualified.
|
|
|
|
|
|
|
|
NameInfo.setName(Context.DeclarationNames.getCXXConstructorName(
|
|
|
|
Context.getCanonicalType(CurClassType)));
|
|
|
|
NameInfo.setLoc(Name.StartLocation);
|
|
|
|
// FIXME: should we retrieve TypeSourceInfo?
|
|
|
|
NameInfo.setNamedTypeInfo(0);
|
|
|
|
return NameInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UnqualifiedId::IK_DestructorName: {
|
|
|
|
TypeSourceInfo *TInfo;
|
|
|
|
QualType Ty = GetTypeFromParser(Name.DestructorName, &TInfo);
|
|
|
|
if (Ty.isNull())
|
|
|
|
return DeclarationNameInfo();
|
|
|
|
NameInfo.setName(Context.DeclarationNames.getCXXDestructorName(
|
|
|
|
Context.getCanonicalType(Ty)));
|
|
|
|
NameInfo.setLoc(Name.StartLocation);
|
|
|
|
NameInfo.setNamedTypeInfo(TInfo);
|
|
|
|
return NameInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UnqualifiedId::IK_TemplateId: {
|
2010-08-23 11:28:44 +04:00
|
|
|
TemplateName TName = Name.TemplateId->Template.get();
|
2010-08-12 02:01:17 +04:00
|
|
|
SourceLocation TNameLoc = Name.TemplateId->TemplateNameLoc;
|
|
|
|
return Context.getNameForTemplate(TName, TNameLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // switch (Name.getKind())
|
|
|
|
|
2008-11-18 01:58:34 +03:00
|
|
|
assert(false && "Unknown name kind");
|
2010-08-12 02:01:17 +04:00
|
|
|
return DeclarationNameInfo();
|
2008-11-18 01:58:34 +03:00
|
|
|
}
|
|
|
|
|
2009-02-06 20:46:57 +03:00
|
|
|
/// isNearlyMatchingFunction - Determine whether the C++ functions
|
|
|
|
/// Declaration and Definition are "nearly" matching. This heuristic
|
|
|
|
/// is used to improve diagnostics in the case where an out-of-line
|
|
|
|
/// function definition doesn't match any declaration within
|
|
|
|
/// the class or namespace.
|
|
|
|
static bool isNearlyMatchingFunction(ASTContext &Context,
|
|
|
|
FunctionDecl *Declaration,
|
|
|
|
FunctionDecl *Definition) {
|
2008-12-16 02:53:10 +03:00
|
|
|
if (Declaration->param_size() != Definition->param_size())
|
|
|
|
return false;
|
|
|
|
for (unsigned Idx = 0; Idx < Declaration->param_size(); ++Idx) {
|
|
|
|
QualType DeclParamTy = Declaration->getParamDecl(Idx)->getType();
|
|
|
|
QualType DefParamTy = Definition->getParamDecl(Idx)->getType();
|
|
|
|
|
First part of changes to eliminate problems with cv-qualifiers and
sugared types. The basic problem is that our qualifier accessors
(getQualifiers, getCVRQualifiers, isConstQualified, etc.) only look at
the current QualType and not at any qualifiers that come from sugared
types, meaning that we won't see these qualifiers through, e.g.,
typedefs:
typedef const int CInt;
typedef CInt Self;
Self.isConstQualified() currently returns false!
Various bugs (e.g., PR5383) have cropped up all over the front end due
to such problems. I'm addressing this problem by splitting each
qualifier accessor into two versions:
- the "local" version only returns qualifiers on this particular
QualType instance
- the "normal" version that will eventually combine qualifiers from this
QualType instance with the qualifiers on the canonical type to
produce the full set of qualifiers.
This commit adds the local versions and switches a few callers from
the "normal" version (e.g., isConstQualified) over to the "local"
version (e.g., isLocalConstQualified) when that is the right thing to
do, e.g., because we're printing or serializing the qualifiers. Also,
switch a bunch of
Context.getCanonicalType(T1).getUnqualifiedType() == Context.getCanonicalType(T2).getQualifiedType()
expressions over to
Context.hasSameUnqualifiedType(T1, T2)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@88969 91177308-0d34-0410-b5e6-96231b3b80d8
2009-11-17 00:35:15 +03:00
|
|
|
if (!Context.hasSameUnqualifiedType(DeclParamTy.getNonReferenceType(),
|
|
|
|
DefParamTy.getNonReferenceType()))
|
2008-12-16 02:53:10 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-30 03:50:39 +04:00
|
|
|
/// NeedsRebuildingInCurrentInstantiation - Checks whether the given
|
|
|
|
/// declarator needs to be rebuilt in the current instantiation.
|
|
|
|
/// Any bits of declarator which appear before the name are valid for
|
|
|
|
/// consideration here. That's specifically the type in the decl spec
|
|
|
|
/// and the base type in any member-pointer chunks.
|
|
|
|
static bool RebuildDeclaratorInCurrentInstantiation(Sema &S, Declarator &D,
|
|
|
|
DeclarationName Name) {
|
|
|
|
// The types we specifically need to rebuild are:
|
|
|
|
// - typenames, typeofs, and decltypes
|
|
|
|
// - types which will become injected class names
|
|
|
|
// Of course, we also need to rebuild any type referencing such a
|
|
|
|
// type. It's safest to just say "dependent", but we call out a
|
|
|
|
// few cases here.
|
|
|
|
|
|
|
|
DeclSpec &DS = D.getMutableDeclSpec();
|
|
|
|
switch (DS.getTypeSpecType()) {
|
|
|
|
case DeclSpec::TST_typename:
|
|
|
|
case DeclSpec::TST_typeofType:
|
|
|
|
case DeclSpec::TST_decltype: {
|
|
|
|
// Grab the type from the parser.
|
|
|
|
TypeSourceInfo *TSI = 0;
|
2010-08-24 09:47:05 +04:00
|
|
|
QualType T = S.GetTypeFromParser(DS.getRepAsType(), &TSI);
|
2010-04-30 03:50:39 +04:00
|
|
|
if (T.isNull() || !T->isDependentType()) break;
|
|
|
|
|
|
|
|
// Make sure there's a type source info. This isn't really much
|
|
|
|
// of a waste; most dependent types should have type source info
|
|
|
|
// attached already.
|
|
|
|
if (!TSI)
|
|
|
|
TSI = S.Context.getTrivialTypeSourceInfo(T, DS.getTypeSpecTypeLoc());
|
|
|
|
|
|
|
|
// Rebuild the type in the current instantiation.
|
|
|
|
TSI = S.RebuildTypeInCurrentInstantiation(TSI, D.getIdentifierLoc(), Name);
|
|
|
|
if (!TSI) return true;
|
|
|
|
|
|
|
|
// Store the new type back in the decl spec.
|
2010-08-24 09:47:05 +04:00
|
|
|
ParsedType LocType = S.CreateParsedType(TSI->getType(), TSI);
|
|
|
|
DS.UpdateTypeRep(LocType);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeclSpec::TST_typeofExpr: {
|
|
|
|
Expr *E = DS.getRepAsExpr();
|
2010-08-24 10:29:42 +04:00
|
|
|
ExprResult Result = S.RebuildExprInCurrentInstantiation(E);
|
2010-08-24 09:47:05 +04:00
|
|
|
if (Result.isInvalid()) return true;
|
|
|
|
DS.UpdateExprRep(Result.get());
|
2010-04-30 03:50:39 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Nothing to do for these decl specs.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It doesn't matter what order we do this in.
|
|
|
|
for (unsigned I = 0, E = D.getNumTypeObjects(); I != E; ++I) {
|
|
|
|
DeclaratorChunk &Chunk = D.getTypeObject(I);
|
|
|
|
|
|
|
|
// The only type information in the declarator which can come
|
|
|
|
// before the declaration name is the base type of a member
|
|
|
|
// pointer.
|
|
|
|
if (Chunk.Kind != DeclaratorChunk::MemberPointer)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Rebuild the scope specifier in-place.
|
|
|
|
CXXScopeSpec &SS = Chunk.Mem.Scope();
|
|
|
|
if (S.RebuildNestedNameSpecifierInCurrentInstantiation(SS))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-24 11:21:54 +04:00
|
|
|
Decl *Sema::ActOnDeclarator(Scope *S, Declarator &D) {
|
|
|
|
return HandleDeclarator(S, D, MultiTemplateParamsArg(*this), false);
|
|
|
|
}
|
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *Sema::HandleDeclarator(Scope *S, Declarator &D,
|
|
|
|
MultiTemplateParamsArg TemplateParamLists,
|
|
|
|
bool IsFunctionDefinition) {
|
2010-08-12 02:01:17 +04:00
|
|
|
// TODO: consider using NameInfo for diagnostic.
|
|
|
|
DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
|
|
|
|
DeclarationName Name = NameInfo.getName();
|
2008-11-18 01:58:34 +03:00
|
|
|
|
2007-07-25 04:24:17 +04:00
|
|
|
// All of these full declarators require an identifier. If it doesn't have
|
|
|
|
// one, the ParsedFreeStandingDeclSpec action should be used.
|
2008-11-18 01:58:34 +03:00
|
|
|
if (!Name) {
|
2009-04-25 12:06:05 +04:00
|
|
|
if (!D.isInvalidType()) // Reject this if we think it is valid.
|
2008-11-11 09:13:16 +03:00
|
|
|
Diag(D.getDeclSpec().getSourceRange().getBegin(),
|
2008-11-19 08:08:23 +03:00
|
|
|
diag::err_declarator_need_ident)
|
|
|
|
<< D.getDeclSpec().getSourceRange() << D.getSourceRange();
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2007-07-25 04:24:17 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-08-26 10:24:45 +04:00
|
|
|
// The scope passed in may not be a decl scope. Zip up the scope tree until
|
|
|
|
// we find one that is.
|
2008-12-11 19:49:14 +03:00
|
|
|
while ((S->getFlags() & Scope::DeclScope) == 0 ||
|
2009-02-04 22:02:06 +03:00
|
|
|
(S->getFlags() & Scope::TemplateParamScope) != 0)
|
2007-08-26 10:24:45 +04:00
|
|
|
S = S->getParent();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-30 03:50:39 +04:00
|
|
|
DeclContext *DC = CurContext;
|
|
|
|
if (D.getCXXScopeSpec().isInvalid())
|
|
|
|
D.setInvalidType();
|
|
|
|
else if (D.getCXXScopeSpec().isSet()) {
|
|
|
|
bool EnteringContext = !D.getDeclSpec().isFriendSpecified();
|
|
|
|
DC = computeDeclContext(D.getCXXScopeSpec(), EnteringContext);
|
|
|
|
if (!DC) {
|
|
|
|
// If we could not compute the declaration context, it's because the
|
|
|
|
// declaration context is dependent but does not refer to a class,
|
|
|
|
// class template, or class template partial specialization. Complain
|
|
|
|
// and return early, to avoid the coming semantic disaster.
|
|
|
|
Diag(D.getIdentifierLoc(),
|
|
|
|
diag::err_template_qualified_declarator_no_match)
|
|
|
|
<< (NestedNameSpecifier*)D.getCXXScopeSpec().getScopeRep()
|
|
|
|
<< D.getCXXScopeSpec().getRange();
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2010-04-30 03:50:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsDependentContext = DC->isDependentContext();
|
|
|
|
|
|
|
|
if (!IsDependentContext &&
|
2010-05-01 04:40:08 +04:00
|
|
|
RequireCompleteDeclContext(D.getCXXScopeSpec(), DC))
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2010-04-30 03:50:39 +04:00
|
|
|
|
2010-10-14 02:19:53 +04:00
|
|
|
if (isa<CXXRecordDecl>(DC)) {
|
|
|
|
if (!cast<CXXRecordDecl>(DC)->hasDefinition()) {
|
|
|
|
Diag(D.getIdentifierLoc(),
|
|
|
|
diag::err_member_def_undefined_record)
|
|
|
|
<< Name << DC << D.getCXXScopeSpec().getRange();
|
|
|
|
D.setInvalidType();
|
|
|
|
} else if (isa<CXXRecordDecl>(CurContext) &&
|
|
|
|
!D.getDeclSpec().isFriendSpecified()) {
|
|
|
|
// The user provided a superfluous scope specifier inside a class
|
|
|
|
// definition:
|
|
|
|
//
|
|
|
|
// class X {
|
|
|
|
// void X::f();
|
|
|
|
// };
|
|
|
|
if (CurContext->Equals(DC))
|
|
|
|
Diag(D.getIdentifierLoc(), diag::warn_member_extra_qualification)
|
|
|
|
<< Name << FixItHint::CreateRemoval(D.getCXXScopeSpec().getRange());
|
|
|
|
else
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_member_qualification)
|
|
|
|
<< Name << D.getCXXScopeSpec().getRange();
|
|
|
|
|
|
|
|
// Pretend that this qualifier was not here.
|
|
|
|
D.getCXXScopeSpec().clear();
|
|
|
|
}
|
2010-04-30 03:50:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether we need to rebuild the type of the given
|
|
|
|
// declaration in the current instantiation.
|
|
|
|
if (EnteringContext && IsDependentContext &&
|
|
|
|
TemplateParamLists.size() != 0) {
|
|
|
|
ContextRAII SavedContext(*this, DC);
|
|
|
|
if (RebuildDeclaratorInCurrentInstantiation(*this, D, Name))
|
|
|
|
D.setInvalidType();
|
2009-08-06 20:20:37 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-01-20 04:17:11 +03:00
|
|
|
NamedDecl *New;
|
2009-02-16 20:45:42 +03:00
|
|
|
|
2010-06-05 03:28:52 +04:00
|
|
|
TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
|
|
|
|
QualType R = TInfo->getType();
|
2009-02-24 23:03:32 +03:00
|
|
|
|
2010-08-12 02:01:17 +04:00
|
|
|
LookupResult Previous(*this, NameInfo, LookupOrdinaryName,
|
2009-11-19 01:49:29 +03:00
|
|
|
ForRedeclaration);
|
|
|
|
|
2008-11-08 20:17:31 +03:00
|
|
|
// See if this is a redefinition of a variable in the same scope.
|
2010-04-30 03:50:39 +04:00
|
|
|
if (!D.getCXXScopeSpec().isSet()) {
|
2009-11-19 01:49:29 +03:00
|
|
|
bool IsLinkageLookup = false;
|
2009-02-24 23:03:32 +03:00
|
|
|
|
|
|
|
// If the declaration we're planning to build will be a function
|
|
|
|
// or object with linkage, then look for another declaration with
|
|
|
|
// linkage (C99 6.2.2p4-5 and C++ [basic.link]p6).
|
|
|
|
if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
|
|
|
|
/* Do nothing*/;
|
|
|
|
else if (R->isFunctionType()) {
|
2009-07-07 21:00:05 +04:00
|
|
|
if (CurContext->isFunctionOrMethod() ||
|
|
|
|
D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static)
|
2009-11-19 01:49:29 +03:00
|
|
|
IsLinkageLookup = true;
|
2009-02-24 23:03:32 +03:00
|
|
|
} else if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern)
|
2009-11-19 01:49:29 +03:00
|
|
|
IsLinkageLookup = true;
|
2010-08-31 04:36:30 +04:00
|
|
|
else if (CurContext->getRedeclContext()->isTranslationUnit() &&
|
2009-07-07 21:00:05 +04:00
|
|
|
D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static)
|
2009-11-19 01:49:29 +03:00
|
|
|
IsLinkageLookup = true;
|
2009-02-24 23:03:32 +03:00
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (IsLinkageLookup)
|
|
|
|
Previous.clear(LookupRedeclarationWithLinkage);
|
2009-11-17 05:14:36 +03:00
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
LookupName(Previous, S, /* CreateBuiltins = */ IsLinkageLookup);
|
2008-11-08 20:17:31 +03:00
|
|
|
} else { // Something like "int foo::x;"
|
2009-11-19 01:49:29 +03:00
|
|
|
LookupQualifiedName(Previous, DC);
|
|
|
|
|
|
|
|
// Don't consider using declarations as previous declarations for
|
|
|
|
// out-of-line members.
|
|
|
|
RemoveUsingDecls(Previous);
|
2008-11-08 20:17:31 +03:00
|
|
|
|
|
|
|
// C++ 7.3.1.2p2:
|
|
|
|
// Members (including explicit specializations of templates) of a named
|
|
|
|
// namespace can also be defined outside that namespace by explicit
|
|
|
|
// qualification of the name being defined, provided that the entity being
|
|
|
|
// defined was already declared in the namespace and the definition appears
|
|
|
|
// after the point of declaration in a namespace that encloses the
|
|
|
|
// declarations namespace.
|
|
|
|
//
|
2008-12-16 02:53:10 +03:00
|
|
|
// Note that we only check the context at this point. We don't yet
|
|
|
|
// have enough information to make sure that PrevDecl is actually
|
|
|
|
// the declaration we want to match. For example, given:
|
|
|
|
//
|
2008-12-12 11:25:50 +03:00
|
|
|
// class X {
|
|
|
|
// void f();
|
2008-12-16 02:53:10 +03:00
|
|
|
// void f(float);
|
2008-12-12 11:25:50 +03:00
|
|
|
// };
|
|
|
|
//
|
2008-12-16 02:53:10 +03:00
|
|
|
// void X::f(int) { } // ill-formed
|
|
|
|
//
|
|
|
|
// In this case, PrevDecl will point to the overload set
|
|
|
|
// containing the two f's declared in X, but neither of them
|
2009-09-09 19:08:12 +04:00
|
|
|
// matches.
|
2009-02-06 20:46:57 +03:00
|
|
|
|
|
|
|
// First check whether we named the global scope.
|
|
|
|
if (isa<TranslationUnitDecl>(DC)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(D.getIdentifierLoc(), diag::err_invalid_declarator_global_scope)
|
2009-02-06 20:46:57 +03:00
|
|
|
<< Name << D.getCXXScopeSpec().getRange();
|
2009-11-08 14:36:54 +03:00
|
|
|
} else {
|
|
|
|
DeclContext *Cur = CurContext;
|
|
|
|
while (isa<LinkageSpecDecl>(Cur))
|
|
|
|
Cur = Cur->getParent();
|
|
|
|
if (!Cur->Encloses(DC)) {
|
|
|
|
// The qualifying scope doesn't enclose the original declaration.
|
|
|
|
// Emit diagnostic based on current scope.
|
|
|
|
SourceLocation L = D.getIdentifierLoc();
|
|
|
|
SourceRange R = D.getCXXScopeSpec().getRange();
|
|
|
|
if (isa<FunctionDecl>(Cur))
|
|
|
|
Diag(L, diag::err_invalid_declarator_in_function) << Name << R;
|
|
|
|
else
|
|
|
|
Diag(L, diag::err_invalid_declarator_scope)
|
|
|
|
<< Name << cast<NamedDecl>(DC) << R;
|
|
|
|
D.setInvalidType();
|
|
|
|
}
|
2008-11-08 20:17:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Previous.isSingleResult() &&
|
|
|
|
Previous.getFoundDecl()->isTemplateParameter()) {
|
2008-12-05 21:15:24 +03:00
|
|
|
// Maybe we will complain about the shadowed template parameter.
|
2009-09-09 19:08:12 +04:00
|
|
|
if (!D.isInvalidType())
|
2009-11-19 01:49:29 +03:00
|
|
|
if (DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
|
|
|
|
Previous.getFoundDecl()))
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-12-05 21:15:24 +03:00
|
|
|
// Just pretend that we didn't see the previous declaration.
|
2009-11-19 01:49:29 +03:00
|
|
|
Previous.clear();
|
2008-12-05 21:15:24 +03:00
|
|
|
}
|
|
|
|
|
2008-04-14 01:07:44 +04:00
|
|
|
// In C++, the previous declaration we find might be a tag type
|
|
|
|
// (class or enum). In this case, the new declaration will hide the
|
2009-01-28 20:15:10 +03:00
|
|
|
// tag type. Note that this does does not apply if we're declaring a
|
|
|
|
// typedef (C++ [dcl.typedef]p4).
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Previous.isSingleTagDecl() &&
|
2009-01-28 20:15:10 +03:00
|
|
|
D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef)
|
2009-11-19 01:49:29 +03:00
|
|
|
Previous.clear();
|
2008-04-14 01:07:44 +04:00
|
|
|
|
2009-02-16 20:45:42 +03:00
|
|
|
bool Redeclaration = false;
|
2007-07-11 21:01:13 +04:00
|
|
|
if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
|
2009-06-24 03:11:28 +04:00
|
|
|
if (TemplateParamLists.size()) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_template_typedef);
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2009-06-24 03:11:28 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-12-07 05:54:59 +03:00
|
|
|
New = ActOnTypedefDeclarator(S, D, DC, R, TInfo, Previous, Redeclaration);
|
2009-02-24 23:03:32 +03:00
|
|
|
} else if (R->isFunctionType()) {
|
2009-12-07 05:54:59 +03:00
|
|
|
New = ActOnFunctionDeclarator(S, D, DC, R, TInfo, Previous,
|
2009-06-24 03:11:28 +04:00
|
|
|
move(TemplateParamLists),
|
2009-04-25 12:06:05 +04:00
|
|
|
IsFunctionDefinition, Redeclaration);
|
2007-07-11 21:01:13 +04:00
|
|
|
} else {
|
2009-12-07 05:54:59 +03:00
|
|
|
New = ActOnVariableDeclarator(S, D, DC, R, TInfo, Previous,
|
2009-07-22 21:18:37 +04:00
|
|
|
move(TemplateParamLists),
|
|
|
|
Redeclaration);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2009-01-16 04:13:29 +03:00
|
|
|
|
|
|
|
if (New == 0)
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-25 03:14:47 +04:00
|
|
|
// If this has an identifier and is not an invalid redeclaration or
|
|
|
|
// function template specialization, add it to the scope stack.
|
2010-07-24 04:10:38 +04:00
|
|
|
if (New->getDeclName() && !(Redeclaration && New->isInvalidDecl()))
|
2008-04-12 04:47:19 +04:00
|
|
|
PushOnScopeChains(New, S);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
return New;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-02-21 03:44:51 +03:00
|
|
|
/// TryToFixInvalidVariablyModifiedType - Helper method to turn variable array
|
|
|
|
/// types into constant array types in certain situations which would otherwise
|
|
|
|
/// be errors (for GCC compatibility).
|
|
|
|
static QualType TryToFixInvalidVariablyModifiedType(QualType T,
|
|
|
|
ASTContext &Context,
|
2010-08-18 04:39:00 +04:00
|
|
|
bool &SizeIsNegative,
|
|
|
|
llvm::APSInt &Oversized) {
|
2009-02-21 03:44:51 +03:00
|
|
|
// This method tries to turn a variable array into a constant
|
|
|
|
// array even when the size isn't an ICE. This is necessary
|
|
|
|
// for compatibility with code that depends on gcc's buggy
|
|
|
|
// constant expression folding, like struct {char x[(int)(char*)2];}
|
|
|
|
SizeIsNegative = false;
|
2010-08-18 04:39:00 +04:00
|
|
|
Oversized = 0;
|
|
|
|
|
|
|
|
if (T->isDependentType())
|
|
|
|
return QualType();
|
|
|
|
|
2009-09-24 23:53:00 +04:00
|
|
|
QualifierCollector Qs;
|
|
|
|
const Type *Ty = Qs.strip(T);
|
|
|
|
|
|
|
|
if (const PointerType* PTy = dyn_cast<PointerType>(Ty)) {
|
2009-02-21 03:44:51 +03:00
|
|
|
QualType Pointee = PTy->getPointeeType();
|
|
|
|
QualType FixedType =
|
2010-08-18 04:39:00 +04:00
|
|
|
TryToFixInvalidVariablyModifiedType(Pointee, Context, SizeIsNegative,
|
|
|
|
Oversized);
|
2009-02-21 03:44:51 +03:00
|
|
|
if (FixedType.isNull()) return FixedType;
|
2009-02-21 03:58:02 +03:00
|
|
|
FixedType = Context.getPointerType(FixedType);
|
2009-09-24 23:53:00 +04:00
|
|
|
return Qs.apply(FixedType);
|
2009-02-21 03:44:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T);
|
2009-02-26 06:58:54 +03:00
|
|
|
if (!VLATy)
|
|
|
|
return QualType();
|
|
|
|
// FIXME: We should probably handle this case
|
|
|
|
if (VLATy->getElementType()->isVariablyModifiedType())
|
|
|
|
return QualType();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-02-21 03:44:51 +03:00
|
|
|
Expr::EvalResult EvalResult;
|
|
|
|
if (!VLATy->getSizeExpr() ||
|
2009-02-26 06:58:54 +03:00
|
|
|
!VLATy->getSizeExpr()->Evaluate(EvalResult, Context) ||
|
|
|
|
!EvalResult.Val.isInt())
|
2009-02-21 03:44:51 +03:00
|
|
|
return QualType();
|
2009-02-26 06:58:54 +03:00
|
|
|
|
2010-08-18 04:39:00 +04:00
|
|
|
// Check whether the array size is negative.
|
2009-02-21 03:44:51 +03:00
|
|
|
llvm::APSInt &Res = EvalResult.Val.getInt();
|
2010-08-18 04:39:00 +04:00
|
|
|
if (Res.isSigned() && Res.isNegative()) {
|
|
|
|
SizeIsNegative = true;
|
|
|
|
return QualType();
|
2009-07-06 19:59:29 +04:00
|
|
|
}
|
2009-02-21 03:44:51 +03:00
|
|
|
|
2010-08-18 04:39:00 +04:00
|
|
|
// Check whether the array is too large to be addressed.
|
|
|
|
unsigned ActiveSizeBits
|
|
|
|
= ConstantArrayType::getNumAddressingBits(Context, VLATy->getElementType(),
|
|
|
|
Res);
|
|
|
|
if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) {
|
|
|
|
Oversized = Res;
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Context.getConstantArrayType(VLATy->getElementType(),
|
|
|
|
Res, ArrayType::Normal, 0);
|
2009-02-21 03:44:51 +03:00
|
|
|
}
|
|
|
|
|
2009-03-02 03:19:53 +03:00
|
|
|
/// \brief Register the given locally-scoped external C declaration so
|
|
|
|
/// that it can be found later for redeclarations
|
2009-09-09 19:08:12 +04:00
|
|
|
void
|
2009-11-19 01:49:29 +03:00
|
|
|
Sema::RegisterLocallyScopedExternCDecl(NamedDecl *ND,
|
|
|
|
const LookupResult &Previous,
|
2009-03-02 03:19:53 +03:00
|
|
|
Scope *S) {
|
|
|
|
assert(ND->getLexicalDeclContext()->isFunctionOrMethod() &&
|
|
|
|
"Decl is not a locally-scoped decl!");
|
|
|
|
// Note that we have a locally-scoped external with this name.
|
|
|
|
LocallyScopedExternalDecls[ND->getDeclName()] = ND;
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (!Previous.isSingleResult())
|
2009-03-02 03:19:53 +03:00
|
|
|
return;
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
NamedDecl *PrevDecl = Previous.getFoundDecl();
|
|
|
|
|
2009-03-02 03:19:53 +03:00
|
|
|
// If there was a previous declaration of this variable, it may be
|
|
|
|
// in our identifier chain. Update the identifier chain with the new
|
|
|
|
// declaration.
|
2009-03-24 02:06:20 +03:00
|
|
|
if (S && IdResolver.ReplaceDecl(PrevDecl, ND)) {
|
2009-03-02 03:19:53 +03:00
|
|
|
// The previous declaration was found on the identifer resolver
|
|
|
|
// chain, so remove it from its scope.
|
2010-08-21 13:40:31 +04:00
|
|
|
while (S && !S->isDeclScope(PrevDecl))
|
2009-03-02 03:19:53 +03:00
|
|
|
S = S->getParent();
|
|
|
|
|
|
|
|
if (S)
|
2010-08-21 13:40:31 +04:00
|
|
|
S->RemoveDecl(PrevDecl);
|
2009-03-02 03:19:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-07 23:37:57 +04:00
|
|
|
/// \brief Diagnose function specifiers on a declaration of an identifier that
|
|
|
|
/// does not identify a function.
|
|
|
|
void Sema::DiagnoseFunctionSpecifiers(Declarator& D) {
|
|
|
|
// FIXME: We should probably indicate the identifier in question to avoid
|
|
|
|
// confusion for constructs like "inline int a(), b;"
|
|
|
|
if (D.getDeclSpec().isInlineSpecified())
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(D.getDeclSpec().getInlineSpecLoc(),
|
2009-04-07 23:37:57 +04:00
|
|
|
diag::err_inline_non_function);
|
|
|
|
|
|
|
|
if (D.getDeclSpec().isVirtualSpecified())
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(D.getDeclSpec().getVirtualSpecLoc(),
|
2009-04-07 23:37:57 +04:00
|
|
|
diag::err_virtual_non_function);
|
|
|
|
|
|
|
|
if (D.getDeclSpec().isExplicitSpecified())
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(D.getDeclSpec().getExplicitSpecLoc(),
|
2009-04-07 23:37:57 +04:00
|
|
|
diag::err_explicit_non_function);
|
|
|
|
}
|
|
|
|
|
2009-01-20 04:17:11 +03:00
|
|
|
NamedDecl*
|
2009-01-16 06:34:13 +03:00
|
|
|
Sema::ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC,
|
2009-12-07 05:54:59 +03:00
|
|
|
QualType R, TypeSourceInfo *TInfo,
|
2009-11-19 01:49:29 +03:00
|
|
|
LookupResult &Previous, bool &Redeclaration) {
|
2009-01-16 06:34:13 +03:00
|
|
|
// Typedef declarators cannot be qualified (C++ [dcl.meaning]p1).
|
|
|
|
if (D.getCXXScopeSpec().isSet()) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_qualified_typedef_declarator)
|
|
|
|
<< D.getCXXScopeSpec().getRange();
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2009-01-16 06:34:13 +03:00
|
|
|
// Pretend we didn't see the scope specifier.
|
2010-03-23 18:26:55 +03:00
|
|
|
DC = CurContext;
|
|
|
|
Previous.clear();
|
2009-01-16 06:34:13 +03:00
|
|
|
}
|
|
|
|
|
2009-03-12 02:00:04 +03:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
// Check that there are no default arguments (C++ only).
|
2009-01-16 06:34:13 +03:00
|
|
|
CheckExtraCXXDefaultArguments(D);
|
2009-03-12 02:00:04 +03:00
|
|
|
}
|
|
|
|
|
2009-04-07 23:37:57 +04:00
|
|
|
DiagnoseFunctionSpecifiers(D);
|
|
|
|
|
2009-04-20 00:27:55 +04:00
|
|
|
if (D.getDeclSpec().isThreadSpecified())
|
|
|
|
Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
|
|
|
|
|
2010-07-13 10:37:01 +04:00
|
|
|
if (D.getName().Kind != UnqualifiedId::IK_Identifier) {
|
|
|
|
Diag(D.getName().StartLocation, diag::err_typedef_not_identifier)
|
|
|
|
<< D.getName().getSourceRange();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-07 05:54:59 +03:00
|
|
|
TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, TInfo);
|
2009-01-16 06:34:13 +03:00
|
|
|
if (!NewTD) return 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-01-16 06:34:13 +03:00
|
|
|
// Handle attributes prior to checking for duplicates in MergeVarDecl
|
2009-06-18 01:51:59 +04:00
|
|
|
ProcessDeclAttributes(S, NewTD, D);
|
2009-11-19 01:49:29 +03:00
|
|
|
|
2009-04-19 09:21:20 +04:00
|
|
|
// C99 6.7.7p2: If a typedef name specifies a variably modified type
|
|
|
|
// then it shall have block scope.
|
2010-08-10 07:13:15 +04:00
|
|
|
// Note that variably modified types must be fixed before merging the decl so
|
|
|
|
// that redeclarations will match.
|
2009-04-19 09:21:20 +04:00
|
|
|
QualType T = NewTD->getUnderlyingType();
|
|
|
|
if (T->isVariablyModifiedType()) {
|
2010-08-25 12:40:02 +04:00
|
|
|
getCurFunction()->setHasBranchProtectedScope();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-19 09:21:20 +04:00
|
|
|
if (S->getFnParent() == 0) {
|
2009-02-21 03:44:51 +03:00
|
|
|
bool SizeIsNegative;
|
2010-08-18 04:39:00 +04:00
|
|
|
llvm::APSInt Oversized;
|
2009-02-21 03:44:51 +03:00
|
|
|
QualType FixedTy =
|
2010-08-18 04:39:00 +04:00
|
|
|
TryToFixInvalidVariablyModifiedType(T, Context, SizeIsNegative,
|
|
|
|
Oversized);
|
2009-02-21 03:44:51 +03:00
|
|
|
if (!FixedTy.isNull()) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::warn_illegal_constant_array_size);
|
2009-12-07 05:54:59 +03:00
|
|
|
NewTD->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(FixedTy));
|
2009-02-21 03:44:51 +03:00
|
|
|
} else {
|
|
|
|
if (SizeIsNegative)
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_typecheck_negative_array_size);
|
|
|
|
else if (T->isVariableArrayType())
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_vla_decl_in_file_scope);
|
2010-08-18 04:39:00 +04:00
|
|
|
else if (Oversized.getBoolValue())
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_array_too_large)
|
|
|
|
<< Oversized.toString(10);
|
2009-02-21 03:44:51 +03:00
|
|
|
else
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_vm_decl_in_file_scope);
|
2009-04-25 12:06:05 +04:00
|
|
|
NewTD->setInvalidDecl();
|
2009-02-21 03:44:51 +03:00
|
|
|
}
|
2009-01-16 06:34:13 +03:00
|
|
|
}
|
|
|
|
}
|
2009-07-07 20:35:42 +04:00
|
|
|
|
2010-08-10 07:13:15 +04:00
|
|
|
// Merge the decl with the existing one if appropriate. If the decl is
|
|
|
|
// in an outer scope, it isn't the same thing.
|
|
|
|
FilterLookupForScope(*this, Previous, DC, S, /*ConsiderLinkage*/ false);
|
|
|
|
if (!Previous.empty()) {
|
|
|
|
Redeclaration = true;
|
|
|
|
MergeTypeDefDecl(NewTD, Previous);
|
|
|
|
}
|
|
|
|
|
2009-07-07 20:35:42 +04:00
|
|
|
// If this is the C FILE type, notify the AST context.
|
|
|
|
if (IdentifierInfo *II = NewTD->getIdentifier())
|
|
|
|
if (!NewTD->isInvalidDecl() &&
|
2010-08-31 04:36:30 +04:00
|
|
|
NewTD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
|
2009-07-28 06:25:19 +04:00
|
|
|
if (II->isStr("FILE"))
|
|
|
|
Context.setFILEDecl(NewTD);
|
|
|
|
else if (II->isStr("jmp_buf"))
|
|
|
|
Context.setjmp_bufDecl(NewTD);
|
|
|
|
else if (II->isStr("sigjmp_buf"))
|
|
|
|
Context.setsigjmp_bufDecl(NewTD);
|
2010-10-05 19:41:24 +04:00
|
|
|
else if (II->isStr("__builtin_va_list"))
|
|
|
|
Context.setBuiltinVaListType(Context.getTypedefType(NewTD));
|
2009-07-28 06:25:19 +04:00
|
|
|
}
|
|
|
|
|
2009-01-16 06:34:13 +03:00
|
|
|
return NewTD;
|
|
|
|
}
|
|
|
|
|
2009-02-24 22:23:27 +03:00
|
|
|
/// \brief Determines whether the given declaration is an out-of-scope
|
|
|
|
/// previous declaration.
|
|
|
|
///
|
|
|
|
/// This routine should be invoked when name lookup has found a
|
|
|
|
/// previous declaration (PrevDecl) that is not in the scope where a
|
|
|
|
/// new declaration by the same name is being introduced. If the new
|
|
|
|
/// declaration occurs in a local scope, previous declarations with
|
|
|
|
/// linkage may still be considered previous declarations (C99
|
|
|
|
/// 6.2.2p4-5, C++ [basic.link]p6).
|
|
|
|
///
|
|
|
|
/// \param PrevDecl the previous declaration found by name
|
|
|
|
/// lookup
|
2009-09-09 19:08:12 +04:00
|
|
|
///
|
2009-02-24 22:23:27 +03:00
|
|
|
/// \param DC the context in which the new declaration is being
|
|
|
|
/// declared.
|
|
|
|
///
|
|
|
|
/// \returns true if PrevDecl is an out-of-scope previous declaration
|
|
|
|
/// for a new delcaration with the same name.
|
2009-09-09 19:08:12 +04:00
|
|
|
static bool
|
2009-02-24 22:23:27 +03:00
|
|
|
isOutOfScopePreviousDeclaration(NamedDecl *PrevDecl, DeclContext *DC,
|
|
|
|
ASTContext &Context) {
|
|
|
|
if (!PrevDecl)
|
2010-08-31 04:36:30 +04:00
|
|
|
return false;
|
2009-02-24 22:23:27 +03:00
|
|
|
|
2009-02-24 23:03:32 +03:00
|
|
|
if (!PrevDecl->hasLinkage())
|
|
|
|
return false;
|
2009-02-24 22:23:27 +03:00
|
|
|
|
|
|
|
if (Context.getLangOptions().CPlusPlus) {
|
|
|
|
// C++ [basic.link]p6:
|
|
|
|
// If there is a visible declaration of an entity with linkage
|
|
|
|
// having the same name and type, ignoring entities declared
|
|
|
|
// outside the innermost enclosing namespace scope, the block
|
|
|
|
// scope declaration declares that same entity and receives the
|
|
|
|
// linkage of the previous declaration.
|
2010-08-31 04:36:30 +04:00
|
|
|
DeclContext *OuterContext = DC->getRedeclContext();
|
2009-02-24 22:23:27 +03:00
|
|
|
if (!OuterContext->isFunctionOrMethod())
|
|
|
|
// This rule only applies to block-scope declarations.
|
|
|
|
return false;
|
2010-08-28 02:55:10 +04:00
|
|
|
|
|
|
|
DeclContext *PrevOuterContext = PrevDecl->getDeclContext();
|
|
|
|
if (PrevOuterContext->isRecord())
|
|
|
|
// We found a member function: ignore it.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Find the innermost enclosing namespace for the new and
|
|
|
|
// previous declarations.
|
2010-08-31 04:36:30 +04:00
|
|
|
OuterContext = OuterContext->getEnclosingNamespaceContext();
|
|
|
|
PrevOuterContext = PrevOuterContext->getEnclosingNamespaceContext();
|
2010-08-28 02:55:10 +04:00
|
|
|
|
|
|
|
// The previous declaration is in a different namespace, so it
|
|
|
|
// isn't the same function.
|
|
|
|
if (!OuterContext->Equals(PrevOuterContext))
|
|
|
|
return false;
|
2009-02-24 22:23:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-15 13:12:16 +03:00
|
|
|
static void SetNestedNameSpecifier(DeclaratorDecl *DD, Declarator &D) {
|
|
|
|
CXXScopeSpec &SS = D.getCXXScopeSpec();
|
|
|
|
if (!SS.isSet()) return;
|
|
|
|
DD->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()),
|
|
|
|
SS.getRange());
|
|
|
|
}
|
|
|
|
|
2009-01-20 04:17:11 +03:00
|
|
|
NamedDecl*
|
2010-10-10 22:16:20 +04:00
|
|
|
Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC,
|
2009-12-07 05:54:59 +03:00
|
|
|
QualType R, TypeSourceInfo *TInfo,
|
2009-11-19 01:49:29 +03:00
|
|
|
LookupResult &Previous,
|
2009-07-22 21:18:37 +04:00
|
|
|
MultiTemplateParamsArg TemplateParamLists,
|
2009-02-16 20:45:42 +03:00
|
|
|
bool &Redeclaration) {
|
2010-08-12 02:01:17 +04:00
|
|
|
DeclarationName Name = GetNameForDeclarator(D).getName();
|
2009-01-16 05:36:34 +03:00
|
|
|
|
|
|
|
// Check that there are no default arguments (C++ only).
|
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
CheckExtraCXXDefaultArguments(D);
|
|
|
|
|
2010-04-20 02:54:31 +04:00
|
|
|
DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpec();
|
|
|
|
assert(SCSpec != DeclSpec::SCS_typedef &&
|
|
|
|
"Parser allowed 'typedef' as storage class VarDecl.");
|
2010-07-30 20:47:02 +04:00
|
|
|
VarDecl::StorageClass SC = StorageClassSpecToVarDeclStorageClass(SCSpec);
|
2010-04-20 02:54:31 +04:00
|
|
|
if (SCSpec == DeclSpec::SCS_mutable) {
|
2009-01-16 05:36:34 +03:00
|
|
|
// mutable can only appear on non-static class members, so it's always
|
|
|
|
// an error here
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_mutable_nonmember);
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2010-08-26 07:08:43 +04:00
|
|
|
SC = SC_None;
|
2009-01-16 05:36:34 +03:00
|
|
|
}
|
2010-04-20 02:54:31 +04:00
|
|
|
SCSpec = D.getDeclSpec().getStorageClassSpecAsWritten();
|
|
|
|
VarDecl::StorageClass SCAsWritten
|
2010-07-30 20:47:02 +04:00
|
|
|
= StorageClassSpecToVarDeclStorageClass(SCSpec);
|
2009-01-16 05:36:34 +03:00
|
|
|
|
|
|
|
IdentifierInfo *II = Name.getAsIdentifierInfo();
|
|
|
|
if (!II) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_bad_variable_name)
|
|
|
|
<< Name.getAsString();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-07 23:37:57 +04:00
|
|
|
DiagnoseFunctionSpecifiers(D);
|
2009-03-12 02:00:04 +03:00
|
|
|
|
2009-03-11 23:22:50 +03:00
|
|
|
if (!DC->isRecord() && S->getFnParent() == 0) {
|
|
|
|
// C99 6.9p2: The storage-class specifiers auto and register shall not
|
|
|
|
// appear in the declaration specifiers in an external declaration.
|
2010-08-26 07:08:43 +04:00
|
|
|
if (SC == SC_Auto || SC == SC_Register) {
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-13 01:44:00 +04:00
|
|
|
// If this is a register variable with an asm label specified, then this
|
|
|
|
// is a GNU extension.
|
2010-08-26 07:08:43 +04:00
|
|
|
if (SC == SC_Register && D.getAsmLabel())
|
2009-05-13 01:44:00 +04:00
|
|
|
Diag(D.getIdentifierLoc(), diag::err_unsupported_global_register);
|
|
|
|
else
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope);
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2009-01-16 05:36:34 +03:00
|
|
|
}
|
|
|
|
}
|
2009-03-12 02:52:16 +03:00
|
|
|
if (DC->isRecord() && !CurContext->isRecord()) {
|
|
|
|
// This is an out-of-line definition of a static data member.
|
2010-08-26 07:08:43 +04:00
|
|
|
if (SC == SC_Static) {
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(D.getDeclSpec().getStorageClassSpecLoc(),
|
2009-03-12 02:52:16 +03:00
|
|
|
diag::err_static_out_of_line)
|
2010-03-31 21:46:05 +04:00
|
|
|
<< FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
|
2010-08-26 07:08:43 +04:00
|
|
|
} else if (SC == SC_None)
|
|
|
|
SC = SC_Static;
|
2009-03-12 02:52:16 +03:00
|
|
|
}
|
2010-08-26 07:08:43 +04:00
|
|
|
if (SC == SC_Static) {
|
2009-06-24 04:28:53 +04:00
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
|
|
|
|
if (RD->isLocalClass())
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(D.getIdentifierLoc(),
|
2009-06-24 04:28:53 +04:00
|
|
|
diag::err_static_data_member_not_allowed_in_local_class)
|
|
|
|
<< Name << RD->getDeclName();
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-22 21:18:37 +04:00
|
|
|
// Match up the template parameter lists with the scope specifier, then
|
|
|
|
// determine whether we have a template or a template specialization.
|
2009-10-08 02:35:40 +04:00
|
|
|
bool isExplicitSpecialization = false;
|
2010-06-12 12:15:14 +04:00
|
|
|
unsigned NumMatchedTemplateParamLists = TemplateParamLists.size();
|
2010-07-15 03:14:12 +04:00
|
|
|
bool Invalid = false;
|
2009-07-22 21:18:37 +04:00
|
|
|
if (TemplateParameterList *TemplateParams
|
2009-10-08 02:35:40 +04:00
|
|
|
= MatchTemplateParametersToScopeSpecifier(
|
2009-07-22 21:18:37 +04:00
|
|
|
D.getDeclSpec().getSourceRange().getBegin(),
|
2009-10-08 02:35:40 +04:00
|
|
|
D.getCXXScopeSpec(),
|
2009-07-23 02:05:02 +04:00
|
|
|
(TemplateParameterList**)TemplateParamLists.get(),
|
2009-10-08 02:35:40 +04:00
|
|
|
TemplateParamLists.size(),
|
2010-04-14 00:37:33 +04:00
|
|
|
/*never a friend*/ false,
|
2010-07-15 03:14:12 +04:00
|
|
|
isExplicitSpecialization,
|
|
|
|
Invalid)) {
|
2010-06-12 12:15:14 +04:00
|
|
|
// All but one template parameter lists have been matching.
|
|
|
|
--NumMatchedTemplateParamLists;
|
|
|
|
|
2009-07-22 21:18:37 +04:00
|
|
|
if (TemplateParams->size() > 0) {
|
|
|
|
// There is no such thing as a variable template.
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_template_variable)
|
|
|
|
<< II
|
|
|
|
<< SourceRange(TemplateParams->getTemplateLoc(),
|
|
|
|
TemplateParams->getRAngleLoc());
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
// There is an extraneous 'template<>' for this variable. Complain
|
|
|
|
// about it, but allow the declaration of the variable.
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(TemplateParams->getTemplateLoc(),
|
2009-07-23 03:48:44 +04:00
|
|
|
diag::err_template_variable_noparams)
|
2009-07-22 21:18:37 +04:00
|
|
|
<< II
|
|
|
|
<< SourceRange(TemplateParams->getTemplateLoc(),
|
2009-09-09 19:08:12 +04:00
|
|
|
TemplateParams->getRAngleLoc());
|
2009-10-08 02:35:40 +04:00
|
|
|
|
|
|
|
isExplicitSpecialization = true;
|
2009-07-22 21:18:37 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
|
|
|
|
2010-04-20 02:54:31 +04:00
|
|
|
VarDecl *NewVD = VarDecl::Create(Context, DC, D.getIdentifierLoc(),
|
|
|
|
II, R, TInfo, SC, SCAsWritten);
|
2009-04-20 00:27:55 +04:00
|
|
|
|
2010-07-15 03:14:12 +04:00
|
|
|
if (D.isInvalidType() || Invalid)
|
2009-04-25 12:06:05 +04:00
|
|
|
NewVD->setInvalidDecl();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-03-15 13:12:16 +03:00
|
|
|
SetNestedNameSpecifier(NewVD, D);
|
|
|
|
|
2010-07-29 03:59:57 +04:00
|
|
|
if (NumMatchedTemplateParamLists > 0 && D.getCXXScopeSpec().isSet()) {
|
2010-06-15 21:44:38 +04:00
|
|
|
NewVD->setTemplateParameterListsInfo(Context,
|
|
|
|
NumMatchedTemplateParamLists,
|
2010-06-12 12:15:14 +04:00
|
|
|
(TemplateParameterList**)TemplateParamLists.release());
|
|
|
|
}
|
|
|
|
|
2009-04-20 00:27:55 +04:00
|
|
|
if (D.getDeclSpec().isThreadSpecified()) {
|
|
|
|
if (NewVD->hasLocalStorage())
|
|
|
|
Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_thread_non_global);
|
2009-04-20 01:48:33 +04:00
|
|
|
else if (!Context.Target.isTLSSupported())
|
|
|
|
Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_thread_unsupported);
|
2009-04-20 00:27:55 +04:00
|
|
|
else
|
|
|
|
NewVD->setThreadSpecified(true);
|
|
|
|
}
|
2009-01-20 04:17:11 +03:00
|
|
|
|
2009-03-12 02:52:16 +03:00
|
|
|
// Set the lexical context. If the declarator has a C++ scope specifier, the
|
|
|
|
// lexical context will be different from the semantic context.
|
|
|
|
NewVD->setLexicalDeclContext(CurContext);
|
|
|
|
|
2009-01-16 05:36:34 +03:00
|
|
|
// Handle attributes prior to checking for duplicates in MergeVarDecl
|
2009-06-18 01:51:59 +04:00
|
|
|
ProcessDeclAttributes(S, NewVD, D);
|
2009-01-16 05:36:34 +03:00
|
|
|
|
|
|
|
// Handle GNU asm-label extension (encoded as an attribute).
|
2010-10-10 22:16:20 +04:00
|
|
|
if (Expr *E = (Expr*)D.getAsmLabel()) {
|
2009-01-16 05:36:34 +03:00
|
|
|
// The parser guarantees this is a string.
|
2009-09-09 19:08:12 +04:00
|
|
|
StringLiteral *SE = cast<StringLiteral>(E);
|
2010-08-19 03:23:40 +04:00
|
|
|
NewVD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0),
|
|
|
|
Context, SE->getString()));
|
2009-01-16 05:36:34 +03:00
|
|
|
}
|
|
|
|
|
2010-03-17 00:48:18 +03:00
|
|
|
// Diagnose shadowed variables before filtering for scope.
|
2010-03-20 07:12:52 +03:00
|
|
|
if (!D.getCXXScopeSpec().isSet())
|
2010-03-22 12:20:08 +03:00
|
|
|
CheckShadow(S, NewVD, Previous);
|
2010-03-17 00:48:18 +03:00
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
// Don't consider existing declarations that are in a different
|
|
|
|
// scope and are out-of-semantic-context declarations (if the new
|
|
|
|
// declaration has linkage).
|
|
|
|
FilterLookupForScope(*this, Previous, DC, S, NewVD->hasLinkage());
|
2009-10-08 11:24:58 +04:00
|
|
|
|
2009-03-26 02:32:15 +03:00
|
|
|
// Merge the decl with the existing one if appropriate.
|
2009-11-19 01:49:29 +03:00
|
|
|
if (!Previous.empty()) {
|
|
|
|
if (Previous.isSingleResult() &&
|
|
|
|
isa<FieldDecl>(Previous.getFoundDecl()) &&
|
|
|
|
D.getCXXScopeSpec().isSet()) {
|
2009-03-26 02:32:15 +03:00
|
|
|
// The user tried to define a non-static data member
|
|
|
|
// out-of-line (C++ [dcl.meaning]p1).
|
|
|
|
Diag(NewVD->getLocation(), diag::err_nonstatic_member_out_of_line)
|
|
|
|
<< D.getCXXScopeSpec().getRange();
|
2009-11-19 01:49:29 +03:00
|
|
|
Previous.clear();
|
2009-04-25 12:06:05 +04:00
|
|
|
NewVD->setInvalidDecl();
|
2009-03-26 02:32:15 +03:00
|
|
|
}
|
|
|
|
} else if (D.getCXXScopeSpec().isSet()) {
|
|
|
|
// No previous declaration in the qualifying scope.
|
2009-10-14 01:16:44 +04:00
|
|
|
Diag(D.getIdentifierLoc(), diag::err_no_member)
|
|
|
|
<< Name << computeDeclContext(D.getCXXScopeSpec(), true)
|
|
|
|
<< D.getCXXScopeSpec().getRange();
|
2009-04-25 12:06:05 +04:00
|
|
|
NewVD->setInvalidDecl();
|
2009-03-26 02:32:15 +03:00
|
|
|
}
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
CheckVariableDeclaration(NewVD, Previous, Redeclaration);
|
2009-03-26 02:32:15 +03:00
|
|
|
|
2009-10-08 11:24:58 +04:00
|
|
|
// This is an explicit specialization of a static data member. Check it.
|
|
|
|
if (isExplicitSpecialization && !NewVD->isInvalidDecl() &&
|
2009-11-19 01:49:29 +03:00
|
|
|
CheckMemberSpecialization(NewVD, Previous))
|
2009-10-08 11:24:58 +04:00
|
|
|
NewVD->setInvalidDecl();
|
2009-11-19 01:49:29 +03:00
|
|
|
|
2009-07-26 02:29:44 +04:00
|
|
|
// attributes declared post-definition are currently ignored
|
2010-08-19 03:23:40 +04:00
|
|
|
// FIXME: This should be handled in attribute merging, not
|
|
|
|
// here.
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Previous.isSingleResult()) {
|
2010-02-01 23:16:42 +03:00
|
|
|
VarDecl *Def = dyn_cast<VarDecl>(Previous.getFoundDecl());
|
|
|
|
if (Def && (Def = Def->getDefinition()) &&
|
|
|
|
Def != NewVD && D.hasAttributes()) {
|
2009-07-26 02:29:44 +04:00
|
|
|
Diag(NewVD->getLocation(), diag::warn_attribute_precede_definition);
|
|
|
|
Diag(Def->getLocation(), diag::note_previous_definition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-26 02:32:15 +03:00
|
|
|
// If this is a locally-scoped extern C variable, update the map of
|
|
|
|
// such variables.
|
2009-09-12 04:17:51 +04:00
|
|
|
if (CurContext->isFunctionOrMethod() && NewVD->isExternC() &&
|
2009-04-25 12:06:05 +04:00
|
|
|
!NewVD->isInvalidDecl())
|
2009-11-19 01:49:29 +03:00
|
|
|
RegisterLocallyScopedExternCDecl(NewVD, Previous, S);
|
2009-03-26 02:32:15 +03:00
|
|
|
|
2010-08-05 10:57:20 +04:00
|
|
|
// If there's a #pragma GCC visibility in scope, and this isn't a class
|
|
|
|
// member, set the visibility of this variable.
|
|
|
|
if (NewVD->getLinkage() == ExternalLinkage && !DC->isRecord())
|
|
|
|
AddPushedVisibilityAttribute(NewVD);
|
|
|
|
|
2010-08-15 05:15:20 +04:00
|
|
|
MarkUnusedFileScopedDecl(NewVD);
|
|
|
|
|
2009-03-26 02:32:15 +03:00
|
|
|
return NewVD;
|
|
|
|
}
|
|
|
|
|
2010-03-22 12:20:08 +03:00
|
|
|
/// \brief Diagnose variable or built-in function shadowing. Implements
|
|
|
|
/// -Wshadow.
|
2010-03-17 00:48:18 +03:00
|
|
|
///
|
2010-03-22 12:20:08 +03:00
|
|
|
/// This method is called whenever a VarDecl is added to a "useful"
|
|
|
|
/// scope.
|
2010-03-17 00:48:18 +03:00
|
|
|
///
|
2010-03-20 07:12:52 +03:00
|
|
|
/// \param S the scope in which the shadowing name is being declared
|
|
|
|
/// \param R the lookup of the name
|
2010-03-17 00:48:18 +03:00
|
|
|
///
|
2010-03-22 12:20:08 +03:00
|
|
|
void Sema::CheckShadow(Scope *S, VarDecl *D, const LookupResult& R) {
|
2010-03-17 00:48:18 +03:00
|
|
|
// Return if warning is ignored.
|
|
|
|
if (Diags.getDiagnosticLevel(diag::warn_decl_shadow) == Diagnostic::Ignored)
|
|
|
|
return;
|
|
|
|
|
2010-03-20 07:12:52 +03:00
|
|
|
// Don't diagnose declarations at file scope. The scope might not
|
|
|
|
// have a DeclContext if (e.g.) we're parsing a function prototype.
|
|
|
|
DeclContext *NewDC = static_cast<DeclContext*>(S->getEntity());
|
|
|
|
if (NewDC && NewDC->isFileContext())
|
2010-03-17 00:48:18 +03:00
|
|
|
return;
|
2010-03-20 07:12:52 +03:00
|
|
|
|
|
|
|
// Only diagnose if we're shadowing an unambiguous field or variable.
|
2010-03-17 19:03:44 +03:00
|
|
|
if (R.getResultKind() != LookupResult::Found)
|
2010-03-17 00:48:18 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
NamedDecl* ShadowedDecl = R.getFoundDecl();
|
|
|
|
if (!isa<VarDecl>(ShadowedDecl) && !isa<FieldDecl>(ShadowedDecl))
|
|
|
|
return;
|
|
|
|
|
2010-03-20 07:12:52 +03:00
|
|
|
DeclContext *OldDC = ShadowedDecl->getDeclContext();
|
|
|
|
|
|
|
|
// Only warn about certain kinds of shadowing for class members.
|
|
|
|
if (NewDC && NewDC->isRecord()) {
|
|
|
|
// In particular, don't warn about shadowing non-class members.
|
|
|
|
if (!OldDC->isRecord())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// TODO: should we warn about static data members shadowing
|
|
|
|
// static data members from base classes?
|
|
|
|
|
|
|
|
// TODO: don't diagnose for inaccessible shadowed members.
|
|
|
|
// This is hard to do perfectly because we might friend the
|
|
|
|
// shadowing context, but that's just a false negative.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine what kind of declaration we're shadowing.
|
2010-03-17 00:48:18 +03:00
|
|
|
unsigned Kind;
|
2010-03-20 07:12:52 +03:00
|
|
|
if (isa<RecordDecl>(OldDC)) {
|
2010-03-17 00:48:18 +03:00
|
|
|
if (isa<FieldDecl>(ShadowedDecl))
|
|
|
|
Kind = 3; // field
|
|
|
|
else
|
|
|
|
Kind = 2; // static data member
|
2010-03-20 07:12:52 +03:00
|
|
|
} else if (OldDC->isFileContext())
|
2010-03-17 00:48:18 +03:00
|
|
|
Kind = 1; // global
|
|
|
|
else
|
|
|
|
Kind = 0; // local
|
|
|
|
|
2010-03-20 07:12:52 +03:00
|
|
|
DeclarationName Name = R.getLookupName();
|
|
|
|
|
2010-03-17 00:48:18 +03:00
|
|
|
// Emit warning and note.
|
2010-03-20 07:12:52 +03:00
|
|
|
Diag(R.getNameLoc(), diag::warn_decl_shadow) << Name << Kind << OldDC;
|
2010-03-17 00:48:18 +03:00
|
|
|
Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
|
|
|
|
}
|
|
|
|
|
2010-03-22 12:20:08 +03:00
|
|
|
/// \brief Check -Wshadow without the advantage of a previous lookup.
|
|
|
|
void Sema::CheckShadow(Scope *S, VarDecl *D) {
|
|
|
|
LookupResult R(*this, D->getDeclName(), D->getLocation(),
|
|
|
|
Sema::LookupOrdinaryName, Sema::ForRedeclaration);
|
|
|
|
LookupName(R, S);
|
|
|
|
CheckShadow(S, D, R);
|
|
|
|
}
|
|
|
|
|
2009-03-26 02:32:15 +03:00
|
|
|
/// \brief Perform semantic checking on a newly-created variable
|
|
|
|
/// declaration.
|
|
|
|
///
|
|
|
|
/// This routine performs all of the type-checking required for a
|
2009-05-01 19:47:09 +04:00
|
|
|
/// variable declaration once it has been built. It is used both to
|
2009-03-26 02:32:15 +03:00
|
|
|
/// check variables after they have been parsed and their declarators
|
2009-05-01 19:47:09 +04:00
|
|
|
/// have been translated into a declaration, and to check variables
|
|
|
|
/// that have been instantiated from a template.
|
2009-03-26 02:32:15 +03:00
|
|
|
///
|
2009-04-25 12:06:05 +04:00
|
|
|
/// Sets NewVD->isInvalidDecl() if an error was encountered.
|
2009-11-19 01:49:29 +03:00
|
|
|
void Sema::CheckVariableDeclaration(VarDecl *NewVD,
|
|
|
|
LookupResult &Previous,
|
2009-03-26 02:32:15 +03:00
|
|
|
bool &Redeclaration) {
|
2009-04-25 12:06:05 +04:00
|
|
|
// If the decl is already known invalid, don't check it.
|
|
|
|
if (NewVD->isInvalidDecl())
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-26 02:32:15 +03:00
|
|
|
QualType T = NewVD->getType();
|
|
|
|
|
2010-05-15 15:32:37 +04:00
|
|
|
if (T->isObjCObjectType()) {
|
2009-03-26 02:32:15 +03:00
|
|
|
Diag(NewVD->getLocation(), diag::err_statically_allocated_object);
|
2009-04-25 12:06:05 +04:00
|
|
|
return NewVD->setInvalidDecl();
|
2009-03-26 02:32:15 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-01-16 05:36:34 +03:00
|
|
|
// Emit an error if an address space was applied to decl with local storage.
|
|
|
|
// This includes arrays of objects with address space qualifiers, but not
|
|
|
|
// automatic variables that point to other address spaces.
|
|
|
|
// ISO/IEC TR 18037 S5.1.2
|
2010-10-10 22:16:20 +04:00
|
|
|
if (NewVD->hasLocalStorage() && T.getAddressSpace() != 0) {
|
2009-03-26 02:32:15 +03:00
|
|
|
Diag(NewVD->getLocation(), diag::err_as_qualified_auto_decl);
|
2009-04-25 12:06:05 +04:00
|
|
|
return NewVD->setInvalidDecl();
|
2009-01-16 05:36:34 +03:00
|
|
|
}
|
2009-02-21 22:44:02 +03:00
|
|
|
|
2009-04-14 04:57:29 +04:00
|
|
|
if (NewVD->hasLocalStorage() && T.isObjCGCWeak()
|
2009-06-30 06:34:44 +04:00
|
|
|
&& !NewVD->hasAttr<BlocksAttr>())
|
2009-03-26 02:32:15 +03:00
|
|
|
Diag(NewVD->getLocation(), diag::warn_attribute_weak_on_local);
|
2010-10-10 22:16:20 +04:00
|
|
|
|
2009-04-19 09:21:20 +04:00
|
|
|
bool isVM = T->isVariablyModifiedType();
|
2009-07-20 00:17:11 +04:00
|
|
|
if (isVM || NewVD->hasAttr<CleanupAttr>() ||
|
2010-08-01 05:24:59 +04:00
|
|
|
NewVD->hasAttr<BlocksAttr>())
|
2010-08-25 12:40:02 +04:00
|
|
|
getCurFunction()->setHasBranchProtectedScope();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-19 09:21:20 +04:00
|
|
|
if ((isVM && NewVD->hasLinkage()) ||
|
|
|
|
(T->isVariableArrayType() && NewVD->hasGlobalStorage())) {
|
2009-03-01 00:56:50 +03:00
|
|
|
bool SizeIsNegative;
|
2010-08-18 04:39:00 +04:00
|
|
|
llvm::APSInt Oversized;
|
2009-03-01 00:56:50 +03:00
|
|
|
QualType FixedTy =
|
2010-08-18 04:39:00 +04:00
|
|
|
TryToFixInvalidVariablyModifiedType(T, Context, SizeIsNegative,
|
|
|
|
Oversized);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
if (FixedTy.isNull() && T->isVariableArrayType()) {
|
2009-03-26 02:32:15 +03:00
|
|
|
const VariableArrayType *VAT = Context.getAsVariableArrayType(T);
|
2009-09-09 19:08:12 +04:00
|
|
|
// FIXME: This won't give the correct result for
|
|
|
|
// int a[10][n];
|
2009-03-01 00:56:50 +03:00
|
|
|
SourceRange SizeRange = VAT->getSizeExpr()->getSourceRange();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-01 00:56:50 +03:00
|
|
|
if (NewVD->isFileVarDecl())
|
|
|
|
Diag(NewVD->getLocation(), diag::err_vla_decl_in_file_scope)
|
2009-04-25 12:06:05 +04:00
|
|
|
<< SizeRange;
|
2010-08-26 07:08:43 +04:00
|
|
|
else if (NewVD->getStorageClass() == SC_Static)
|
2009-03-01 00:56:50 +03:00
|
|
|
Diag(NewVD->getLocation(), diag::err_vla_decl_has_static_storage)
|
2009-04-25 12:06:05 +04:00
|
|
|
<< SizeRange;
|
2009-03-01 00:56:50 +03:00
|
|
|
else
|
|
|
|
Diag(NewVD->getLocation(), diag::err_vla_decl_has_extern_linkage)
|
2009-04-25 12:06:05 +04:00
|
|
|
<< SizeRange;
|
|
|
|
return NewVD->setInvalidDecl();
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
if (FixedTy.isNull()) {
|
2009-03-01 00:56:50 +03:00
|
|
|
if (NewVD->isFileVarDecl())
|
|
|
|
Diag(NewVD->getLocation(), diag::err_vm_decl_in_file_scope);
|
|
|
|
else
|
|
|
|
Diag(NewVD->getLocation(), diag::err_vm_decl_has_extern_linkage);
|
2009-04-25 12:06:05 +04:00
|
|
|
return NewVD->setInvalidDecl();
|
2009-03-01 00:56:50 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
Diag(NewVD->getLocation(), diag::warn_illegal_constant_array_size);
|
|
|
|
NewVD->setType(FixedTy);
|
2009-03-01 00:56:50 +03:00
|
|
|
}
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Previous.empty() && NewVD->isExternC()) {
|
2009-03-02 03:19:53 +03:00
|
|
|
// Since we did not find anything by this name and we're declaring
|
|
|
|
// an extern "C" variable, look for a non-visible extern "C"
|
|
|
|
// declaration with the same name.
|
|
|
|
llvm::DenseMap<DeclarationName, NamedDecl *>::iterator Pos
|
2009-03-26 02:32:15 +03:00
|
|
|
= LocallyScopedExternalDecls.find(NewVD->getDeclName());
|
2009-03-02 03:19:53 +03:00
|
|
|
if (Pos != LocallyScopedExternalDecls.end())
|
2009-11-19 01:49:29 +03:00
|
|
|
Previous.addDecl(Pos->second);
|
2009-03-02 03:19:53 +03:00
|
|
|
}
|
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
if (T->isVoidType() && !NewVD->hasExternalStorage()) {
|
2009-03-26 02:32:15 +03:00
|
|
|
Diag(NewVD->getLocation(), diag::err_typecheck_decl_incomplete_type)
|
|
|
|
<< T;
|
2009-04-25 12:06:05 +04:00
|
|
|
return NewVD->setInvalidDecl();
|
2009-03-26 02:32:15 +03:00
|
|
|
}
|
2009-01-16 05:36:34 +03:00
|
|
|
|
2009-06-30 06:34:44 +04:00
|
|
|
if (!NewVD->hasLocalStorage() && NewVD->hasAttr<BlocksAttr>()) {
|
2009-04-30 04:19:40 +04:00
|
|
|
Diag(NewVD->getLocation(), diag::err_block_on_nonlocal);
|
|
|
|
return NewVD->setInvalidDecl();
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-30 06:34:44 +04:00
|
|
|
if (isVM && NewVD->hasAttr<BlocksAttr>()) {
|
2009-05-02 03:41:47 +04:00
|
|
|
Diag(NewVD->getLocation(), diag::err_block_on_vm);
|
|
|
|
return NewVD->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
2010-07-13 03:11:43 +04:00
|
|
|
// Function pointers and references cannot have qualified function type, only
|
|
|
|
// function pointer-to-members can do that.
|
|
|
|
QualType Pointee;
|
|
|
|
unsigned PtrOrRef = 0;
|
|
|
|
if (const PointerType *Ptr = T->getAs<PointerType>())
|
|
|
|
Pointee = Ptr->getPointeeType();
|
|
|
|
else if (const ReferenceType *Ref = T->getAs<ReferenceType>()) {
|
|
|
|
Pointee = Ref->getPointeeType();
|
|
|
|
PtrOrRef = 1;
|
|
|
|
}
|
|
|
|
if (!Pointee.isNull() && Pointee->isFunctionProtoType() &&
|
|
|
|
Pointee->getAs<FunctionProtoType>()->getTypeQuals() != 0) {
|
|
|
|
Diag(NewVD->getLocation(), diag::err_invalid_qualified_function_pointer)
|
|
|
|
<< PtrOrRef;
|
|
|
|
return NewVD->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (!Previous.empty()) {
|
2009-02-16 20:45:42 +03:00
|
|
|
Redeclaration = true;
|
2009-11-19 01:49:29 +03:00
|
|
|
MergeVarDecl(NewVD, Previous);
|
2009-03-11 00:58:27 +03:00
|
|
|
}
|
2009-01-16 05:36:34 +03:00
|
|
|
}
|
|
|
|
|
2009-10-06 21:59:45 +04:00
|
|
|
/// \brief Data used with FindOverriddenMethod
|
|
|
|
struct FindOverriddenMethodData {
|
|
|
|
Sema *S;
|
|
|
|
CXXMethodDecl *Method;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Member lookup function that determines whether a given C++
|
|
|
|
/// method overrides a method in a base class, to be used with
|
|
|
|
/// CXXRecordDecl::lookupInBases().
|
2009-11-12 06:15:40 +03:00
|
|
|
static bool FindOverriddenMethod(const CXXBaseSpecifier *Specifier,
|
2009-10-06 21:59:45 +04:00
|
|
|
CXXBasePath &Path,
|
|
|
|
void *UserData) {
|
|
|
|
RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
|
2009-11-26 23:50:40 +03:00
|
|
|
|
2009-10-06 21:59:45 +04:00
|
|
|
FindOverriddenMethodData *Data
|
|
|
|
= reinterpret_cast<FindOverriddenMethodData*>(UserData);
|
2009-11-26 23:50:40 +03:00
|
|
|
|
|
|
|
DeclarationName Name = Data->Method->getDeclName();
|
|
|
|
|
|
|
|
// FIXME: Do we care about other names here too?
|
|
|
|
if (Name.getNameKind() == DeclarationName::CXXDestructorName) {
|
2010-06-16 12:42:20 +04:00
|
|
|
// We really want to find the base class destructor here.
|
2009-11-26 23:50:40 +03:00
|
|
|
QualType T = Data->S->Context.getTypeDeclType(BaseRecord);
|
|
|
|
CanQualType CT = Data->S->Context.getCanonicalType(T);
|
|
|
|
|
2009-11-27 04:26:58 +03:00
|
|
|
Name = Data->S->Context.DeclarationNames.getCXXDestructorName(CT);
|
2009-11-26 23:50:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Path.Decls = BaseRecord->lookup(Name);
|
2009-10-06 21:59:45 +04:00
|
|
|
Path.Decls.first != Path.Decls.second;
|
|
|
|
++Path.Decls.first) {
|
2010-06-16 13:33:39 +04:00
|
|
|
NamedDecl *D = *Path.Decls.first;
|
2010-06-16 12:42:20 +04:00
|
|
|
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
|
|
|
|
if (MD->isVirtual() && !Data->S->IsOverload(Data->Method, MD, false))
|
2009-10-06 21:59:45 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-19 00:51:29 +03:00
|
|
|
/// AddOverriddenMethods - See if a method overrides any in the base classes,
|
|
|
|
/// and if so, check that it's a valid override and remember it.
|
2010-10-14 02:55:32 +04:00
|
|
|
bool Sema::AddOverriddenMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) {
|
2009-11-19 00:51:29 +03:00
|
|
|
// Look for virtual methods in base classes that this method might override.
|
|
|
|
CXXBasePaths Paths;
|
|
|
|
FindOverriddenMethodData Data;
|
|
|
|
Data.Method = MD;
|
|
|
|
Data.S = this;
|
2010-10-14 02:55:32 +04:00
|
|
|
bool AddedAny = false;
|
2009-11-19 00:51:29 +03:00
|
|
|
if (DC->lookupInBases(&FindOverriddenMethod, &Data, Paths)) {
|
|
|
|
for (CXXBasePaths::decl_iterator I = Paths.found_decls_begin(),
|
|
|
|
E = Paths.found_decls_end(); I != E; ++I) {
|
|
|
|
if (CXXMethodDecl *OldMD = dyn_cast<CXXMethodDecl>(*I)) {
|
|
|
|
if (!CheckOverridingFunctionReturnType(MD, OldMD) &&
|
2009-11-21 11:43:09 +03:00
|
|
|
!CheckOverridingFunctionExceptionSpec(MD, OldMD) &&
|
2010-10-14 02:55:32 +04:00
|
|
|
!CheckOverridingFunctionAttributes(MD, OldMD)) {
|
2009-12-04 08:51:56 +03:00
|
|
|
MD->addOverriddenMethod(OldMD->getCanonicalDecl());
|
2010-10-14 02:55:32 +04:00
|
|
|
AddedAny = true;
|
|
|
|
}
|
2009-11-19 00:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-14 02:55:32 +04:00
|
|
|
|
|
|
|
return AddedAny;
|
2009-11-19 00:51:29 +03:00
|
|
|
}
|
|
|
|
|
2010-10-13 09:45:15 +04:00
|
|
|
static void DiagnoseInvalidRedeclaration(Sema &S, FunctionDecl *NewFD) {
|
|
|
|
LookupResult Prev(S, NewFD->getDeclName(), NewFD->getLocation(),
|
|
|
|
Sema::LookupOrdinaryName, Sema::ForRedeclaration);
|
|
|
|
S.LookupQualifiedName(Prev, NewFD->getDeclContext());
|
|
|
|
assert(!Prev.isAmbiguous() &&
|
|
|
|
"Cannot have an ambiguity in previous-declaration lookup");
|
|
|
|
for (LookupResult::iterator Func = Prev.begin(), FuncEnd = Prev.end();
|
|
|
|
Func != FuncEnd; ++Func) {
|
|
|
|
if (isa<FunctionDecl>(*Func) &&
|
|
|
|
isNearlyMatchingFunction(S.Context, cast<FunctionDecl>(*Func), NewFD))
|
|
|
|
S.Diag((*Func)->getLocation(), diag::note_member_def_close_match);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
NamedDecl*
|
2009-01-16 04:13:29 +03:00
|
|
|
Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
|
2009-12-07 05:54:59 +03:00
|
|
|
QualType R, TypeSourceInfo *TInfo,
|
2009-11-19 01:49:29 +03:00
|
|
|
LookupResult &Previous,
|
2009-06-24 03:11:28 +04:00
|
|
|
MultiTemplateParamsArg TemplateParamLists,
|
2009-04-25 12:06:05 +04:00
|
|
|
bool IsFunctionDefinition, bool &Redeclaration) {
|
2009-01-16 04:13:29 +03:00
|
|
|
assert(R.getTypePtr()->isFunctionType());
|
|
|
|
|
2010-08-12 02:01:17 +04:00
|
|
|
// TODO: consider using NameInfo for diagnostic.
|
|
|
|
DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
|
|
|
|
DeclarationName Name = NameInfo.getName();
|
2010-08-26 07:08:43 +04:00
|
|
|
FunctionDecl::StorageClass SC = SC_None;
|
2009-01-16 04:13:29 +03:00
|
|
|
switch (D.getDeclSpec().getStorageClassSpec()) {
|
|
|
|
default: assert(0 && "Unknown storage class!");
|
|
|
|
case DeclSpec::SCS_auto:
|
|
|
|
case DeclSpec::SCS_register:
|
|
|
|
case DeclSpec::SCS_mutable:
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(D.getDeclSpec().getStorageClassSpecLoc(),
|
2009-02-24 04:23:02 +03:00
|
|
|
diag::err_typecheck_sclass_func);
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2009-01-16 04:13:29 +03:00
|
|
|
break;
|
2010-08-26 07:08:43 +04:00
|
|
|
case DeclSpec::SCS_unspecified: SC = SC_None; break;
|
|
|
|
case DeclSpec::SCS_extern: SC = SC_Extern; break;
|
2009-02-24 04:23:02 +03:00
|
|
|
case DeclSpec::SCS_static: {
|
2010-08-31 04:36:30 +04:00
|
|
|
if (CurContext->getRedeclContext()->isFunctionOrMethod()) {
|
2009-02-24 04:23:02 +03:00
|
|
|
// C99 6.7.1p5:
|
|
|
|
// The declaration of an identifier for a function that has
|
|
|
|
// block scope shall have no explicit storage-class specifier
|
|
|
|
// other than extern
|
|
|
|
// See also (C++ [dcl.stc]p4).
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(D.getDeclSpec().getStorageClassSpecLoc(),
|
2009-02-24 04:23:02 +03:00
|
|
|
diag::err_static_block_func);
|
2010-08-26 07:08:43 +04:00
|
|
|
SC = SC_None;
|
2009-02-24 04:23:02 +03:00
|
|
|
} else
|
2010-08-26 07:08:43 +04:00
|
|
|
SC = SC_Static;
|
2009-02-24 04:23:02 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-09-08 04:31:13 +04:00
|
|
|
case DeclSpec::SCS_private_extern: SC = SC_PrivateExtern; break;
|
2009-01-16 04:13:29 +03:00
|
|
|
}
|
|
|
|
|
2009-04-20 00:27:55 +04:00
|
|
|
if (D.getDeclSpec().isThreadSpecified())
|
|
|
|
Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
|
|
|
|
|
2009-08-11 10:59:38 +04:00
|
|
|
bool isFriend = D.getDeclSpec().isFriendSpecified();
|
2009-01-16 04:13:29 +03:00
|
|
|
bool isInline = D.getDeclSpec().isInlineSpecified();
|
2009-03-12 02:00:04 +03:00
|
|
|
bool isVirtual = D.getDeclSpec().isVirtualSpecified();
|
2009-01-16 04:13:29 +03:00
|
|
|
bool isExplicit = D.getDeclSpec().isExplicitSpecified();
|
|
|
|
|
2010-04-20 02:54:31 +04:00
|
|
|
DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpecAsWritten();
|
|
|
|
FunctionDecl::StorageClass SCAsWritten
|
2010-07-30 20:47:02 +04:00
|
|
|
= StorageClassSpecToFunctionDeclStorageClass(SCSpec);
|
2010-04-20 02:54:31 +04:00
|
|
|
|
2009-03-22 23:18:17 +03:00
|
|
|
// Check that the return type is not an abstract class type.
|
2009-03-24 04:19:16 +03:00
|
|
|
// For record types, this is done by the AbstractClassUsageDiagnoser once
|
2009-09-09 19:08:12 +04:00
|
|
|
// the class has been completely parsed.
|
2009-03-24 04:19:16 +03:00
|
|
|
if (!DC->isRecord() &&
|
2009-09-09 19:08:12 +04:00
|
|
|
RequireNonAbstractType(D.getIdentifierLoc(),
|
2009-09-22 03:43:11 +04:00
|
|
|
R->getAs<FunctionType>()->getResultType(),
|
2009-09-09 19:08:12 +04:00
|
|
|
diag::err_abstract_type_in_decl,
|
2009-03-24 04:19:16 +03:00
|
|
|
AbstractReturnType))
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-11 23:17:25 +04:00
|
|
|
// Do not allow returning a objc interface by-value.
|
2010-05-15 15:32:37 +04:00
|
|
|
if (R->getAs<FunctionType>()->getResultType()->isObjCObjectType()) {
|
2009-04-11 23:17:25 +04:00
|
|
|
Diag(D.getIdentifierLoc(),
|
|
|
|
diag::err_object_cannot_be_passed_returned_by_value) << 0
|
2009-09-22 03:43:11 +04:00
|
|
|
<< R->getAs<FunctionType>()->getResultType();
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2009-04-11 23:17:25 +04:00
|
|
|
}
|
2009-06-24 03:11:28 +04:00
|
|
|
|
2009-03-12 02:00:04 +03:00
|
|
|
bool isVirtualOkay = false;
|
2009-01-16 04:13:29 +03:00
|
|
|
FunctionDecl *NewFD;
|
2009-08-28 11:59:38 +04:00
|
|
|
|
2009-08-11 10:59:38 +04:00
|
|
|
if (isFriend) {
|
|
|
|
// C++ [class.friend]p5
|
|
|
|
// A function can be defined in a friend declaration of a
|
|
|
|
// class . . . . Such a function is implicitly inline.
|
|
|
|
isInline |= IsFunctionDefinition;
|
2009-08-28 11:59:38 +04:00
|
|
|
}
|
2009-08-11 10:59:38 +04:00
|
|
|
|
2009-11-03 04:35:08 +03:00
|
|
|
if (Name.getNameKind() == DeclarationName::CXXConstructorName) {
|
2009-01-16 04:13:29 +03:00
|
|
|
// This is a C++ constructor declaration.
|
|
|
|
assert(DC->isRecord() &&
|
|
|
|
"Constructors can only be declared in a member context");
|
|
|
|
|
2009-04-25 12:28:21 +04:00
|
|
|
R = CheckConstructorDeclarator(D, R, SC);
|
2009-01-16 04:13:29 +03:00
|
|
|
|
|
|
|
// Create the new declaration
|
2009-09-09 19:08:12 +04:00
|
|
|
NewFD = CXXConstructorDecl::Create(Context,
|
2009-01-16 04:13:29 +03:00
|
|
|
cast<CXXRecordDecl>(DC),
|
2010-08-12 02:01:17 +04:00
|
|
|
NameInfo, R, TInfo,
|
2009-01-16 04:13:29 +03:00
|
|
|
isExplicit, isInline,
|
|
|
|
/*isImplicitlyDeclared=*/false);
|
2009-11-03 04:35:08 +03:00
|
|
|
} else if (Name.getNameKind() == DeclarationName::CXXDestructorName) {
|
2009-01-16 04:13:29 +03:00
|
|
|
// This is a C++ destructor declaration.
|
|
|
|
if (DC->isRecord()) {
|
2010-07-01 09:10:53 +04:00
|
|
|
R = CheckDestructorDeclarator(D, R, SC);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-01-16 04:13:29 +03:00
|
|
|
NewFD = CXXDestructorDecl::Create(Context,
|
|
|
|
cast<CXXRecordDecl>(DC),
|
2010-08-12 02:01:17 +04:00
|
|
|
NameInfo, R,
|
2009-01-16 04:13:29 +03:00
|
|
|
isInline,
|
|
|
|
/*isImplicitlyDeclared=*/false);
|
2010-03-11 12:03:00 +03:00
|
|
|
NewFD->setTypeSourceInfo(TInfo);
|
2009-01-16 04:13:29 +03:00
|
|
|
|
2009-03-12 02:00:04 +03:00
|
|
|
isVirtualOkay = true;
|
2009-01-16 04:13:29 +03:00
|
|
|
} else {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_destructor_not_member);
|
|
|
|
|
|
|
|
// Create a FunctionDecl to satisfy the function definition parsing
|
|
|
|
// code path.
|
|
|
|
NewFD = FunctionDecl::Create(Context, DC, D.getIdentifierLoc(),
|
2010-04-20 02:54:31 +04:00
|
|
|
Name, R, TInfo, SC, SCAsWritten, isInline,
|
2009-08-21 04:31:54 +04:00
|
|
|
/*hasPrototype=*/true);
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2009-01-16 04:13:29 +03:00
|
|
|
}
|
2009-11-03 04:35:08 +03:00
|
|
|
} else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
|
2009-01-16 04:13:29 +03:00
|
|
|
if (!DC->isRecord()) {
|
|
|
|
Diag(D.getIdentifierLoc(),
|
|
|
|
diag::err_conv_function_not_member);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-25 12:35:12 +04:00
|
|
|
CheckConversionDeclarator(D, R, SC);
|
|
|
|
NewFD = CXXConversionDecl::Create(Context, cast<CXXRecordDecl>(DC),
|
2010-08-12 02:01:17 +04:00
|
|
|
NameInfo, R, TInfo,
|
2009-04-25 12:35:12 +04:00
|
|
|
isInline, isExplicit);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-25 12:35:12 +04:00
|
|
|
isVirtualOkay = true;
|
2009-01-16 04:13:29 +03:00
|
|
|
} else if (DC->isRecord()) {
|
2009-05-01 02:41:11 +04:00
|
|
|
// If the of the function is the same as the name of the record, then this
|
2009-09-09 19:08:12 +04:00
|
|
|
// must be an invalid constructor that has a return type.
|
|
|
|
// (The parser checks for a return type and makes the declarator a
|
2009-05-01 02:41:11 +04:00
|
|
|
// constructor if it has no return type).
|
2009-09-09 19:08:12 +04:00
|
|
|
// must have an invalid constructor that has a return type
|
2010-02-05 09:12:42 +03:00
|
|
|
if (Name.getAsIdentifierInfo() &&
|
|
|
|
Name.getAsIdentifierInfo() == cast<CXXRecordDecl>(DC)->getIdentifier()){
|
2009-05-01 02:41:11 +04:00
|
|
|
Diag(D.getIdentifierLoc(), diag::err_constructor_return_type)
|
|
|
|
<< SourceRange(D.getDeclSpec().getTypeSpecTypeLoc())
|
|
|
|
<< SourceRange(D.getIdentifierLoc());
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-26 07:08:43 +04:00
|
|
|
bool isStatic = SC == SC_Static;
|
2009-11-15 21:59:32 +03:00
|
|
|
|
|
|
|
// [class.free]p1:
|
|
|
|
// Any allocation function for a class T is a static member
|
|
|
|
// (even if not explicitly declared static).
|
|
|
|
if (Name.getCXXOverloadedOperator() == OO_New ||
|
|
|
|
Name.getCXXOverloadedOperator() == OO_Array_New)
|
|
|
|
isStatic = true;
|
2009-11-15 22:08:46 +03:00
|
|
|
|
|
|
|
// [class.free]p6 Any deallocation function for a class X is a static member
|
|
|
|
// (even if not explicitly declared static).
|
|
|
|
if (Name.getCXXOverloadedOperator() == OO_Delete ||
|
|
|
|
Name.getCXXOverloadedOperator() == OO_Array_Delete)
|
|
|
|
isStatic = true;
|
2009-11-15 21:59:32 +03:00
|
|
|
|
2009-01-16 04:13:29 +03:00
|
|
|
// This is a C++ method declaration.
|
|
|
|
NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(DC),
|
2010-08-12 02:01:17 +04:00
|
|
|
NameInfo, R, TInfo,
|
2010-04-20 02:54:31 +04:00
|
|
|
isStatic, SCAsWritten, isInline);
|
2009-03-12 02:00:04 +03:00
|
|
|
|
2009-11-15 22:08:46 +03:00
|
|
|
isVirtualOkay = !isStatic;
|
2009-01-16 04:13:29 +03:00
|
|
|
} else {
|
2009-03-19 21:33:54 +03:00
|
|
|
// Determine whether the function was written with a
|
|
|
|
// prototype. This true when:
|
|
|
|
// - we're in C++ (where every function has a prototype),
|
|
|
|
// - there is a prototype in the declarator, or
|
|
|
|
// - the type R of the function is some kind of typedef or other reference
|
|
|
|
// to a type name (which eventually refers to a function type).
|
2009-09-09 19:08:12 +04:00
|
|
|
bool HasPrototype =
|
2009-03-18 02:17:04 +03:00
|
|
|
getLangOptions().CPlusPlus ||
|
2009-03-19 21:14:46 +03:00
|
|
|
(D.getNumTypeObjects() && D.getTypeObject(0).Fun.hasPrototype) ||
|
2009-03-23 19:26:51 +03:00
|
|
|
(!isa<FunctionType>(R.getTypePtr()) && R->isFunctionProtoType());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-01-16 04:13:29 +03:00
|
|
|
NewFD = FunctionDecl::Create(Context, DC,
|
2010-08-12 02:01:17 +04:00
|
|
|
NameInfo, R, TInfo, SC, SCAsWritten, isInline,
|
2010-04-20 02:54:31 +04:00
|
|
|
HasPrototype);
|
2009-01-16 04:13:29 +03:00
|
|
|
}
|
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
if (D.isInvalidType())
|
2009-04-25 09:44:12 +04:00
|
|
|
NewFD->setInvalidDecl();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-03-15 13:12:16 +03:00
|
|
|
SetNestedNameSpecifier(NewFD, D);
|
|
|
|
|
2009-01-16 04:13:29 +03:00
|
|
|
// Set the lexical context. If the declarator has a C++
|
2009-08-28 11:59:38 +04:00
|
|
|
// scope specifier, or is the object of a friend declaration, the
|
|
|
|
// lexical context will be different from the semantic context.
|
2009-01-16 04:13:29 +03:00
|
|
|
NewFD->setLexicalDeclContext(CurContext);
|
|
|
|
|
2009-07-22 03:53:31 +04:00
|
|
|
// Match up the template parameter lists with the scope specifier, then
|
|
|
|
// determine whether we have a template or a template specialization.
|
2009-06-24 04:23:40 +04:00
|
|
|
FunctionTemplateDecl *FunctionTemplate = 0;
|
2009-10-08 02:35:40 +04:00
|
|
|
bool isExplicitSpecialization = false;
|
2009-09-25 03:14:47 +04:00
|
|
|
bool isFunctionTemplateSpecialization = false;
|
2010-06-12 12:15:14 +04:00
|
|
|
unsigned NumMatchedTemplateParamLists = TemplateParamLists.size();
|
2010-07-15 03:14:12 +04:00
|
|
|
bool Invalid = false;
|
2009-07-22 03:53:31 +04:00
|
|
|
if (TemplateParameterList *TemplateParams
|
|
|
|
= MatchTemplateParametersToScopeSpecifier(
|
|
|
|
D.getDeclSpec().getSourceRange().getBegin(),
|
|
|
|
D.getCXXScopeSpec(),
|
2009-07-23 02:05:02 +04:00
|
|
|
(TemplateParameterList**)TemplateParamLists.get(),
|
2009-10-08 02:35:40 +04:00
|
|
|
TemplateParamLists.size(),
|
2010-04-14 00:37:33 +04:00
|
|
|
isFriend,
|
2010-07-15 03:14:12 +04:00
|
|
|
isExplicitSpecialization,
|
|
|
|
Invalid)) {
|
2010-06-12 12:15:14 +04:00
|
|
|
// All but one template parameter lists have been matching.
|
|
|
|
--NumMatchedTemplateParamLists;
|
|
|
|
|
2009-06-24 04:23:40 +04:00
|
|
|
if (TemplateParams->size() > 0) {
|
|
|
|
// This is a function template
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-25 21:23:04 +04:00
|
|
|
// Check that we can declare a template here.
|
|
|
|
if (CheckTemplateDeclScope(S, TemplateParams))
|
|
|
|
return 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-27 20:57:43 +04:00
|
|
|
FunctionTemplate = FunctionTemplateDecl::Create(Context, DC,
|
2009-06-24 04:23:40 +04:00
|
|
|
NewFD->getLocation(),
|
|
|
|
Name, TemplateParams,
|
|
|
|
NewFD);
|
2009-08-27 20:57:43 +04:00
|
|
|
FunctionTemplate->setLexicalDeclContext(CurContext);
|
2009-06-24 04:23:40 +04:00
|
|
|
NewFD->setDescribedFunctionTemplate(FunctionTemplate);
|
|
|
|
} else {
|
2009-09-25 03:14:47 +04:00
|
|
|
// This is a function template specialization.
|
|
|
|
isFunctionTemplateSpecialization = true;
|
2010-03-24 10:46:06 +03:00
|
|
|
|
2010-04-08 13:05:18 +04:00
|
|
|
// C++0x [temp.expl.spec]p20 forbids "template<> friend void foo(int);".
|
2010-03-24 10:46:06 +03:00
|
|
|
if (isFriend && isFunctionTemplateSpecialization) {
|
2010-03-24 11:27:58 +03:00
|
|
|
// We want to remove the "template<>", found here.
|
|
|
|
SourceRange RemoveRange = TemplateParams->getSourceRange();
|
|
|
|
|
|
|
|
// If we remove the template<> and the name is not a
|
|
|
|
// template-id, we're actually silently creating a problem:
|
|
|
|
// the friend declaration will refer to an untemplated decl,
|
|
|
|
// and clearly the user wants a template specialization. So
|
|
|
|
// we need to insert '<>' after the name.
|
|
|
|
SourceLocation InsertLoc;
|
|
|
|
if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
|
|
|
|
InsertLoc = D.getName().getSourceRange().getEnd();
|
|
|
|
InsertLoc = PP.getLocForEndOfToken(InsertLoc);
|
|
|
|
}
|
|
|
|
|
2010-03-24 10:46:06 +03:00
|
|
|
Diag(D.getIdentifierLoc(), diag::err_template_spec_decl_friend)
|
2010-03-24 11:27:58 +03:00
|
|
|
<< Name << RemoveRange
|
2010-03-31 21:46:05 +04:00
|
|
|
<< FixItHint::CreateRemoval(RemoveRange)
|
|
|
|
<< FixItHint::CreateInsertion(InsertLoc, "<>");
|
2010-03-24 10:46:06 +03:00
|
|
|
}
|
2009-06-24 04:23:40 +04:00
|
|
|
}
|
2010-06-12 12:15:14 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-07-29 03:59:57 +04:00
|
|
|
if (NumMatchedTemplateParamLists > 0 && D.getCXXScopeSpec().isSet()) {
|
2010-06-15 21:44:38 +04:00
|
|
|
NewFD->setTemplateParameterListsInfo(Context,
|
|
|
|
NumMatchedTemplateParamLists,
|
2010-06-12 12:15:14 +04:00
|
|
|
(TemplateParameterList**)TemplateParamLists.release());
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2010-06-12 12:15:14 +04:00
|
|
|
|
2010-07-15 03:14:12 +04:00
|
|
|
if (Invalid) {
|
|
|
|
NewFD->setInvalidDecl();
|
|
|
|
if (FunctionTemplate)
|
|
|
|
FunctionTemplate->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
2009-03-12 02:00:04 +03:00
|
|
|
// C++ [dcl.fct.spec]p5:
|
|
|
|
// The virtual specifier shall only be used in declarations of
|
|
|
|
// nonstatic class member functions that appear within a
|
|
|
|
// member-specification of a class declaration; see 10.3.
|
|
|
|
//
|
2009-04-25 12:06:05 +04:00
|
|
|
if (isVirtual && !NewFD->isInvalidDecl()) {
|
2009-03-12 02:00:04 +03:00
|
|
|
if (!isVirtualOkay) {
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(D.getDeclSpec().getVirtualSpecLoc(),
|
2009-03-12 02:00:04 +03:00
|
|
|
diag::err_virtual_non_function);
|
|
|
|
} else if (!CurContext->isRecord()) {
|
|
|
|
// 'virtual' was specified outside of the class.
|
|
|
|
Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_virtual_out_of_class)
|
2010-03-31 21:46:05 +04:00
|
|
|
<< FixItHint::CreateRemoval(D.getDeclSpec().getVirtualSpecLoc());
|
2009-03-12 02:00:04 +03:00
|
|
|
} else {
|
|
|
|
// Okay: Add virtual to the method.
|
2010-09-29 00:50:54 +04:00
|
|
|
NewFD->setVirtualAsWritten(true);
|
2009-03-12 02:00:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-06 15:44:10 +04:00
|
|
|
// C++ [dcl.fct.spec]p3:
|
|
|
|
// The inline specifier shall not appear on a block scope function declaration.
|
|
|
|
if (isInline && !NewFD->isInvalidDecl() && getLangOptions().CPlusPlus) {
|
|
|
|
if (CurContext->isFunctionOrMethod()) {
|
|
|
|
// 'inline' is not allowed on block scope function declaration.
|
|
|
|
Diag(D.getDeclSpec().getInlineSpecLoc(),
|
|
|
|
diag::err_inline_declaration_block_scope) << Name
|
|
|
|
<< FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-24 19:49:46 +03:00
|
|
|
// C++ [dcl.fct.spec]p6:
|
|
|
|
// The explicit specifier shall be used only in the declaration of a
|
|
|
|
// constructor or conversion function within its class definition; see 12.3.1
|
|
|
|
// and 12.3.2.
|
|
|
|
if (isExplicit && !NewFD->isInvalidDecl()) {
|
|
|
|
if (!CurContext->isRecord()) {
|
|
|
|
// 'explicit' was specified outside of the class.
|
|
|
|
Diag(D.getDeclSpec().getExplicitSpecLoc(),
|
|
|
|
diag::err_explicit_out_of_class)
|
2010-03-31 21:46:05 +04:00
|
|
|
<< FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecLoc());
|
2010-01-24 19:49:46 +03:00
|
|
|
} else if (!isa<CXXConstructorDecl>(NewFD) &&
|
|
|
|
!isa<CXXConversionDecl>(NewFD)) {
|
|
|
|
// 'explicit' was specified on a function that wasn't a constructor
|
|
|
|
// or conversion function.
|
|
|
|
Diag(D.getDeclSpec().getExplicitSpecLoc(),
|
|
|
|
diag::err_explicit_non_ctor_or_conv_function)
|
2010-03-31 21:46:05 +04:00
|
|
|
<< FixItHint::CreateRemoval(D.getDeclSpec().getExplicitSpecLoc());
|
2010-01-24 19:49:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
// Filter out previous declarations that don't match the scope.
|
|
|
|
FilterLookupForScope(*this, Previous, DC, S, NewFD->hasLinkage());
|
|
|
|
|
2009-10-13 18:39:41 +04:00
|
|
|
if (isFriend) {
|
2010-03-27 08:57:59 +03:00
|
|
|
// For now, claim that the objects have no previous declaration.
|
2009-10-13 18:39:41 +04:00
|
|
|
if (FunctionTemplate) {
|
2010-03-27 08:57:59 +03:00
|
|
|
FunctionTemplate->setObjectOfFriendDecl(false);
|
2009-10-13 18:39:41 +04:00
|
|
|
FunctionTemplate->setAccess(AS_public);
|
|
|
|
}
|
2010-04-14 00:37:33 +04:00
|
|
|
NewFD->setObjectOfFriendDecl(false);
|
2009-10-13 18:39:41 +04:00
|
|
|
NewFD->setAccess(AS_public);
|
|
|
|
}
|
|
|
|
|
2010-08-26 07:08:43 +04:00
|
|
|
if (SC == SC_Static && isa<CXXMethodDecl>(NewFD) &&
|
2009-03-12 02:52:16 +03:00
|
|
|
!CurContext->isRecord()) {
|
|
|
|
// C++ [class.static]p1:
|
|
|
|
// A data or function member of a class may be declared static
|
|
|
|
// in a class definition, in which case it is a static member of
|
|
|
|
// the class.
|
|
|
|
|
|
|
|
// Complain about the 'static' specifier if it's on an out-of-line
|
|
|
|
// member function definition.
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(D.getDeclSpec().getStorageClassSpecLoc(),
|
2009-03-12 02:52:16 +03:00
|
|
|
diag::err_static_out_of_line)
|
2010-03-31 21:46:05 +04:00
|
|
|
<< FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
|
2009-03-12 02:52:16 +03:00
|
|
|
}
|
|
|
|
|
2009-01-16 04:13:29 +03:00
|
|
|
// Handle GNU asm-label extension (encoded as an attribute).
|
|
|
|
if (Expr *E = (Expr*) D.getAsmLabel()) {
|
|
|
|
// The parser guarantees this is a string.
|
2009-09-09 19:08:12 +04:00
|
|
|
StringLiteral *SE = cast<StringLiteral>(E);
|
2010-08-19 03:23:40 +04:00
|
|
|
NewFD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), Context,
|
|
|
|
SE->getString()));
|
2009-01-16 04:13:29 +03:00
|
|
|
}
|
|
|
|
|
2009-04-25 10:12:16 +04:00
|
|
|
// Copy the parameter declarations from the declarator D to the function
|
|
|
|
// declaration NewFD, if they are available. First scavenge them into Params.
|
|
|
|
llvm::SmallVector<ParmVarDecl*, 16> Params;
|
2009-01-16 04:13:29 +03:00
|
|
|
if (D.getNumTypeObjects() > 0) {
|
|
|
|
DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
|
|
|
|
|
|
|
|
// Check for C99 6.7.5.3p10 - foo(void) is a non-varargs
|
|
|
|
// function that takes no arguments, not a function that takes a
|
|
|
|
// single void argument.
|
|
|
|
// We let through "const void" here because Sema::GetTypeForDeclarator
|
|
|
|
// already checks for that case.
|
|
|
|
if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
|
|
|
|
FTI.ArgInfo[0].Param &&
|
2010-08-21 13:40:31 +04:00
|
|
|
cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()) {
|
2009-04-25 10:12:16 +04:00
|
|
|
// Empty arg list, don't push any params.
|
2010-08-21 13:40:31 +04:00
|
|
|
ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[0].Param);
|
2009-01-16 04:13:29 +03:00
|
|
|
|
|
|
|
// In C++, the empty parameter-type-list must be spelled "void"; a
|
|
|
|
// typedef of void is not permitted.
|
|
|
|
if (getLangOptions().CPlusPlus &&
|
2009-04-25 09:44:12 +04:00
|
|
|
Param->getType().getUnqualifiedType() != Context.VoidTy)
|
2009-04-02 03:51:29 +04:00
|
|
|
Diag(Param->getLocation(), diag::err_param_typedef_of_void);
|
2009-04-25 10:12:16 +04:00
|
|
|
// FIXME: Leaks decl?
|
2009-01-16 04:13:29 +03:00
|
|
|
} else if (FTI.NumArgs > 0 && FTI.ArgInfo[0].Param != 0) {
|
2009-07-14 07:18:53 +04:00
|
|
|
for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
|
2010-08-21 13:40:31 +04:00
|
|
|
ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
|
2009-07-14 07:18:53 +04:00
|
|
|
assert(Param->getDeclContext() != NewFD && "Was set before ?");
|
|
|
|
Param->setDeclContext(NewFD);
|
|
|
|
Params.push_back(Param);
|
2010-04-14 05:27:20 +04:00
|
|
|
|
|
|
|
if (Param->isInvalidDecl())
|
|
|
|
NewFD->setInvalidDecl();
|
2009-07-14 07:18:53 +04:00
|
|
|
}
|
2009-01-16 04:13:29 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-22 03:43:11 +04:00
|
|
|
} else if (const FunctionProtoType *FT = R->getAs<FunctionProtoType>()) {
|
2009-04-25 10:03:53 +04:00
|
|
|
// When we're declaring a function with a typedef, typeof, etc as in the
|
2009-01-16 04:13:29 +03:00
|
|
|
// following example, we'll need to synthesize (unnamed)
|
|
|
|
// parameters for use in the declaration.
|
|
|
|
//
|
|
|
|
// @code
|
|
|
|
// typedef void fn(int);
|
|
|
|
// fn f;
|
|
|
|
// @endcode
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-25 10:03:53 +04:00
|
|
|
// Synthesize a parameter for each argument type.
|
|
|
|
for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
|
|
|
|
AE = FT->arg_type_end(); AI != AE; ++AI) {
|
2010-06-04 15:21:44 +04:00
|
|
|
ParmVarDecl *Param =
|
|
|
|
BuildParmVarDeclForTypedef(NewFD, D.getIdentifierLoc(), *AI);
|
2009-04-25 10:03:53 +04:00
|
|
|
Params.push_back(Param);
|
2009-01-16 04:13:29 +03:00
|
|
|
}
|
2009-04-25 22:38:18 +04:00
|
|
|
} else {
|
|
|
|
assert(R->isFunctionNoProtoType() && NewFD->getNumParams() == 0 &&
|
|
|
|
"Should not need args for typedef of non-prototype fn");
|
2009-01-16 04:13:29 +03:00
|
|
|
}
|
2009-04-25 10:12:16 +04:00
|
|
|
// Finally, we know we have the right number of parameters, install them.
|
2010-02-11 04:19:42 +03:00
|
|
|
NewFD->setParams(Params.data(), Params.size());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-26 03:53:26 +04:00
|
|
|
// If the declarator is a template-id, translate the parser's template
|
|
|
|
// argument list into our AST format.
|
|
|
|
bool HasExplicitTemplateArgs = false;
|
2009-11-23 04:53:49 +03:00
|
|
|
TemplateArgumentListInfo TemplateArgs;
|
2009-11-03 04:35:08 +03:00
|
|
|
if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
|
|
|
|
TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
|
2009-11-23 04:53:49 +03:00
|
|
|
TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
|
|
|
|
TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
|
2009-09-26 03:53:26 +04:00
|
|
|
ASTTemplateArgsPtr TemplateArgsPtr(*this,
|
|
|
|
TemplateId->getTemplateArgs(),
|
|
|
|
TemplateId->NumArgs);
|
|
|
|
translateTemplateArguments(TemplateArgsPtr,
|
|
|
|
TemplateArgs);
|
|
|
|
TemplateArgsPtr.release();
|
|
|
|
|
|
|
|
HasExplicitTemplateArgs = true;
|
|
|
|
|
|
|
|
if (FunctionTemplate) {
|
2009-10-08 02:35:40 +04:00
|
|
|
// FIXME: Diagnose function template with explicit template
|
2009-09-26 03:53:26 +04:00
|
|
|
// arguments.
|
|
|
|
HasExplicitTemplateArgs = false;
|
|
|
|
} else if (!isFunctionTemplateSpecialization &&
|
|
|
|
!D.getDeclSpec().isFriendSpecified()) {
|
|
|
|
// We have encountered something that the user meant to be a
|
|
|
|
// specialization (because it has explicitly-specified template
|
|
|
|
// arguments) but that was not introduced with a "template<>" (or had
|
|
|
|
// too few of them).
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_template_spec_needs_header)
|
|
|
|
<< SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc)
|
2010-03-31 21:46:05 +04:00
|
|
|
<< FixItHint::CreateInsertion(
|
2009-09-26 03:53:26 +04:00
|
|
|
D.getDeclSpec().getSourceRange().getBegin(),
|
|
|
|
"template<> ");
|
|
|
|
isFunctionTemplateSpecialization = true;
|
2010-03-24 10:46:06 +03:00
|
|
|
} else {
|
|
|
|
// "friend void foo<>(int);" is an implicit specialization decl.
|
|
|
|
isFunctionTemplateSpecialization = true;
|
2009-09-26 03:53:26 +04:00
|
|
|
}
|
2010-04-08 13:05:18 +04:00
|
|
|
} else if (isFriend && isFunctionTemplateSpecialization) {
|
|
|
|
// This combination is only possible in a recovery case; the user
|
|
|
|
// wrote something like:
|
|
|
|
// template <> friend void foo(int);
|
|
|
|
// which we're recovering from as if the user had written:
|
|
|
|
// friend void foo<>(int);
|
|
|
|
// Go ahead and fake up a template id.
|
|
|
|
HasExplicitTemplateArgs = true;
|
|
|
|
TemplateArgs.setLAngleLoc(D.getIdentifierLoc());
|
|
|
|
TemplateArgs.setRAngleLoc(D.getIdentifierLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a friend (and only if it's a friend), it's possible
|
|
|
|
// that either the specialized function type or the specialized
|
|
|
|
// template is dependent, and therefore matching will fail. In
|
|
|
|
// this case, don't check the specialization yet.
|
|
|
|
if (isFunctionTemplateSpecialization && isFriend &&
|
|
|
|
(NewFD->getType()->isDependentType() || DC->isDependentContext())) {
|
|
|
|
assert(HasExplicitTemplateArgs &&
|
|
|
|
"friend function specialization without template args");
|
|
|
|
if (CheckDependentFunctionTemplateSpecialization(NewFD, TemplateArgs,
|
|
|
|
Previous))
|
2010-03-24 20:31:23 +03:00
|
|
|
NewFD->setInvalidDecl();
|
2010-04-08 13:05:18 +04:00
|
|
|
} else if (isFunctionTemplateSpecialization) {
|
|
|
|
if (CheckFunctionTemplateSpecialization(NewFD,
|
|
|
|
(HasExplicitTemplateArgs ? &TemplateArgs : 0),
|
|
|
|
Previous))
|
|
|
|
NewFD->setInvalidDecl();
|
|
|
|
} else if (isExplicitSpecialization && isa<CXXMethodDecl>(NewFD)) {
|
|
|
|
if (CheckMemberSpecialization(NewFD, Previous))
|
|
|
|
NewFD->setInvalidDecl();
|
|
|
|
}
|
2010-04-13 10:39:49 +04:00
|
|
|
|
2009-03-24 02:06:20 +03:00
|
|
|
// Perform semantic checking on the function declaration.
|
|
|
|
bool OverloadableAttrRequired = false; // FIXME: HACK!
|
2009-12-10 12:41:52 +03:00
|
|
|
CheckFunctionDeclaration(S, NewFD, Previous, isExplicitSpecialization,
|
2009-10-13 20:30:37 +04:00
|
|
|
Redeclaration, /*FIXME:*/OverloadableAttrRequired);
|
2009-03-24 02:06:20 +03:00
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
assert((NewFD->isInvalidDecl() || !Redeclaration ||
|
|
|
|
Previous.getResultKind() != LookupResult::FoundOverloaded) &&
|
|
|
|
"previous declaration set still overloaded");
|
|
|
|
|
2010-04-24 05:30:58 +04:00
|
|
|
NamedDecl *PrincipalDecl = (FunctionTemplate
|
|
|
|
? cast<NamedDecl>(FunctionTemplate)
|
|
|
|
: NewFD);
|
|
|
|
|
2010-03-27 08:57:59 +03:00
|
|
|
if (isFriend && Redeclaration) {
|
2010-04-13 10:39:49 +04:00
|
|
|
AccessSpecifier Access = AS_public;
|
|
|
|
if (!NewFD->isInvalidDecl())
|
|
|
|
Access = NewFD->getPreviousDeclaration()->getAccess();
|
|
|
|
|
2010-03-27 08:57:59 +03:00
|
|
|
NewFD->setAccess(Access);
|
2010-04-24 05:30:58 +04:00
|
|
|
if (FunctionTemplate) FunctionTemplate->setAccess(Access);
|
|
|
|
|
|
|
|
PrincipalDecl->setObjectOfFriendDecl(true);
|
2010-03-27 08:57:59 +03:00
|
|
|
}
|
|
|
|
|
2010-04-24 05:30:58 +04:00
|
|
|
if (NewFD->isOverloadedOperator() && !DC->isRecord() &&
|
|
|
|
PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
|
|
|
|
PrincipalDecl->setNonMemberOperator();
|
|
|
|
|
2009-11-25 20:50:39 +03:00
|
|
|
// If we have a function template, check the template parameter
|
|
|
|
// list. This will check and merge default template arguments.
|
|
|
|
if (FunctionTemplate) {
|
|
|
|
FunctionTemplateDecl *PrevTemplate = FunctionTemplate->getPreviousDeclaration();
|
|
|
|
CheckTemplateParameterList(FunctionTemplate->getTemplateParameters(),
|
|
|
|
PrevTemplate? PrevTemplate->getTemplateParameters() : 0,
|
|
|
|
D.getDeclSpec().isFriendSpecified()? TPC_FriendFunctionTemplate
|
|
|
|
: TPC_FunctionTemplate);
|
|
|
|
}
|
|
|
|
|
2010-10-13 09:45:15 +04:00
|
|
|
if (NewFD->isInvalidDecl()) {
|
|
|
|
// Ignore all the rest of this.
|
|
|
|
} else if (!Redeclaration) {
|
|
|
|
// Fake up an access specifier if it's supposed to be a class member.
|
|
|
|
if (isa<CXXRecordDecl>(NewFD->getDeclContext()))
|
|
|
|
NewFD->setAccess(AS_public);
|
|
|
|
|
|
|
|
// Qualified decls generally require a previous declaration.
|
|
|
|
if (D.getCXXScopeSpec().isSet()) {
|
|
|
|
// ...with the major exception of dependent friend declarations.
|
|
|
|
// In theory, this condition could be whether the qualifier
|
|
|
|
// is dependent; in practice, the way we nest template parameters
|
|
|
|
// prevents this sort of matching from working, so we have to base it
|
|
|
|
// on the general dependence of the context.
|
|
|
|
if (isFriend && CurContext->isDependentContext()) {
|
|
|
|
// ignore these
|
|
|
|
|
|
|
|
} else {
|
2010-10-02 01:19:28 +04:00
|
|
|
// The user tried to provide an out-of-line definition for a
|
|
|
|
// function that is a member of a class or namespace, but there
|
|
|
|
// was no such member function declared (C++ [class.mfct]p2,
|
|
|
|
// C++ [namespace.memdef]p2). For example:
|
|
|
|
//
|
|
|
|
// class X {
|
|
|
|
// void f() const;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// void X::f() { } // ill-formed
|
|
|
|
//
|
|
|
|
// Complain about this problem, and attempt to suggest close
|
|
|
|
// matches (e.g., those that differ only in cv-qualifiers and
|
|
|
|
// whether the parameter types are references).
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_member_def_does_not_match)
|
|
|
|
<< Name << DC << D.getCXXScopeSpec().getRange();
|
|
|
|
NewFD->setInvalidDecl();
|
|
|
|
|
2010-10-13 09:45:15 +04:00
|
|
|
DiagnoseInvalidRedeclaration(*this, NewFD);
|
2010-10-02 01:19:28 +04:00
|
|
|
}
|
2010-10-13 09:45:15 +04:00
|
|
|
|
|
|
|
// Unqualified local friend declarations are required to resolve
|
|
|
|
// to something.
|
|
|
|
} else if (isFriend && cast<CXXRecordDecl>(CurContext)->isLocalClass()) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_no_matching_local_friend);
|
|
|
|
NewFD->setInvalidDecl();
|
|
|
|
DiagnoseInvalidRedeclaration(*this, NewFD);
|
2009-03-24 02:06:20 +03:00
|
|
|
}
|
2010-10-13 09:45:15 +04:00
|
|
|
|
|
|
|
} else if (!IsFunctionDefinition && D.getCXXScopeSpec().isSet() &&
|
|
|
|
!isFriend && !isFunctionTemplateSpecialization &&
|
|
|
|
!isExplicitSpecialization) {
|
|
|
|
// An out-of-line member function declaration must also be a
|
|
|
|
// definition (C++ [dcl.meaning]p1).
|
|
|
|
// Note that this is not the case for explicit specializations of
|
|
|
|
// function templates or member functions of class templates, per
|
|
|
|
// C++ [temp.expl.spec]p2. We also allow these declarations as an extension
|
|
|
|
// for compatibility with old SWIG code which likes to generate them.
|
|
|
|
Diag(NewFD->getLocation(), diag::ext_out_of_line_declaration)
|
|
|
|
<< D.getCXXScopeSpec().getRange();
|
2009-03-24 02:06:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle attributes. We need to have merged decls when handling attributes
|
|
|
|
// (for example to check for conflicts, etc).
|
|
|
|
// FIXME: This needs to happen before we merge declarations. Then,
|
|
|
|
// let attribute merging cope with attribute conflicts.
|
2009-06-18 01:51:59 +04:00
|
|
|
ProcessDeclAttributes(S, NewFD, D);
|
2009-07-26 02:29:44 +04:00
|
|
|
|
|
|
|
// attributes declared post-definition are currently ignored
|
2010-08-19 03:23:40 +04:00
|
|
|
// FIXME: This should happen during attribute merging
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Redeclaration && Previous.isSingleResult()) {
|
|
|
|
const FunctionDecl *Def;
|
|
|
|
FunctionDecl *PrevFD = dyn_cast<FunctionDecl>(Previous.getFoundDecl());
|
2010-07-07 15:31:19 +04:00
|
|
|
if (PrevFD && PrevFD->hasBody(Def) && D.hasAttributes()) {
|
2009-07-26 02:29:44 +04:00
|
|
|
Diag(NewFD->getLocation(), diag::warn_attribute_precede_definition);
|
|
|
|
Diag(Def->getLocation(), diag::note_previous_definition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-24 02:06:20 +03:00
|
|
|
AddKnownFunctionAttributes(NewFD);
|
|
|
|
|
2010-08-19 03:23:40 +04:00
|
|
|
if (OverloadableAttrRequired && !NewFD->hasAttr<OverloadableAttr>()) {
|
2009-03-24 02:06:20 +03:00
|
|
|
// If a function name is overloadable in C, then every function
|
|
|
|
// with that name must be marked "overloadable".
|
|
|
|
Diag(NewFD->getLocation(), diag::err_attribute_overloadable_missing)
|
|
|
|
<< Redeclaration << NewFD;
|
2009-11-19 01:49:29 +03:00
|
|
|
if (!Previous.empty())
|
|
|
|
Diag(Previous.getRepresentativeDecl()->getLocation(),
|
2009-03-24 02:06:20 +03:00
|
|
|
diag::note_attribute_overloadable_prev_overload);
|
2010-08-19 03:23:40 +04:00
|
|
|
NewFD->addAttr(::new (Context) OverloadableAttr(SourceLocation(), Context));
|
2009-03-24 02:06:20 +03:00
|
|
|
}
|
|
|
|
|
2010-08-06 17:50:58 +04:00
|
|
|
if (NewFD->hasAttr<OverloadableAttr>() &&
|
|
|
|
!NewFD->getType()->getAs<FunctionProtoType>()) {
|
|
|
|
Diag(NewFD->getLocation(),
|
|
|
|
diag::err_attribute_overloadable_no_prototype)
|
|
|
|
<< NewFD;
|
|
|
|
|
|
|
|
// Turn this into a variadic function with no parameters.
|
|
|
|
const FunctionType *FT = NewFD->getType()->getAs<FunctionType>();
|
|
|
|
QualType R = Context.getFunctionType(FT->getResultType(),
|
|
|
|
0, 0, true, 0, false, false, 0, 0,
|
|
|
|
FT->getExtInfo());
|
|
|
|
NewFD->setType(R);
|
|
|
|
}
|
|
|
|
|
2010-08-05 10:57:20 +04:00
|
|
|
// If there's a #pragma GCC visibility in scope, and this isn't a class
|
|
|
|
// member, set the visibility of this function.
|
|
|
|
if (NewFD->getLinkage() == ExternalLinkage && !DC->isRecord())
|
|
|
|
AddPushedVisibilityAttribute(NewFD);
|
|
|
|
|
2009-03-24 02:06:20 +03:00
|
|
|
// If this is a locally-scoped extern C function, update the
|
|
|
|
// map of such names.
|
2009-09-12 04:17:51 +04:00
|
|
|
if (CurContext->isFunctionOrMethod() && NewFD->isExternC()
|
2009-04-25 12:06:05 +04:00
|
|
|
&& !NewFD->isInvalidDecl())
|
2009-11-19 01:49:29 +03:00
|
|
|
RegisterLocallyScopedExternCDecl(NewFD, Previous, S);
|
2009-03-24 02:06:20 +03:00
|
|
|
|
2009-06-25 22:22:24 +04:00
|
|
|
// Set this FunctionDecl's range up to the right paren.
|
|
|
|
NewFD->setLocEnd(D.getSourceRange().getEnd());
|
|
|
|
|
2009-06-26 02:08:12 +04:00
|
|
|
if (FunctionTemplate && NewFD->isInvalidDecl())
|
|
|
|
FunctionTemplate->setInvalidDecl();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-26 02:08:12 +04:00
|
|
|
if (FunctionTemplate)
|
|
|
|
return FunctionTemplate;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-15 05:15:20 +04:00
|
|
|
MarkUnusedFileScopedDecl(NewFD);
|
2010-08-13 22:42:17 +04:00
|
|
|
|
2009-03-24 02:06:20 +03:00
|
|
|
return NewFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Perform semantic checking of a new function declaration.
|
|
|
|
///
|
|
|
|
/// Performs semantic analysis of the new function declaration
|
|
|
|
/// NewFD. This routine performs all semantic checking that does not
|
|
|
|
/// require the actual declarator involved in the declaration, and is
|
|
|
|
/// used both for the declaration of functions as they are parsed
|
|
|
|
/// (called via ActOnDeclarator) and for the declaration of functions
|
|
|
|
/// that have been instantiated via C++ template instantiation (called
|
|
|
|
/// via InstantiateDecl).
|
|
|
|
///
|
2009-10-13 20:30:37 +04:00
|
|
|
/// \param IsExplicitSpecialiation whether this new function declaration is
|
|
|
|
/// an explicit specialization of the previous declaration.
|
|
|
|
///
|
2009-04-25 12:06:05 +04:00
|
|
|
/// This sets NewFD->isInvalidDecl() to true if there was an error.
|
2009-12-10 12:41:52 +03:00
|
|
|
void Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD,
|
2009-11-19 01:49:29 +03:00
|
|
|
LookupResult &Previous,
|
2009-10-13 20:30:37 +04:00
|
|
|
bool IsExplicitSpecialization,
|
2009-03-24 02:06:20 +03:00
|
|
|
bool &Redeclaration,
|
|
|
|
bool &OverloadableAttrRequired) {
|
2009-04-25 12:06:05 +04:00
|
|
|
// If NewFD is already known erroneous, don't do any of this checking.
|
2010-08-12 04:57:17 +04:00
|
|
|
if (NewFD->isInvalidDecl()) {
|
|
|
|
// If this is a class member, mark the class invalid immediately.
|
|
|
|
// This avoids some consistency errors later.
|
|
|
|
if (isa<CXXMethodDecl>(NewFD))
|
|
|
|
cast<CXXMethodDecl>(NewFD)->getParent()->setInvalidDecl();
|
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
return;
|
2010-08-12 04:57:17 +04:00
|
|
|
}
|
2009-03-24 02:06:20 +03:00
|
|
|
|
2009-05-16 16:15:55 +04:00
|
|
|
if (NewFD->getResultType()->isVariablyModifiedType()) {
|
|
|
|
// Functions returning a variably modified type violate C99 6.7.5.2p2
|
|
|
|
// because all functions have linkage.
|
|
|
|
Diag(NewFD->getLocation(), diag::err_vm_func_decl);
|
|
|
|
return NewFD->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
2009-09-12 04:17:51 +04:00
|
|
|
if (NewFD->isMain())
|
|
|
|
CheckMain(NewFD);
|
2009-07-24 07:03:21 +04:00
|
|
|
|
2009-03-24 02:06:20 +03:00
|
|
|
// Check for a previous declaration of this name.
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Previous.empty() && NewFD->isExternC()) {
|
2009-03-02 03:19:53 +03:00
|
|
|
// Since we did not find anything by this name and we're declaring
|
|
|
|
// an extern "C" function, look for a non-visible extern "C"
|
|
|
|
// declaration with the same name.
|
|
|
|
llvm::DenseMap<DeclarationName, NamedDecl *>::iterator Pos
|
2009-03-24 02:06:20 +03:00
|
|
|
= LocallyScopedExternalDecls.find(NewFD->getDeclName());
|
2009-03-02 03:19:53 +03:00
|
|
|
if (Pos != LocallyScopedExternalDecls.end())
|
2009-11-19 01:49:29 +03:00
|
|
|
Previous.addDecl(Pos->second);
|
2009-03-02 03:19:53 +03:00
|
|
|
}
|
|
|
|
|
2009-02-24 04:23:02 +03:00
|
|
|
// Merge or overload the declaration with an existing declaration of
|
|
|
|
// the same name, if appropriate.
|
2009-11-19 01:49:29 +03:00
|
|
|
if (!Previous.empty()) {
|
Initial implementation of function overloading in C.
This commit adds a new attribute, "overloadable", that enables C++
function overloading in C. The attribute can only be added to function
declarations, e.g.,
int *f(int) __attribute__((overloadable));
If the "overloadable" attribute exists on a function with a given
name, *all* functions with that name (and in that scope) must have the
"overloadable" attribute. Sets of overloaded functions with the
"overloadable" attribute then follow the normal C++ rules for
overloaded functions, e.g., overloads must have different
parameter-type-lists from each other.
When calling an overloaded function in C, we follow the same
overloading rules as C++, with three extensions to the set of standard
conversions:
- A value of a given struct or union type T can be converted to the
type T. This is just the identity conversion. (In C++, this would
go through a copy constructor).
- A value of pointer type T* can be converted to a value of type U*
if T and U are compatible types. This conversion has Conversion
rank (it's considered a pointer conversion in C).
- A value of type T can be converted to a value of type U if T and U
are compatible (and are not both pointer types). This conversion
has Conversion rank (it's considered to be a new kind of
conversion unique to C, a "compatible" conversion).
Known defects (and, therefore, next steps):
1) The standard-conversion handling does not understand conversions
involving _Complex or vector extensions, so it is likely to get
these wrong. We need to add these conversions.
2) All overloadable functions with the same name will have the same
linkage name, which means we'll get a collision in the linker (if
not sooner). We'll need to mangle the names of these functions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64336 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-12 02:02:49 +03:00
|
|
|
// Determine whether NewFD is an overload of PrevDecl or
|
2009-01-16 04:13:29 +03:00
|
|
|
// a declaration that requires merging. If it's an overload,
|
|
|
|
// there's no more work to do here; we'll just add the new
|
|
|
|
// function to the scope.
|
2009-02-13 03:26:38 +03:00
|
|
|
|
2009-12-09 06:35:25 +03:00
|
|
|
NamedDecl *OldDecl = 0;
|
|
|
|
if (!AllowOverloadingOfFunction(Previous, Context)) {
|
|
|
|
Redeclaration = true;
|
|
|
|
OldDecl = Previous.getFoundDecl();
|
|
|
|
} else {
|
2010-08-06 17:50:58 +04:00
|
|
|
if (!getLangOptions().CPlusPlus)
|
2009-12-09 06:35:25 +03:00
|
|
|
OverloadableAttrRequired = true;
|
|
|
|
|
2010-06-16 12:42:20 +04:00
|
|
|
switch (CheckOverload(S, NewFD, Previous, OldDecl,
|
|
|
|
/*NewIsUsingDecl*/ false)) {
|
2009-12-09 06:35:25 +03:00
|
|
|
case Ovl_Match:
|
2009-12-10 12:41:52 +03:00
|
|
|
Redeclaration = true;
|
2009-12-09 06:35:25 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Ovl_NonFunction:
|
2009-11-19 01:49:29 +03:00
|
|
|
Redeclaration = true;
|
2009-12-09 06:35:25 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Ovl_Overload:
|
|
|
|
Redeclaration = false;
|
|
|
|
break;
|
2009-11-19 01:49:29 +03:00
|
|
|
}
|
|
|
|
}
|
2009-01-16 04:13:29 +03:00
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Redeclaration) {
|
2009-03-24 02:06:20 +03:00
|
|
|
// NewFD and OldDecl represent declarations that need to be
|
2009-09-09 19:08:12 +04:00
|
|
|
// merged.
|
2009-02-16 20:45:42 +03:00
|
|
|
if (MergeFunctionDecl(NewFD, OldDecl))
|
2009-04-25 12:06:05 +04:00
|
|
|
return NewFD->setInvalidDecl();
|
2009-01-16 04:13:29 +03:00
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
Previous.clear();
|
|
|
|
Previous.addDecl(OldDecl);
|
|
|
|
|
2009-06-26 02:08:12 +04:00
|
|
|
if (FunctionTemplateDecl *OldTemplateDecl
|
2009-10-13 02:27:17 +04:00
|
|
|
= dyn_cast<FunctionTemplateDecl>(OldDecl)) {
|
2009-10-13 20:30:37 +04:00
|
|
|
NewFD->setPreviousDeclaration(OldTemplateDecl->getTemplatedDecl());
|
2009-10-13 02:27:17 +04:00
|
|
|
FunctionTemplateDecl *NewTemplateDecl
|
|
|
|
= NewFD->getDescribedFunctionTemplate();
|
|
|
|
assert(NewTemplateDecl && "Template/non-template mismatch");
|
|
|
|
if (CXXMethodDecl *Method
|
|
|
|
= dyn_cast<CXXMethodDecl>(NewTemplateDecl->getTemplatedDecl())) {
|
|
|
|
Method->setAccess(OldTemplateDecl->getAccess());
|
|
|
|
NewTemplateDecl->setAccess(OldTemplateDecl->getAccess());
|
|
|
|
}
|
2009-10-13 20:30:37 +04:00
|
|
|
|
|
|
|
// If this is an explicit specialization of a member that is a function
|
|
|
|
// template, mark it as a member specialization.
|
|
|
|
if (IsExplicitSpecialization &&
|
|
|
|
NewTemplateDecl->getInstantiatedFromMemberTemplate()) {
|
|
|
|
NewTemplateDecl->setMemberSpecialization();
|
|
|
|
assert(OldTemplateDecl->isMemberSpecialization());
|
|
|
|
}
|
2009-10-13 02:27:17 +04:00
|
|
|
} else {
|
2009-07-14 07:18:53 +04:00
|
|
|
if (isa<CXXMethodDecl>(NewFD)) // Set access for out-of-line definitions
|
|
|
|
NewFD->setAccess(OldDecl->getAccess());
|
2009-06-26 02:08:12 +04:00
|
|
|
NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl));
|
2009-07-14 07:18:53 +04:00
|
|
|
}
|
2009-01-16 04:13:29 +03:00
|
|
|
}
|
2009-02-06 20:46:57 +03:00
|
|
|
}
|
2009-01-16 04:13:29 +03:00
|
|
|
|
2009-09-14 01:33:06 +04:00
|
|
|
// Semantic checking for this function declaration (in isolation).
|
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
// C++-specific checks.
|
|
|
|
if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(NewFD)) {
|
|
|
|
CheckConstructor(Constructor);
|
2009-11-16 01:49:34 +03:00
|
|
|
} else if (CXXDestructorDecl *Destructor =
|
|
|
|
dyn_cast<CXXDestructorDecl>(NewFD)) {
|
|
|
|
CXXRecordDecl *Record = Destructor->getParent();
|
2009-09-14 01:33:06 +04:00
|
|
|
QualType ClassType = Context.getTypeDeclType(Record);
|
2009-11-16 01:49:34 +03:00
|
|
|
|
2010-07-03 00:37:36 +04:00
|
|
|
// FIXME: Shouldn't we be able to perform this check even when the class
|
2009-11-16 01:49:34 +03:00
|
|
|
// type is dependent? Both gcc and edg can handle that.
|
2009-09-14 01:33:06 +04:00
|
|
|
if (!ClassType->isDependentType()) {
|
|
|
|
DeclarationName Name
|
|
|
|
= Context.DeclarationNames.getCXXDestructorName(
|
|
|
|
Context.getCanonicalType(ClassType));
|
|
|
|
if (NewFD->getDeclName() != Name) {
|
|
|
|
Diag(NewFD->getLocation(), diag::err_destructor_name);
|
|
|
|
return NewFD->setInvalidDecl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (CXXConversionDecl *Conversion
|
2009-12-01 20:24:26 +03:00
|
|
|
= dyn_cast<CXXConversionDecl>(NewFD)) {
|
2009-09-14 01:33:06 +04:00
|
|
|
ActOnConversionDeclarator(Conversion);
|
2009-12-01 20:24:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find any virtual functions that this function overrides.
|
2009-12-01 20:35:23 +03:00
|
|
|
if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD)) {
|
|
|
|
if (!Method->isFunctionTemplateSpecialization() &&
|
2010-10-14 02:55:32 +04:00
|
|
|
!Method->getDescribedFunctionTemplate()) {
|
|
|
|
if (AddOverriddenMethods(Method->getParent(), Method)) {
|
|
|
|
// If the function was marked as "static", we have a problem.
|
|
|
|
if (NewFD->getStorageClass() == SC_Static) {
|
|
|
|
Diag(NewFD->getLocation(), diag::err_static_overrides_virtual)
|
|
|
|
<< NewFD->getDeclName();
|
|
|
|
for (CXXMethodDecl::method_iterator
|
|
|
|
Overridden = Method->begin_overridden_methods(),
|
|
|
|
OverriddenEnd = Method->end_overridden_methods();
|
|
|
|
Overridden != OverriddenEnd;
|
|
|
|
++Overridden) {
|
|
|
|
Diag((*Overridden)->getLocation(),
|
|
|
|
diag::note_overridden_virtual_function);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-01 20:35:23 +03:00
|
|
|
}
|
2009-09-14 01:33:06 +04:00
|
|
|
|
|
|
|
// Extra checking for C++ overloaded operators (C++ [over.oper]).
|
|
|
|
if (NewFD->isOverloadedOperator() &&
|
|
|
|
CheckOverloadedOperatorDeclaration(NewFD))
|
|
|
|
return NewFD->setInvalidDecl();
|
2010-01-13 12:01:02 +03:00
|
|
|
|
|
|
|
// Extra checking for C++0x literal operators (C++0x [over.literal]).
|
|
|
|
if (NewFD->getLiteralIdentifier() &&
|
|
|
|
CheckLiteralOperatorDeclaration(NewFD))
|
|
|
|
return NewFD->setInvalidDecl();
|
|
|
|
|
2009-09-14 01:33:06 +04:00
|
|
|
// In C++, check default arguments now that we have merged decls. Unless
|
|
|
|
// the lexical context is the class, because in this case this is done
|
|
|
|
// during delayed parsing anyway.
|
|
|
|
if (!CurContext->isRecord())
|
|
|
|
CheckCXXDefaultArguments(NewFD);
|
|
|
|
}
|
2009-01-16 04:13:29 +03:00
|
|
|
}
|
|
|
|
|
2009-07-24 07:03:21 +04:00
|
|
|
void Sema::CheckMain(FunctionDecl* FD) {
|
2009-07-25 08:36:53 +04:00
|
|
|
// C++ [basic.start.main]p3: A program that declares main to be inline
|
|
|
|
// or static is ill-formed.
|
|
|
|
// C99 6.7.4p4: In a hosted environment, the inline function specifier
|
|
|
|
// shall not appear in a declaration of main.
|
|
|
|
// static main is not an error under C99, but we should warn about it.
|
2009-10-28 00:01:01 +03:00
|
|
|
bool isInline = FD->isInlineSpecified();
|
2010-08-26 07:08:43 +04:00
|
|
|
bool isStatic = FD->getStorageClass() == SC_Static;
|
2009-07-25 08:36:53 +04:00
|
|
|
if (isInline || isStatic) {
|
|
|
|
unsigned diagID = diag::warn_unusual_main_decl;
|
|
|
|
if (isInline || getLangOptions().CPlusPlus)
|
|
|
|
diagID = diag::err_unusual_main_decl;
|
|
|
|
|
|
|
|
int which = isStatic + (isInline << 1) - 1;
|
|
|
|
Diag(FD->getLocation(), diagID) << which;
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType T = FD->getType();
|
|
|
|
assert(T->isFunctionType() && "function decl is not of function type");
|
2009-09-22 03:43:11 +04:00
|
|
|
const FunctionType* FT = T->getAs<FunctionType>();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 08:36:53 +04:00
|
|
|
if (!Context.hasSameUnqualifiedType(FT->getResultType(), Context.IntTy)) {
|
2010-09-08 04:31:13 +04:00
|
|
|
TypeSourceInfo *TSI = FD->getTypeSourceInfo();
|
|
|
|
TypeLoc TL = TSI->getTypeLoc();
|
|
|
|
const SemaDiagnosticBuilder& D = Diag(FD->getTypeSpecStartLoc(),
|
|
|
|
diag::err_main_returns_nonint);
|
|
|
|
if (FunctionTypeLoc* PTL = dyn_cast<FunctionTypeLoc>(&TL)) {
|
|
|
|
D << FixItHint::CreateReplacement(PTL->getResultLoc().getSourceRange(),
|
|
|
|
"int");
|
|
|
|
}
|
2009-07-25 08:36:53 +04:00
|
|
|
FD->setInvalidDecl(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Treat protoless main() as nullary.
|
|
|
|
if (isa<FunctionNoProtoType>(FT)) return;
|
|
|
|
|
|
|
|
const FunctionProtoType* FTP = cast<const FunctionProtoType>(FT);
|
|
|
|
unsigned nparams = FTP->getNumArgs();
|
|
|
|
assert(FD->getNumParams() == nparams);
|
|
|
|
|
2009-12-24 12:58:38 +03:00
|
|
|
bool HasExtraParameters = (nparams > 3);
|
|
|
|
|
|
|
|
// Darwin passes an undocumented fourth argument of type char**. If
|
|
|
|
// other platforms start sprouting these, the logic below will start
|
|
|
|
// getting shifty.
|
|
|
|
if (nparams == 4 &&
|
|
|
|
Context.Target.getTriple().getOS() == llvm::Triple::Darwin)
|
|
|
|
HasExtraParameters = false;
|
|
|
|
|
|
|
|
if (HasExtraParameters) {
|
2009-07-25 08:36:53 +04:00
|
|
|
Diag(FD->getLocation(), diag::err_main_surplus_args) << nparams;
|
|
|
|
FD->setInvalidDecl(true);
|
|
|
|
nparams = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: a lot of the following diagnostics would be improved
|
|
|
|
// if we had some location information about types.
|
|
|
|
|
|
|
|
QualType CharPP =
|
|
|
|
Context.getPointerType(Context.getPointerType(Context.CharTy));
|
2009-12-24 12:58:38 +03:00
|
|
|
QualType Expected[] = { Context.IntTy, CharPP, CharPP, CharPP };
|
2009-07-25 08:36:53 +04:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < nparams; ++i) {
|
|
|
|
QualType AT = FTP->getArgType(i);
|
|
|
|
|
|
|
|
bool mismatch = true;
|
|
|
|
|
|
|
|
if (Context.hasSameUnqualifiedType(AT, Expected[i]))
|
|
|
|
mismatch = false;
|
|
|
|
else if (Expected[i] == CharPP) {
|
|
|
|
// As an extension, the following forms are okay:
|
|
|
|
// char const **
|
|
|
|
// char const * const *
|
|
|
|
// char * const *
|
|
|
|
|
2009-09-24 23:53:00 +04:00
|
|
|
QualifierCollector qs;
|
2009-07-25 08:36:53 +04:00
|
|
|
const PointerType* PT;
|
2009-07-30 01:53:49 +04:00
|
|
|
if ((PT = qs.strip(AT)->getAs<PointerType>()) &&
|
|
|
|
(PT = qs.strip(PT->getPointeeType())->getAs<PointerType>()) &&
|
2009-07-25 08:36:53 +04:00
|
|
|
(QualType(qs.strip(PT->getPointeeType()), 0) == Context.CharTy)) {
|
|
|
|
qs.removeConst();
|
|
|
|
mismatch = !qs.empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mismatch) {
|
|
|
|
Diag(FD->getLocation(), diag::err_main_arg_wrong) << i << Expected[i];
|
|
|
|
// TODO: suggest replacing given type with expected type
|
|
|
|
FD->setInvalidDecl(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nparams == 1 && !FD->isInvalidDecl()) {
|
|
|
|
Diag(FD->getLocation(), diag::warn_main_one_arg);
|
|
|
|
}
|
2009-07-24 07:03:21 +04:00
|
|
|
}
|
|
|
|
|
2008-05-20 17:48:25 +04:00
|
|
|
bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) {
|
2009-02-27 07:17:12 +03:00
|
|
|
// FIXME: Need strict checking. In C89, we need to check for
|
|
|
|
// any assignment, increment, decrement, function-calls, or
|
|
|
|
// commas outside of a sizeof. In C99, it's the same list,
|
|
|
|
// except that the aforementioned are allowed in unevaluated
|
|
|
|
// expressions. Everything else falls under the
|
|
|
|
// "may accept other forms of constant expressions" exception.
|
|
|
|
// (We never end up here for C++, so the constant expression
|
|
|
|
// rules there don't matter.)
|
2010-08-03 01:13:48 +04:00
|
|
|
if (Init->isConstantInitializer(Context, false))
|
2009-02-22 09:45:27 +03:00
|
|
|
return false;
|
2009-02-26 07:47:58 +03:00
|
|
|
Diag(Init->getExprLoc(), diag::err_init_element_not_constant)
|
|
|
|
<< Init->getSourceRange();
|
2008-05-20 17:48:25 +04:00
|
|
|
return true;
|
2008-01-11 01:15:12 +03:00
|
|
|
}
|
|
|
|
|
2010-08-24 03:25:46 +04:00
|
|
|
void Sema::AddInitializerToDecl(Decl *dcl, Expr *init) {
|
|
|
|
AddInitializerToDecl(dcl, init, /*DirectInit=*/false);
|
2009-01-14 18:45:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// AddInitializerToDecl - Adds the initializer Init to the
|
|
|
|
/// declaration dcl. If DirectInit is true, this is C++ direct
|
|
|
|
/// initialization rather than copy initialization.
|
2010-08-24 03:25:46 +04:00
|
|
|
void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
|
2007-10-20 00:10:30 +04:00
|
|
|
// If there is no declaration, there was an error parsing it. Just ignore
|
|
|
|
// the initializer.
|
2009-02-27 07:17:12 +03:00
|
|
|
if (RealDecl == 0)
|
2007-10-20 00:10:30 +04:00
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-12 02:00:04 +03:00
|
|
|
if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(RealDecl)) {
|
|
|
|
// With declarators parsed the way they are, the parser cannot
|
|
|
|
// distinguish between a normal initializer and a pure-specifier.
|
|
|
|
// Thus this grotesque test.
|
|
|
|
IntegerLiteral *IL;
|
|
|
|
if ((IL = dyn_cast<IntegerLiteral>(Init)) && IL->getValue() == 0 &&
|
2009-12-01 20:24:26 +03:00
|
|
|
Context.getCanonicalType(IL->getType()) == Context.IntTy)
|
|
|
|
CheckPureMethod(Method, Init->getSourceRange());
|
|
|
|
else {
|
2009-03-12 02:00:04 +03:00
|
|
|
Diag(Method->getLocation(), diag::err_member_function_initialization)
|
|
|
|
<< Method->getDeclName() << Init->getSourceRange();
|
|
|
|
Method->setInvalidDecl();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-13 00:13:48 +04:00
|
|
|
VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl);
|
|
|
|
if (!VDecl) {
|
2009-03-12 02:00:04 +03:00
|
|
|
if (getLangOptions().CPlusPlus &&
|
|
|
|
RealDecl->getLexicalDeclContext()->isRecord() &&
|
|
|
|
isa<NamedDecl>(RealDecl))
|
2010-09-11 03:21:22 +04:00
|
|
|
Diag(RealDecl->getLocation(), diag::err_member_initialization);
|
2009-03-12 02:00:04 +03:00
|
|
|
else
|
|
|
|
Diag(RealDecl->getLocation(), diag::err_illegal_initializer);
|
2007-09-13 00:13:48 +04:00
|
|
|
RealDecl->setInvalidDecl();
|
|
|
|
return;
|
2009-02-27 07:17:12 +03:00
|
|
|
}
|
|
|
|
|
2010-08-24 09:27:49 +04:00
|
|
|
|
|
|
|
|
2009-11-14 06:40:14 +03:00
|
|
|
// A definition must end up with a complete type, which means it must be
|
|
|
|
// complete with the restriction that an array type might be completed by the
|
|
|
|
// initializer; note that later code assumes this restriction.
|
|
|
|
QualType BaseDeclType = VDecl->getType();
|
|
|
|
if (const ArrayType *Array = Context.getAsIncompleteArrayType(BaseDeclType))
|
|
|
|
BaseDeclType = Array->getElementType();
|
|
|
|
if (RequireCompleteType(VDecl->getLocation(), BaseDeclType,
|
2009-04-14 01:28:54 +04:00
|
|
|
diag::err_typecheck_decl_incomplete_type)) {
|
|
|
|
RealDecl->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-03 21:33:45 +03:00
|
|
|
// The variable can not have an abstract class type.
|
|
|
|
if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(),
|
|
|
|
diag::err_abstract_type_in_decl,
|
|
|
|
AbstractVariableType))
|
|
|
|
VDecl->setInvalidDecl();
|
|
|
|
|
2010-02-01 23:16:42 +03:00
|
|
|
const VarDecl *Def;
|
|
|
|
if ((Def = VDecl->getDefinition()) && Def != VDecl) {
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(VDecl->getLocation(), diag::err_redefinition)
|
2009-03-11 02:43:53 +03:00
|
|
|
<< VDecl->getDeclName();
|
|
|
|
Diag(Def->getLocation(), diag::note_previous_definition);
|
|
|
|
VDecl->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
2010-08-24 09:27:49 +04:00
|
|
|
|
|
|
|
// C++ [class.static.data]p4
|
|
|
|
// If a static data member is of const integral or const
|
|
|
|
// enumeration type, its declaration in the class definition can
|
|
|
|
// specify a constant-initializer which shall be an integral
|
|
|
|
// constant expression (5.19). In that case, the member can appear
|
|
|
|
// in integral constant expressions. The member shall still be
|
|
|
|
// defined in a namespace scope if it is used in the program and the
|
|
|
|
// namespace scope definition shall not contain an initializer.
|
|
|
|
//
|
|
|
|
// We already performed a redefinition check above, but for static
|
|
|
|
// data members we also need to check whether there was an in-class
|
|
|
|
// declaration with an initializer.
|
|
|
|
const VarDecl* PrevInit = 0;
|
|
|
|
if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) {
|
|
|
|
Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName();
|
|
|
|
Diag(PrevInit->getLocation(), diag::note_previous_definition);
|
|
|
|
return;
|
|
|
|
}
|
2009-03-11 02:43:53 +03:00
|
|
|
|
2010-08-01 05:24:59 +04:00
|
|
|
if (getLangOptions().CPlusPlus && VDecl->hasLocalStorage())
|
2010-08-25 12:40:02 +04:00
|
|
|
getCurFunction()->setHasBranchProtectedScope();
|
2010-08-01 05:24:59 +04:00
|
|
|
|
2009-12-16 04:38:02 +03:00
|
|
|
// Capture the variable that is being initialized and the style of
|
|
|
|
// initialization.
|
|
|
|
InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl);
|
|
|
|
|
|
|
|
// FIXME: Poor source location information.
|
|
|
|
InitializationKind Kind
|
|
|
|
= DirectInit? InitializationKind::CreateDirect(VDecl->getLocation(),
|
|
|
|
Init->getLocStart(),
|
|
|
|
Init->getLocEnd())
|
|
|
|
: InitializationKind::CreateCopy(VDecl->getLocation(),
|
|
|
|
Init->getLocStart());
|
|
|
|
|
2007-09-12 18:07:44 +04:00
|
|
|
// Get the decls type and save a reference for later, since
|
2008-01-11 01:15:12 +03:00
|
|
|
// CheckInitializerTypes may change it.
|
2007-09-13 00:13:48 +04:00
|
|
|
QualType DclT = VDecl->getType(), SavT = DclT;
|
2008-04-16 02:42:06 +04:00
|
|
|
if (VDecl->isBlockVarDecl()) {
|
2009-04-14 06:25:56 +04:00
|
|
|
if (VDecl->hasExternalStorage()) { // C99 6.7.8p5
|
2007-09-13 00:13:48 +04:00
|
|
|
Diag(VDecl->getLocation(), diag::err_block_extern_cant_init);
|
2008-04-16 02:42:06 +04:00
|
|
|
VDecl->setInvalidDecl();
|
|
|
|
} else if (!VDecl->isInvalidDecl()) {
|
2009-12-19 11:11:05 +03:00
|
|
|
InitializationSequence InitSeq(*this, Entity, Kind, &Init, 1);
|
2010-08-24 10:29:42 +04:00
|
|
|
ExprResult Result = InitSeq.Perform(*this, Entity, Kind,
|
2010-08-23 10:44:23 +04:00
|
|
|
MultiExprArg(*this, &Init, 1),
|
2009-12-22 05:10:53 +03:00
|
|
|
&DclT);
|
|
|
|
if (Result.isInvalid()) {
|
2008-04-16 02:42:06 +04:00
|
|
|
VDecl->setInvalidDecl();
|
2009-12-19 11:11:05 +03:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-12-22 05:10:53 +03:00
|
|
|
Init = Result.takeAs<Expr>();
|
|
|
|
|
2008-08-22 09:00:02 +04:00
|
|
|
// C++ 3.6.2p2, allow dynamic initialization of static initializers.
|
2009-02-20 04:34:21 +03:00
|
|
|
// Don't check invalid declarations to avoid emitting useless diagnostics.
|
|
|
|
if (!getLangOptions().CPlusPlus && !VDecl->isInvalidDecl()) {
|
2010-08-26 07:08:43 +04:00
|
|
|
if (VDecl->getStorageClass() == SC_Static) // C99 6.7.8p4.
|
2008-08-22 09:00:02 +04:00
|
|
|
CheckForConstantInitializer(Init, DclT);
|
|
|
|
}
|
2007-09-12 18:07:44 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
} else if (VDecl->isStaticDataMember() &&
|
2009-03-12 02:00:04 +03:00
|
|
|
VDecl->getLexicalDeclContext()->isRecord()) {
|
|
|
|
// This is an in-class initialization for a static data member, e.g.,
|
|
|
|
//
|
|
|
|
// struct S {
|
|
|
|
// static const int value = 17;
|
|
|
|
// };
|
|
|
|
|
2010-09-11 03:21:22 +04:00
|
|
|
// Try to perform the initialization regardless.
|
|
|
|
if (!VDecl->isInvalidDecl()) {
|
|
|
|
InitializationSequence InitSeq(*this, Entity, Kind, &Init, 1);
|
|
|
|
ExprResult Result = InitSeq.Perform(*this, Entity, Kind,
|
|
|
|
MultiExprArg(*this, &Init, 1),
|
|
|
|
&DclT);
|
|
|
|
if (Result.isInvalid()) {
|
|
|
|
VDecl->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Init = Result.takeAs<Expr>();
|
|
|
|
}
|
2009-03-12 02:00:04 +03:00
|
|
|
|
|
|
|
// C++ [class.mem]p4:
|
|
|
|
// A member-declarator can contain a constant-initializer only
|
|
|
|
// if it declares a static member (9.4) of const integral or
|
|
|
|
// const enumeration type, see 9.4.2.
|
|
|
|
QualType T = VDecl->getType();
|
2010-09-11 03:21:22 +04:00
|
|
|
|
|
|
|
// Do nothing on dependent types.
|
|
|
|
if (T->isDependentType()) {
|
|
|
|
|
|
|
|
// Require constness.
|
|
|
|
} else if (!T.isConstQualified()) {
|
|
|
|
Diag(VDecl->getLocation(), diag::err_in_class_initializer_non_const)
|
|
|
|
<< Init->getSourceRange();
|
2009-03-12 02:00:04 +03:00
|
|
|
VDecl->setInvalidDecl();
|
2010-09-11 03:21:22 +04:00
|
|
|
|
|
|
|
// We allow integer constant expressions in all cases.
|
|
|
|
} else if (T->isIntegralOrEnumerationType()) {
|
|
|
|
if (!Init->isValueDependent()) {
|
2009-03-12 02:00:04 +03:00
|
|
|
// Check whether the expression is a constant expression.
|
|
|
|
llvm::APSInt Value;
|
|
|
|
SourceLocation Loc;
|
|
|
|
if (!Init->isIntegerConstantExpr(Value, Context, &Loc)) {
|
|
|
|
Diag(Loc, diag::err_in_class_initializer_non_constant)
|
|
|
|
<< Init->getSourceRange();
|
|
|
|
VDecl->setInvalidDecl();
|
2010-09-11 03:21:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We allow floating-point constants as an extension in C++03, and
|
|
|
|
// C++0x has far more complicated rules that we don't really
|
|
|
|
// implement fully.
|
|
|
|
} else {
|
|
|
|
bool Allowed = false;
|
|
|
|
if (getLangOptions().CPlusPlus0x) {
|
|
|
|
Allowed = T->isLiteralType();
|
|
|
|
} else if (T->isFloatingType()) { // also permits complex, which is ok
|
|
|
|
Diag(VDecl->getLocation(), diag::ext_in_class_initializer_float_type)
|
|
|
|
<< T << Init->getSourceRange();
|
|
|
|
Allowed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Allowed) {
|
|
|
|
Diag(VDecl->getLocation(), diag::err_in_class_initializer_bad_type)
|
|
|
|
<< T << Init->getSourceRange();
|
|
|
|
VDecl->setInvalidDecl();
|
|
|
|
|
|
|
|
// TODO: there are probably expressions that pass here that shouldn't.
|
|
|
|
} else if (!Init->isValueDependent() &&
|
|
|
|
!Init->isConstantInitializer(Context, false)) {
|
|
|
|
Diag(Init->getExprLoc(), diag::err_in_class_initializer_non_constant)
|
|
|
|
<< Init->getSourceRange();
|
|
|
|
VDecl->setInvalidDecl();
|
2009-03-12 02:00:04 +03:00
|
|
|
}
|
|
|
|
}
|
2008-04-16 02:42:06 +04:00
|
|
|
} else if (VDecl->isFileVarDecl()) {
|
2010-10-15 05:21:46 +04:00
|
|
|
if (VDecl->getStorageClassAsWritten() == SC_Extern &&
|
2010-04-22 18:36:26 +04:00
|
|
|
(!getLangOptions().CPlusPlus ||
|
|
|
|
!Context.getBaseElementType(VDecl->getType()).isConstQualified()))
|
2007-09-13 00:13:48 +04:00
|
|
|
Diag(VDecl->getLocation(), diag::warn_extern_init);
|
2009-12-22 05:10:53 +03:00
|
|
|
if (!VDecl->isInvalidDecl()) {
|
|
|
|
InitializationSequence InitSeq(*this, Entity, Kind, &Init, 1);
|
2010-08-24 10:29:42 +04:00
|
|
|
ExprResult Result = InitSeq.Perform(*this, Entity, Kind,
|
2010-08-23 10:44:23 +04:00
|
|
|
MultiExprArg(*this, &Init, 1),
|
2009-12-22 05:10:53 +03:00
|
|
|
&DclT);
|
|
|
|
if (Result.isInvalid()) {
|
2008-04-16 02:42:06 +04:00
|
|
|
VDecl->setInvalidDecl();
|
2009-12-22 05:10:53 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Init = Result.takeAs<Expr>();
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-08-22 09:00:02 +04:00
|
|
|
// C++ 3.6.2p2, allow dynamic initialization of static initializers.
|
2009-02-20 04:34:21 +03:00
|
|
|
// Don't check invalid declarations to avoid emitting useless diagnostics.
|
|
|
|
if (!getLangOptions().CPlusPlus && !VDecl->isInvalidDecl()) {
|
2008-08-22 09:00:02 +04:00
|
|
|
// C99 6.7.8p4. All file scoped initializers need to be constant.
|
|
|
|
CheckForConstantInitializer(Init, DclT);
|
|
|
|
}
|
2007-09-12 18:07:44 +04:00
|
|
|
}
|
|
|
|
// If the type changed, it means we had an incomplete type that was
|
2009-09-09 19:08:12 +04:00
|
|
|
// completed by the initializer. For example:
|
2007-09-12 18:07:44 +04:00
|
|
|
// int ary[] = { 1, 3, 5 };
|
|
|
|
// "ary" transitions from a VariableArrayType to a ConstantArrayType.
|
2007-11-29 22:09:19 +03:00
|
|
|
if (!VDecl->isInvalidDecl() && (DclT != SavT)) {
|
2007-09-13 00:13:48 +04:00
|
|
|
VDecl->setType(DclT);
|
2007-11-29 22:09:19 +03:00
|
|
|
Init->setType(DclT);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-10-10 22:16:20 +04:00
|
|
|
|
|
|
|
// If this variable is a local declaration with record type, make sure it
|
|
|
|
// doesn't have a flexible member initialization. We only support this as a
|
|
|
|
// global/static definition.
|
|
|
|
if (VDecl->hasLocalStorage())
|
|
|
|
if (const RecordType *RT = VDecl->getType()->getAs<RecordType>())
|
|
|
|
if (RT->getDecl()->hasFlexibleArrayMember() && isa<InitListExpr>(Init)) {
|
|
|
|
Diag(VDecl->getLocation(), diag::err_nonstatic_flexible_variable);
|
|
|
|
VDecl->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
2010-10-08 06:01:28 +04:00
|
|
|
// Check any implicit conversions within the expression.
|
|
|
|
CheckImplicitConversions(Init, VDecl->getLocation());
|
|
|
|
|
2009-12-15 23:51:39 +03:00
|
|
|
Init = MaybeCreateCXXExprWithTemporaries(Init);
|
2007-09-12 18:07:44 +04:00
|
|
|
// Attach the initializer to the decl.
|
2010-02-11 04:19:42 +03:00
|
|
|
VDecl->setInit(Init);
|
2009-04-21 21:11:58 +04:00
|
|
|
|
2009-12-21 01:29:11 +03:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
2010-08-02 00:20:59 +04:00
|
|
|
if (!VDecl->isInvalidDecl() &&
|
|
|
|
!VDecl->getDeclContext()->isDependentContext() &&
|
2010-09-03 05:11:38 +04:00
|
|
|
VDecl->hasGlobalStorage() && !VDecl->isStaticLocal() &&
|
2010-08-03 01:13:48 +04:00
|
|
|
!Init->isConstantInitializer(Context,
|
|
|
|
VDecl->getType()->isReferenceType()))
|
|
|
|
Diag(VDecl->getLocation(), diag::warn_global_constructor)
|
|
|
|
<< Init->getSourceRange();
|
2010-08-02 00:20:59 +04:00
|
|
|
|
2009-12-21 01:29:11 +03:00
|
|
|
// Make sure we mark the destructor as used if necessary.
|
|
|
|
QualType InitType = VDecl->getType();
|
2010-01-05 22:06:31 +03:00
|
|
|
while (const ArrayType *Array = Context.getAsArrayType(InitType))
|
2009-12-21 01:29:11 +03:00
|
|
|
InitType = Context.getBaseElementType(Array);
|
2010-02-02 12:10:11 +03:00
|
|
|
if (const RecordType *Record = InitType->getAs<RecordType>())
|
|
|
|
FinalizeVarWithDestructor(VDecl, Record);
|
2009-12-21 01:29:11 +03:00
|
|
|
}
|
|
|
|
|
2007-09-12 18:07:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-31 06:13:20 +04:00
|
|
|
/// ActOnInitializerError - Given that there was an error parsing an
|
|
|
|
/// initializer for the given declaration, try to return to some form
|
|
|
|
/// of sanity.
|
2010-08-21 13:40:31 +04:00
|
|
|
void Sema::ActOnInitializerError(Decl *D) {
|
2010-03-31 06:13:20 +04:00
|
|
|
// Our main concern here is re-establishing invariants like "a
|
|
|
|
// variable's type is either dependent or complete".
|
|
|
|
if (!D || D->isInvalidDecl()) return;
|
|
|
|
|
|
|
|
VarDecl *VD = dyn_cast<VarDecl>(D);
|
|
|
|
if (!VD) return;
|
|
|
|
|
|
|
|
QualType Ty = VD->getType();
|
|
|
|
if (Ty->isDependentType()) return;
|
|
|
|
|
|
|
|
// Require a complete type.
|
|
|
|
if (RequireCompleteType(VD->getLocation(),
|
|
|
|
Context.getBaseElementType(Ty),
|
|
|
|
diag::err_typecheck_decl_incomplete_type)) {
|
|
|
|
VD->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Require an abstract type.
|
|
|
|
if (RequireNonAbstractType(VD->getLocation(), Ty,
|
|
|
|
diag::err_abstract_type_in_decl,
|
|
|
|
AbstractVariableType)) {
|
|
|
|
VD->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't bother complaining about constructors or destructors,
|
|
|
|
// though.
|
|
|
|
}
|
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
void Sema::ActOnUninitializedDecl(Decl *RealDecl,
|
2009-07-11 04:34:39 +04:00
|
|
|
bool TypeContainsUndeducedAuto) {
|
2008-11-07 16:01:22 +03:00
|
|
|
// If there is no declaration, there was an error parsing it. Just ignore it.
|
|
|
|
if (RealDecl == 0)
|
|
|
|
return;
|
|
|
|
|
2008-10-29 03:13:59 +03:00
|
|
|
if (VarDecl *Var = dyn_cast<VarDecl>(RealDecl)) {
|
|
|
|
QualType Type = Var->getType();
|
2009-04-21 21:11:58 +04:00
|
|
|
|
2009-07-11 04:34:39 +04:00
|
|
|
// C++0x [dcl.spec.auto]p3
|
|
|
|
if (TypeContainsUndeducedAuto) {
|
|
|
|
Diag(Var->getLocation(), diag::err_auto_var_requires_init)
|
|
|
|
<< Var->getDeclName() << Type;
|
|
|
|
Var->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-02-09 10:26:29 +03:00
|
|
|
switch (Var->isThisDeclarationADefinition()) {
|
|
|
|
case VarDecl::Definition:
|
|
|
|
if (!Var->isStaticDataMember() || !Var->getAnyInitializer())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// We have an out-of-line definition of a static data member
|
|
|
|
// that has an in-class initializer, so we type-check this like
|
|
|
|
// a declaration.
|
|
|
|
//
|
|
|
|
// Fall through
|
|
|
|
|
|
|
|
case VarDecl::DeclarationOnly:
|
|
|
|
// It's only a declaration.
|
|
|
|
|
|
|
|
// Block scope. C99 6.7p7: If an identifier for an object is
|
|
|
|
// declared with no linkage (C99 6.2.2p6), the type for the
|
|
|
|
// object shall be complete.
|
|
|
|
if (!Type->isDependentType() && Var->isBlockVarDecl() &&
|
|
|
|
!Var->getLinkage() && !Var->isInvalidDecl() &&
|
|
|
|
RequireCompleteType(Var->getLocation(), Type,
|
|
|
|
diag::err_typecheck_decl_incomplete_type))
|
|
|
|
Var->setInvalidDecl();
|
|
|
|
|
|
|
|
// Make sure that the type is not abstract.
|
|
|
|
if (!Type->isDependentType() && !Var->isInvalidDecl() &&
|
|
|
|
RequireNonAbstractType(Var->getLocation(), Type,
|
|
|
|
diag::err_abstract_type_in_decl,
|
|
|
|
AbstractVariableType))
|
|
|
|
Var->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
|
|
|
|
case VarDecl::TentativeDefinition:
|
|
|
|
// File scope. C99 6.9.2p2: A declaration of an identifier for an
|
|
|
|
// object that has file scope without an initializer, and without a
|
|
|
|
// storage-class specifier or with the storage-class specifier "static",
|
|
|
|
// constitutes a tentative definition. Note: A tentative definition with
|
|
|
|
// external linkage is valid (C99 6.2.2p5).
|
|
|
|
if (!Var->isInvalidDecl()) {
|
|
|
|
if (const IncompleteArrayType *ArrayT
|
|
|
|
= Context.getAsIncompleteArrayType(Type)) {
|
|
|
|
if (RequireCompleteType(Var->getLocation(),
|
|
|
|
ArrayT->getElementType(),
|
|
|
|
diag::err_illegal_decl_array_incomplete_type))
|
|
|
|
Var->setInvalidDecl();
|
2010-08-26 07:08:43 +04:00
|
|
|
} else if (Var->getStorageClass() == SC_Static) {
|
2010-02-09 10:26:29 +03:00
|
|
|
// C99 6.9.2p3: If the declaration of an identifier for an object is
|
|
|
|
// a tentative definition and has internal linkage (C99 6.2.2p3), the
|
|
|
|
// declared type shall not be an incomplete type.
|
|
|
|
// NOTE: code such as the following
|
|
|
|
// static struct s;
|
|
|
|
// struct s { int a; };
|
|
|
|
// is accepted by gcc. Hence here we issue a warning instead of
|
|
|
|
// an error and we do not invalidate the static declaration.
|
|
|
|
// NOTE: to avoid multiple warnings, only check the first declaration.
|
|
|
|
if (Var->getPreviousDeclaration() == 0)
|
|
|
|
RequireCompleteType(Var->getLocation(), Type,
|
|
|
|
diag::ext_typecheck_decl_incomplete_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the tentative definition; we're done.
|
|
|
|
if (!Var->isInvalidDecl())
|
|
|
|
TentativeDefinitions.push_back(Var);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide a specific diagnostic for uninitialized variable
|
|
|
|
// definitions with incomplete array type.
|
|
|
|
if (Type->isIncompleteArrayType()) {
|
2009-11-05 22:47:47 +03:00
|
|
|
Diag(Var->getLocation(),
|
|
|
|
diag::err_typecheck_incomplete_array_needs_initializer);
|
|
|
|
Var->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-01 05:25:24 +04:00
|
|
|
// Provide a specific diagnostic for uninitialized variable
|
|
|
|
// definitions with reference type.
|
|
|
|
if (Type->isReferenceType()) {
|
|
|
|
Diag(Var->getLocation(), diag::err_reference_var_requires_init)
|
|
|
|
<< Var->getDeclName()
|
|
|
|
<< SourceRange(Var->getLocation(), Var->getLocation());
|
|
|
|
Var->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
2010-02-09 10:26:29 +03:00
|
|
|
|
|
|
|
// Do not attempt to type-check the default initializer for a
|
|
|
|
// variable with dependent type.
|
|
|
|
if (Type->isDependentType())
|
2009-10-08 11:24:58 +04:00
|
|
|
return;
|
2009-12-03 21:33:45 +03:00
|
|
|
|
2010-02-09 10:26:29 +03:00
|
|
|
if (Var->isInvalidDecl())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (RequireCompleteType(Var->getLocation(),
|
|
|
|
Context.getBaseElementType(Type),
|
|
|
|
diag::err_typecheck_decl_incomplete_type)) {
|
|
|
|
Var->setInvalidDecl();
|
|
|
|
return;
|
2008-11-03 23:45:27 +03:00
|
|
|
}
|
2008-10-29 03:13:59 +03:00
|
|
|
|
2010-02-09 10:26:29 +03:00
|
|
|
// The variable can not have an abstract class type.
|
|
|
|
if (RequireNonAbstractType(Var->getLocation(), Type,
|
|
|
|
diag::err_abstract_type_in_decl,
|
|
|
|
AbstractVariableType)) {
|
|
|
|
Var->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-08 05:45:10 +03:00
|
|
|
const RecordType *Record
|
|
|
|
= Context.getBaseElementType(Type)->getAs<RecordType>();
|
|
|
|
if (Record && getLangOptions().CPlusPlus && !getLangOptions().CPlusPlus0x &&
|
|
|
|
cast<CXXRecordDecl>(Record->getDecl())->isPOD()) {
|
|
|
|
// C++03 [dcl.init]p9:
|
|
|
|
// If no initializer is specified for an object, and the
|
|
|
|
// object is of (possibly cv-qualified) non-POD class type (or
|
|
|
|
// array thereof), the object shall be default-initialized; if
|
|
|
|
// the object is of const-qualified type, the underlying class
|
|
|
|
// type shall have a user-declared default
|
|
|
|
// constructor. Otherwise, if no initializer is specified for
|
|
|
|
// a non- static object, the object and its subobjects, if
|
|
|
|
// any, have an indeterminate initial value); if the object
|
|
|
|
// or any of its subobjects are of const-qualified type, the
|
|
|
|
// program is ill-formed.
|
|
|
|
// FIXME: DPG thinks it is very fishy that C++0x disables this.
|
|
|
|
} else {
|
2010-08-01 05:24:59 +04:00
|
|
|
// Check for jumps past the implicit initializer. C++0x
|
|
|
|
// clarifies that this applies to a "variable with automatic
|
|
|
|
// storage duration", not a "local variable".
|
|
|
|
if (getLangOptions().CPlusPlus && Var->hasLocalStorage())
|
2010-08-25 12:40:02 +04:00
|
|
|
getCurFunction()->setHasBranchProtectedScope();
|
2010-08-01 05:24:59 +04:00
|
|
|
|
2010-03-08 05:45:10 +03:00
|
|
|
InitializedEntity Entity = InitializedEntity::InitializeVariable(Var);
|
|
|
|
InitializationKind Kind
|
|
|
|
= InitializationKind::CreateDefault(Var->getLocation());
|
2010-02-09 10:26:29 +03:00
|
|
|
|
2010-03-08 05:45:10 +03:00
|
|
|
InitializationSequence InitSeq(*this, Entity, Kind, 0, 0);
|
2010-08-24 10:29:42 +04:00
|
|
|
ExprResult Init = InitSeq.Perform(*this, Entity, Kind,
|
|
|
|
MultiExprArg(*this, 0, 0));
|
2010-03-08 05:45:10 +03:00
|
|
|
if (Init.isInvalid())
|
|
|
|
Var->setInvalidDecl();
|
2010-08-02 00:20:59 +04:00
|
|
|
else if (Init.get()) {
|
2010-02-11 04:19:42 +03:00
|
|
|
Var->setInit(MaybeCreateCXXExprWithTemporaries(Init.takeAs<Expr>()));
|
2010-08-02 00:20:59 +04:00
|
|
|
|
|
|
|
if (getLangOptions().CPlusPlus && !Var->isInvalidDecl() &&
|
2010-09-03 05:11:38 +04:00
|
|
|
Var->hasGlobalStorage() && !Var->isStaticLocal() &&
|
2010-08-02 00:20:59 +04:00
|
|
|
!Var->getDeclContext()->isDependentContext() &&
|
2010-08-03 01:13:48 +04:00
|
|
|
!Var->getInit()->isConstantInitializer(Context, false))
|
2010-08-02 00:20:59 +04:00
|
|
|
Diag(Var->getLocation(), diag::warn_global_constructor);
|
|
|
|
}
|
2010-02-09 10:26:29 +03:00
|
|
|
}
|
2010-03-08 05:45:10 +03:00
|
|
|
|
|
|
|
if (!Var->isInvalidDecl() && getLangOptions().CPlusPlus && Record)
|
|
|
|
FinalizeVarWithDestructor(Var, Record);
|
2008-10-29 03:13:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-24 09:47:05 +04:00
|
|
|
Sema::DeclGroupPtrTy
|
|
|
|
Sema::FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS,
|
|
|
|
Decl **Group, unsigned NumDecls) {
|
2009-03-29 20:50:03 +04:00
|
|
|
llvm::SmallVector<Decl*, 8> Decls;
|
2009-05-29 05:49:24 +04:00
|
|
|
|
|
|
|
if (DS.isTypeSpecOwned())
|
2010-08-24 09:47:05 +04:00
|
|
|
Decls.push_back(DS.getRepAsDecl());
|
2009-05-29 05:49:24 +04:00
|
|
|
|
2009-03-29 20:50:03 +04:00
|
|
|
for (unsigned i = 0; i != NumDecls; ++i)
|
2010-08-21 13:40:31 +04:00
|
|
|
if (Decl *D = Group[i])
|
2009-03-29 20:50:03 +04:00
|
|
|
Decls.push_back(D);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-29 20:50:03 +04:00
|
|
|
return DeclGroupPtrTy::make(DeclGroupRef::Create(Context,
|
2009-05-21 13:52:38 +04:00
|
|
|
Decls.data(), Decls.size()));
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-08-28 07:03:08 +04:00
|
|
|
|
2009-03-29 20:50:03 +04:00
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
/// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator()
|
|
|
|
/// to introduce parameters into function prototype scope.
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D) {
|
2008-06-26 10:49:43 +04:00
|
|
|
const DeclSpec &DS = D.getDeclSpec();
|
2008-12-16 02:53:10 +03:00
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
// Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
|
2010-08-26 07:08:43 +04:00
|
|
|
VarDecl::StorageClass StorageClass = SC_None;
|
|
|
|
VarDecl::StorageClass StorageClassAsWritten = SC_None;
|
2008-09-04 01:54:21 +04:00
|
|
|
if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
|
2010-08-26 07:08:43 +04:00
|
|
|
StorageClass = SC_Register;
|
|
|
|
StorageClassAsWritten = SC_Register;
|
2008-09-04 01:54:21 +04:00
|
|
|
} else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
|
2008-04-08 08:40:51 +04:00
|
|
|
Diag(DS.getStorageClassSpecLoc(),
|
|
|
|
diag::err_invalid_storage_class_in_func_decl);
|
2008-06-26 10:49:43 +04:00
|
|
|
D.getMutableDeclSpec().ClearStorageClassSpecs();
|
2008-04-08 08:40:51 +04:00
|
|
|
}
|
2009-04-20 00:27:55 +04:00
|
|
|
|
|
|
|
if (D.getDeclSpec().isThreadSpecified())
|
|
|
|
Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
|
|
|
|
|
2009-04-07 23:37:57 +04:00
|
|
|
DiagnoseFunctionSpecifiers(D);
|
|
|
|
|
2008-05-07 08:49:29 +04:00
|
|
|
// Check that there are no default arguments inside the type of this
|
|
|
|
// parameter (C++ only).
|
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
CheckExtraCXXDefaultArguments(D);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
When we parse a tag specifier, keep track of whether that tag
specifier resulted in the creation of a new TagDecl node, which
happens either when the tag specifier was a definition or when the tag
specifier was the first declaration of that tag type. This information
has several uses, the first of which is implemented in this commit:
1) In C++, one is not allowed to define tag types within a type
specifier (e.g., static_cast<struct S { int x; } *>(0) is
ill-formed) or within the result or parameter types of a
function. We now diagnose this.
2) We can extend DeclGroups to contain information about any tags
that are declared/defined within the declaration specifiers of a
variable, e.g.,
struct Point { int x, y, z; } p;
This will help improve AST printing and template instantiation,
among other things.
3) For C99, we can keep track of whether a tag type is defined
within the type of a parameter, to properly cope with cases like,
e.g.,
int bar(struct T2 { int x; } y) {
struct T2 z;
}
We can also do similar things wherever there is a type specifier,
e.g., to keep track of where the definition of S occurs in this
legal C99 code:
(struct S { int x, y; } *)0
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72555 91177308-0d34-0410-b5e6-96231b3b80d8
2009-05-29 03:31:59 +04:00
|
|
|
TagDecl *OwnedDecl = 0;
|
2010-06-05 03:28:52 +04:00
|
|
|
TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
|
|
|
|
QualType parmDeclType = TInfo->getType();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
When we parse a tag specifier, keep track of whether that tag
specifier resulted in the creation of a new TagDecl node, which
happens either when the tag specifier was a definition or when the tag
specifier was the first declaration of that tag type. This information
has several uses, the first of which is implemented in this commit:
1) In C++, one is not allowed to define tag types within a type
specifier (e.g., static_cast<struct S { int x; } *>(0) is
ill-formed) or within the result or parameter types of a
function. We now diagnose this.
2) We can extend DeclGroups to contain information about any tags
that are declared/defined within the declaration specifiers of a
variable, e.g.,
struct Point { int x, y, z; } p;
This will help improve AST printing and template instantiation,
among other things.
3) For C99, we can keep track of whether a tag type is defined
within the type of a parameter, to properly cope with cases like,
e.g.,
int bar(struct T2 { int x; } y) {
struct T2 z;
}
We can also do similar things wherever there is a type specifier,
e.g., to keep track of where the definition of S occurs in this
legal C99 code:
(struct S { int x, y; } *)0
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72555 91177308-0d34-0410-b5e6-96231b3b80d8
2009-05-29 03:31:59 +04:00
|
|
|
if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
|
|
|
|
// C++ [dcl.fct]p6:
|
|
|
|
// Types shall not be defined in return or parameter types.
|
|
|
|
Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
|
|
|
|
<< Context.getTypeDeclType(OwnedDecl);
|
|
|
|
}
|
|
|
|
|
2010-02-22 03:40:25 +03:00
|
|
|
// Check for redeclaration of parameters, e.g. int foo(int x, int x);
|
2008-04-08 08:40:51 +04:00
|
|
|
IdentifierInfo *II = D.getIdentifier();
|
2009-01-21 05:38:50 +03:00
|
|
|
if (II) {
|
2010-03-18 09:42:38 +03:00
|
|
|
LookupResult R(*this, II, D.getIdentifierLoc(), LookupOrdinaryName,
|
|
|
|
ForRedeclaration);
|
|
|
|
LookupName(R, S);
|
|
|
|
if (R.isSingleResult()) {
|
|
|
|
NamedDecl *PrevDecl = R.getFoundDecl();
|
2009-01-21 05:38:50 +03:00
|
|
|
if (PrevDecl->isTemplateParameter()) {
|
|
|
|
// Maybe we will complain about the shadowed template parameter.
|
|
|
|
DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
|
|
|
|
// Just pretend that we didn't see the previous declaration.
|
|
|
|
PrevDecl = 0;
|
2010-08-21 13:40:31 +04:00
|
|
|
} else if (S->isDeclScope(PrevDecl)) {
|
2009-01-21 05:38:50 +03:00
|
|
|
Diag(D.getIdentifierLoc(), diag::err_param_redefinition) << II;
|
2010-02-22 03:40:25 +03:00
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_declaration);
|
2009-01-21 05:38:50 +03:00
|
|
|
|
|
|
|
// Recover by removing the name
|
|
|
|
II = 0;
|
|
|
|
D.SetIdentifier(0, D.getIdentifierLoc());
|
2010-02-13 00:53:14 +03:00
|
|
|
D.setInvalidType(true);
|
2009-01-21 05:38:50 +03:00
|
|
|
}
|
2008-04-08 08:40:51 +04:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-08-08 02:44:21 +04:00
|
|
|
|
2010-01-22 03:28:27 +03:00
|
|
|
// Temporarily put parameter variables in the translation unit, not
|
|
|
|
// the enclosing context. This prevents them from accidentally
|
|
|
|
// looking like class members in C++.
|
Be sure to instantiate the parameters of a function, even when the
function's type is (strictly speaking) non-dependent. This ensures
that, e.g., default function arguments get instantiated properly.
And, since I couldn't resist, collapse the two implementations of
function-parameter instantiation into calls to a single, new function
(Sema::SubstParmVarDecl), since the two had nearly identical code (and
each had bugs the other didn't!). More importantly, factored out the
semantic analysis of a parameter declaration into
Sema::CheckParameter, which is called both by
Sema::ActOnParamDeclarator (when parameters are parsed) and when a
parameter is instantiated. Previously, we were missing some
Objective-C and address-space checks on instantiated function
parameters.
Fixes PR6733.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101029 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-12 11:48:19 +04:00
|
|
|
ParmVarDecl *New = CheckParameter(Context.getTranslationUnitDecl(),
|
|
|
|
TInfo, parmDeclType, II,
|
2010-04-20 02:54:31 +04:00
|
|
|
D.getIdentifierLoc(),
|
|
|
|
StorageClass, StorageClassAsWritten);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
if (D.isInvalidType())
|
Be sure to instantiate the parameters of a function, even when the
function's type is (strictly speaking) non-dependent. This ensures
that, e.g., default function arguments get instantiated properly.
And, since I couldn't resist, collapse the two implementations of
function-parameter instantiation into calls to a single, new function
(Sema::SubstParmVarDecl), since the two had nearly identical code (and
each had bugs the other didn't!). More importantly, factored out the
semantic analysis of a parameter declaration into
Sema::CheckParameter, which is called both by
Sema::ActOnParamDeclarator (when parameters are parsed) and when a
parameter is instantiated. Previously, we were missing some
Objective-C and address-space checks on instantiated function
parameters.
Fixes PR6733.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101029 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-12 11:48:19 +04:00
|
|
|
New->setInvalidDecl();
|
|
|
|
|
2009-04-11 23:34:56 +04:00
|
|
|
// Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
|
|
|
|
if (D.getCXXScopeSpec().isSet()) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_qualified_param_declarator)
|
|
|
|
<< D.getCXXScopeSpec().getRange();
|
|
|
|
New->setInvalidDecl();
|
|
|
|
}
|
Be sure to instantiate the parameters of a function, even when the
function's type is (strictly speaking) non-dependent. This ensures
that, e.g., default function arguments get instantiated properly.
And, since I couldn't resist, collapse the two implementations of
function-parameter instantiation into calls to a single, new function
(Sema::SubstParmVarDecl), since the two had nearly identical code (and
each had bugs the other didn't!). More importantly, factored out the
semantic analysis of a parameter declaration into
Sema::CheckParameter, which is called both by
Sema::ActOnParamDeclarator (when parameters are parsed) and when a
parameter is instantiated. Previously, we were missing some
Objective-C and address-space checks on instantiated function
parameters.
Fixes PR6733.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101029 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-12 11:48:19 +04:00
|
|
|
|
2008-12-11 19:49:14 +03:00
|
|
|
// Add the parameter declaration into this scope.
|
2010-08-21 13:40:31 +04:00
|
|
|
S->AddDecl(New);
|
2008-04-12 04:47:19 +04:00
|
|
|
if (II)
|
2008-12-11 19:49:14 +03:00
|
|
|
IdResolver.AddDecl(New);
|
2008-02-18 00:20:31 +03:00
|
|
|
|
2009-06-18 01:51:59 +04:00
|
|
|
ProcessDeclAttributes(S, New, D);
|
2009-04-30 04:19:40 +04:00
|
|
|
|
2009-06-30 06:34:44 +04:00
|
|
|
if (New->hasAttr<BlocksAttr>()) {
|
2009-04-30 04:19:40 +04:00
|
|
|
Diag(New->getLocation(), diag::err_block_on_nonlocal);
|
|
|
|
}
|
2010-08-21 13:40:31 +04:00
|
|
|
return New;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-11-09 02:49:49 +03:00
|
|
|
|
2010-06-04 15:21:44 +04:00
|
|
|
/// \brief Synthesizes a variable for a parameter arising from a
|
|
|
|
/// typedef.
|
|
|
|
ParmVarDecl *Sema::BuildParmVarDeclForTypedef(DeclContext *DC,
|
|
|
|
SourceLocation Loc,
|
|
|
|
QualType T) {
|
|
|
|
ParmVarDecl *Param = ParmVarDecl::Create(Context, DC, Loc, 0,
|
|
|
|
T, Context.getTrivialTypeSourceInfo(T, Loc),
|
2010-08-26 07:08:43 +04:00
|
|
|
SC_None, SC_None, 0);
|
2010-06-04 15:21:44 +04:00
|
|
|
Param->setImplicit();
|
|
|
|
return Param;
|
|
|
|
}
|
|
|
|
|
2010-08-24 13:05:15 +04:00
|
|
|
void Sema::DiagnoseUnusedParameters(ParmVarDecl * const *Param,
|
|
|
|
ParmVarDecl * const *ParamEnd) {
|
|
|
|
if (Diags.getDiagnosticLevel(diag::warn_unused_parameter) ==
|
|
|
|
Diagnostic::Ignored)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Don't diagnose unused-parameter errors in template instantiations; we
|
|
|
|
// will already have done so in the template itself.
|
|
|
|
if (!ActiveTemplateInstantiations.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (; Param != ParamEnd; ++Param) {
|
|
|
|
if (!(*Param)->isUsed() && (*Param)->getDeclName() &&
|
|
|
|
!(*Param)->hasAttr<UnusedAttr>()) {
|
|
|
|
Diag((*Param)->getLocation(), diag::warn_unused_parameter)
|
|
|
|
<< (*Param)->getDeclName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Be sure to instantiate the parameters of a function, even when the
function's type is (strictly speaking) non-dependent. This ensures
that, e.g., default function arguments get instantiated properly.
And, since I couldn't resist, collapse the two implementations of
function-parameter instantiation into calls to a single, new function
(Sema::SubstParmVarDecl), since the two had nearly identical code (and
each had bugs the other didn't!). More importantly, factored out the
semantic analysis of a parameter declaration into
Sema::CheckParameter, which is called both by
Sema::ActOnParamDeclarator (when parameters are parsed) and when a
parameter is instantiated. Previously, we were missing some
Objective-C and address-space checks on instantiated function
parameters.
Fixes PR6733.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101029 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-12 11:48:19 +04:00
|
|
|
ParmVarDecl *Sema::CheckParameter(DeclContext *DC,
|
|
|
|
TypeSourceInfo *TSInfo, QualType T,
|
|
|
|
IdentifierInfo *Name,
|
|
|
|
SourceLocation NameLoc,
|
2010-04-20 02:54:31 +04:00
|
|
|
VarDecl::StorageClass StorageClass,
|
|
|
|
VarDecl::StorageClass StorageClassAsWritten) {
|
Be sure to instantiate the parameters of a function, even when the
function's type is (strictly speaking) non-dependent. This ensures
that, e.g., default function arguments get instantiated properly.
And, since I couldn't resist, collapse the two implementations of
function-parameter instantiation into calls to a single, new function
(Sema::SubstParmVarDecl), since the two had nearly identical code (and
each had bugs the other didn't!). More importantly, factored out the
semantic analysis of a parameter declaration into
Sema::CheckParameter, which is called both by
Sema::ActOnParamDeclarator (when parameters are parsed) and when a
parameter is instantiated. Previously, we were missing some
Objective-C and address-space checks on instantiated function
parameters.
Fixes PR6733.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101029 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-12 11:48:19 +04:00
|
|
|
ParmVarDecl *New = ParmVarDecl::Create(Context, DC, NameLoc, Name,
|
|
|
|
adjustParameterType(T), TSInfo,
|
2010-04-20 02:54:31 +04:00
|
|
|
StorageClass, StorageClassAsWritten,
|
|
|
|
0);
|
Be sure to instantiate the parameters of a function, even when the
function's type is (strictly speaking) non-dependent. This ensures
that, e.g., default function arguments get instantiated properly.
And, since I couldn't resist, collapse the two implementations of
function-parameter instantiation into calls to a single, new function
(Sema::SubstParmVarDecl), since the two had nearly identical code (and
each had bugs the other didn't!). More importantly, factored out the
semantic analysis of a parameter declaration into
Sema::CheckParameter, which is called both by
Sema::ActOnParamDeclarator (when parameters are parsed) and when a
parameter is instantiated. Previously, we were missing some
Objective-C and address-space checks on instantiated function
parameters.
Fixes PR6733.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101029 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-12 11:48:19 +04:00
|
|
|
|
|
|
|
// Parameters can not be abstract class types.
|
|
|
|
// For record types, this is done by the AbstractClassUsageDiagnoser once
|
|
|
|
// the class has been completely parsed.
|
|
|
|
if (!CurContext->isRecord() &&
|
|
|
|
RequireNonAbstractType(NameLoc, T, diag::err_abstract_type_in_decl,
|
|
|
|
AbstractParamType))
|
|
|
|
New->setInvalidDecl();
|
|
|
|
|
|
|
|
// Parameter declarators cannot be interface types. All ObjC objects are
|
|
|
|
// passed by reference.
|
2010-05-15 15:32:37 +04:00
|
|
|
if (T->isObjCObjectType()) {
|
Be sure to instantiate the parameters of a function, even when the
function's type is (strictly speaking) non-dependent. This ensures
that, e.g., default function arguments get instantiated properly.
And, since I couldn't resist, collapse the two implementations of
function-parameter instantiation into calls to a single, new function
(Sema::SubstParmVarDecl), since the two had nearly identical code (and
each had bugs the other didn't!). More importantly, factored out the
semantic analysis of a parameter declaration into
Sema::CheckParameter, which is called both by
Sema::ActOnParamDeclarator (when parameters are parsed) and when a
parameter is instantiated. Previously, we were missing some
Objective-C and address-space checks on instantiated function
parameters.
Fixes PR6733.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101029 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-12 11:48:19 +04:00
|
|
|
Diag(NameLoc,
|
|
|
|
diag::err_object_cannot_be_passed_returned_by_value) << 1 << T;
|
|
|
|
New->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
|
|
|
|
// duration shall not be qualified by an address-space qualifier."
|
|
|
|
// Since all parameters have automatic store duration, they can not have
|
|
|
|
// an address space.
|
|
|
|
if (T.getAddressSpace() != 0) {
|
|
|
|
Diag(NameLoc, diag::err_arg_with_address_space);
|
|
|
|
New->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2009-04-02 03:51:29 +04:00
|
|
|
void Sema::ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D,
|
|
|
|
SourceLocation LocAfterDecls) {
|
2007-07-11 21:01:13 +04:00
|
|
|
assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
|
|
|
|
"Not a function declarator!");
|
|
|
|
DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
|
2008-04-08 08:40:51 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
|
|
|
|
// for a K&R function.
|
|
|
|
if (!FTI.hasPrototype) {
|
2009-04-02 07:14:12 +04:00
|
|
|
for (int i = FTI.NumArgs; i != 0; /* decrement in loop */) {
|
|
|
|
--i;
|
2008-04-08 08:40:51 +04:00
|
|
|
if (FTI.ArgInfo[i].Param == 0) {
|
2009-10-19 00:26:27 +04:00
|
|
|
llvm::SmallString<256> Code;
|
|
|
|
llvm::raw_svector_ostream(Code) << " int "
|
2009-10-19 01:17:35 +04:00
|
|
|
<< FTI.ArgInfo[i].Ident->getName()
|
2009-10-19 00:26:27 +04:00
|
|
|
<< ";\n";
|
2008-11-19 11:23:25 +03:00
|
|
|
Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared)
|
2009-04-02 03:51:29 +04:00
|
|
|
<< FTI.ArgInfo[i].Ident
|
2010-03-31 21:46:05 +04:00
|
|
|
<< FixItHint::CreateInsertion(LocAfterDecls, Code.str());
|
2009-04-02 03:51:29 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Implicitly declare the argument as type 'int' for lack of a better
|
|
|
|
// type.
|
2008-04-08 08:40:51 +04:00
|
|
|
DeclSpec DS;
|
|
|
|
const char* PrevSpec; // unused
|
2009-08-04 00:12:06 +04:00
|
|
|
unsigned DiagID; // unused
|
2009-09-09 19:08:12 +04:00
|
|
|
DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc,
|
2009-08-04 00:12:06 +04:00
|
|
|
PrevSpec, DiagID);
|
2008-04-08 08:40:51 +04:00
|
|
|
Declarator ParamD(DS, Declarator::KNRTypeListContext);
|
|
|
|
ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc);
|
2009-01-23 19:23:13 +03:00
|
|
|
FTI.ArgInfo[i].Param = ActOnParamDeclarator(S, ParamD);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2009-01-23 19:23:13 +03:00
|
|
|
}
|
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope,
|
|
|
|
Declarator &D) {
|
2009-01-23 19:23:13 +03:00
|
|
|
assert(getCurFunctionDecl() == 0 && "Function parsing confused");
|
|
|
|
assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
|
|
|
|
"Not a function declarator!");
|
|
|
|
DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
|
|
|
|
|
|
|
|
if (FTI.hasPrototype) {
|
2009-09-09 19:08:12 +04:00
|
|
|
// FIXME: Diagnose arguments without names in C.
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-12-16 02:53:10 +03:00
|
|
|
Scope *ParentScope = FnBodyScope->getParent();
|
2008-01-14 23:51:29 +03:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *DP = HandleDeclarator(ParentScope, D,
|
|
|
|
MultiTemplateParamsArg(*this),
|
|
|
|
/*IsFunctionDefinition=*/true);
|
2009-03-29 20:50:03 +04:00
|
|
|
return ActOnStartOfFunctionDef(FnBodyScope, DP);
|
2008-07-01 14:37:29 +04:00
|
|
|
}
|
|
|
|
|
2009-12-09 06:30:09 +03:00
|
|
|
static bool ShouldWarnAboutMissingPrototype(const FunctionDecl *FD) {
|
|
|
|
// Don't warn about invalid declarations.
|
|
|
|
if (FD->isInvalidDecl())
|
|
|
|
return false;
|
2009-12-09 06:44:46 +03:00
|
|
|
|
2009-12-09 06:30:09 +03:00
|
|
|
// Or declarations that aren't global.
|
|
|
|
if (!FD->isGlobal())
|
|
|
|
return false;
|
2009-12-09 06:44:46 +03:00
|
|
|
|
2009-12-09 06:30:09 +03:00
|
|
|
// Don't warn about C++ member functions.
|
|
|
|
if (isa<CXXMethodDecl>(FD))
|
|
|
|
return false;
|
2009-12-09 06:44:46 +03:00
|
|
|
|
2009-12-09 06:30:09 +03:00
|
|
|
// Don't warn about 'main'.
|
|
|
|
if (FD->isMain())
|
|
|
|
return false;
|
2009-12-09 06:44:46 +03:00
|
|
|
|
2009-12-09 06:30:09 +03:00
|
|
|
// Don't warn about inline functions.
|
|
|
|
if (FD->isInlineSpecified())
|
|
|
|
return false;
|
2009-12-09 06:44:46 +03:00
|
|
|
|
|
|
|
// Don't warn about function templates.
|
|
|
|
if (FD->getDescribedFunctionTemplate())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Don't warn about function template specializations.
|
|
|
|
if (FD->isFunctionTemplateSpecialization())
|
|
|
|
return false;
|
|
|
|
|
2009-12-09 06:30:09 +03:00
|
|
|
bool MissingPrototype = true;
|
|
|
|
for (const FunctionDecl *Prev = FD->getPreviousDeclaration();
|
|
|
|
Prev; Prev = Prev->getPreviousDeclaration()) {
|
|
|
|
// Ignore any declarations that occur in function or method
|
|
|
|
// scope, because they aren't visible from the header.
|
|
|
|
if (Prev->getDeclContext()->isFunctionOrMethod())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MissingPrototype = !Prev->getType()->isFunctionProtoType();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MissingPrototype;
|
|
|
|
}
|
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D) {
|
2009-10-29 18:46:07 +03:00
|
|
|
// Clear the last template instantiation error context.
|
|
|
|
LastTemplateInstantiationErrorContext = ActiveTemplateInstantiation();
|
|
|
|
|
2009-06-24 04:54:41 +04:00
|
|
|
if (!D)
|
|
|
|
return D;
|
2009-08-22 04:34:47 +04:00
|
|
|
FunctionDecl *FD = 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
|
2009-08-22 04:34:47 +04:00
|
|
|
FD = FunTmpl->getTemplatedDecl();
|
|
|
|
else
|
2010-08-21 13:40:31 +04:00
|
|
|
FD = cast<FunctionDecl>(D);
|
2008-10-29 18:10:40 +03:00
|
|
|
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97518 91177308-0d34-0410-b5e6-96231b3b80d8
2010-03-02 02:15:13 +03:00
|
|
|
// Enter a new function scope
|
|
|
|
PushFunctionScope();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-10-29 18:10:40 +03:00
|
|
|
// See if this is a redefinition.
|
2010-02-18 05:00:42 +03:00
|
|
|
// But don't complain if we're in GNU89 mode and the previous definition
|
|
|
|
// was an extern inline function.
|
2008-10-29 18:10:40 +03:00
|
|
|
const FunctionDecl *Definition;
|
2010-07-07 15:31:19 +04:00
|
|
|
if (FD->hasBody(Definition) &&
|
2010-02-18 05:00:42 +03:00
|
|
|
!canRedefineFunction(Definition, getLangOptions())) {
|
2010-09-07 19:51:01 +04:00
|
|
|
if (getLangOptions().GNUMode && Definition->isInlineSpecified() &&
|
|
|
|
Definition->getStorageClass() == SC_Extern)
|
|
|
|
Diag(FD->getLocation(), diag::err_redefinition_extern_inline)
|
|
|
|
<< FD->getDeclName() << getLangOptions().CPlusPlus;
|
|
|
|
else
|
|
|
|
Diag(FD->getLocation(), diag::err_redefinition) << FD->getDeclName();
|
2008-11-24 02:12:31 +03:00
|
|
|
Diag(Definition->getLocation(), diag::note_previous_definition);
|
2008-10-29 18:10:40 +03:00
|
|
|
}
|
|
|
|
|
2009-02-16 20:45:42 +03:00
|
|
|
// Builtin functions cannot be defined.
|
2009-09-12 04:22:50 +04:00
|
|
|
if (unsigned BuiltinID = FD->getBuiltinID()) {
|
2009-02-17 19:03:01 +03:00
|
|
|
if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) {
|
2009-02-16 20:45:42 +03:00
|
|
|
Diag(FD->getLocation(), diag::err_builtin_definition) << FD;
|
2009-02-17 19:03:01 +03:00
|
|
|
FD->setInvalidDecl();
|
|
|
|
}
|
2009-02-16 20:45:42 +03:00
|
|
|
}
|
|
|
|
|
2009-03-04 10:30:59 +03:00
|
|
|
// The return type of a function definition must be complete
|
2009-03-24 22:52:54 +03:00
|
|
|
// (C99 6.9.1p3, C++ [dcl.fct]p6).
|
|
|
|
QualType ResultType = FD->getResultType();
|
|
|
|
if (!ResultType->isDependentType() && !ResultType->isVoidType() &&
|
2009-04-29 09:12:23 +04:00
|
|
|
!FD->isInvalidDecl() &&
|
2009-03-24 22:52:54 +03:00
|
|
|
RequireCompleteType(FD->getLocation(), ResultType,
|
|
|
|
diag::err_func_def_incomplete_result))
|
2009-03-04 10:30:59 +03:00
|
|
|
FD->setInvalidDecl();
|
|
|
|
|
2009-03-31 20:35:03 +04:00
|
|
|
// GNU warning -Wmissing-prototypes:
|
|
|
|
// Warn if a global function is defined without a previous
|
|
|
|
// prototype declaration. This warning is issued even if the
|
|
|
|
// definition itself provides a prototype. The aim is to detect
|
|
|
|
// global functions that fail to be declared in header files.
|
2009-12-09 06:30:09 +03:00
|
|
|
if (ShouldWarnAboutMissingPrototype(FD))
|
|
|
|
Diag(FD->getLocation(), diag::warn_missing_prototype) << FD;
|
2009-03-31 20:35:03 +04:00
|
|
|
|
2009-05-15 21:59:04 +04:00
|
|
|
if (FnBodyScope)
|
|
|
|
PushDeclContext(FnBodyScope, FD);
|
2008-12-26 03:52:02 +03:00
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
// Check the validity of our function parameters
|
|
|
|
CheckParmsForFunctionDef(FD);
|
|
|
|
|
2010-03-22 12:20:08 +03:00
|
|
|
bool ShouldCheckShadow =
|
|
|
|
Diags.getDiagnosticLevel(diag::warn_decl_shadow) != Diagnostic::Ignored;
|
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
// Introduce our parameters into the function scope
|
|
|
|
for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
|
|
|
|
ParmVarDecl *Param = FD->getParamDecl(p);
|
2009-01-09 21:51:29 +03:00
|
|
|
Param->setOwningFunction(FD);
|
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
// If this has an identifier, add it to the scope stack.
|
2010-03-22 12:20:08 +03:00
|
|
|
if (Param->getIdentifier() && FnBodyScope) {
|
|
|
|
if (ShouldCheckShadow)
|
|
|
|
CheckShadow(FnBodyScope, Param);
|
|
|
|
|
2008-04-12 04:47:19 +04:00
|
|
|
PushOnScopeChains(Param, FnBodyScope);
|
2010-03-22 12:20:08 +03:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-04-08 08:40:51 +04:00
|
|
|
|
2008-12-26 03:52:02 +03:00
|
|
|
// Checking attributes of current function definition
|
|
|
|
// dllimport attribute.
|
2010-08-19 03:23:40 +04:00
|
|
|
DLLImportAttr *DA = FD->getAttr<DLLImportAttr>();
|
|
|
|
if (DA && (!FD->getAttr<DLLExportAttr>())) {
|
|
|
|
// dllimport attribute cannot be directly applied to definition.
|
|
|
|
if (!DA->isInherited()) {
|
2008-12-26 03:52:02 +03:00
|
|
|
Diag(FD->getLocation(),
|
|
|
|
diag::err_attribute_can_be_applied_only_to_symbol_declaration)
|
|
|
|
<< "dllimport";
|
|
|
|
FD->setInvalidDecl();
|
2010-08-21 13:40:31 +04:00
|
|
|
return FD;
|
2010-02-21 08:12:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Visual C++ appears to not think this is an issue, so only issue
|
|
|
|
// a warning when Microsoft extensions are disabled.
|
|
|
|
if (!LangOpts.Microsoft) {
|
2008-12-26 03:52:02 +03:00
|
|
|
// If a symbol previously declared dllimport is later defined, the
|
|
|
|
// attribute is ignored in subsequent references, and a warning is
|
|
|
|
// emitted.
|
|
|
|
Diag(FD->getLocation(),
|
|
|
|
diag::warn_redeclaration_without_attribute_prev_attribute_ignored)
|
2010-08-18 02:39:59 +04:00
|
|
|
<< FD->getName() << "dllimport";
|
2008-12-26 03:52:02 +03:00
|
|
|
}
|
|
|
|
}
|
2010-08-21 13:40:31 +04:00
|
|
|
return FD;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2010-05-15 10:01:05 +04:00
|
|
|
/// \brief Given the set of return statements within a function body,
|
|
|
|
/// compute the variables that are subject to the named return value
|
|
|
|
/// optimization.
|
|
|
|
///
|
|
|
|
/// Each of the variables that is subject to the named return value
|
|
|
|
/// optimization will be marked as NRVO variables in the AST, and any
|
|
|
|
/// return statement that has a marked NRVO variable as its NRVO candidate can
|
|
|
|
/// use the named return value optimization.
|
|
|
|
///
|
|
|
|
/// This function applies a very simplistic algorithm for NRVO: if every return
|
|
|
|
/// statement in the function has the same NRVO candidate, that candidate is
|
|
|
|
/// the NRVO variable.
|
|
|
|
///
|
|
|
|
/// FIXME: Employ a smarter algorithm that accounts for multiple return
|
|
|
|
/// statements and the lifetimes of the NRVO candidates. We should be able to
|
|
|
|
/// find a maximal set of NRVO variables.
|
2010-08-25 12:40:02 +04:00
|
|
|
static void ComputeNRVO(Stmt *Body, FunctionScopeInfo *Scope) {
|
|
|
|
ReturnStmt **Returns = Scope->Returns.data();
|
|
|
|
|
2010-05-15 10:01:05 +04:00
|
|
|
const VarDecl *NRVOCandidate = 0;
|
2010-08-25 12:40:02 +04:00
|
|
|
for (unsigned I = 0, E = Scope->Returns.size(); I != E; ++I) {
|
2010-05-15 10:01:05 +04:00
|
|
|
if (!Returns[I]->getNRVOCandidate())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!NRVOCandidate)
|
|
|
|
NRVOCandidate = Returns[I]->getNRVOCandidate();
|
|
|
|
else if (NRVOCandidate != Returns[I]->getNRVOCandidate())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NRVOCandidate)
|
|
|
|
const_cast<VarDecl*>(NRVOCandidate)->setNRVOVariable(true);
|
|
|
|
}
|
|
|
|
|
2010-08-27 03:41:50 +04:00
|
|
|
Decl *Sema::ActOnFinishFunctionBody(Decl *D, Stmt *BodyArg) {
|
2009-05-15 21:59:04 +04:00
|
|
|
return ActOnFinishFunctionBody(D, move(BodyArg), false);
|
|
|
|
}
|
|
|
|
|
2010-08-24 03:25:46 +04:00
|
|
|
Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
|
|
|
|
bool IsInstantiation) {
|
2009-08-22 04:34:47 +04:00
|
|
|
FunctionDecl *FD = 0;
|
|
|
|
FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(dcl);
|
|
|
|
if (FunTmpl)
|
|
|
|
FD = FunTmpl->getTemplatedDecl();
|
|
|
|
else
|
|
|
|
FD = dyn_cast_or_null<FunctionDecl>(dcl);
|
|
|
|
|
2010-03-23 03:13:23 +03:00
|
|
|
sema::AnalysisBasedWarnings::Policy WP = AnalysisWarnings.getDefaultPolicy();
|
2010-03-21 00:06:02 +03:00
|
|
|
|
2009-08-22 04:34:47 +04:00
|
|
|
if (FD) {
|
2009-04-18 13:36:27 +04:00
|
|
|
FD->setBody(Body);
|
2010-03-21 00:06:02 +03:00
|
|
|
if (FD->isMain()) {
|
2009-07-24 06:49:01 +04:00
|
|
|
// C and C++ allow for main to automagically return 0.
|
2009-07-28 05:00:58 +04:00
|
|
|
// Implements C++ [basic.start.main]p5 and C99 5.1.2.2.3.
|
|
|
|
FD->setHasImplicitReturnZero(true);
|
2010-03-23 03:13:23 +03:00
|
|
|
WP.disableCheckFallThrough();
|
2010-03-21 00:06:02 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions.
The new scheme:
- For every use of a vtable, Sema calls MarkVTableUsed() to indicate
the use. For example, this occurs when calling a virtual member
function of the class, defining a constructor of that class type,
dynamic_cast'ing from that type to a derived class, casting
to/through a virtual base class, etc.
- For every definition of a vtable, Sema calls MarkVTableUsed() to
indicate the definition. This happens at the end of the translation
unit for classes whose key function has been defined (so we can
delay computation of the key function; see PR6564), and will also
occur with explicit template instantiation definitions.
- For every vtable defined/used, we mark all of the virtual member
functions of that vtable as defined/used, unless we know that the key
function is in another translation unit. This instantiates virtual
member functions when needed.
- At the end of the translation unit, Sema tells CodeGen (via the
ASTConsumer) which vtables must be defined (CodeGen will define
them) and which may be used (for which CodeGen will define the
vtables lazily).
From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).
Notes:
(1) There's a ton of churn in the tests, because the order in which
vtables get emitted to IR has changed. I've tried to isolate some of
the larger tests from these issues.
(2) Some diagnostics related to
implicitly-instantiated/implicitly-defined virtual member functions
have moved to the point of first use/definition. It's better this
way.
(3) I could use a review of the places where we MarkVTableUsed, to
see if I missed any place where the language effectively requires a
vtable.
Fixes PR7114 and PR6564.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@103718 91177308-0d34-0410-b5e6-96231b3b80d8
2010-05-13 20:44:06 +04:00
|
|
|
if (!FD->isInvalidDecl()) {
|
2009-06-20 03:52:42 +04:00
|
|
|
DiagnoseUnusedParameters(FD->param_begin(), FD->param_end());
|
Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions.
The new scheme:
- For every use of a vtable, Sema calls MarkVTableUsed() to indicate
the use. For example, this occurs when calling a virtual member
function of the class, defining a constructor of that class type,
dynamic_cast'ing from that type to a derived class, casting
to/through a virtual base class, etc.
- For every definition of a vtable, Sema calls MarkVTableUsed() to
indicate the definition. This happens at the end of the translation
unit for classes whose key function has been defined (so we can
delay computation of the key function; see PR6564), and will also
occur with explicit template instantiation definitions.
- For every vtable defined/used, we mark all of the virtual member
functions of that vtable as defined/used, unless we know that the key
function is in another translation unit. This instantiates virtual
member functions when needed.
- At the end of the translation unit, Sema tells CodeGen (via the
ASTConsumer) which vtables must be defined (CodeGen will define
them) and which may be used (for which CodeGen will define the
vtables lazily).
From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).
Notes:
(1) There's a ton of churn in the tests, because the order in which
vtables get emitted to IR has changed. I've tried to isolate some of
the larger tests from these issues.
(2) Some diagnostics related to
implicitly-instantiated/implicitly-defined virtual member functions
have moved to the point of first use/definition. It's better this
way.
(3) I could use a review of the places where we MarkVTableUsed, to
see if I missed any place where the language effectively requires a
vtable.
Fixes PR7114 and PR6564.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@103718 91177308-0d34-0410-b5e6-96231b3b80d8
2010-05-13 20:44:06 +04:00
|
|
|
|
|
|
|
// If this is a constructor, we need a vtable.
|
|
|
|
if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(FD))
|
|
|
|
MarkVTableUsed(FD->getLocation(), Constructor->getParent());
|
2010-05-15 10:01:05 +04:00
|
|
|
|
2010-08-25 12:40:02 +04:00
|
|
|
ComputeNRVO(Body, getCurFunction());
|
Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions.
The new scheme:
- For every use of a vtable, Sema calls MarkVTableUsed() to indicate
the use. For example, this occurs when calling a virtual member
function of the class, defining a constructor of that class type,
dynamic_cast'ing from that type to a derived class, casting
to/through a virtual base class, etc.
- For every definition of a vtable, Sema calls MarkVTableUsed() to
indicate the definition. This happens at the end of the translation
unit for classes whose key function has been defined (so we can
delay computation of the key function; see PR6564), and will also
occur with explicit template instantiation definitions.
- For every vtable defined/used, we mark all of the virtual member
functions of that vtable as defined/used, unless we know that the key
function is in another translation unit. This instantiates virtual
member functions when needed.
- At the end of the translation unit, Sema tells CodeGen (via the
ASTConsumer) which vtables must be defined (CodeGen will define
them) and which may be used (for which CodeGen will define the
vtables lazily).
From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).
Notes:
(1) There's a ton of churn in the tests, because the order in which
vtables get emitted to IR has changed. I've tried to isolate some of
the larger tests from these issues.
(2) Some diagnostics related to
implicitly-instantiated/implicitly-defined virtual member functions
have moved to the point of first use/definition. It's better this
way.
(3) I could use a review of the places where we MarkVTableUsed, to
see if I missed any place where the language effectively requires a
vtable.
Fixes PR7114 and PR6564.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@103718 91177308-0d34-0410-b5e6-96231b3b80d8
2010-05-13 20:44:06 +04:00
|
|
|
}
|
|
|
|
|
2008-06-28 10:07:14 +04:00
|
|
|
assert(FD == getCurFunctionDecl() && "Function parsing confused");
|
2008-07-25 21:57:26 +04:00
|
|
|
} else if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(dcl)) {
|
2009-02-16 22:27:54 +03:00
|
|
|
assert(MD == getCurMethodDecl() && "Method parsing confused");
|
2009-04-18 13:36:27 +04:00
|
|
|
MD->setBody(Body);
|
2009-07-18 04:33:33 +04:00
|
|
|
MD->setEndLoc(Body->getLocEnd());
|
2009-06-20 03:52:42 +04:00
|
|
|
if (!MD->isInvalidDecl())
|
|
|
|
DiagnoseUnusedParameters(MD->param_begin(), MD->param_end());
|
2009-02-07 04:47:29 +03:00
|
|
|
} else {
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2009-02-07 04:47:29 +03:00
|
|
|
}
|
2009-05-15 21:59:04 +04:00
|
|
|
|
2007-11-10 19:31:34 +03:00
|
|
|
// Verify and clean out per-function state.
|
Start of checking for gotos which jump to an illegal destination.
As far as I know, this catches all cases of jumping into the scope of a
variable with a variably modified type (excluding statement
expressions) in C. This is missing some stuff we probably want to check
(other kinds of variably modified declarations, statement expressions,
indirect gotos/addresses of labels in a scope, ObjC @try/@finally, cleanup
attribute), the diagnostics aren't very good, and it's not particularly
efficient, but it's a decent start.
This patch is a slightly modified version of the patch I attached to
PR3259, and it fixes that bug. I was sort of planning on improving
it, but I think it's okay as-is, especially since it looks like CodeGen
doesn't have any use for this sort of data structure. The only
significant change I can think of from the version I attached to PR3259
is that this version skips running the checking code when a function
doesn't contain any labels.
This patch doesn't cover case statements, which also need similar
checking; I'm not sure how we should deal with that. Extending the goto
checking to also check case statements wouldn't be too hard; it's just a
matter of keeping track of the scope of the closest switch and checking that
the scope of every case is the same as the scope of the switch. That said,
it would likely be a performance hit to run this check on every
function (it's an extra pass over the entire function), so we probably want
some other solution.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@65678 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-28 08:41:13 +03:00
|
|
|
|
2007-11-10 19:31:34 +03:00
|
|
|
// Check goto/label use.
|
2010-08-25 12:40:02 +04:00
|
|
|
FunctionScopeInfo *CurFn = getCurFunction();
|
2009-03-13 18:38:40 +03:00
|
|
|
for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator
|
2010-08-25 12:40:02 +04:00
|
|
|
I = CurFn->LabelMap.begin(), E = CurFn->LabelMap.end(); I != E; ++I) {
|
2009-04-18 23:30:02 +04:00
|
|
|
LabelStmt *L = I->second;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-11-10 19:31:34 +03:00
|
|
|
// Verify that we have no forward references left. If so, there was a goto
|
|
|
|
// or address of a label taken, but no definition of it. Label fwd
|
|
|
|
// definitions are indicated with a null substmt.
|
2010-09-20 01:21:25 +04:00
|
|
|
if (L->getSubStmt() != 0) {
|
|
|
|
if (!L->isUsed())
|
|
|
|
Diag(L->getIdentLoc(), diag::warn_unused_label) << L->getName();
|
2009-04-18 23:30:02 +04:00
|
|
|
continue;
|
2010-09-20 01:21:25 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-18 23:30:02 +04:00
|
|
|
// Emit error.
|
|
|
|
Diag(L->getIdentLoc(), diag::err_undeclared_label_use) << L->getName();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-18 23:30:02 +04:00
|
|
|
// At this point, we have gotos that use the bogus label. Stitch it into
|
|
|
|
// the function body so that they aren't leaked and that the AST is well
|
|
|
|
// formed.
|
|
|
|
if (Body == 0) {
|
2010-07-25 22:17:45 +04:00
|
|
|
// The whole function wasn't parsed correctly.
|
2009-04-18 23:30:02 +04:00
|
|
|
continue;
|
2007-11-10 19:31:34 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-18 23:30:02 +04:00
|
|
|
// Otherwise, the body is valid: we want to stitch the label decl into the
|
|
|
|
// function somewhere so that it is properly owned and so that the goto
|
|
|
|
// has a valid target. Do this by creating a new compound stmt with the
|
|
|
|
// label in it.
|
2009-04-27 00:35:05 +04:00
|
|
|
|
2009-04-18 23:30:02 +04:00
|
|
|
// Give the label a sub-statement.
|
|
|
|
L->setSubStmt(new (Context) NullStmt(L->getIdentLoc()));
|
2009-04-27 00:35:05 +04:00
|
|
|
|
|
|
|
CompoundStmt *Compound = isa<CXXTryStmt>(Body) ?
|
|
|
|
cast<CXXTryStmt>(Body)->getTryBlock() :
|
|
|
|
cast<CompoundStmt>(Body);
|
2010-03-13 01:22:36 +03:00
|
|
|
llvm::SmallVector<Stmt*, 64> Elements(Compound->body_begin(),
|
|
|
|
Compound->body_end());
|
2009-04-18 23:30:02 +04:00
|
|
|
Elements.push_back(L);
|
2010-03-13 01:22:36 +03:00
|
|
|
Compound->setStmts(Context, Elements.data(), Elements.size());
|
2007-11-10 19:31:34 +03:00
|
|
|
}
|
Start of checking for gotos which jump to an illegal destination.
As far as I know, this catches all cases of jumping into the scope of a
variable with a variably modified type (excluding statement
expressions) in C. This is missing some stuff we probably want to check
(other kinds of variably modified declarations, statement expressions,
indirect gotos/addresses of labels in a scope, ObjC @try/@finally, cleanup
attribute), the diagnostics aren't very good, and it's not particularly
efficient, but it's a decent start.
This patch is a slightly modified version of the patch I attached to
PR3259, and it fixes that bug. I was sort of planning on improving
it, but I think it's okay as-is, especially since it looks like CodeGen
doesn't have any use for this sort of data structure. The only
significant change I can think of from the version I attached to PR3259
is that this version skips running the checking code when a function
doesn't contain any labels.
This patch doesn't cover case statements, which also need similar
checking; I'm not sure how we should deal with that. Extending the goto
checking to also check case statements wouldn't be too hard; it's just a
matter of keeping track of the scope of the closest switch and checking that
the scope of every case is the same as the scope of the switch. That said,
it would likely be a performance hit to run this check on every
function (it's an extra pass over the entire function), so we probably want
some other solution.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@65678 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-28 08:41:13 +03:00
|
|
|
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97518 91177308-0d34-0410-b5e6-96231b3b80d8
2010-03-02 02:15:13 +03:00
|
|
|
if (Body) {
|
|
|
|
// C++ constructors that have function-try-blocks can't have return
|
|
|
|
// statements in the handlers of that block. (C++ [except.handle]p14)
|
|
|
|
// Verify this.
|
|
|
|
if (FD && isa<CXXConstructorDecl>(FD) && isa<CXXTryStmt>(Body))
|
|
|
|
DiagnoseReturnInConstructorExceptionHandler(cast<CXXTryStmt>(Body));
|
|
|
|
|
2010-03-21 00:06:02 +03:00
|
|
|
// Verify that that gotos and switch cases don't jump into scopes illegally.
|
|
|
|
// Verify that that gotos and switch cases don't jump into scopes illegally.
|
2010-08-25 12:40:02 +04:00
|
|
|
if (getCurFunction()->NeedsScopeChecking() &&
|
2010-05-20 11:13:26 +04:00
|
|
|
!dcl->isInvalidDecl() &&
|
2010-05-20 11:05:55 +04:00
|
|
|
!hasAnyErrorsInThisFunction())
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97518 91177308-0d34-0410-b5e6-96231b3b80d8
2010-03-02 02:15:13 +03:00
|
|
|
DiagnoseInvalidJumps(Body);
|
|
|
|
|
2010-08-04 05:04:25 +04:00
|
|
|
if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(dcl)) {
|
|
|
|
if (!Destructor->getParent()->isDependentType())
|
|
|
|
CheckDestructor(Destructor);
|
|
|
|
|
2010-03-17 00:39:52 +03:00
|
|
|
MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(),
|
|
|
|
Destructor->getParent());
|
2010-08-04 05:04:25 +04:00
|
|
|
}
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97518 91177308-0d34-0410-b5e6-96231b3b80d8
2010-03-02 02:15:13 +03:00
|
|
|
|
|
|
|
// If any errors have occurred, clear out any temporaries that may have
|
|
|
|
// been leftover. This ensures that these temporaries won't be picked up for
|
|
|
|
// deletion in some later function.
|
|
|
|
if (PP.getDiagnostics().hasErrorOccurred())
|
|
|
|
ExprTemporaries.clear();
|
2010-03-21 00:06:02 +03:00
|
|
|
else if (!isa<FunctionTemplateDecl>(dcl)) {
|
|
|
|
// Since the body is valid, issue any analysis-based warnings that are
|
|
|
|
// enabled.
|
|
|
|
QualType ResultType;
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(dcl)) {
|
2010-08-25 09:56:39 +04:00
|
|
|
AnalysisWarnings.IssueWarnings(WP, FD);
|
|
|
|
} else {
|
2010-03-21 00:06:02 +03:00
|
|
|
ObjCMethodDecl *MD = cast<ObjCMethodDecl>(dcl);
|
2010-08-25 09:56:39 +04:00
|
|
|
AnalysisWarnings.IssueWarnings(WP, MD);
|
2010-03-21 00:06:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97518 91177308-0d34-0410-b5e6-96231b3b80d8
2010-03-02 02:15:13 +03:00
|
|
|
assert(ExprTemporaries.empty() && "Leftover temporaries in function");
|
|
|
|
}
|
|
|
|
|
2010-03-26 01:08:03 +03:00
|
|
|
if (!IsInstantiation)
|
|
|
|
PopDeclContext();
|
|
|
|
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97518 91177308-0d34-0410-b5e6-96231b3b80d8
2010-03-02 02:15:13 +03:00
|
|
|
PopFunctionOrBlockScope();
|
2009-11-13 22:21:49 +03:00
|
|
|
|
2009-11-15 10:07:58 +03:00
|
|
|
// If any errors have occurred, clear out any temporaries that may have
|
|
|
|
// been leftover. This ensures that these temporaries won't be picked up for
|
|
|
|
// deletion in some later function.
|
2010-03-01 23:59:53 +03:00
|
|
|
if (getDiagnostics().hasErrorOccurred())
|
2009-11-15 10:07:58 +03:00
|
|
|
ExprTemporaries.clear();
|
2010-08-13 22:42:17 +04:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
return dcl;
|
2007-11-10 19:31:34 +03:00
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
|
|
|
|
/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
|
2009-09-09 19:08:12 +04:00
|
|
|
NamedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc,
|
2009-01-20 04:17:11 +03:00
|
|
|
IdentifierInfo &II, Scope *S) {
|
2009-03-02 03:19:53 +03:00
|
|
|
// Before we produce a declaration for an implicitly defined
|
|
|
|
// function, see whether there was a locally-scoped declaration of
|
|
|
|
// this name as a function or variable. If so, use that
|
|
|
|
// (non-visible) declaration, and complain about it.
|
|
|
|
llvm::DenseMap<DeclarationName, NamedDecl *>::iterator Pos
|
|
|
|
= LocallyScopedExternalDecls.find(&II);
|
|
|
|
if (Pos != LocallyScopedExternalDecls.end()) {
|
|
|
|
Diag(Loc, diag::warn_use_out_of_scope_declaration) << Pos->second;
|
|
|
|
Diag(Pos->second->getLocation(), diag::note_previous_declaration);
|
|
|
|
return Pos->second;
|
|
|
|
}
|
|
|
|
|
2008-05-06 01:18:06 +04:00
|
|
|
// Extension in C99. Legal in C90, but warn about it.
|
2009-10-19 01:17:35 +04:00
|
|
|
if (II.getName().startswith("__builtin_"))
|
2009-09-29 01:14:19 +04:00
|
|
|
Diag(Loc, diag::warn_builtin_unknown) << &II;
|
|
|
|
else if (getLangOptions().C99)
|
2008-11-19 11:23:25 +03:00
|
|
|
Diag(Loc, diag::ext_implicit_function_decl) << &II;
|
2008-05-06 01:18:06 +04:00
|
|
|
else
|
2008-11-19 11:23:25 +03:00
|
|
|
Diag(Loc, diag::warn_implicit_function_decl) << &II;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Set a Declarator for the implicit definition: int foo();
|
|
|
|
const char *Dummy;
|
|
|
|
DeclSpec DS;
|
2009-08-04 00:12:06 +04:00
|
|
|
unsigned DiagID;
|
|
|
|
bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy, DiagID);
|
2007-07-11 21:01:13 +04:00
|
|
|
Error = Error; // Silence warning.
|
|
|
|
assert(!Error && "Error setting up implicit decl!");
|
|
|
|
Declarator D(DS, Declarator::BlockContext);
|
2009-04-29 21:30:04 +04:00
|
|
|
D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, SourceLocation(), 0,
|
2009-05-31 15:47:27 +04:00
|
|
|
0, 0, false, SourceLocation(),
|
2009-08-20 03:14:54 +04:00
|
|
|
false, 0,0,0, Loc, Loc, D),
|
2009-02-09 21:23:29 +03:00
|
|
|
SourceLocation());
|
2007-07-11 21:01:13 +04:00
|
|
|
D.SetIdentifier(&II, Loc);
|
2009-02-09 21:23:29 +03:00
|
|
|
|
2008-05-02 01:04:16 +04:00
|
|
|
// Insert this function into translation-unit scope.
|
|
|
|
|
|
|
|
DeclContext *PrevDC = CurContext;
|
|
|
|
CurContext = Context.getTranslationUnitDecl();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
FunctionDecl *FD = dyn_cast<FunctionDecl>(ActOnDeclarator(TUScope, D));
|
2008-04-04 18:32:09 +04:00
|
|
|
FD->setImplicit();
|
2008-05-02 01:04:16 +04:00
|
|
|
|
|
|
|
CurContext = PrevDC;
|
|
|
|
|
2009-02-14 21:57:46 +03:00
|
|
|
AddKnownFunctionAttributes(FD);
|
|
|
|
|
2008-04-04 18:32:09 +04:00
|
|
|
return FD;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-02-14 21:57:46 +03:00
|
|
|
/// \brief Adds any function attributes that we know a priori based on
|
|
|
|
/// the declaration of this function.
|
|
|
|
///
|
|
|
|
/// These attributes can apply both to implicitly-declared builtins
|
|
|
|
/// (like __builtin___printf_chk) or to library-declared functions
|
|
|
|
/// like NSLog or printf.
|
|
|
|
void Sema::AddKnownFunctionAttributes(FunctionDecl *FD) {
|
|
|
|
if (FD->isInvalidDecl())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If this is a built-in function, map its builtin attributes to
|
|
|
|
// actual attributes.
|
2009-09-12 04:22:50 +04:00
|
|
|
if (unsigned BuiltinID = FD->getBuiltinID()) {
|
2009-02-14 21:57:46 +03:00
|
|
|
// Handle printf-formatting attributes.
|
|
|
|
unsigned FormatIdx;
|
|
|
|
bool HasVAListArg;
|
|
|
|
if (Context.BuiltinInfo.isPrintfLike(BuiltinID, FormatIdx, HasVAListArg)) {
|
2009-06-30 06:34:44 +04:00
|
|
|
if (!FD->getAttr<FormatAttr>())
|
2010-08-19 03:23:40 +04:00
|
|
|
FD->addAttr(::new (Context) FormatAttr(FD->getLocation(), Context,
|
|
|
|
"printf", FormatIdx+1,
|
2010-02-11 08:28:37 +03:00
|
|
|
HasVAListArg ? 0 : FormatIdx+2));
|
2009-02-14 21:57:46 +03:00
|
|
|
}
|
2010-07-16 06:11:15 +04:00
|
|
|
if (Context.BuiltinInfo.isScanfLike(BuiltinID, FormatIdx,
|
|
|
|
HasVAListArg)) {
|
|
|
|
if (!FD->getAttr<FormatAttr>())
|
2010-08-19 03:23:40 +04:00
|
|
|
FD->addAttr(::new (Context) FormatAttr(FD->getLocation(), Context,
|
|
|
|
"scanf", FormatIdx+1,
|
2010-07-16 06:11:15 +04:00
|
|
|
HasVAListArg ? 0 : FormatIdx+2));
|
|
|
|
}
|
2009-02-17 01:43:43 +03:00
|
|
|
|
|
|
|
// Mark const if we don't care about errno and that is the only
|
|
|
|
// thing preventing the function from being const. This allows
|
|
|
|
// IRgen to use LLVM intrinsics for such functions.
|
|
|
|
if (!getLangOptions().MathErrno &&
|
|
|
|
Context.BuiltinInfo.isConstWithoutErrno(BuiltinID)) {
|
2009-06-30 06:34:44 +04:00
|
|
|
if (!FD->getAttr<ConstAttr>())
|
2010-08-19 03:23:40 +04:00
|
|
|
FD->addAttr(::new (Context) ConstAttr(FD->getLocation(), Context));
|
2009-02-17 01:43:43 +03:00
|
|
|
}
|
2009-07-27 23:14:18 +04:00
|
|
|
|
|
|
|
if (Context.BuiltinInfo.isNoReturn(BuiltinID))
|
2010-02-06 00:31:56 +03:00
|
|
|
FD->setType(Context.getNoReturnType(FD->getType()));
|
2009-12-31 01:06:22 +03:00
|
|
|
if (Context.BuiltinInfo.isNoThrow(BuiltinID))
|
2010-08-19 03:23:40 +04:00
|
|
|
FD->addAttr(::new (Context) NoThrowAttr(FD->getLocation(), Context));
|
2009-12-31 01:06:22 +03:00
|
|
|
if (Context.BuiltinInfo.isConst(BuiltinID))
|
2010-08-19 03:23:40 +04:00
|
|
|
FD->addAttr(::new (Context) ConstAttr(FD->getLocation(), Context));
|
2009-02-14 21:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
IdentifierInfo *Name = FD->getIdentifier();
|
|
|
|
if (!Name)
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
if ((!getLangOptions().CPlusPlus &&
|
2009-02-14 21:57:46 +03:00
|
|
|
FD->getDeclContext()->isTranslationUnit()) ||
|
|
|
|
(isa<LinkageSpecDecl>(FD->getDeclContext()) &&
|
2009-09-09 19:08:12 +04:00
|
|
|
cast<LinkageSpecDecl>(FD->getDeclContext())->getLanguage() ==
|
2009-02-14 21:57:46 +03:00
|
|
|
LinkageSpecDecl::lang_c)) {
|
|
|
|
// Okay: this could be a libc/libm/Objective-C function we know
|
|
|
|
// about.
|
|
|
|
} else
|
|
|
|
return;
|
|
|
|
|
2009-04-23 00:56:09 +04:00
|
|
|
if (Name->isStr("NSLog") || Name->isStr("NSLogv")) {
|
2009-07-28 04:07:08 +04:00
|
|
|
// FIXME: NSLog and NSLogv should be target specific
|
2009-06-30 06:34:44 +04:00
|
|
|
if (const FormatAttr *Format = FD->getAttr<FormatAttr>()) {
|
2009-02-14 21:57:46 +03:00
|
|
|
// FIXME: We known better than our headers.
|
2010-02-11 08:28:37 +03:00
|
|
|
const_cast<FormatAttr *>(Format)->setType(Context, "printf");
|
2009-09-09 19:08:12 +04:00
|
|
|
} else
|
2010-08-19 03:23:40 +04:00
|
|
|
FD->addAttr(::new (Context) FormatAttr(FD->getLocation(), Context,
|
|
|
|
"printf", 1,
|
2009-06-10 08:01:38 +04:00
|
|
|
Name->isStr("NSLogv") ? 0 : 2));
|
2009-04-23 00:56:09 +04:00
|
|
|
} else if (Name->isStr("asprintf") || Name->isStr("vasprintf")) {
|
2009-07-28 04:07:08 +04:00
|
|
|
// FIXME: asprintf and vasprintf aren't C99 functions. Should they be
|
2009-09-09 19:08:12 +04:00
|
|
|
// target-specific builtins, perhaps?
|
2009-06-30 06:34:44 +04:00
|
|
|
if (!FD->getAttr<FormatAttr>())
|
2010-08-19 03:23:40 +04:00
|
|
|
FD->addAttr(::new (Context) FormatAttr(FD->getLocation(), Context,
|
|
|
|
"printf", 2,
|
2009-06-10 08:01:38 +04:00
|
|
|
Name->isStr("vasprintf") ? 0 : 3));
|
2009-07-28 06:25:19 +04:00
|
|
|
}
|
2009-02-14 21:57:46 +03:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2009-10-24 12:00:42 +04:00
|
|
|
TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T,
|
2009-12-07 05:54:59 +03:00
|
|
|
TypeSourceInfo *TInfo) {
|
2007-07-11 21:01:13 +04:00
|
|
|
assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
|
2007-08-29 00:14:24 +04:00
|
|
|
assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-12-07 05:54:59 +03:00
|
|
|
if (!TInfo) {
|
2009-10-24 12:00:42 +04:00
|
|
|
assert(D.isInvalidType() && "no declarator info for valid type");
|
2009-12-07 05:54:59 +03:00
|
|
|
TInfo = Context.getTrivialTypeSourceInfo(T);
|
2009-10-24 12:00:42 +04:00
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Scope manipulation handled by caller.
|
2008-04-04 10:12:32 +04:00
|
|
|
TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext,
|
|
|
|
D.getIdentifierLoc(),
|
2009-09-09 19:08:12 +04:00
|
|
|
D.getIdentifier(),
|
2009-12-07 05:54:59 +03:00
|
|
|
TInfo);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-05 10:31:47 +04:00
|
|
|
if (const TagType *TT = T->getAs<TagType>()) {
|
2009-03-10 20:07:44 +03:00
|
|
|
TagDecl *TD = TT->getDecl();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-10 20:07:44 +03:00
|
|
|
// If the TagDecl that the TypedefDecl points to is an anonymous decl
|
|
|
|
// keep track of the TypedefDecl.
|
|
|
|
if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
|
|
|
|
TD->setTypedefForAnonDecl(NewTD);
|
|
|
|
}
|
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
if (D.isInvalidType())
|
2007-08-29 00:14:24 +04:00
|
|
|
NewTD->setInvalidDecl();
|
|
|
|
return NewTD;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-05-14 20:41:31 +04:00
|
|
|
|
|
|
|
/// \brief Determine whether a tag with a given kind is acceptable
|
|
|
|
/// as a redeclaration of the given tag declaration.
|
|
|
|
///
|
|
|
|
/// \returns true if the new tag kind is acceptable, false otherwise.
|
2009-09-09 19:08:12 +04:00
|
|
|
bool Sema::isAcceptableTagRedeclaration(const TagDecl *Previous,
|
2010-05-12 01:36:43 +04:00
|
|
|
TagTypeKind NewTag,
|
2009-05-14 20:41:31 +04:00
|
|
|
SourceLocation NewTagLoc,
|
|
|
|
const IdentifierInfo &Name) {
|
|
|
|
// C++ [dcl.type.elab]p3:
|
|
|
|
// The class-key or enum keyword present in the
|
|
|
|
// elaborated-type-specifier shall agree in kind with the
|
2010-05-12 01:36:43 +04:00
|
|
|
// declaration to which the name in the elaborated-type-specifier
|
2009-05-14 20:41:31 +04:00
|
|
|
// refers. This rule also applies to the form of
|
|
|
|
// elaborated-type-specifier that declares a class-name or
|
|
|
|
// friend class since it can be construed as referring to the
|
|
|
|
// definition of the class. Thus, in any
|
|
|
|
// elaborated-type-specifier, the enum keyword shall be used to
|
2010-05-12 01:36:43 +04:00
|
|
|
// refer to an enumeration (7.2), the union class-key shall be
|
2009-05-14 20:41:31 +04:00
|
|
|
// used to refer to a union (clause 9), and either the class or
|
|
|
|
// struct class-key shall be used to refer to a class (clause 9)
|
|
|
|
// declared using the class or struct class-key.
|
2010-05-12 01:36:43 +04:00
|
|
|
TagTypeKind OldTag = Previous->getTagKind();
|
2009-05-14 20:41:31 +04:00
|
|
|
if (OldTag == NewTag)
|
|
|
|
return true;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-05-12 01:36:43 +04:00
|
|
|
if ((OldTag == TTK_Struct || OldTag == TTK_Class) &&
|
|
|
|
(NewTag == TTK_Struct || NewTag == TTK_Class)) {
|
2009-05-14 20:41:31 +04:00
|
|
|
// Warn about the struct/class tag mismatch.
|
|
|
|
bool isTemplate = false;
|
|
|
|
if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Previous))
|
|
|
|
isTemplate = Record->getDescribedClassTemplate();
|
|
|
|
|
|
|
|
Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch)
|
2010-05-12 01:36:43 +04:00
|
|
|
<< (NewTag == TTK_Class)
|
2009-05-14 20:41:31 +04:00
|
|
|
<< isTemplate << &Name
|
2010-03-31 21:46:05 +04:00
|
|
|
<< FixItHint::CreateReplacement(SourceRange(NewTagLoc),
|
2010-05-12 01:36:43 +04:00
|
|
|
OldTag == TTK_Class? "class" : "struct");
|
2009-05-14 20:41:31 +04:00
|
|
|
Diag(Previous->getLocation(), diag::note_previous_use);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-09-15 22:49:24 +04:00
|
|
|
/// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the
|
2007-07-11 21:01:13 +04:00
|
|
|
/// former case, Name will be non-null. In the later case, Name will be null.
|
2009-07-31 06:45:11 +04:00
|
|
|
/// TagSpec indicates what kind of tag this is. TUK indicates whether this is a
|
2007-07-11 21:01:13 +04:00
|
|
|
/// reference/declaration/definition of a tag.
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
|
2010-09-17 03:58:57 +04:00
|
|
|
SourceLocation KWLoc, CXXScopeSpec &SS,
|
|
|
|
IdentifierInfo *Name, SourceLocation NameLoc,
|
|
|
|
AttributeList *Attr, AccessSpecifier AS,
|
|
|
|
MultiTemplateParamsArg TemplateParameterLists,
|
2010-10-09 03:50:27 +04:00
|
|
|
bool &OwnedDecl, bool &IsDependent, bool ScopedEnum,
|
|
|
|
TypeResult UnderlyingType) {
|
2008-12-15 19:32:14 +03:00
|
|
|
// If this is not a definition, it must have a name.
|
2009-07-31 06:45:11 +04:00
|
|
|
assert((Name != 0 || TUK == TUK_Definition) &&
|
2007-07-11 21:01:13 +04:00
|
|
|
"Nameless record must be a definition!");
|
2009-02-04 22:02:06 +03:00
|
|
|
|
When we parse a tag specifier, keep track of whether that tag
specifier resulted in the creation of a new TagDecl node, which
happens either when the tag specifier was a definition or when the tag
specifier was the first declaration of that tag type. This information
has several uses, the first of which is implemented in this commit:
1) In C++, one is not allowed to define tag types within a type
specifier (e.g., static_cast<struct S { int x; } *>(0) is
ill-formed) or within the result or parameter types of a
function. We now diagnose this.
2) We can extend DeclGroups to contain information about any tags
that are declared/defined within the declaration specifiers of a
variable, e.g.,
struct Point { int x, y, z; } p;
This will help improve AST printing and template instantiation,
among other things.
3) For C99, we can keep track of whether a tag type is defined
within the type of a parameter, to properly cope with cases like,
e.g.,
int bar(struct T2 { int x; } y) {
struct T2 z;
}
We can also do similar things wherever there is a type specifier,
e.g., to keep track of where the definition of S occurs in this
legal C99 code:
(struct S { int x, y; } *)0
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72555 91177308-0d34-0410-b5e6-96231b3b80d8
2009-05-29 03:31:59 +04:00
|
|
|
OwnedDecl = false;
|
2010-05-12 01:36:43 +04:00
|
|
|
TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-10-08 02:35:40 +04:00
|
|
|
// FIXME: Check explicit specializations more carefully.
|
|
|
|
bool isExplicitSpecialization = false;
|
2010-06-12 12:15:14 +04:00
|
|
|
unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
|
2010-07-15 03:14:12 +04:00
|
|
|
bool Invalid = false;
|
2009-07-31 06:45:11 +04:00
|
|
|
if (TUK != TUK_Reference) {
|
2009-07-23 03:48:44 +04:00
|
|
|
if (TemplateParameterList *TemplateParams
|
|
|
|
= MatchTemplateParametersToScopeSpecifier(KWLoc, SS,
|
|
|
|
(TemplateParameterList**)TemplateParameterLists.get(),
|
2009-10-08 02:35:40 +04:00
|
|
|
TemplateParameterLists.size(),
|
2010-04-14 00:37:33 +04:00
|
|
|
TUK == TUK_Friend,
|
2010-07-15 03:14:12 +04:00
|
|
|
isExplicitSpecialization,
|
|
|
|
Invalid)) {
|
2010-06-12 12:15:14 +04:00
|
|
|
// All but one template parameter lists have been matching.
|
|
|
|
--NumMatchedTemplateParamLists;
|
|
|
|
|
2009-09-26 10:47:28 +04:00
|
|
|
if (TemplateParams->size() > 0) {
|
2009-07-23 03:48:44 +04:00
|
|
|
// This is a declaration or definition of a class template (which may
|
|
|
|
// be a member of another template).
|
2010-07-15 03:14:12 +04:00
|
|
|
if (Invalid)
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2010-07-15 03:14:12 +04:00
|
|
|
|
2009-07-23 03:48:44 +04:00
|
|
|
OwnedDecl = false;
|
2009-07-31 06:45:11 +04:00
|
|
|
DeclResult Result = CheckClassTemplate(S, TagSpec, TUK, KWLoc,
|
2009-07-23 03:48:44 +04:00
|
|
|
SS, Name, NameLoc, Attr,
|
2009-08-25 21:23:04 +04:00
|
|
|
TemplateParams,
|
2009-07-23 03:48:44 +04:00
|
|
|
AS);
|
2009-08-25 21:23:04 +04:00
|
|
|
TemplateParameterLists.release();
|
2009-07-23 03:48:44 +04:00
|
|
|
return Result.get();
|
|
|
|
} else {
|
2009-10-08 19:14:33 +04:00
|
|
|
// The "template<>" header is extraneous.
|
|
|
|
Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams)
|
2010-05-12 01:36:43 +04:00
|
|
|
<< TypeWithKeyword::getTagTypeKindName(Kind) << Name;
|
2009-10-08 19:14:33 +04:00
|
|
|
isExplicitSpecialization = true;
|
2009-07-23 03:48:44 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-09 03:50:27 +04:00
|
|
|
// Figure out the underlying type if this a enum declaration. We need to do
|
|
|
|
// this early, because it's needed to detect if this is an incompatible
|
|
|
|
// redeclaration.
|
|
|
|
llvm::PointerUnion<const Type*, TypeSourceInfo*> EnumUnderlying;
|
|
|
|
|
|
|
|
if (Kind == TTK_Enum) {
|
|
|
|
if (UnderlyingType.isInvalid() || (!UnderlyingType.get() && ScopedEnum))
|
|
|
|
// No underlying type explicitly specified, or we failed to parse the
|
|
|
|
// type, default to int.
|
|
|
|
EnumUnderlying = Context.IntTy.getTypePtr();
|
|
|
|
else if (UnderlyingType.get()) {
|
|
|
|
// C++0x 7.2p2: The type-specifier-seq of an enum-base shall name an
|
|
|
|
// integral type; any cv-qualification is ignored.
|
|
|
|
TypeSourceInfo *TI = 0;
|
|
|
|
QualType T = GetTypeFromParser(UnderlyingType.get(), &TI);
|
|
|
|
EnumUnderlying = TI;
|
|
|
|
|
|
|
|
SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
|
|
|
|
|
|
|
|
if (!T->isDependentType() && !T->isIntegralType(Context)) {
|
|
|
|
Diag(UnderlyingLoc, diag::err_enum_invalid_underlying)
|
|
|
|
<< T;
|
|
|
|
// Recover by falling back to int.
|
|
|
|
EnumUnderlying = Context.IntTy.getTypePtr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-13 01:49:06 +03:00
|
|
|
DeclContext *SearchDC = CurContext;
|
2008-11-10 01:53:32 +03:00
|
|
|
DeclContext *DC = CurContext;
|
2009-09-16 02:30:29 +04:00
|
|
|
bool isStdBadAlloc = false;
|
2009-01-17 03:42:38 +03:00
|
|
|
|
2010-03-02 00:17:36 +03:00
|
|
|
RedeclarationKind Redecl = ForRedeclaration;
|
|
|
|
if (TUK == TUK_Friend || TUK == TUK_Reference)
|
|
|
|
Redecl = NotForRedeclaration;
|
2009-11-19 01:49:29 +03:00
|
|
|
|
|
|
|
LookupResult Previous(*this, Name, NameLoc, LookupTagName, Redecl);
|
2009-10-10 09:48:19 +04:00
|
|
|
|
2008-11-08 20:17:31 +03:00
|
|
|
if (Name && SS.isNotEmpty()) {
|
2008-11-10 01:09:58 +03:00
|
|
|
// We have a nested-name tag ('struct foo::bar').
|
|
|
|
|
|
|
|
// Check for invalid 'foo::'.
|
2008-11-10 01:53:32 +03:00
|
|
|
if (SS.isInvalid()) {
|
2008-11-08 20:17:31 +03:00
|
|
|
Name = 0;
|
|
|
|
goto CreateNewDecl;
|
|
|
|
}
|
|
|
|
|
2009-09-11 08:59:25 +04:00
|
|
|
// If this is a friend or a reference to a class in a dependent
|
|
|
|
// context, don't try to make a decl for it.
|
|
|
|
if (TUK == TUK_Friend || TUK == TUK_Reference) {
|
|
|
|
DC = computeDeclContext(SS, false);
|
|
|
|
if (!DC) {
|
|
|
|
IsDependent = true;
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2009-09-11 08:59:25 +04:00
|
|
|
}
|
2010-05-01 04:40:08 +04:00
|
|
|
} else {
|
|
|
|
DC = computeDeclContext(SS, true);
|
|
|
|
if (!DC) {
|
|
|
|
Diag(SS.getRange().getBegin(), diag::err_dependent_nested_name_spec)
|
|
|
|
<< SS.getRange();
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2010-05-01 04:40:08 +04:00
|
|
|
}
|
2009-09-11 08:59:25 +04:00
|
|
|
}
|
|
|
|
|
2010-05-01 04:40:08 +04:00
|
|
|
if (RequireCompleteDeclContext(SS, DC))
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2009-05-14 04:28:11 +04:00
|
|
|
|
2009-02-03 03:34:39 +03:00
|
|
|
SearchDC = DC;
|
2008-11-10 01:53:32 +03:00
|
|
|
// Look-up name inside 'foo::'.
|
2009-11-19 01:49:29 +03:00
|
|
|
LookupQualifiedName(Previous, DC);
|
2009-10-10 09:48:19 +04:00
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Previous.isAmbiguous())
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2009-10-10 09:48:19 +04:00
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Previous.empty()) {
|
2010-01-14 20:47:39 +03:00
|
|
|
// Name lookup did not find anything. However, if the
|
|
|
|
// nested-name-specifier refers to the current instantiation,
|
|
|
|
// and that current instantiation has any dependent base
|
|
|
|
// classes, we might find something at instantiation time: treat
|
|
|
|
// this as a dependent elaborated-type-specifier.
|
2010-01-15 04:44:47 +03:00
|
|
|
if (Previous.wasNotFoundInCurrentInstantiation()) {
|
2010-01-14 20:47:39 +03:00
|
|
|
IsDependent = true;
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2010-01-14 20:47:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A tag 'foo::bar' must already exist.
|
2010-04-01 03:17:41 +04:00
|
|
|
Diag(NameLoc, diag::err_not_tag_in_scope)
|
|
|
|
<< Kind << Name << DC << SS.getRange();
|
2008-11-08 20:17:31 +03:00
|
|
|
Name = 0;
|
2009-05-27 21:30:49 +04:00
|
|
|
Invalid = true;
|
2008-11-08 20:17:31 +03:00
|
|
|
goto CreateNewDecl;
|
|
|
|
}
|
2009-01-21 05:38:50 +03:00
|
|
|
} else if (Name) {
|
2008-11-10 01:09:58 +03:00
|
|
|
// If this is a named struct, check to see if there was a previous forward
|
|
|
|
// declaration or definition.
|
2009-02-03 22:21:40 +03:00
|
|
|
// FIXME: We're looking into outer scopes here, even when we
|
|
|
|
// shouldn't be. Doing so can result in ambiguities that we
|
|
|
|
// shouldn't be diagnosing.
|
2009-11-19 01:49:29 +03:00
|
|
|
LookupName(Previous, S);
|
|
|
|
|
|
|
|
// Note: there used to be some attempt at recovery here.
|
|
|
|
if (Previous.isAmbiguous())
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
|
2009-07-31 06:45:11 +04:00
|
|
|
if (!getLangOptions().CPlusPlus && TUK != TUK_Reference) {
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
// FIXME: This makes sure that we ignore the contexts associated
|
|
|
|
// with C structs, unions, and enums when looking for a matching
|
|
|
|
// tag declaration or definition. See the similar lookup tweak
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63354 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-30 04:04:22 +03:00
|
|
|
// in Sema::LookupName; is there a better way to deal with this?
|
2009-01-13 01:49:06 +03:00
|
|
|
while (isa<RecordDecl>(SearchDC) || isa<EnumDecl>(SearchDC))
|
|
|
|
SearchDC = SearchDC->getParent();
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
}
|
2010-09-17 03:58:57 +04:00
|
|
|
} else if (S->isFunctionPrototypeScope()) {
|
|
|
|
// If this is an enum declaration in function prototype scope, set its
|
|
|
|
// initial context to the translation unit.
|
|
|
|
SearchDC = Context.getTranslationUnitDecl();
|
2008-11-08 20:17:31 +03:00
|
|
|
}
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Previous.isSingleResult() &&
|
|
|
|
Previous.getFoundDecl()->isTemplateParameter()) {
|
2008-12-05 21:15:24 +03:00
|
|
|
// Maybe we will complain about the shadowed template parameter.
|
2009-11-19 01:49:29 +03:00
|
|
|
DiagnoseTemplateParameterShadow(NameLoc, Previous.getFoundDecl());
|
2008-12-05 21:15:24 +03:00
|
|
|
// Just pretend that we didn't see the previous declaration.
|
2009-11-19 01:49:29 +03:00
|
|
|
Previous.clear();
|
2008-12-05 21:15:24 +03:00
|
|
|
}
|
|
|
|
|
2009-09-16 02:30:29 +04:00
|
|
|
if (getLangOptions().CPlusPlus && Name && DC && StdNamespace &&
|
2010-08-02 11:14:54 +04:00
|
|
|
DC->Equals(getStdNamespace()) && Name->isStr("bad_alloc")) {
|
2009-09-16 02:30:29 +04:00
|
|
|
// This is a declaration of or a reference to "std::bad_alloc".
|
|
|
|
isStdBadAlloc = true;
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Previous.empty() && StdBadAlloc) {
|
2009-09-16 02:30:29 +04:00
|
|
|
// std::bad_alloc has been implicitly declared (but made invisible to
|
|
|
|
// name lookup). Fill in this implicit declaration as the previous
|
|
|
|
// declaration, so that the declarations get chained appropriately.
|
2010-08-02 11:14:54 +04:00
|
|
|
Previous.addDecl(getStdBadAlloc());
|
2009-09-16 02:30:29 +04:00
|
|
|
}
|
|
|
|
}
|
2009-11-19 01:49:29 +03:00
|
|
|
|
2010-03-26 00:28:06 +03:00
|
|
|
// If we didn't find a previous declaration, and this is a reference
|
|
|
|
// (or friend reference), move to the correct scope. In C++, we
|
|
|
|
// also need to do a redeclaration lookup there, just in case
|
|
|
|
// there's a shadow friend decl.
|
|
|
|
if (Name && Previous.empty() &&
|
|
|
|
(TUK == TUK_Reference || TUK == TUK_Friend)) {
|
|
|
|
if (Invalid) goto CreateNewDecl;
|
|
|
|
assert(SS.isEmpty());
|
|
|
|
|
|
|
|
if (TUK == TUK_Reference) {
|
|
|
|
// C++ [basic.scope.pdecl]p5:
|
|
|
|
// -- for an elaborated-type-specifier of the form
|
|
|
|
//
|
|
|
|
// class-key identifier
|
|
|
|
//
|
|
|
|
// if the elaborated-type-specifier is used in the
|
|
|
|
// decl-specifier-seq or parameter-declaration-clause of a
|
|
|
|
// function defined in namespace scope, the identifier is
|
|
|
|
// declared as a class-name in the namespace that contains
|
|
|
|
// the declaration; otherwise, except as a friend
|
|
|
|
// declaration, the identifier is declared in the smallest
|
|
|
|
// non-class, non-function-prototype scope that contains the
|
|
|
|
// declaration.
|
|
|
|
//
|
|
|
|
// C99 6.7.2.3p8 has a similar (but not identical!) provision for
|
|
|
|
// C structs and unions.
|
|
|
|
//
|
|
|
|
// It is an error in C++ to declare (rather than define) an enum
|
|
|
|
// type, including via an elaborated type specifier. We'll
|
|
|
|
// diagnose that later; for now, declare the enum in the same
|
|
|
|
// scope as we would have picked for any other tag type.
|
|
|
|
//
|
|
|
|
// GNU C also supports this behavior as part of its incomplete
|
|
|
|
// enum types extension, while GNU C++ does not.
|
|
|
|
//
|
|
|
|
// Find the context where we'll be declaring the tag.
|
|
|
|
// FIXME: We would like to maintain the current DeclContext as the
|
|
|
|
// lexical context,
|
|
|
|
while (SearchDC->isRecord())
|
|
|
|
SearchDC = SearchDC->getParent();
|
|
|
|
|
|
|
|
// Find the scope where we'll be declaring the tag.
|
|
|
|
while (S->isClassScope() ||
|
|
|
|
(getLangOptions().CPlusPlus &&
|
|
|
|
S->isFunctionPrototypeScope()) ||
|
|
|
|
((S->getFlags() & Scope::DeclScope) == 0) ||
|
|
|
|
(S->getEntity() &&
|
|
|
|
((DeclContext *)S->getEntity())->isTransparentContext()))
|
|
|
|
S = S->getParent();
|
|
|
|
} else {
|
|
|
|
assert(TUK == TUK_Friend);
|
|
|
|
// C++ [namespace.memdef]p3:
|
|
|
|
// If a friend declaration in a non-local class first declares a
|
|
|
|
// class or function, the friend class or function is a member of
|
|
|
|
// the innermost enclosing namespace.
|
|
|
|
SearchDC = SearchDC->getEnclosingNamespaceContext();
|
|
|
|
}
|
|
|
|
|
2010-04-23 22:46:30 +04:00
|
|
|
// In C++, we need to do a redeclaration lookup to properly
|
|
|
|
// diagnose some problems.
|
2010-03-26 00:28:06 +03:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
Previous.setRedeclarationKind(ForRedeclaration);
|
|
|
|
LookupQualifiedName(Previous, SearchDC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (!Previous.empty()) {
|
2010-04-12 20:00:01 +04:00
|
|
|
NamedDecl *PrevDecl = (*Previous.begin())->getUnderlyingDecl();
|
2010-04-23 22:46:30 +04:00
|
|
|
|
|
|
|
// It's okay to have a tag decl in the same scope as a typedef
|
|
|
|
// which hides a tag decl in the same scope. Finding this
|
|
|
|
// insanity with a redeclaration lookup can only actually happen
|
|
|
|
// in C++.
|
|
|
|
//
|
|
|
|
// This is also okay for elaborated-type-specifiers, which is
|
|
|
|
// technically forbidden by the current standard but which is
|
|
|
|
// okay according to the likely resolution of an open issue;
|
|
|
|
// see http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#407
|
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
if (TypedefDecl *TD = dyn_cast<TypedefDecl>(PrevDecl)) {
|
|
|
|
if (const TagType *TT = TD->getUnderlyingType()->getAs<TagType>()) {
|
|
|
|
TagDecl *Tag = TT->getDecl();
|
|
|
|
if (Tag->getDeclName() == Name &&
|
2010-08-31 04:36:30 +04:00
|
|
|
Tag->getDeclContext()->getRedeclContext()
|
|
|
|
->Equals(TD->getDeclContext()->getRedeclContext())) {
|
2010-04-23 22:46:30 +04:00
|
|
|
PrevDecl = Tag;
|
|
|
|
Previous.clear();
|
|
|
|
Previous.addDecl(Tag);
|
2010-08-28 02:55:10 +04:00
|
|
|
Previous.resolveKind();
|
2010-04-23 22:46:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-27 17:50:30 +04:00
|
|
|
if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) {
|
2008-07-03 07:30:58 +04:00
|
|
|
// If this is a use of a previous tag, or if the tag is already declared
|
|
|
|
// in the same scope (so that the definition/declaration completes or
|
2008-04-27 17:50:30 +04:00
|
|
|
// rementions the tag), reuse the decl.
|
2009-08-06 06:15:43 +04:00
|
|
|
if (TUK == TUK_Reference || TUK == TUK_Friend ||
|
|
|
|
isDeclInScope(PrevDecl, SearchDC, S)) {
|
2008-07-03 07:30:58 +04:00
|
|
|
// Make sure that this wasn't declared as an enum and now used as a
|
|
|
|
// struct or something similar.
|
2009-05-14 20:41:31 +04:00
|
|
|
if (!isAcceptableTagRedeclaration(PrevTagDecl, Kind, KWLoc, *Name)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
bool SafeToContinue
|
2010-05-12 01:36:43 +04:00
|
|
|
= (PrevTagDecl->getTagKind() != TTK_Enum &&
|
|
|
|
Kind != TTK_Enum);
|
2009-04-02 03:51:29 +04:00
|
|
|
if (SafeToContinue)
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(KWLoc, diag::err_use_with_wrong_tag)
|
2009-04-02 03:51:29 +04:00
|
|
|
<< Name
|
2010-03-31 21:46:05 +04:00
|
|
|
<< FixItHint::CreateReplacement(SourceRange(KWLoc),
|
|
|
|
PrevTagDecl->getKindName());
|
2009-04-02 03:51:29 +04:00
|
|
|
else
|
|
|
|
Diag(KWLoc, diag::err_use_with_wrong_tag) << Name;
|
2009-11-19 01:49:29 +03:00
|
|
|
Diag(PrevTagDecl->getLocation(), diag::note_previous_use);
|
2009-04-02 03:51:29 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
if (SafeToContinue)
|
2009-04-02 03:51:29 +04:00
|
|
|
Kind = PrevTagDecl->getTagKind();
|
|
|
|
else {
|
|
|
|
// Recover by making this an anonymous redefinition.
|
|
|
|
Name = 0;
|
2009-11-19 01:49:29 +03:00
|
|
|
Previous.clear();
|
2009-04-02 03:51:29 +04:00
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-09 03:50:27 +04:00
|
|
|
if (Kind == TTK_Enum && PrevTagDecl->getTagKind() == TTK_Enum) {
|
|
|
|
const EnumDecl *PrevEnum = cast<EnumDecl>(PrevTagDecl);
|
|
|
|
|
|
|
|
// All conflicts with previous declarations are recovered by
|
|
|
|
// returning the previous declaration.
|
|
|
|
if (ScopedEnum != PrevEnum->isScoped()) {
|
|
|
|
Diag(KWLoc, diag::err_enum_redeclare_scoped_mismatch)
|
|
|
|
<< PrevEnum->isScoped();
|
|
|
|
Diag(PrevTagDecl->getLocation(), diag::note_previous_use);
|
|
|
|
return PrevTagDecl;
|
|
|
|
}
|
|
|
|
else if (EnumUnderlying && PrevEnum->isFixed()) {
|
|
|
|
QualType T;
|
|
|
|
if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>())
|
|
|
|
T = TI->getType();
|
|
|
|
else
|
|
|
|
T = QualType(EnumUnderlying.get<const Type*>(), 0);
|
|
|
|
|
|
|
|
if (!Context.hasSameUnqualifiedType(T, PrevEnum->getIntegerType())) {
|
|
|
|
Diag(KWLoc, diag::err_enum_redeclare_type_mismatch);
|
|
|
|
Diag(PrevTagDecl->getLocation(), diag::note_previous_use);
|
|
|
|
return PrevTagDecl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!EnumUnderlying.isNull() != PrevEnum->isFixed()) {
|
|
|
|
Diag(KWLoc, diag::err_enum_redeclare_fixed_mismatch)
|
|
|
|
<< PrevEnum->isFixed();
|
|
|
|
Diag(PrevTagDecl->getLocation(), diag::note_previous_use);
|
|
|
|
return PrevTagDecl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-02 03:51:29 +04:00
|
|
|
if (!Invalid) {
|
2008-12-15 19:32:14 +03:00
|
|
|
// If this is a use, just return the declaration we found.
|
2008-07-03 07:30:58 +04:00
|
|
|
|
2008-12-15 19:32:14 +03:00
|
|
|
// FIXME: In the future, return a variant or some other clue
|
|
|
|
// for the consumer of this Decl to know it doesn't own it.
|
|
|
|
// For our current ASTs this shouldn't be a problem, but will
|
|
|
|
// need to be changed with DeclGroups.
|
2010-06-09 01:27:36 +04:00
|
|
|
if ((TUK == TUK_Reference && !PrevTagDecl->getFriendObjectKind()) ||
|
|
|
|
TUK == TUK_Friend)
|
2010-08-21 13:40:31 +04:00
|
|
|
return PrevTagDecl;
|
2009-02-04 22:02:06 +03:00
|
|
|
|
2008-07-03 07:30:58 +04:00
|
|
|
// Diagnose attempts to redefine a tag.
|
2009-07-31 06:45:11 +04:00
|
|
|
if (TUK == TUK_Definition) {
|
2010-02-11 04:04:33 +03:00
|
|
|
if (TagDecl *Def = PrevTagDecl->getDefinition()) {
|
2009-10-13 00:18:28 +04:00
|
|
|
// If we're defining a specialization and the previous definition
|
|
|
|
// is from an implicit instantiation, don't emit an error
|
|
|
|
// here; we'll catch this in the general case below.
|
|
|
|
if (!isExplicitSpecialization ||
|
|
|
|
!isa<CXXRecordDecl>(Def) ||
|
|
|
|
cast<CXXRecordDecl>(Def)->getTemplateSpecializationKind()
|
|
|
|
== TSK_ExplicitSpecialization) {
|
|
|
|
Diag(NameLoc, diag::err_redefinition) << Name;
|
|
|
|
Diag(Def->getLocation(), diag::note_previous_definition);
|
|
|
|
// If this is a redefinition, recover by making this
|
|
|
|
// struct be anonymous, which will make any later
|
|
|
|
// references get the previous definition.
|
|
|
|
Name = 0;
|
2009-11-19 01:49:29 +03:00
|
|
|
Previous.clear();
|
2009-10-13 00:18:28 +04:00
|
|
|
Invalid = true;
|
|
|
|
}
|
2009-01-17 03:42:38 +03:00
|
|
|
} else {
|
|
|
|
// If the type is currently being defined, complain
|
|
|
|
// about a nested redefinition.
|
|
|
|
TagType *Tag = cast<TagType>(Context.getTagDeclType(PrevTagDecl));
|
|
|
|
if (Tag->isBeingDefined()) {
|
|
|
|
Diag(NameLoc, diag::err_nested_redefinition) << Name;
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(PrevTagDecl->getLocation(),
|
2009-01-17 03:42:38 +03:00
|
|
|
diag::note_previous_definition);
|
|
|
|
Name = 0;
|
2009-11-19 01:49:29 +03:00
|
|
|
Previous.clear();
|
2009-01-17 03:42:38 +03:00
|
|
|
Invalid = true;
|
|
|
|
}
|
2008-12-15 19:32:14 +03:00
|
|
|
}
|
2009-01-17 03:42:38 +03:00
|
|
|
|
2008-07-03 07:30:58 +04:00
|
|
|
// Okay, this is definition of a previously declared or referenced
|
2008-12-15 19:32:14 +03:00
|
|
|
// tag PrevDecl. We're going to create a new Decl for it.
|
2009-01-17 03:42:38 +03:00
|
|
|
}
|
2008-04-27 17:50:30 +04:00
|
|
|
}
|
2008-12-15 19:32:14 +03:00
|
|
|
// If we get here we have (another) forward declaration or we
|
2009-08-06 06:15:43 +04:00
|
|
|
// have a definition. Just create a new decl.
|
|
|
|
|
2008-12-15 19:32:14 +03:00
|
|
|
} else {
|
|
|
|
// If we get here, this is a definition of a new tag type in a nested
|
2009-09-09 19:08:12 +04:00
|
|
|
// scope, e.g. "struct foo; void bar() { struct foo; }", just create a
|
2008-12-15 19:32:14 +03:00
|
|
|
// new decl/type. We set PrevDecl to NULL so that the entities
|
|
|
|
// have distinct types.
|
2009-11-19 01:49:29 +03:00
|
|
|
Previous.clear();
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-12-15 19:32:14 +03:00
|
|
|
// If we get here, we're going to create a new Decl. If PrevDecl
|
|
|
|
// is non-NULL, it's a definition of the tag declared by
|
|
|
|
// PrevDecl. If it's NULL, we have a new definition.
|
2010-04-23 22:46:30 +04:00
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, PrevDecl is not a tag, but was found with tag
|
|
|
|
// lookup. This is only actually possible in C++, where a few
|
|
|
|
// things like templates still live in the tag namespace.
|
2008-04-27 17:50:30 +04:00
|
|
|
} else {
|
2010-04-23 22:46:30 +04:00
|
|
|
assert(getLangOptions().CPlusPlus);
|
|
|
|
|
|
|
|
// Use a better diagnostic if an elaborated-type-specifier
|
|
|
|
// found the wrong kind of type on the first
|
|
|
|
// (non-redeclaration) lookup.
|
|
|
|
if ((TUK == TUK_Reference || TUK == TUK_Friend) &&
|
|
|
|
!Previous.isForRedeclaration()) {
|
|
|
|
unsigned Kind = 0;
|
|
|
|
if (isa<TypedefDecl>(PrevDecl)) Kind = 1;
|
|
|
|
else if (isa<ClassTemplateDecl>(PrevDecl)) Kind = 2;
|
|
|
|
Diag(NameLoc, diag::err_tag_reference_non_tag) << Kind;
|
|
|
|
Diag(PrevDecl->getLocation(), diag::note_declared_at);
|
|
|
|
Invalid = true;
|
|
|
|
|
|
|
|
// Otherwise, only diagnose if the declaration is in scope.
|
|
|
|
} else if (!isDeclInScope(PrevDecl, SearchDC, S)) {
|
|
|
|
// do nothing
|
|
|
|
|
|
|
|
// Diagnose implicit declarations introduced by elaborated types.
|
|
|
|
} else if (TUK == TUK_Reference || TUK == TUK_Friend) {
|
|
|
|
unsigned Kind = 0;
|
|
|
|
if (isa<TypedefDecl>(PrevDecl)) Kind = 1;
|
|
|
|
else if (isa<ClassTemplateDecl>(PrevDecl)) Kind = 2;
|
|
|
|
Diag(NameLoc, diag::err_tag_reference_conflict) << Kind;
|
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl;
|
|
|
|
Invalid = true;
|
|
|
|
|
|
|
|
// Otherwise it's a declaration. Call out a particularly common
|
|
|
|
// case here.
|
|
|
|
} else if (isa<TypedefDecl>(PrevDecl)) {
|
|
|
|
Diag(NameLoc, diag::err_tag_definition_of_typedef)
|
|
|
|
<< Name
|
|
|
|
<< cast<TypedefDecl>(PrevDecl)->getUnderlyingType();
|
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_decl) << PrevDecl;
|
|
|
|
Invalid = true;
|
|
|
|
|
|
|
|
// Otherwise, diagnose.
|
|
|
|
} else {
|
|
|
|
// The tag name clashes with something else in the target scope,
|
|
|
|
// issue an error and recover by making this tag be anonymous.
|
2008-11-19 11:23:25 +03:00
|
|
|
Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
|
2008-11-24 02:12:31 +03:00
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_definition);
|
2008-07-16 11:45:46 +04:00
|
|
|
Name = 0;
|
2009-01-17 03:42:38 +03:00
|
|
|
Invalid = true;
|
2008-07-16 11:45:46 +04:00
|
|
|
}
|
2010-04-23 22:46:30 +04:00
|
|
|
|
|
|
|
// The existing declaration isn't relevant to us; we're in a
|
|
|
|
// new scope, so clear out the previous declaration.
|
|
|
|
Previous.clear();
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
}
|
2008-11-08 20:17:31 +03:00
|
|
|
|
2008-12-17 10:13:27 +03:00
|
|
|
CreateNewDecl:
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
TagDecl *PrevDecl = 0;
|
|
|
|
if (Previous.isSingleResult())
|
|
|
|
PrevDecl = cast<TagDecl>(Previous.getFoundDecl());
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// If there is an identifier, use the location of the identifier as the
|
|
|
|
// location of the decl, otherwise use the location of the struct/union
|
|
|
|
// keyword.
|
|
|
|
SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-12-15 19:32:14 +03:00
|
|
|
// Otherwise, create a new declaration. If there is a previous
|
|
|
|
// declaration of the same entity, the two will be linked via
|
|
|
|
// PrevDecl.
|
2007-07-11 21:01:13 +04:00
|
|
|
TagDecl *New;
|
2009-01-07 03:43:41 +03:00
|
|
|
|
2010-10-09 03:50:27 +04:00
|
|
|
bool IsForwardReference = false;
|
2010-05-12 01:36:43 +04:00
|
|
|
if (Kind == TTK_Enum) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
|
|
|
|
// enum X { A, B, C } D; D should chain to X.
|
2009-07-21 18:46:17 +04:00
|
|
|
New = EnumDecl::Create(Context, SearchDC, Loc, Name, KWLoc,
|
2010-10-09 03:50:27 +04:00
|
|
|
cast_or_null<EnumDecl>(PrevDecl), ScopedEnum,
|
|
|
|
!EnumUnderlying.isNull());
|
2007-07-11 21:01:13 +04:00
|
|
|
// If this is an undefined enum, warn.
|
2010-06-22 18:26:35 +04:00
|
|
|
if (TUK != TUK_Definition && !Invalid) {
|
|
|
|
TagDecl *Def;
|
2010-10-09 03:50:27 +04:00
|
|
|
if (getLangOptions().CPlusPlus0x && cast<EnumDecl>(New)->isFixed()) {
|
|
|
|
// C++0x: 7.2p2: opaque-enum-declaration.
|
|
|
|
// Conflicts are diagnosed above. Do nothing.
|
|
|
|
}
|
|
|
|
else if (PrevDecl && (Def = cast<EnumDecl>(PrevDecl)->getDefinition())) {
|
2010-06-22 18:26:35 +04:00
|
|
|
Diag(Loc, diag::ext_forward_ref_enum_def)
|
|
|
|
<< New;
|
|
|
|
Diag(Def->getLocation(), diag::note_previous_definition);
|
|
|
|
} else {
|
2010-09-12 09:06:55 +04:00
|
|
|
unsigned DiagID = diag::ext_forward_ref_enum;
|
|
|
|
if (getLangOptions().Microsoft)
|
|
|
|
DiagID = diag::ext_ms_forward_ref_enum;
|
|
|
|
else if (getLangOptions().CPlusPlus)
|
|
|
|
DiagID = diag::err_forward_ref_enum;
|
|
|
|
Diag(Loc, DiagID);
|
2010-10-09 03:50:27 +04:00
|
|
|
|
|
|
|
// If this is a forward-declared reference to an enumeration, make a
|
|
|
|
// note of it; we won't actually be introducing the declaration into
|
|
|
|
// the declaration context.
|
|
|
|
if (TUK == TUK_Reference)
|
|
|
|
IsForwardReference = true;
|
2010-06-22 18:26:35 +04:00
|
|
|
}
|
2009-03-06 21:34:03 +03:00
|
|
|
}
|
2010-10-09 03:50:27 +04:00
|
|
|
|
|
|
|
if (EnumUnderlying) {
|
|
|
|
EnumDecl *ED = cast<EnumDecl>(New);
|
|
|
|
if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>())
|
|
|
|
ED->setIntegerTypeSourceInfo(TI);
|
|
|
|
else
|
|
|
|
ED->setIntegerType(QualType(EnumUnderlying.get<const Type*>(), 0));
|
|
|
|
ED->setPromotionType(ED->getIntegerType());
|
|
|
|
}
|
|
|
|
|
2008-06-10 03:19:58 +04:00
|
|
|
} else {
|
|
|
|
// struct/union/class
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
|
|
|
|
// struct X { int A; } D; D should chain to X.
|
2009-09-16 02:30:29 +04:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
2008-09-05 21:39:33 +04:00
|
|
|
// FIXME: Look for a way to use RecordDecl for simple structs.
|
2009-07-21 18:46:17 +04:00
|
|
|
New = CXXRecordDecl::Create(Context, Kind, SearchDC, Loc, Name, KWLoc,
|
2008-12-15 19:32:14 +03:00
|
|
|
cast_or_null<CXXRecordDecl>(PrevDecl));
|
2009-09-16 02:30:29 +04:00
|
|
|
|
2010-08-02 11:14:54 +04:00
|
|
|
if (isStdBadAlloc && (!StdBadAlloc || getStdBadAlloc()->isImplicit()))
|
2009-09-16 02:30:29 +04:00
|
|
|
StdBadAlloc = cast<CXXRecordDecl>(New);
|
|
|
|
} else
|
2009-07-21 18:46:17 +04:00
|
|
|
New = RecordDecl::Create(Context, Kind, SearchDC, Loc, Name, KWLoc,
|
2008-12-15 19:32:14 +03:00
|
|
|
cast_or_null<RecordDecl>(PrevDecl));
|
2008-07-01 14:37:29 +04:00
|
|
|
}
|
2008-12-15 19:32:14 +03:00
|
|
|
|
2010-03-15 13:12:16 +03:00
|
|
|
// Maybe add qualifier info.
|
|
|
|
if (SS.isNotEmpty()) {
|
2010-05-15 01:35:02 +04:00
|
|
|
if (SS.isSet()) {
|
|
|
|
NestedNameSpecifier *NNS
|
|
|
|
= static_cast<NestedNameSpecifier*>(SS.getScopeRep());
|
|
|
|
New->setQualifierInfo(NNS, SS.getRange());
|
2010-06-12 12:15:14 +04:00
|
|
|
if (NumMatchedTemplateParamLists > 0) {
|
2010-06-15 21:44:38 +04:00
|
|
|
New->setTemplateParameterListsInfo(Context,
|
|
|
|
NumMatchedTemplateParamLists,
|
2010-06-12 12:15:14 +04:00
|
|
|
(TemplateParameterList**) TemplateParameterLists.release());
|
|
|
|
}
|
2010-05-15 01:35:02 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Invalid = true;
|
2010-03-15 13:12:16 +03:00
|
|
|
}
|
|
|
|
|
2010-05-27 05:53:40 +04:00
|
|
|
if (RecordDecl *RD = dyn_cast<RecordDecl>(New)) {
|
|
|
|
// Add alignment attributes if necessary; these attributes are checked when
|
|
|
|
// the ASTContext lays out the structure.
|
2008-12-15 19:32:14 +03:00
|
|
|
//
|
|
|
|
// It is important for implementing the correct semantics that this
|
|
|
|
// happen here (in act on tag decl). The #pragma pack stack is
|
|
|
|
// maintained as a result of parser callbacks which can occur at
|
|
|
|
// many points during the parsing of a struct declaration (because
|
|
|
|
// the #pragma tokens are effectively skipped over during the
|
|
|
|
// parsing of the struct).
|
2010-05-27 05:53:40 +04:00
|
|
|
AddAlignmentAttributesForRecord(RD);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-12-15 19:32:14 +03:00
|
|
|
|
2009-10-08 19:14:33 +04:00
|
|
|
// If this is a specialization of a member class (of a class template),
|
|
|
|
// check the specialization.
|
2009-11-19 01:49:29 +03:00
|
|
|
if (isExplicitSpecialization && CheckMemberSpecialization(New, Previous))
|
2009-10-08 19:14:33 +04:00
|
|
|
Invalid = true;
|
2010-05-27 05:53:40 +04:00
|
|
|
|
2009-01-17 03:42:38 +03:00
|
|
|
if (Invalid)
|
|
|
|
New->setInvalidDecl();
|
|
|
|
|
2008-06-29 03:58:55 +04:00
|
|
|
if (Attr)
|
2009-06-18 01:51:59 +04:00
|
|
|
ProcessDeclAttributeList(S, New, Attr);
|
2008-11-10 02:41:00 +03:00
|
|
|
|
2009-01-17 03:42:38 +03:00
|
|
|
// If we're declaring or defining a tag in function prototype scope
|
|
|
|
// in C, note that this type can only be used within the function.
|
2009-01-10 01:42:13 +03:00
|
|
|
if (Name && S->isFunctionPrototypeScope() && !getLangOptions().CPlusPlus)
|
|
|
|
Diag(Loc, diag::warn_decl_in_param_list) << Context.getTagDeclType(New);
|
|
|
|
|
2008-11-10 02:41:00 +03:00
|
|
|
// Set the lexical context. If the tag has a C++ scope specifier, the
|
|
|
|
// lexical context will be different from the semantic context.
|
2009-02-03 03:34:39 +03:00
|
|
|
New->setLexicalDeclContext(CurContext);
|
2009-01-17 03:42:38 +03:00
|
|
|
|
2009-08-28 11:59:38 +04:00
|
|
|
// Mark this as a friend decl if applicable.
|
|
|
|
if (TUK == TUK_Friend)
|
2009-11-19 01:49:29 +03:00
|
|
|
New->setObjectOfFriendDecl(/* PreviouslyDeclared = */ !Previous.empty());
|
2009-08-28 11:59:38 +04:00
|
|
|
|
2009-03-26 04:19:02 +03:00
|
|
|
// Set the access specifier.
|
2010-03-26 00:28:06 +03:00
|
|
|
if (!Invalid && SearchDC->isRecord())
|
2009-05-27 21:30:49 +04:00
|
|
|
SetMemberAccessSpecifier(New, PrevDecl, AS);
|
2009-03-26 01:00:53 +03:00
|
|
|
|
2009-07-31 06:45:11 +04:00
|
|
|
if (TUK == TUK_Definition)
|
2009-01-17 03:42:38 +03:00
|
|
|
New->startDefinition();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
Change struct forward declarations and definitions to use unique RecordDecls, as opposed to creating a single RecordDecl and reusing it.
This change effects both RecordDecls and CXXRecordDecls, but does not effect EnumDecls (yet).
The motivation of this patch is as follows:
- Capture more source information, necessary for refactoring/rewriting clients.
- Pave the way to resolve ownership issues with RecordDecls with the forthcoming
addition of DeclGroups.
Current caveats:
- Until DeclGroups are in place, we will leak RecordDecls not explicitly
referenced by the AST. For example:
typedef struct { ... } x;
The RecordDecl for the struct will be leaked because the TypedefDecl doesn't
refer to it. This will be solved with DeclGroups.
- This patch also (temporarily) breaks CodeGen. More below.
High-level changes:
- As before, TagType still refers to a TagDecl, but it doesn't own it. When
a struct/union/class is first referenced, a RecordType and RecordDecl are
created for it, and the RecordType refers to that RecordDecl. Later, if
a new RecordDecl is created, the pointer to a RecordDecl in RecordType is
updated to point to the RecordDecl that defines the struct/union/class.
- TagDecl and RecordDecl now how a method 'getDefinition()' to return the
TagDecl*/RecordDecl* that refers to the TagDecl* that defines a particular
enum/struct/class/union. This is useful from going from a RecordDecl* that
defines a forward declaration to the RecordDecl* that provides the actual
definition. Note that this also works for EnumDecls, except that in this case
there is no distinction between forward declarations and definitions (yet).
- Clients should no longer assume that 'isDefinition()' returns true from a
RecordDecl if the corresponding struct/union/class has been defined.
isDefinition() only returns true if a particular RecordDecl is the defining
Decl. Use 'getDefinition()' instead to determine if a struct has been defined.
- The main changes to Sema happen in ActOnTag. To make the changes more
incremental, I split off the processing of enums and structs et al into two
code paths. Enums use the original code path (which is in ActOnTag) and
structs use the ActOnTagStruct. Eventually the two code paths will be merged,
but the idea was to preserve the original logic both for comparison and not to
change the logic for both enums and structs all at once.
- There is NO CHAINING of RecordDecls for the same RecordType. All RecordDecls
that correspond to the same type simply have a pointer to that type. If we
need to figure out what are all the RecordDecls for a given type we can build
a backmap.
- The diff in CXXRecordDecl.[cpp,h] is actually very small; it just mimics the
changes to RecordDecl. For some reason 'svn' marks the entire file as changed.
Why is CodeGen broken:
- Codegen assumes that there is an equivalence between RecordDecl* and
RecordType*. This was true before because we only created one RecordDecl* for
a given RecordType*, but it is no longer true. I believe this shouldn't be too
hard to change, but the patch was big enough as it is.
I have tested this patch on both the clang test suite, and by running the static analyzer over Postgresql and a large Apple-internal project (mix of Objective-C and C).
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55839 91177308-0d34-0410-b5e6-96231b3b80d8
2008-09-05 21:16:31 +04:00
|
|
|
// If this has an identifier, add it to the scope stack.
|
2009-09-02 04:55:30 +04:00
|
|
|
if (TUK == TUK_Friend) {
|
2009-09-02 23:32:14 +04:00
|
|
|
// We might be replacing an existing declaration in the lookup tables;
|
|
|
|
// if so, borrow its access specifier.
|
|
|
|
if (PrevDecl)
|
|
|
|
New->setAccess(PrevDecl->getAccess());
|
|
|
|
|
2010-08-31 04:36:30 +04:00
|
|
|
DeclContext *DC = New->getDeclContext()->getRedeclContext();
|
2010-03-26 00:28:06 +03:00
|
|
|
DC->makeDeclVisibleInContext(New, /* Recoverable = */ false);
|
|
|
|
if (Name) // can be null along some error paths
|
2009-09-02 04:55:30 +04:00
|
|
|
if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
|
|
|
|
PushOnScopeChains(New, EnclosingScope, /* AddToContext = */ false);
|
|
|
|
} else if (Name) {
|
2009-01-12 21:45:55 +03:00
|
|
|
S = getNonFieldDeclScope(S);
|
2010-10-09 03:50:27 +04:00
|
|
|
PushOnScopeChains(New, S, !IsForwardReference);
|
|
|
|
if (IsForwardReference)
|
|
|
|
SearchDC->makeDeclVisibleInContext(New, /* Recoverable = */ false);
|
|
|
|
|
2009-01-13 01:49:06 +03:00
|
|
|
} else {
|
2009-06-30 06:36:12 +04:00
|
|
|
CurContext->addDecl(New);
|
Change struct forward declarations and definitions to use unique RecordDecls, as opposed to creating a single RecordDecl and reusing it.
This change effects both RecordDecls and CXXRecordDecls, but does not effect EnumDecls (yet).
The motivation of this patch is as follows:
- Capture more source information, necessary for refactoring/rewriting clients.
- Pave the way to resolve ownership issues with RecordDecls with the forthcoming
addition of DeclGroups.
Current caveats:
- Until DeclGroups are in place, we will leak RecordDecls not explicitly
referenced by the AST. For example:
typedef struct { ... } x;
The RecordDecl for the struct will be leaked because the TypedefDecl doesn't
refer to it. This will be solved with DeclGroups.
- This patch also (temporarily) breaks CodeGen. More below.
High-level changes:
- As before, TagType still refers to a TagDecl, but it doesn't own it. When
a struct/union/class is first referenced, a RecordType and RecordDecl are
created for it, and the RecordType refers to that RecordDecl. Later, if
a new RecordDecl is created, the pointer to a RecordDecl in RecordType is
updated to point to the RecordDecl that defines the struct/union/class.
- TagDecl and RecordDecl now how a method 'getDefinition()' to return the
TagDecl*/RecordDecl* that refers to the TagDecl* that defines a particular
enum/struct/class/union. This is useful from going from a RecordDecl* that
defines a forward declaration to the RecordDecl* that provides the actual
definition. Note that this also works for EnumDecls, except that in this case
there is no distinction between forward declarations and definitions (yet).
- Clients should no longer assume that 'isDefinition()' returns true from a
RecordDecl if the corresponding struct/union/class has been defined.
isDefinition() only returns true if a particular RecordDecl is the defining
Decl. Use 'getDefinition()' instead to determine if a struct has been defined.
- The main changes to Sema happen in ActOnTag. To make the changes more
incremental, I split off the processing of enums and structs et al into two
code paths. Enums use the original code path (which is in ActOnTag) and
structs use the ActOnTagStruct. Eventually the two code paths will be merged,
but the idea was to preserve the original logic both for comparison and not to
change the logic for both enums and structs all at once.
- There is NO CHAINING of RecordDecls for the same RecordType. All RecordDecls
that correspond to the same type simply have a pointer to that type. If we
need to figure out what are all the RecordDecls for a given type we can build
a backmap.
- The diff in CXXRecordDecl.[cpp,h] is actually very small; it just mimics the
changes to RecordDecl. For some reason 'svn' marks the entire file as changed.
Why is CodeGen broken:
- Codegen assumes that there is an equivalence between RecordDecl* and
RecordType*. This was true before because we only created one RecordDecl* for
a given RecordType*, but it is no longer true. I believe this shouldn't be too
hard to change, but the patch was big enough as it is.
I have tested this patch on both the clang test suite, and by running the static analyzer over Postgresql and a large Apple-internal project (mix of Objective-C and C).
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55839 91177308-0d34-0410-b5e6-96231b3b80d8
2008-09-05 21:16:31 +04:00
|
|
|
}
|
2008-10-16 06:34:03 +04:00
|
|
|
|
2009-07-07 20:35:42 +04:00
|
|
|
// If this is the C FILE type, notify the AST context.
|
|
|
|
if (IdentifierInfo *II = New->getIdentifier())
|
|
|
|
if (!New->isInvalidDecl() &&
|
2010-08-31 04:36:30 +04:00
|
|
|
New->getDeclContext()->getRedeclContext()->isTranslationUnit() &&
|
2009-07-07 20:35:42 +04:00
|
|
|
II->isStr("FILE"))
|
|
|
|
Context.setFILEDecl(New);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
When we parse a tag specifier, keep track of whether that tag
specifier resulted in the creation of a new TagDecl node, which
happens either when the tag specifier was a definition or when the tag
specifier was the first declaration of that tag type. This information
has several uses, the first of which is implemented in this commit:
1) In C++, one is not allowed to define tag types within a type
specifier (e.g., static_cast<struct S { int x; } *>(0) is
ill-formed) or within the result or parameter types of a
function. We now diagnose this.
2) We can extend DeclGroups to contain information about any tags
that are declared/defined within the declaration specifiers of a
variable, e.g.,
struct Point { int x, y, z; } p;
This will help improve AST printing and template instantiation,
among other things.
3) For C99, we can keep track of whether a tag type is defined
within the type of a parameter, to properly cope with cases like,
e.g.,
int bar(struct T2 { int x; } y) {
struct T2 z;
}
We can also do similar things wherever there is a type specifier,
e.g., to keep track of where the definition of S occurs in this
legal C99 code:
(struct S { int x, y; } *)0
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72555 91177308-0d34-0410-b5e6-96231b3b80d8
2009-05-29 03:31:59 +04:00
|
|
|
OwnedDecl = true;
|
2010-08-21 13:40:31 +04:00
|
|
|
return New;
|
Change struct forward declarations and definitions to use unique RecordDecls, as opposed to creating a single RecordDecl and reusing it.
This change effects both RecordDecls and CXXRecordDecls, but does not effect EnumDecls (yet).
The motivation of this patch is as follows:
- Capture more source information, necessary for refactoring/rewriting clients.
- Pave the way to resolve ownership issues with RecordDecls with the forthcoming
addition of DeclGroups.
Current caveats:
- Until DeclGroups are in place, we will leak RecordDecls not explicitly
referenced by the AST. For example:
typedef struct { ... } x;
The RecordDecl for the struct will be leaked because the TypedefDecl doesn't
refer to it. This will be solved with DeclGroups.
- This patch also (temporarily) breaks CodeGen. More below.
High-level changes:
- As before, TagType still refers to a TagDecl, but it doesn't own it. When
a struct/union/class is first referenced, a RecordType and RecordDecl are
created for it, and the RecordType refers to that RecordDecl. Later, if
a new RecordDecl is created, the pointer to a RecordDecl in RecordType is
updated to point to the RecordDecl that defines the struct/union/class.
- TagDecl and RecordDecl now how a method 'getDefinition()' to return the
TagDecl*/RecordDecl* that refers to the TagDecl* that defines a particular
enum/struct/class/union. This is useful from going from a RecordDecl* that
defines a forward declaration to the RecordDecl* that provides the actual
definition. Note that this also works for EnumDecls, except that in this case
there is no distinction between forward declarations and definitions (yet).
- Clients should no longer assume that 'isDefinition()' returns true from a
RecordDecl if the corresponding struct/union/class has been defined.
isDefinition() only returns true if a particular RecordDecl is the defining
Decl. Use 'getDefinition()' instead to determine if a struct has been defined.
- The main changes to Sema happen in ActOnTag. To make the changes more
incremental, I split off the processing of enums and structs et al into two
code paths. Enums use the original code path (which is in ActOnTag) and
structs use the ActOnTagStruct. Eventually the two code paths will be merged,
but the idea was to preserve the original logic both for comparison and not to
change the logic for both enums and structs all at once.
- There is NO CHAINING of RecordDecls for the same RecordType. All RecordDecls
that correspond to the same type simply have a pointer to that type. If we
need to figure out what are all the RecordDecls for a given type we can build
a backmap.
- The diff in CXXRecordDecl.[cpp,h] is actually very small; it just mimics the
changes to RecordDecl. For some reason 'svn' marks the entire file as changed.
Why is CodeGen broken:
- Codegen assumes that there is an equivalence between RecordDecl* and
RecordType*. This was true before because we only created one RecordDecl* for
a given RecordType*, but it is no longer true. I believe this shouldn't be too
hard to change, but the patch was big enough as it is.
I have tested this patch on both the clang test suite, and by running the static analyzer over Postgresql and a large Apple-internal project (mix of Objective-C and C).
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55839 91177308-0d34-0410-b5e6-96231b3b80d8
2008-09-05 21:16:31 +04:00
|
|
|
}
|
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
void Sema::ActOnTagStartDefinition(Scope *S, Decl *TagD) {
|
2009-02-04 22:02:06 +03:00
|
|
|
AdjustDeclIfTemplate(TagD);
|
2010-08-21 13:40:31 +04:00
|
|
|
TagDecl *Tag = cast<TagDecl>(TagD);
|
2010-04-24 20:38:41 +04:00
|
|
|
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
// Enter the tag context.
|
|
|
|
PushDeclContext(S, Tag);
|
2009-12-20 10:58:13 +03:00
|
|
|
}
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
void Sema::ActOnStartCXXMemberDeclarations(Scope *S, Decl *TagD,
|
2009-12-20 10:58:13 +03:00
|
|
|
SourceLocation LBraceLoc) {
|
|
|
|
AdjustDeclIfTemplate(TagD);
|
2010-08-21 13:40:31 +04:00
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(TagD);
|
2009-12-20 10:58:13 +03:00
|
|
|
|
|
|
|
FieldCollector->StartClass();
|
|
|
|
|
|
|
|
if (!Record->getIdentifier())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// C++ [class]p2:
|
|
|
|
// [...] The class-name is also inserted into the scope of the
|
|
|
|
// class itself; this is known as the injected-class-name. For
|
|
|
|
// purposes of access checking, the injected-class-name is treated
|
|
|
|
// as if it were a public member name.
|
|
|
|
CXXRecordDecl *InjectedClassName
|
|
|
|
= CXXRecordDecl::Create(Context, Record->getTagKind(),
|
|
|
|
CurContext, Record->getLocation(),
|
|
|
|
Record->getIdentifier(),
|
|
|
|
Record->getTagKeywordLoc(),
|
2010-10-15 00:14:21 +04:00
|
|
|
/*PrevDecl=*/0,
|
|
|
|
/*DelayTypeCreation=*/true);
|
|
|
|
Context.getTypeDeclType(InjectedClassName, Record);
|
2009-12-20 10:58:13 +03:00
|
|
|
InjectedClassName->setImplicit();
|
|
|
|
InjectedClassName->setAccess(AS_public);
|
|
|
|
if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate())
|
|
|
|
InjectedClassName->setDescribedClassTemplate(Template);
|
|
|
|
PushOnScopeChains(InjectedClassName, S);
|
|
|
|
assert(InjectedClassName->isInjectedClassName() &&
|
|
|
|
"Broken injected-class-name");
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
}
|
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
void Sema::ActOnTagFinishDefinition(Scope *S, Decl *TagD,
|
2009-07-14 07:17:52 +04:00
|
|
|
SourceLocation RBraceLoc) {
|
2009-02-04 22:02:06 +03:00
|
|
|
AdjustDeclIfTemplate(TagD);
|
2010-08-21 13:40:31 +04:00
|
|
|
TagDecl *Tag = cast<TagDecl>(TagD);
|
2009-07-14 07:17:52 +04:00
|
|
|
Tag->setRBraceLoc(RBraceLoc);
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
|
|
|
|
if (isa<CXXRecordDecl>(Tag))
|
|
|
|
FieldCollector->FinishClass();
|
|
|
|
|
|
|
|
// Exit this scope of this tag's definition.
|
|
|
|
PopDeclContext();
|
2010-01-06 20:00:51 +03:00
|
|
|
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
// Notify the consumer that we've defined a tag.
|
|
|
|
Consumer.HandleTagDeclDefinition(Tag);
|
|
|
|
}
|
2008-06-21 23:39:06 +04:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
void Sema::ActOnTagDefinitionError(Scope *S, Decl *TagD) {
|
2010-03-17 03:38:33 +03:00
|
|
|
AdjustDeclIfTemplate(TagD);
|
2010-08-21 13:40:31 +04:00
|
|
|
TagDecl *Tag = cast<TagDecl>(TagD);
|
2010-03-17 03:38:33 +03:00
|
|
|
Tag->setInvalidDecl();
|
|
|
|
|
2010-03-17 22:25:57 +03:00
|
|
|
// We're undoing ActOnTagStartDefinition here, not
|
|
|
|
// ActOnStartCXXMemberDeclarations, so we don't have to mess with
|
|
|
|
// the FieldCollector.
|
2010-03-17 03:38:33 +03:00
|
|
|
|
|
|
|
PopDeclContext();
|
|
|
|
}
|
|
|
|
|
2009-04-20 21:29:38 +04:00
|
|
|
// Note that FieldName may be null for anonymous bitfields.
|
2009-09-09 19:08:12 +04:00
|
|
|
bool Sema::VerifyBitField(SourceLocation FieldLoc, IdentifierInfo *FieldName,
|
2009-08-16 01:55:26 +04:00
|
|
|
QualType FieldTy, const Expr *BitWidth,
|
|
|
|
bool *ZeroWidth) {
|
|
|
|
// Default to true; that shouldn't confuse checks for emptiness
|
|
|
|
if (ZeroWidth)
|
|
|
|
*ZeroWidth = true;
|
|
|
|
|
2009-03-06 01:45:59 +03:00
|
|
|
// C99 6.7.2.1p4 - verify the field type.
|
2009-03-06 02:01:03 +03:00
|
|
|
// C++ 9.6p3: A bit-field shall have integral or enumeration type.
|
2010-06-16 04:17:44 +04:00
|
|
|
if (!FieldTy->isDependentType() && !FieldTy->isIntegralOrEnumerationType()) {
|
2009-03-06 01:45:59 +03:00
|
|
|
// Handle incomplete types with specific error.
|
2009-03-11 00:58:27 +03:00
|
|
|
if (RequireCompleteType(FieldLoc, FieldTy, diag::err_field_incomplete))
|
|
|
|
return true;
|
2009-04-20 21:29:38 +04:00
|
|
|
if (FieldName)
|
|
|
|
return Diag(FieldLoc, diag::err_not_integral_type_bitfield)
|
|
|
|
<< FieldName << FieldTy << BitWidth->getSourceRange();
|
|
|
|
return Diag(FieldLoc, diag::err_not_integral_type_anon_bitfield)
|
|
|
|
<< FieldTy << BitWidth->getSourceRange();
|
2009-03-06 01:45:59 +03:00
|
|
|
}
|
2009-03-11 21:59:21 +03:00
|
|
|
|
|
|
|
// If the bit-width is type- or value-dependent, don't try to check
|
|
|
|
// it now.
|
|
|
|
if (BitWidth->isValueDependent() || BitWidth->isTypeDependent())
|
|
|
|
return false;
|
|
|
|
|
2008-12-06 23:33:04 +03:00
|
|
|
llvm::APSInt Value;
|
|
|
|
if (VerifyIntegerConstantExpression(BitWidth, &Value))
|
|
|
|
return true;
|
|
|
|
|
2009-08-16 01:55:26 +04:00
|
|
|
if (Value != 0 && ZeroWidth)
|
|
|
|
*ZeroWidth = false;
|
|
|
|
|
2008-12-12 07:56:04 +03:00
|
|
|
// Zero-width bitfield is ok for anonymous field.
|
|
|
|
if (Value == 0 && FieldName)
|
|
|
|
return Diag(FieldLoc, diag::err_bitfield_has_zero_width) << FieldName;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-20 21:29:38 +04:00
|
|
|
if (Value.isSigned() && Value.isNegative()) {
|
|
|
|
if (FieldName)
|
2009-09-09 19:08:12 +04:00
|
|
|
return Diag(FieldLoc, diag::err_bitfield_has_negative_width)
|
2009-04-20 21:29:38 +04:00
|
|
|
<< FieldName << Value.toString(10);
|
|
|
|
return Diag(FieldLoc, diag::err_anon_bitfield_has_negative_width)
|
|
|
|
<< Value.toString(10);
|
|
|
|
}
|
2008-12-06 23:33:04 +03:00
|
|
|
|
2009-03-11 21:59:21 +03:00
|
|
|
if (!FieldTy->isDependentType()) {
|
|
|
|
uint64_t TypeSize = Context.getTypeSize(FieldTy);
|
2009-04-20 21:29:38 +04:00
|
|
|
if (Value.getZExtValue() > TypeSize) {
|
2010-04-16 19:16:32 +04:00
|
|
|
if (!getLangOptions().CPlusPlus) {
|
|
|
|
if (FieldName)
|
|
|
|
return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_size)
|
|
|
|
<< FieldName << (unsigned)Value.getZExtValue()
|
|
|
|
<< (unsigned)TypeSize;
|
|
|
|
|
|
|
|
return Diag(FieldLoc, diag::err_anon_bitfield_width_exceeds_type_size)
|
|
|
|
<< (unsigned)Value.getZExtValue() << (unsigned)TypeSize;
|
|
|
|
}
|
|
|
|
|
2009-04-20 21:29:38 +04:00
|
|
|
if (FieldName)
|
2010-04-16 19:16:32 +04:00
|
|
|
Diag(FieldLoc, diag::warn_bitfield_width_exceeds_type_size)
|
|
|
|
<< FieldName << (unsigned)Value.getZExtValue()
|
|
|
|
<< (unsigned)TypeSize;
|
|
|
|
else
|
|
|
|
Diag(FieldLoc, diag::warn_anon_bitfield_width_exceeds_type_size)
|
|
|
|
<< (unsigned)Value.getZExtValue() << (unsigned)TypeSize;
|
2009-04-20 21:29:38 +04:00
|
|
|
}
|
2009-03-11 21:59:21 +03:00
|
|
|
}
|
2008-12-06 23:33:04 +03:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-09-15 22:49:24 +04:00
|
|
|
/// ActOnField - Each field of a struct/union/class is passed into this in order
|
2007-07-11 21:01:13 +04:00
|
|
|
/// to create a FieldDecl object for it.
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *Sema::ActOnField(Scope *S, Decl *TagD,
|
2009-09-09 19:08:12 +04:00
|
|
|
SourceLocation DeclStart,
|
2009-03-28 22:18:32 +03:00
|
|
|
Declarator &D, ExprTy *BitfieldWidth) {
|
2010-08-21 13:40:31 +04:00
|
|
|
FieldDecl *Res = HandleField(S, cast_or_null<RecordDecl>(TagD),
|
2009-03-28 22:18:32 +03:00
|
|
|
DeclStart, D, static_cast<Expr*>(BitfieldWidth),
|
|
|
|
AS_public);
|
2010-08-21 13:40:31 +04:00
|
|
|
return Res;
|
2009-03-06 01:45:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleField - Analyze a field of a C struct or a C++ data member.
|
|
|
|
///
|
|
|
|
FieldDecl *Sema::HandleField(Scope *S, RecordDecl *Record,
|
|
|
|
SourceLocation DeclStart,
|
2009-03-11 23:50:30 +03:00
|
|
|
Declarator &D, Expr *BitWidth,
|
|
|
|
AccessSpecifier AS) {
|
2007-07-11 21:01:13 +04:00
|
|
|
IdentifierInfo *II = D.getIdentifier();
|
|
|
|
SourceLocation Loc = DeclStart;
|
|
|
|
if (II) Loc = D.getIdentifierLoc();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-06-05 03:28:52 +04:00
|
|
|
TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
|
|
|
|
QualType T = TInfo->getType();
|
2009-04-13 02:15:02 +04:00
|
|
|
if (getLangOptions().CPlusPlus)
|
2009-03-11 21:59:21 +03:00
|
|
|
CheckExtraCXXDefaultArguments(D);
|
2009-03-12 02:00:04 +03:00
|
|
|
|
2009-04-07 23:37:57 +04:00
|
|
|
DiagnoseFunctionSpecifiers(D);
|
|
|
|
|
2009-04-20 00:27:55 +04:00
|
|
|
if (D.getDeclSpec().isThreadSpecified())
|
|
|
|
Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
|
2010-08-30 18:32:14 +04:00
|
|
|
|
|
|
|
// Check to see if this name was declared as a member previously
|
|
|
|
LookupResult Previous(*this, II, Loc, LookupMemberName, ForRedeclaration);
|
|
|
|
LookupName(Previous, S);
|
|
|
|
assert((Previous.empty() || Previous.isOverloadedResult() ||
|
|
|
|
Previous.isSingleResult())
|
|
|
|
&& "Lookup of member name should be either overloaded, single or null");
|
|
|
|
|
|
|
|
// If the name is overloaded then get any declaration else get the single result
|
|
|
|
NamedDecl *PrevDecl = Previous.isOverloadedResult() ?
|
|
|
|
Previous.getRepresentativeDecl() : Previous.getAsSingle<NamedDecl>();
|
2009-06-18 03:37:01 +04:00
|
|
|
|
|
|
|
if (PrevDecl && PrevDecl->isTemplateParameter()) {
|
|
|
|
// Maybe we will complain about the shadowed template parameter.
|
|
|
|
DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
|
|
|
|
// Just pretend that we didn't see the previous declaration.
|
|
|
|
PrevDecl = 0;
|
|
|
|
}
|
|
|
|
|
2009-03-11 21:59:21 +03:00
|
|
|
if (PrevDecl && !isDeclInScope(PrevDecl, Record, S))
|
|
|
|
PrevDecl = 0;
|
|
|
|
|
2009-07-14 18:58:18 +04:00
|
|
|
bool Mutable
|
|
|
|
= (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_mutable);
|
|
|
|
SourceLocation TSSL = D.getSourceRange().getBegin();
|
|
|
|
FieldDecl *NewFD
|
2009-12-07 05:54:59 +03:00
|
|
|
= CheckFieldDecl(II, T, TInfo, Record, Loc, Mutable, BitWidth, TSSL,
|
2009-07-14 18:58:18 +04:00
|
|
|
AS, PrevDecl, &D);
|
2010-03-22 01:56:43 +03:00
|
|
|
|
|
|
|
if (NewFD->isInvalidDecl())
|
|
|
|
Record->setInvalidDecl();
|
|
|
|
|
2009-03-11 21:59:21 +03:00
|
|
|
if (NewFD->isInvalidDecl() && PrevDecl) {
|
|
|
|
// Don't introduce NewFD into scope; there's already something
|
|
|
|
// with the same name in the same scope.
|
|
|
|
} else if (II) {
|
|
|
|
PushOnScopeChains(NewFD, S);
|
|
|
|
} else
|
2009-06-30 06:36:12 +04:00
|
|
|
Record->addDecl(NewFD);
|
2009-03-11 21:59:21 +03:00
|
|
|
|
|
|
|
return NewFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Build a new FieldDecl and check its well-formedness.
|
|
|
|
///
|
|
|
|
/// This routine builds a new FieldDecl given the fields name, type,
|
|
|
|
/// record, etc. \p PrevDecl should refer to any previous declaration
|
|
|
|
/// with the same name and in the same scope as the field to be
|
|
|
|
/// created.
|
|
|
|
///
|
|
|
|
/// \returns a new FieldDecl.
|
|
|
|
///
|
2009-09-09 19:08:12 +04:00
|
|
|
/// \todo The Declarator argument is a hack. It will be removed once
|
2009-08-19 05:27:57 +04:00
|
|
|
FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T,
|
2009-12-07 05:54:59 +03:00
|
|
|
TypeSourceInfo *TInfo,
|
2009-03-11 21:59:21 +03:00
|
|
|
RecordDecl *Record, SourceLocation Loc,
|
2009-09-09 19:08:12 +04:00
|
|
|
bool Mutable, Expr *BitWidth,
|
2009-07-14 18:58:18 +04:00
|
|
|
SourceLocation TSSL,
|
2009-03-11 23:50:30 +03:00
|
|
|
AccessSpecifier AS, NamedDecl *PrevDecl,
|
2009-03-11 21:59:21 +03:00
|
|
|
Declarator *D) {
|
|
|
|
IdentifierInfo *II = Name.getAsIdentifierInfo();
|
2007-08-29 00:14:24 +04:00
|
|
|
bool InvalidDecl = false;
|
2009-04-25 12:06:05 +04:00
|
|
|
if (D) InvalidDecl = D->isInvalidType();
|
2009-01-05 23:52:13 +03:00
|
|
|
|
2009-03-11 21:59:21 +03:00
|
|
|
// If we receive a broken type, recover by assuming 'int' and
|
|
|
|
// marking this declaration as invalid.
|
|
|
|
if (T.isNull()) {
|
|
|
|
InvalidDecl = true;
|
|
|
|
T = Context.IntTy;
|
|
|
|
}
|
|
|
|
|
2009-12-07 03:22:08 +03:00
|
|
|
QualType EltTy = Context.getBaseElementType(T);
|
|
|
|
if (!EltTy->isDependentType() &&
|
2010-08-17 03:42:35 +04:00
|
|
|
RequireCompleteType(Loc, EltTy, diag::err_field_incomplete)) {
|
|
|
|
// Fields of incomplete type force their record to be invalid.
|
|
|
|
Record->setInvalidDecl();
|
2009-12-07 03:22:08 +03:00
|
|
|
InvalidDecl = true;
|
2010-08-17 03:42:35 +04:00
|
|
|
}
|
2009-12-07 03:22:08 +03:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// C99 6.7.2.1p8: A member of a structure or union may have any type other
|
|
|
|
// than a variably modified type.
|
2009-12-07 03:22:08 +03:00
|
|
|
if (!InvalidDecl && T->isVariablyModifiedType()) {
|
2009-02-21 03:44:51 +03:00
|
|
|
bool SizeIsNegative;
|
2010-08-18 04:39:00 +04:00
|
|
|
llvm::APSInt Oversized;
|
2009-02-21 03:44:51 +03:00
|
|
|
QualType FixedTy = TryToFixInvalidVariablyModifiedType(T, Context,
|
2010-08-18 04:39:00 +04:00
|
|
|
SizeIsNegative,
|
|
|
|
Oversized);
|
2009-02-21 03:44:51 +03:00
|
|
|
if (!FixedTy.isNull()) {
|
|
|
|
Diag(Loc, diag::warn_illegal_constant_array_size);
|
|
|
|
T = FixedTy;
|
|
|
|
} else {
|
|
|
|
if (SizeIsNegative)
|
|
|
|
Diag(Loc, diag::err_typecheck_negative_array_size);
|
2010-08-18 04:39:00 +04:00
|
|
|
else if (Oversized.getBoolValue())
|
|
|
|
Diag(Loc, diag::err_array_too_large)
|
|
|
|
<< Oversized.toString(10);
|
2009-02-21 03:44:51 +03:00
|
|
|
else
|
|
|
|
Diag(Loc, diag::err_typecheck_field_variable_size);
|
|
|
|
InvalidDecl = true;
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-22 23:18:17 +03:00
|
|
|
// Fields can not have abstract class types
|
2009-12-07 03:22:08 +03:00
|
|
|
if (!InvalidDecl && RequireNonAbstractType(Loc, T,
|
|
|
|
diag::err_abstract_type_in_decl,
|
|
|
|
AbstractFieldType))
|
2009-03-22 23:18:17 +03:00
|
|
|
InvalidDecl = true;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-16 01:55:26 +04:00
|
|
|
bool ZeroWidth = false;
|
2009-03-11 21:59:21 +03:00
|
|
|
// If this is declared as a bit-field, check the bit-field.
|
2009-12-07 03:22:08 +03:00
|
|
|
if (!InvalidDecl && BitWidth &&
|
|
|
|
VerifyBitField(Loc, II, T, BitWidth, &ZeroWidth)) {
|
2009-03-11 21:59:21 +03:00
|
|
|
InvalidDecl = true;
|
|
|
|
BitWidth = 0;
|
2009-08-16 01:55:26 +04:00
|
|
|
ZeroWidth = false;
|
2008-12-06 23:33:04 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-06-04 12:34:12 +04:00
|
|
|
// Check that 'mutable' is consistent with the type of the declaration.
|
|
|
|
if (!InvalidDecl && Mutable) {
|
|
|
|
unsigned DiagID = 0;
|
|
|
|
if (T->isReferenceType())
|
|
|
|
DiagID = diag::err_mutable_reference;
|
|
|
|
else if (T.isConstQualified())
|
|
|
|
DiagID = diag::err_mutable_const;
|
|
|
|
|
|
|
|
if (DiagID) {
|
|
|
|
SourceLocation ErrLoc = Loc;
|
|
|
|
if (D && D->getDeclSpec().getStorageClassSpecLoc().isValid())
|
|
|
|
ErrLoc = D->getDeclSpec().getStorageClassSpecLoc();
|
|
|
|
Diag(ErrLoc, DiagID);
|
|
|
|
Mutable = false;
|
|
|
|
InvalidDecl = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-07 05:54:59 +03:00
|
|
|
FieldDecl *NewFD = FieldDecl::Create(Context, Record, Loc, II, T, TInfo,
|
2009-08-21 04:31:54 +04:00
|
|
|
BitWidth, Mutable);
|
2009-04-25 12:06:05 +04:00
|
|
|
if (InvalidDecl)
|
|
|
|
NewFD->setInvalidDecl();
|
2008-12-11 19:49:14 +03:00
|
|
|
|
2009-03-11 21:59:21 +03:00
|
|
|
if (PrevDecl && !isa<TagDecl>(PrevDecl)) {
|
|
|
|
Diag(Loc, diag::err_duplicate_member) << II;
|
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_declaration);
|
|
|
|
NewFD->setInvalidDecl();
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
}
|
|
|
|
|
2010-02-05 01:26:26 +03:00
|
|
|
if (!InvalidDecl && getLangOptions().CPlusPlus) {
|
2009-07-30 01:53:49 +04:00
|
|
|
if (const RecordType *RT = EltTy->getAs<RecordType>()) {
|
2009-07-22 22:25:24 +04:00
|
|
|
CXXRecordDecl* RDecl = cast<CXXRecordDecl>(RT->getDecl());
|
2010-06-16 20:54:04 +04:00
|
|
|
if (RDecl->getDefinition()) {
|
|
|
|
// C++ 9.5p1: An object of a class with a non-trivial
|
|
|
|
// constructor, a non-trivial copy constructor, a non-trivial
|
|
|
|
// destructor, or a non-trivial copy assignment operator
|
|
|
|
// cannot be a member of a union, nor can an array of such
|
|
|
|
// objects.
|
|
|
|
// TODO: C++0x alters this restriction significantly.
|
2010-08-16 21:27:08 +04:00
|
|
|
if (Record->isUnion() && CheckNontrivialField(NewFD))
|
|
|
|
NewFD->setInvalidDecl();
|
2009-07-22 22:25:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-11 21:59:21 +03:00
|
|
|
// FIXME: We need to pass in the attributes given an AST
|
|
|
|
// representation, not a parser representation.
|
|
|
|
if (D)
|
2009-06-18 01:51:59 +04:00
|
|
|
// FIXME: What to pass instead of TUScope?
|
|
|
|
ProcessDeclAttributes(TUScope, NewFD, *D);
|
2008-12-17 00:30:33 +03:00
|
|
|
|
2009-02-19 03:22:47 +03:00
|
|
|
if (T.isObjCGCWeak())
|
2009-02-18 21:14:41 +03:00
|
|
|
Diag(Loc, diag::warn_attribute_weak_on_field);
|
2008-02-16 03:29:18 +03:00
|
|
|
|
2009-03-11 23:50:30 +03:00
|
|
|
NewFD->setAccess(AS);
|
2007-08-29 00:14:24 +04:00
|
|
|
return NewFD;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2010-08-16 21:27:08 +04:00
|
|
|
bool Sema::CheckNontrivialField(FieldDecl *FD) {
|
|
|
|
assert(FD);
|
|
|
|
assert(getLangOptions().CPlusPlus && "valid check only for C++");
|
|
|
|
|
|
|
|
if (FD->isInvalidDecl())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
QualType EltTy = Context.getBaseElementType(FD->getType());
|
|
|
|
if (const RecordType *RT = EltTy->getAs<RecordType>()) {
|
|
|
|
CXXRecordDecl* RDecl = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
if (RDecl->getDefinition()) {
|
|
|
|
// We check for copy constructors before constructors
|
|
|
|
// because otherwise we'll never get complaints about
|
|
|
|
// copy constructors.
|
|
|
|
|
|
|
|
CXXSpecialMember member = CXXInvalid;
|
|
|
|
if (!RDecl->hasTrivialCopyConstructor())
|
|
|
|
member = CXXCopyConstructor;
|
|
|
|
else if (!RDecl->hasTrivialConstructor())
|
|
|
|
member = CXXConstructor;
|
|
|
|
else if (!RDecl->hasTrivialCopyAssignment())
|
|
|
|
member = CXXCopyAssignment;
|
|
|
|
else if (!RDecl->hasTrivialDestructor())
|
|
|
|
member = CXXDestructor;
|
|
|
|
|
|
|
|
if (member != CXXInvalid) {
|
|
|
|
Diag(FD->getLocation(), diag::err_illegal_union_or_anon_struct_member)
|
|
|
|
<< (int)FD->getParent()->isUnion() << FD->getDeclName() << member;
|
|
|
|
DiagnoseNontrivial(RT, member);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-22 22:25:24 +04:00
|
|
|
/// DiagnoseNontrivial - Given that a class has a non-trivial
|
|
|
|
/// special member, figure out why.
|
|
|
|
void Sema::DiagnoseNontrivial(const RecordType* T, CXXSpecialMember member) {
|
|
|
|
QualType QT(T, 0U);
|
|
|
|
CXXRecordDecl* RD = cast<CXXRecordDecl>(T->getDecl());
|
|
|
|
|
|
|
|
// Check whether the member was user-declared.
|
|
|
|
switch (member) {
|
2010-04-22 18:36:26 +04:00
|
|
|
case CXXInvalid:
|
|
|
|
break;
|
|
|
|
|
2010-04-22 09:40:53 +04:00
|
|
|
case CXXConstructor:
|
2009-07-22 22:25:24 +04:00
|
|
|
if (RD->hasUserDeclaredConstructor()) {
|
|
|
|
typedef CXXRecordDecl::ctor_iterator ctor_iter;
|
2009-10-26 01:31:45 +03:00
|
|
|
for (ctor_iter ci = RD->ctor_begin(), ce = RD->ctor_end(); ci != ce;++ci){
|
|
|
|
const FunctionDecl *body = 0;
|
2010-07-07 15:31:19 +04:00
|
|
|
ci->hasBody(body);
|
2010-04-21 03:32:58 +04:00
|
|
|
if (!body || !cast<CXXConstructorDecl>(body)->isImplicitlyDefined()) {
|
2009-07-22 22:25:24 +04:00
|
|
|
SourceLocation CtorLoc = ci->getLocation();
|
|
|
|
Diag(CtorLoc, diag::note_nontrivial_user_defined) << QT << member;
|
|
|
|
return;
|
|
|
|
}
|
2009-10-26 01:31:45 +03:00
|
|
|
}
|
2009-07-22 22:25:24 +04:00
|
|
|
|
|
|
|
assert(0 && "found no user-declared constructors");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXXCopyConstructor:
|
|
|
|
if (RD->hasUserDeclaredCopyConstructor()) {
|
|
|
|
SourceLocation CtorLoc =
|
|
|
|
RD->getCopyConstructor(Context, 0)->getLocation();
|
|
|
|
Diag(CtorLoc, diag::note_nontrivial_user_defined) << QT << member;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXXCopyAssignment:
|
|
|
|
if (RD->hasUserDeclaredCopyAssignment()) {
|
|
|
|
// FIXME: this should use the location of the copy
|
|
|
|
// assignment, not the type.
|
|
|
|
SourceLocation TyLoc = RD->getSourceRange().getBegin();
|
|
|
|
Diag(TyLoc, diag::note_nontrivial_user_defined) << QT << member;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXXDestructor:
|
|
|
|
if (RD->hasUserDeclaredDestructor()) {
|
2010-07-02 02:47:18 +04:00
|
|
|
SourceLocation DtorLoc = LookupDestructor(RD)->getLocation();
|
2009-07-22 22:25:24 +04:00
|
|
|
Diag(DtorLoc, diag::note_nontrivial_user_defined) << QT << member;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef CXXRecordDecl::base_class_iterator base_iter;
|
|
|
|
|
|
|
|
// Virtual bases and members inhibit trivial copying/construction,
|
|
|
|
// but not trivial destruction.
|
|
|
|
if (member != CXXDestructor) {
|
|
|
|
// Check for virtual bases. vbases includes indirect virtual bases,
|
|
|
|
// so we just iterate through the direct bases.
|
|
|
|
for (base_iter bi = RD->bases_begin(), be = RD->bases_end(); bi != be; ++bi)
|
|
|
|
if (bi->isVirtual()) {
|
|
|
|
SourceLocation BaseLoc = bi->getSourceRange().getBegin();
|
|
|
|
Diag(BaseLoc, diag::note_nontrivial_has_virtual) << QT << 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for virtual methods.
|
|
|
|
typedef CXXRecordDecl::method_iterator meth_iter;
|
|
|
|
for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
|
|
|
|
++mi) {
|
|
|
|
if (mi->isVirtual()) {
|
|
|
|
SourceLocation MLoc = mi->getSourceRange().getBegin();
|
|
|
|
Diag(MLoc, diag::note_nontrivial_has_virtual) << QT << 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-22 22:25:24 +04:00
|
|
|
bool (CXXRecordDecl::*hasTrivial)() const;
|
|
|
|
switch (member) {
|
2010-04-22 09:40:53 +04:00
|
|
|
case CXXConstructor:
|
2009-07-22 22:25:24 +04:00
|
|
|
hasTrivial = &CXXRecordDecl::hasTrivialConstructor; break;
|
|
|
|
case CXXCopyConstructor:
|
|
|
|
hasTrivial = &CXXRecordDecl::hasTrivialCopyConstructor; break;
|
|
|
|
case CXXCopyAssignment:
|
|
|
|
hasTrivial = &CXXRecordDecl::hasTrivialCopyAssignment; break;
|
|
|
|
case CXXDestructor:
|
|
|
|
hasTrivial = &CXXRecordDecl::hasTrivialDestructor; break;
|
|
|
|
default:
|
|
|
|
assert(0 && "unexpected special member"); return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for nontrivial bases (and recurse).
|
|
|
|
for (base_iter bi = RD->bases_begin(), be = RD->bases_end(); bi != be; ++bi) {
|
2009-07-30 01:53:49 +04:00
|
|
|
const RecordType *BaseRT = bi->getType()->getAs<RecordType>();
|
2009-10-25 20:03:50 +03:00
|
|
|
assert(BaseRT && "Don't know how to handle dependent bases");
|
2009-07-22 22:25:24 +04:00
|
|
|
CXXRecordDecl *BaseRecTy = cast<CXXRecordDecl>(BaseRT->getDecl());
|
|
|
|
if (!(BaseRecTy->*hasTrivial)()) {
|
|
|
|
SourceLocation BaseLoc = bi->getSourceRange().getBegin();
|
|
|
|
Diag(BaseLoc, diag::note_nontrivial_has_nontrivial) << QT << 1 << member;
|
|
|
|
DiagnoseNontrivial(BaseRT, member);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-22 22:25:24 +04:00
|
|
|
// Check for nontrivial members (and recurse).
|
|
|
|
typedef RecordDecl::field_iterator field_iter;
|
|
|
|
for (field_iter fi = RD->field_begin(), fe = RD->field_end(); fi != fe;
|
|
|
|
++fi) {
|
2009-07-24 03:49:00 +04:00
|
|
|
QualType EltTy = Context.getBaseElementType((*fi)->getType());
|
2009-07-30 01:53:49 +04:00
|
|
|
if (const RecordType *EltRT = EltTy->getAs<RecordType>()) {
|
2009-07-22 22:25:24 +04:00
|
|
|
CXXRecordDecl* EltRD = cast<CXXRecordDecl>(EltRT->getDecl());
|
|
|
|
|
|
|
|
if (!(EltRD->*hasTrivial)()) {
|
|
|
|
SourceLocation FLoc = (*fi)->getLocation();
|
|
|
|
Diag(FLoc, diag::note_nontrivial_has_nontrivial) << QT << 0 << member;
|
|
|
|
DiagnoseNontrivial(EltRT, member);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0 && "found no explanation for non-trivial member");
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
/// TranslateIvarVisibility - Translate visibility from a token ID to an
|
2007-10-01 20:53:59 +04:00
|
|
|
/// AST enum value.
|
2008-01-07 22:49:32 +03:00
|
|
|
static ObjCIvarDecl::AccessControl
|
2007-10-01 20:53:59 +04:00
|
|
|
TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) {
|
2007-09-15 03:09:53 +04:00
|
|
|
switch (ivarVisibility) {
|
2008-10-12 04:28:42 +04:00
|
|
|
default: assert(0 && "Unknown visitibility kind");
|
|
|
|
case tok::objc_private: return ObjCIvarDecl::Private;
|
|
|
|
case tok::objc_public: return ObjCIvarDecl::Public;
|
|
|
|
case tok::objc_protected: return ObjCIvarDecl::Protected;
|
|
|
|
case tok::objc_package: return ObjCIvarDecl::Package;
|
2007-09-15 03:09:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
/// ActOnIvar - Each ivar field of an objective-c class is passed into this
|
2008-04-11 20:55:42 +04:00
|
|
|
/// in order to create an IvarDecl object for it.
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *Sema::ActOnIvar(Scope *S,
|
2009-09-09 19:08:12 +04:00
|
|
|
SourceLocation DeclStart,
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *IntfDecl,
|
2009-03-28 22:18:32 +03:00
|
|
|
Declarator &D, ExprTy *BitfieldWidth,
|
|
|
|
tok::ObjCKeywordKind Visibility) {
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-04-11 03:32:45 +04:00
|
|
|
IdentifierInfo *II = D.getIdentifier();
|
|
|
|
Expr *BitWidth = (Expr*)BitfieldWidth;
|
|
|
|
SourceLocation Loc = DeclStart;
|
|
|
|
if (II) Loc = D.getIdentifierLoc();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-04-11 03:32:45 +04:00
|
|
|
// FIXME: Unnamed fields can be handled in various different ways, for
|
|
|
|
// example, unnamed unions inject all members into the struct namespace!
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-06-05 03:28:52 +04:00
|
|
|
TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
|
|
|
|
QualType T = TInfo->getType();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-04-11 03:32:45 +04:00
|
|
|
if (BitWidth) {
|
2009-02-20 20:57:11 +03:00
|
|
|
// 6.7.2.1p3, 6.7.2.1p4
|
2009-03-06 01:45:59 +03:00
|
|
|
if (VerifyBitField(Loc, II, T, BitWidth)) {
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2009-03-06 01:45:59 +03:00
|
|
|
BitWidth = 0;
|
|
|
|
}
|
2008-04-11 03:32:45 +04:00
|
|
|
} else {
|
|
|
|
// Not a bitfield.
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-04-11 03:32:45 +04:00
|
|
|
// validate II.
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-04-11 03:32:45 +04:00
|
|
|
}
|
2010-04-27 02:07:03 +04:00
|
|
|
if (T->isReferenceType()) {
|
|
|
|
Diag(Loc, diag::err_ivar_reference_type);
|
|
|
|
D.setInvalidType();
|
|
|
|
}
|
2008-04-11 03:32:45 +04:00
|
|
|
// C99 6.7.2.1p8: A member of a structure or union may have any type other
|
|
|
|
// than a variably modified type.
|
2010-04-27 02:07:03 +04:00
|
|
|
else if (T->isVariablyModifiedType()) {
|
2008-12-07 03:20:55 +03:00
|
|
|
Diag(Loc, diag::err_typecheck_ivar_variable_size);
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2008-04-11 03:32:45 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-07-23 22:04:17 +04:00
|
|
|
// Get the visibility (access control) for this ivar.
|
2009-09-09 19:08:12 +04:00
|
|
|
ObjCIvarDecl::AccessControl ac =
|
2008-07-23 22:04:17 +04:00
|
|
|
Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility)
|
|
|
|
: ObjCIvarDecl::None;
|
2009-06-05 22:16:35 +04:00
|
|
|
// Must set ivar's DeclContext to its enclosing interface.
|
2010-08-21 13:40:31 +04:00
|
|
|
ObjCContainerDecl *EnclosingDecl = cast<ObjCContainerDecl>(IntfDecl);
|
2010-04-02 22:29:09 +04:00
|
|
|
ObjCContainerDecl *EnclosingContext;
|
2009-09-09 19:08:12 +04:00
|
|
|
if (ObjCImplementationDecl *IMPDecl =
|
2009-06-05 22:16:35 +04:00
|
|
|
dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
|
2010-08-23 22:51:39 +04:00
|
|
|
if (!LangOpts.ObjCNonFragileABI2) {
|
2009-06-05 22:16:35 +04:00
|
|
|
// Case of ivar declared in an implementation. Context is that of its class.
|
2010-08-23 22:51:39 +04:00
|
|
|
EnclosingContext = IMPDecl->getClassInterface();
|
|
|
|
assert(EnclosingContext && "Implementation has no class interface!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
EnclosingContext = EnclosingDecl;
|
2010-04-07 02:43:48 +04:00
|
|
|
} else {
|
|
|
|
if (ObjCCategoryDecl *CDecl =
|
|
|
|
dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) {
|
|
|
|
if (!LangOpts.ObjCNonFragileABI2 || !CDecl->IsClassExtension()) {
|
|
|
|
Diag(Loc, diag::err_misplaced_ivar) << CDecl->IsClassExtension();
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2010-04-07 02:43:48 +04:00
|
|
|
}
|
|
|
|
}
|
2010-04-02 22:29:09 +04:00
|
|
|
EnclosingContext = EnclosingDecl;
|
2010-04-07 02:43:48 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-07-23 22:04:17 +04:00
|
|
|
// Construct the decl.
|
2009-09-09 19:08:12 +04:00
|
|
|
ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context,
|
2009-08-19 05:27:57 +04:00
|
|
|
EnclosingContext, Loc, II, T,
|
2009-12-07 05:54:59 +03:00
|
|
|
TInfo, ac, (Expr *)BitfieldWidth);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
if (II) {
|
2010-04-16 02:33:43 +04:00
|
|
|
NamedDecl *PrevDecl = LookupSingleName(S, II, Loc, LookupMemberName,
|
2009-11-18 10:57:50 +03:00
|
|
|
ForRedeclaration);
|
2009-06-05 22:16:35 +04:00
|
|
|
if (PrevDecl && isDeclInScope(PrevDecl, EnclosingContext, S)
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
&& !isa<TagDecl>(PrevDecl)) {
|
|
|
|
Diag(Loc, diag::err_duplicate_member) << II;
|
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_declaration);
|
|
|
|
NewID->setInvalidDecl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-23 22:04:17 +04:00
|
|
|
// Process attributes attached to the ivar.
|
2009-06-18 01:51:59 +04:00
|
|
|
ProcessDeclAttributes(S, NewID, D);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
if (D.isInvalidType())
|
2008-04-11 03:32:45 +04:00
|
|
|
NewID->setInvalidDecl();
|
2008-07-23 22:04:17 +04:00
|
|
|
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
if (II) {
|
|
|
|
// FIXME: When interfaces are DeclContexts, we'll need to add
|
|
|
|
// these to the interface.
|
2010-08-21 13:40:31 +04:00
|
|
|
S->AddDecl(NewID);
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
IdResolver.AddDecl(NewID);
|
|
|
|
}
|
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
return NewID;
|
2008-04-11 03:32:45 +04:00
|
|
|
}
|
|
|
|
|
2010-08-24 02:46:52 +04:00
|
|
|
/// ActOnLastBitfield - This routine handles synthesized bitfields rules for
|
|
|
|
/// class and class extensions. For every class @interface and class
|
|
|
|
/// extension @interface, if the last ivar is a bitfield of any type,
|
|
|
|
/// then add an implicit `char :0` ivar to the end of that interface.
|
|
|
|
void Sema::ActOnLastBitfield(SourceLocation DeclLoc, Decl *EnclosingDecl,
|
|
|
|
llvm::SmallVectorImpl<Decl *> &AllIvarDecls) {
|
|
|
|
if (!LangOpts.ObjCNonFragileABI2 || AllIvarDecls.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Decl *ivarDecl = AllIvarDecls[AllIvarDecls.size()-1];
|
|
|
|
ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(ivarDecl);
|
|
|
|
|
|
|
|
if (!Ivar->isBitField())
|
|
|
|
return;
|
|
|
|
uint64_t BitFieldSize =
|
|
|
|
Ivar->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
|
|
|
|
if (BitFieldSize == 0)
|
|
|
|
return;
|
|
|
|
ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl);
|
|
|
|
if (!ID) {
|
|
|
|
if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) {
|
|
|
|
if (!CD->IsClassExtension())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// No need to add this to end of @implementation.
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// All conditions are met. Add a new bitfield to the tail end of ivars.
|
|
|
|
llvm::APInt Zero(Context.getTypeSize(Context.CharTy), 0);
|
2010-08-28 13:06:06 +04:00
|
|
|
Expr * BW = IntegerLiteral::Create(Context, Zero, Context.CharTy, DeclLoc);
|
2010-08-24 02:46:52 +04:00
|
|
|
|
|
|
|
Ivar = ObjCIvarDecl::Create(Context, cast<ObjCContainerDecl>(EnclosingDecl),
|
|
|
|
DeclLoc, 0,
|
|
|
|
Context.CharTy,
|
|
|
|
Context.CreateTypeSourceInfo(Context.CharTy),
|
|
|
|
ObjCIvarDecl::Private, BW,
|
|
|
|
true);
|
|
|
|
AllIvarDecls.push_back(Ivar);
|
|
|
|
}
|
|
|
|
|
2007-09-29 04:54:24 +04:00
|
|
|
void Sema::ActOnFields(Scope* S,
|
2010-08-21 13:40:31 +04:00
|
|
|
SourceLocation RecLoc, Decl *EnclosingDecl,
|
|
|
|
Decl **Fields, unsigned NumFields,
|
2008-10-03 06:03:53 +04:00
|
|
|
SourceLocation LBrac, SourceLocation RBrac,
|
2008-10-03 21:33:35 +04:00
|
|
|
AttributeList *Attr) {
|
2007-09-15 02:20:54 +04:00
|
|
|
assert(EnclosingDecl && "missing record or interface decl");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-02-24 01:00:08 +03:00
|
|
|
// If the decl this is being inserted into is invalid, then it may be a
|
|
|
|
// redeclaration or some other bogus case. Don't try to add fields to it.
|
|
|
|
if (EnclosingDecl->isInvalidDecl()) {
|
|
|
|
// FIXME: Deallocate fields?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Verify that all the fields are okay.
|
|
|
|
unsigned NumNamedMembers = 0;
|
|
|
|
llvm::SmallVector<FieldDecl*, 32> RecFields;
|
2009-01-07 22:46:03 +03:00
|
|
|
|
2009-02-24 01:00:08 +03:00
|
|
|
RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl);
|
2007-07-11 21:01:13 +04:00
|
|
|
for (unsigned i = 0; i != NumFields; ++i) {
|
2010-08-21 13:40:31 +04:00
|
|
|
FieldDecl *FD = cast<FieldDecl>(Fields[i]);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Get the type for the field.
|
2007-08-01 01:33:24 +04:00
|
|
|
Type *FDTy = FD->getType().getTypePtr();
|
2009-01-07 22:46:03 +03:00
|
|
|
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
if (!FD->isAnonymousStructOrUnion()) {
|
2009-01-07 22:46:03 +03:00
|
|
|
// Remember all fields written by the user.
|
|
|
|
RecFields.push_back(FD);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-06 01:45:59 +03:00
|
|
|
// If the field is already invalid for some reason, don't emit more
|
|
|
|
// diagnostics about it.
|
2009-12-07 03:22:08 +03:00
|
|
|
if (FD->isInvalidDecl()) {
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
2009-03-06 01:45:59 +03:00
|
|
|
continue;
|
2009-12-07 03:22:08 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-24 22:52:54 +03:00
|
|
|
// C99 6.7.2.1p2:
|
|
|
|
// A structure or union shall not contain a member with
|
|
|
|
// incomplete or function type (hence, a structure shall not
|
|
|
|
// contain an instance of itself, but may contain a pointer to
|
|
|
|
// an instance of itself), except that the last member of a
|
|
|
|
// structure with more than one named member may have incomplete
|
|
|
|
// array type; such a structure (and any union containing,
|
|
|
|
// possibly recursively, a member that is such a structure)
|
|
|
|
// shall not be a member of a structure or an element of an
|
|
|
|
// array.
|
2007-08-01 01:33:24 +04:00
|
|
|
if (FDTy->isFunctionType()) {
|
2009-03-24 22:52:54 +03:00
|
|
|
// Field declared as a function.
|
2008-11-20 09:38:18 +03:00
|
|
|
Diag(FD->getLocation(), diag::err_field_declared_as_function)
|
2008-11-24 08:29:24 +03:00
|
|
|
<< FD->getDeclName();
|
2007-09-15 02:20:54 +04:00
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
2007-07-11 21:01:13 +04:00
|
|
|
continue;
|
2010-09-15 04:14:08 +04:00
|
|
|
} else if (FDTy->isIncompleteArrayType() && Record &&
|
|
|
|
((i == NumFields - 1 && !Record->isUnion()) ||
|
|
|
|
(getLangOptions().Microsoft &&
|
|
|
|
(i == NumFields - 1 || Record->isUnion())))) {
|
2009-03-24 22:52:54 +03:00
|
|
|
// Flexible array member.
|
2010-09-15 04:14:08 +04:00
|
|
|
// Microsoft is more permissive regarding flexible array.
|
|
|
|
// It will accept flexible array in union and also
|
|
|
|
// as the sole element of a struct/class.
|
|
|
|
if (getLangOptions().Microsoft) {
|
|
|
|
if (Record->isUnion())
|
|
|
|
Diag(FD->getLocation(), diag::ext_flexible_array_union)
|
|
|
|
<< FD->getDeclName();
|
|
|
|
else if (NumFields == 1)
|
|
|
|
Diag(FD->getLocation(), diag::ext_flexible_array_empty_aggregate)
|
|
|
|
<< FD->getDeclName() << Record->getTagKind();
|
|
|
|
} else if (NumNamedMembers < 1) {
|
2008-11-20 09:38:18 +03:00
|
|
|
Diag(FD->getLocation(), diag::err_flexible_array_empty_struct)
|
2008-11-24 08:29:24 +03:00
|
|
|
<< FD->getDeclName();
|
2007-09-15 02:20:54 +04:00
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
2007-07-11 21:01:13 +04:00
|
|
|
continue;
|
|
|
|
}
|
2010-05-27 00:19:07 +04:00
|
|
|
if (!FD->getType()->isDependentType() &&
|
|
|
|
!Context.getBaseElementType(FD->getType())->isPODType()) {
|
|
|
|
Diag(FD->getLocation(), diag::err_flexible_array_has_nonpod_type)
|
2010-05-27 00:46:24 +04:00
|
|
|
<< FD->getDeclName() << FD->getType();
|
2010-05-27 00:19:07 +04:00
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
|
|
|
continue;
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
// Okay, we have a legal flexible array member at the end of the struct.
|
2007-09-14 20:27:55 +04:00
|
|
|
if (Record)
|
|
|
|
Record->setHasFlexibleArrayMember(true);
|
2009-03-24 22:52:54 +03:00
|
|
|
} else if (!FDTy->isDependentType() &&
|
2009-09-09 19:08:12 +04:00
|
|
|
RequireCompleteType(FD->getLocation(), FD->getType(),
|
2009-03-24 22:52:54 +03:00
|
|
|
diag::err_field_incomplete)) {
|
|
|
|
// Incomplete type
|
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
|
|
|
continue;
|
2009-07-30 01:53:49 +04:00
|
|
|
} else if (const RecordType *FDTTy = FDTy->getAs<RecordType>()) {
|
2007-07-11 21:01:13 +04:00
|
|
|
if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
|
|
|
|
// If this is a member of a union, then entire union becomes "flexible".
|
2008-06-10 03:19:58 +04:00
|
|
|
if (Record && Record->isUnion()) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Record->setHasFlexibleArrayMember(true);
|
|
|
|
} else {
|
|
|
|
// If this is a struct/class and this is not the last element, reject
|
|
|
|
// it. Note that GCC supports variable sized arrays in the middle of
|
|
|
|
// structures.
|
2009-03-07 02:41:27 +03:00
|
|
|
if (i != NumFields-1)
|
|
|
|
Diag(FD->getLocation(), diag::ext_variable_sized_type_in_struct)
|
2009-04-25 22:52:45 +04:00
|
|
|
<< FD->getDeclName() << FD->getType();
|
2009-03-07 02:41:27 +03:00
|
|
|
else {
|
|
|
|
// We support flexible arrays at the end of structs in
|
|
|
|
// other structs as an extension.
|
|
|
|
Diag(FD->getLocation(), diag::ext_flexible_array_in_struct)
|
|
|
|
<< FD->getDeclName();
|
|
|
|
if (Record)
|
|
|
|
Record->setHasFlexibleArrayMember(true);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-08 05:18:33 +04:00
|
|
|
if (Record && FDTTy->getDecl()->hasObjectMember())
|
|
|
|
Record->setHasObjectMember(true);
|
2010-05-15 15:32:37 +04:00
|
|
|
} else if (FDTy->isObjCObjectType()) {
|
2009-03-24 22:52:54 +03:00
|
|
|
/// A field cannot be an Objective-c object
|
2009-02-21 01:59:16 +03:00
|
|
|
Diag(FD->getLocation(), diag::err_statically_allocated_object);
|
2007-10-13 02:10:42 +04:00
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
|
|
|
continue;
|
2009-08-05 01:02:39 +04:00
|
|
|
} else if (getLangOptions().ObjC1 &&
|
|
|
|
getLangOptions().getGCMode() != LangOptions::NonGC &&
|
|
|
|
Record &&
|
|
|
|
(FD->getType()->isObjCObjectPointerType() ||
|
|
|
|
FD->getType().isObjCGCStrong()))
|
2009-07-08 05:18:33 +04:00
|
|
|
Record->setHasObjectMember(true);
|
2010-06-16 02:44:06 +04:00
|
|
|
else if (Context.getAsArrayType(FD->getType())) {
|
|
|
|
QualType BaseType = Context.getBaseElementType(FD->getType());
|
|
|
|
if (Record && BaseType->isRecordType() &&
|
|
|
|
BaseType->getAs<RecordType>()->getDecl()->hasObjectMember())
|
|
|
|
Record->setHasObjectMember(true);
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
// Keep track of the number of named members.
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61940 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-08 23:45:30 +03:00
|
|
|
if (FD->getIdentifier())
|
2007-07-11 21:01:13 +04:00
|
|
|
++NumNamedMembers;
|
|
|
|
}
|
2009-01-05 23:52:13 +03:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Okay, we successfully defined 'Record'.
|
2008-02-06 03:51:33 +03:00
|
|
|
if (Record) {
|
2010-09-29 04:15:42 +04:00
|
|
|
bool Completed = false;
|
|
|
|
if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record)) {
|
|
|
|
if (!CXXRecord->isInvalidDecl()) {
|
|
|
|
// Set access bits correctly on the directly-declared conversions.
|
|
|
|
UnresolvedSetImpl *Convs = CXXRecord->getConversionFunctions();
|
|
|
|
for (UnresolvedSetIterator I = Convs->begin(), E = Convs->end();
|
|
|
|
I != E; ++I)
|
|
|
|
Convs->setAccess(I, (*I)->getAccess());
|
|
|
|
|
|
|
|
if (!CXXRecord->isDependentType()) {
|
|
|
|
// Add any implicitly-declared members to this class.
|
|
|
|
AddImplicitlyDeclaredMembersToClass(CXXRecord);
|
|
|
|
|
|
|
|
// If we have virtual base classes, we may end up finding multiple
|
|
|
|
// final overriders for a given virtual function. Check for this
|
|
|
|
// problem now.
|
|
|
|
if (CXXRecord->getNumVBases()) {
|
|
|
|
CXXFinalOverriderMap FinalOverriders;
|
|
|
|
CXXRecord->getFinalOverriders(FinalOverriders);
|
|
|
|
|
|
|
|
for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(),
|
|
|
|
MEnd = FinalOverriders.end();
|
|
|
|
M != MEnd; ++M) {
|
|
|
|
for (OverridingMethods::iterator SO = M->second.begin(),
|
|
|
|
SOEnd = M->second.end();
|
|
|
|
SO != SOEnd; ++SO) {
|
|
|
|
assert(SO->second.size() > 0 &&
|
|
|
|
"Virtual function without overridding functions?");
|
|
|
|
if (SO->second.size() == 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// C++ [class.virtual]p2:
|
|
|
|
// In a derived class, if a virtual member function of a base
|
|
|
|
// class subobject has more than one final overrider the
|
|
|
|
// program is ill-formed.
|
|
|
|
Diag(Record->getLocation(), diag::err_multiple_final_overriders)
|
|
|
|
<< (NamedDecl *)M->first << Record;
|
|
|
|
Diag(M->first->getLocation(),
|
|
|
|
diag::note_overridden_virtual_function);
|
|
|
|
for (OverridingMethods::overriding_iterator
|
|
|
|
OM = SO->second.begin(),
|
|
|
|
OMEnd = SO->second.end();
|
|
|
|
OM != OMEnd; ++OM)
|
|
|
|
Diag(OM->Method->getLocation(), diag::note_final_overrider)
|
|
|
|
<< (NamedDecl *)M->first << OM->Method->getParent();
|
|
|
|
|
|
|
|
Record->setInvalidDecl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CXXRecord->completeDefinition(&FinalOverriders);
|
|
|
|
Completed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Completed)
|
|
|
|
Record->completeDefinition();
|
2008-02-06 03:51:33 +03:00
|
|
|
} else {
|
2009-05-21 13:52:38 +04:00
|
|
|
ObjCIvarDecl **ClsFields =
|
|
|
|
reinterpret_cast<ObjCIvarDecl**>(RecFields.data());
|
2008-12-13 23:28:25 +03:00
|
|
|
if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) {
|
2009-02-21 00:35:13 +03:00
|
|
|
ID->setLocEnd(RBrac);
|
2009-06-05 22:16:35 +04:00
|
|
|
// Add ivar's to class's DeclContext.
|
|
|
|
for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
|
|
|
|
ClsFields[i]->setLexicalDeclContext(ID);
|
2009-06-30 06:36:12 +04:00
|
|
|
ID->addDecl(ClsFields[i]);
|
2009-06-05 22:16:35 +04:00
|
|
|
}
|
2008-12-16 04:08:35 +03:00
|
|
|
// Must enforce the rule that ivars in the base classes may not be
|
|
|
|
// duplicates.
|
2010-02-24 02:41:11 +03:00
|
|
|
if (ID->getSuperClass())
|
|
|
|
DiagnoseDuplicateIvars(ID, ID->getSuperClass());
|
2009-09-09 19:08:12 +04:00
|
|
|
} else if (ObjCImplementationDecl *IMPDecl =
|
2009-02-24 01:00:08 +03:00
|
|
|
dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
|
2008-01-07 22:49:32 +03:00
|
|
|
assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl");
|
2009-06-05 22:16:35 +04:00
|
|
|
for (unsigned I = 0, N = RecFields.size(); I != N; ++I)
|
|
|
|
// Ivar declared in @implementation never belongs to the implementation.
|
|
|
|
// Only it is in implementation's lexical context.
|
2009-04-23 07:23:08 +04:00
|
|
|
ClsFields[I]->setLexicalDeclContext(IMPDecl);
|
2007-10-31 21:48:14 +03:00
|
|
|
CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac);
|
2010-02-23 02:04:20 +03:00
|
|
|
} else if (ObjCCategoryDecl *CDecl =
|
|
|
|
dyn_cast<ObjCCategoryDecl>(EnclosingDecl)) {
|
2010-04-07 02:43:48 +04:00
|
|
|
// case of ivars in class extension; all other cases have been
|
|
|
|
// reported as errors elsewhere.
|
|
|
|
// FIXME. Class extension does not have a LocEnd field.
|
|
|
|
// CDecl->setLocEnd(RBrac);
|
|
|
|
// Add ivar's to class extension's DeclContext.
|
|
|
|
for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
|
|
|
|
ClsFields[i]->setLexicalDeclContext(CDecl);
|
|
|
|
CDecl->addDecl(ClsFields[i]);
|
2010-02-23 02:04:20 +03:00
|
|
|
}
|
2007-09-26 22:27:25 +04:00
|
|
|
}
|
2007-09-15 01:08:27 +04:00
|
|
|
}
|
2008-10-03 21:33:35 +04:00
|
|
|
|
|
|
|
if (Attr)
|
2009-06-18 01:51:59 +04:00
|
|
|
ProcessDeclAttributeList(S, Record, Attr);
|
2010-08-05 10:57:20 +04:00
|
|
|
|
|
|
|
// If there's a #pragma GCC visibility in scope, and this isn't a subclass,
|
|
|
|
// set the visibility of this record.
|
|
|
|
if (Record && !Record->getDeclContext()->isRecord())
|
|
|
|
AddPushedVisibilityAttribute(Record);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2010-02-02 02:36:03 +03:00
|
|
|
/// \brief Determine whether the given integral value is representable within
|
|
|
|
/// the given type T.
|
|
|
|
static bool isRepresentableIntegerValue(ASTContext &Context,
|
|
|
|
llvm::APSInt &Value,
|
|
|
|
QualType T) {
|
2010-06-16 04:35:25 +04:00
|
|
|
assert(T->isIntegralType(Context) && "Integral type required!");
|
2010-04-15 19:53:31 +04:00
|
|
|
unsigned BitWidth = Context.getIntWidth(T);
|
2010-02-02 02:36:03 +03:00
|
|
|
|
2010-10-09 03:50:27 +04:00
|
|
|
if (Value.isUnsigned() || Value.isNonNegative()) {
|
|
|
|
if (T->isSignedIntegerType())
|
|
|
|
--BitWidth;
|
|
|
|
return Value.getActiveBits() <= BitWidth;
|
|
|
|
}
|
2010-02-02 02:36:03 +03:00
|
|
|
return Value.getMinSignedBits() <= BitWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// \brief Given an integral type, return the next larger integral type
|
|
|
|
// (or a NULL type of no such type exists).
|
|
|
|
static QualType getNextLargerIntegralType(ASTContext &Context, QualType T) {
|
|
|
|
// FIXME: Int128/UInt128 support, which also needs to be introduced into
|
|
|
|
// enum checking below.
|
2010-06-16 04:35:25 +04:00
|
|
|
assert(T->isIntegralType(Context) && "Integral type required!");
|
2010-02-02 02:36:03 +03:00
|
|
|
const unsigned NumTypes = 4;
|
|
|
|
QualType SignedIntegralTypes[NumTypes] = {
|
|
|
|
Context.ShortTy, Context.IntTy, Context.LongTy, Context.LongLongTy
|
|
|
|
};
|
|
|
|
QualType UnsignedIntegralTypes[NumTypes] = {
|
|
|
|
Context.UnsignedShortTy, Context.UnsignedIntTy, Context.UnsignedLongTy,
|
|
|
|
Context.UnsignedLongLongTy
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned BitWidth = Context.getTypeSize(T);
|
|
|
|
QualType *Types = T->isSignedIntegerType()? SignedIntegralTypes
|
|
|
|
: UnsignedIntegralTypes;
|
|
|
|
for (unsigned I = 0; I != NumTypes; ++I)
|
|
|
|
if (Context.getTypeSize(Types[I]) > BitWidth)
|
|
|
|
return Types[I];
|
|
|
|
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
2009-03-17 22:05:46 +03:00
|
|
|
EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum,
|
|
|
|
EnumConstantDecl *LastEnumConst,
|
|
|
|
SourceLocation IdLoc,
|
|
|
|
IdentifierInfo *Id,
|
2010-08-24 03:25:46 +04:00
|
|
|
Expr *Val) {
|
2010-02-02 02:36:03 +03:00
|
|
|
unsigned IntWidth = Context.Target.getIntWidth();
|
|
|
|
llvm::APSInt EnumVal(IntWidth);
|
2009-03-17 22:05:46 +03:00
|
|
|
QualType EltTy;
|
2009-11-06 03:03:12 +03:00
|
|
|
if (Val) {
|
2010-03-02 20:53:14 +03:00
|
|
|
if (Enum->isDependentType() || Val->isTypeDependent())
|
2009-11-06 03:03:12 +03:00
|
|
|
EltTy = Context.DependentTy;
|
|
|
|
else {
|
|
|
|
// C99 6.7.2.2p2: Make sure we have an integer constant expression.
|
|
|
|
SourceLocation ExpLoc;
|
2010-03-02 20:53:14 +03:00
|
|
|
if (!Val->isValueDependent() &&
|
|
|
|
VerifyIntegerConstantExpression(Val, &EnumVal)) {
|
2009-11-06 03:03:12 +03:00
|
|
|
Val = 0;
|
2010-02-02 02:36:03 +03:00
|
|
|
} else {
|
|
|
|
if (!getLangOptions().CPlusPlus) {
|
|
|
|
// C99 6.7.2.2p2:
|
|
|
|
// The expression that defines the value of an enumeration constant
|
|
|
|
// shall be an integer constant expression that has a value
|
|
|
|
// representable as an int.
|
|
|
|
|
|
|
|
// Complain if the value is not representable in an int.
|
|
|
|
if (!isRepresentableIntegerValue(Context, EnumVal, Context.IntTy))
|
|
|
|
Diag(IdLoc, diag::ext_enum_value_not_int)
|
|
|
|
<< EnumVal.toString(10) << Val->getSourceRange()
|
2010-02-18 01:40:11 +03:00
|
|
|
<< (EnumVal.isUnsigned() || EnumVal.isNonNegative());
|
2010-02-02 02:36:03 +03:00
|
|
|
else if (!Context.hasSameType(Val->getType(), Context.IntTy)) {
|
|
|
|
// Force the type of the expression to 'int'.
|
2010-08-25 15:45:40 +04:00
|
|
|
ImpCastExprToType(Val, Context.IntTy, CK_IntegralCast);
|
2010-02-02 02:36:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-09 03:50:27 +04:00
|
|
|
if (Enum->isFixed()) {
|
|
|
|
EltTy = Enum->getIntegerType();
|
|
|
|
|
|
|
|
// C++0x [dcl.enum]p5:
|
|
|
|
// ... if the initializing value of an enumerator cannot be
|
|
|
|
// represented by the underlying type, the program is ill-formed.
|
|
|
|
if (!isRepresentableIntegerValue(Context, EnumVal, EltTy))
|
|
|
|
Diag(IdLoc, diag::err_enumerator_too_large)
|
|
|
|
<< EltTy;
|
|
|
|
else
|
|
|
|
ImpCastExprToType(Val, EltTy, CK_IntegralCast);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// C++0x [dcl.enum]p5:
|
|
|
|
// If the underlying type is not fixed, the type of each enumerator
|
|
|
|
// is the type of its initializing value:
|
|
|
|
// - If an initializer is specified for an enumerator, the
|
|
|
|
// initializing value has the same type as the expression.
|
|
|
|
EltTy = Val->getType();
|
|
|
|
}
|
2009-11-06 03:03:12 +03:00
|
|
|
}
|
2009-03-17 22:05:46 +03:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-17 22:05:46 +03:00
|
|
|
if (!Val) {
|
2009-12-11 04:34:50 +03:00
|
|
|
if (Enum->isDependentType())
|
|
|
|
EltTy = Context.DependentTy;
|
2010-02-02 02:36:03 +03:00
|
|
|
else if (!LastEnumConst) {
|
|
|
|
// C++0x [dcl.enum]p5:
|
|
|
|
// If the underlying type is not fixed, the type of each enumerator
|
|
|
|
// is the type of its initializing value:
|
|
|
|
// - If no initializer is specified for the first enumerator, the
|
|
|
|
// initializing value has an unspecified integral type.
|
|
|
|
//
|
|
|
|
// GCC uses 'int' for its unspecified integral type, as does
|
|
|
|
// C99 6.7.2.2p3.
|
2010-10-09 03:50:27 +04:00
|
|
|
if (Enum->isFixed()) {
|
|
|
|
EltTy = Enum->getIntegerType();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
EltTy = Context.IntTy;
|
|
|
|
}
|
2010-02-02 02:36:03 +03:00
|
|
|
} else {
|
2009-03-17 22:05:46 +03:00
|
|
|
// Assign the last value + 1.
|
|
|
|
EnumVal = LastEnumConst->getInitVal();
|
|
|
|
++EnumVal;
|
2010-02-02 02:36:03 +03:00
|
|
|
EltTy = LastEnumConst->getType();
|
2009-03-17 22:05:46 +03:00
|
|
|
|
|
|
|
// Check for overflow on increment.
|
2010-02-02 02:36:03 +03:00
|
|
|
if (EnumVal < LastEnumConst->getInitVal()) {
|
|
|
|
// C++0x [dcl.enum]p5:
|
|
|
|
// If the underlying type is not fixed, the type of each enumerator
|
|
|
|
// is the type of its initializing value:
|
|
|
|
//
|
|
|
|
// - Otherwise the type of the initializing value is the same as
|
|
|
|
// the type of the initializing value of the preceding enumerator
|
|
|
|
// unless the incremented value is not representable in that type,
|
|
|
|
// in which case the type is an unspecified integral type
|
|
|
|
// sufficient to contain the incremented value. If no such type
|
|
|
|
// exists, the program is ill-formed.
|
|
|
|
QualType T = getNextLargerIntegralType(Context, EltTy);
|
2010-10-09 03:50:27 +04:00
|
|
|
if (T.isNull() || Enum->isFixed()) {
|
2010-02-02 02:36:03 +03:00
|
|
|
// There is no integral type larger enough to represent this
|
|
|
|
// value. Complain, then allow the value to wrap around.
|
|
|
|
EnumVal = LastEnumConst->getInitVal();
|
|
|
|
EnumVal.zext(EnumVal.getBitWidth() * 2);
|
2010-10-09 03:50:27 +04:00
|
|
|
++EnumVal;
|
|
|
|
if (Enum->isFixed())
|
|
|
|
// When the underlying type is fixed, this is ill-formed.
|
|
|
|
Diag(IdLoc, diag::err_enumerator_wrapped)
|
|
|
|
<< EnumVal.toString(10)
|
|
|
|
<< EltTy;
|
|
|
|
else
|
|
|
|
Diag(IdLoc, diag::warn_enumerator_too_large)
|
|
|
|
<< EnumVal.toString(10);
|
2010-02-02 02:36:03 +03:00
|
|
|
} else {
|
|
|
|
EltTy = T;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the last enumerator's value, extent that type to the
|
|
|
|
// type that is supposed to be large enough to represent the incremented
|
|
|
|
// value, then increment.
|
|
|
|
EnumVal = LastEnumConst->getInitVal();
|
|
|
|
EnumVal.setIsSigned(EltTy->isSignedIntegerType());
|
2010-04-15 19:53:31 +04:00
|
|
|
EnumVal.zextOrTrunc(Context.getIntWidth(EltTy));
|
2010-02-02 02:36:03 +03:00
|
|
|
++EnumVal;
|
|
|
|
|
|
|
|
// If we're not in C++, diagnose the overflow of enumerator values,
|
|
|
|
// which in C99 means that the enumerator value is not representable in
|
|
|
|
// an int (C99 6.7.2.2p2). However, we support GCC's extension that
|
|
|
|
// permits enumerator values that are representable in some larger
|
|
|
|
// integral type.
|
|
|
|
if (!getLangOptions().CPlusPlus && !T.isNull())
|
|
|
|
Diag(IdLoc, diag::warn_enum_value_overflow);
|
|
|
|
} else if (!getLangOptions().CPlusPlus &&
|
|
|
|
!isRepresentableIntegerValue(Context, EnumVal, EltTy)) {
|
|
|
|
// Enforce C99 6.7.2.2p2 even when we compute the next value.
|
|
|
|
Diag(IdLoc, diag::ext_enum_value_not_int)
|
|
|
|
<< EnumVal.toString(10) << 1;
|
|
|
|
}
|
2009-03-17 22:05:46 +03:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-03-02 20:53:14 +03:00
|
|
|
if (!EltTy->isDependentType()) {
|
2010-02-02 02:36:03 +03:00
|
|
|
// Make the enumerator value match the signedness and size of the
|
|
|
|
// enumerator's type.
|
2010-04-15 19:53:31 +04:00
|
|
|
EnumVal.zextOrTrunc(Context.getIntWidth(EltTy));
|
2010-02-02 02:36:03 +03:00
|
|
|
EnumVal.setIsSigned(EltTy->isSignedIntegerType());
|
|
|
|
}
|
2009-11-06 03:03:12 +03:00
|
|
|
|
2009-03-17 22:05:46 +03:00
|
|
|
return EnumConstantDecl::Create(Context, Enum, IdLoc, Id, EltTy,
|
2009-09-09 19:08:12 +04:00
|
|
|
Val, EnumVal);
|
2009-03-17 22:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
Decl *Sema::ActOnEnumConstant(Scope *S, Decl *theEnumDecl,
|
|
|
|
Decl *lastEnumConst,
|
2009-03-28 22:18:32 +03:00
|
|
|
SourceLocation IdLoc,
|
|
|
|
IdentifierInfo *Id,
|
|
|
|
SourceLocation EqualLoc, ExprTy *val) {
|
2010-08-21 13:40:31 +04:00
|
|
|
EnumDecl *TheEnumDecl = cast<EnumDecl>(theEnumDecl);
|
2007-07-11 21:01:13 +04:00
|
|
|
EnumConstantDecl *LastEnumConst =
|
2010-08-21 13:40:31 +04:00
|
|
|
cast_or_null<EnumConstantDecl>(lastEnumConst);
|
2007-07-11 21:01:13 +04:00
|
|
|
Expr *Val = static_cast<Expr*>(val);
|
|
|
|
|
2007-08-26 10:24:45 +04:00
|
|
|
// The scope passed in may not be a decl scope. Zip up the scope tree until
|
|
|
|
// we find one that is.
|
2009-01-12 21:45:55 +03:00
|
|
|
S = getNonFieldDeclScope(S);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Verify that there isn't already something declared with this name in this
|
|
|
|
// scope.
|
2010-04-16 02:33:43 +04:00
|
|
|
NamedDecl *PrevDecl = LookupSingleName(S, Id, IdLoc, LookupOrdinaryName,
|
2010-01-19 09:06:57 +03:00
|
|
|
ForRedeclaration);
|
2008-12-08 21:40:42 +03:00
|
|
|
if (PrevDecl && PrevDecl->isTemplateParameter()) {
|
2008-12-05 21:15:24 +03:00
|
|
|
// Maybe we will complain about the shadowed template parameter.
|
|
|
|
DiagnoseTemplateParameterShadow(IdLoc, PrevDecl);
|
|
|
|
// Just pretend that we didn't see the previous declaration.
|
|
|
|
PrevDecl = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PrevDecl) {
|
2008-07-17 01:01:53 +04:00
|
|
|
// When in C++, we may get a TagDecl with the same name; in this case the
|
|
|
|
// enum constant will 'hide' the tag.
|
|
|
|
assert((getLangOptions().CPlusPlus || !isa<TagDecl>(PrevDecl)) &&
|
|
|
|
"Received TagDecl when not in C++!");
|
2008-09-10 01:18:04 +04:00
|
|
|
if (!isa<TagDecl>(PrevDecl) && isDeclInScope(PrevDecl, CurContext, S)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
if (isa<EnumConstantDecl>(PrevDecl))
|
2008-11-19 11:23:25 +03:00
|
|
|
Diag(IdLoc, diag::err_redefinition_of_enumerator) << Id;
|
2007-07-11 21:01:13 +04:00
|
|
|
else
|
2008-11-19 11:23:25 +03:00
|
|
|
Diag(IdLoc, diag::err_redefinition) << Id;
|
2008-11-24 02:12:31 +03:00
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_definition);
|
2010-08-21 13:40:31 +04:00
|
|
|
return 0;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-17 22:05:46 +03:00
|
|
|
EnumConstantDecl *New = CheckEnumConstant(TheEnumDecl, LastEnumConst,
|
2010-08-24 03:25:46 +04:00
|
|
|
IdLoc, Id, Val);
|
2007-08-28 01:16:18 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Register this decl in the current scope stack.
|
2010-01-23 03:46:32 +03:00
|
|
|
if (New) {
|
|
|
|
New->setAccess(TheEnumDecl->getAccess());
|
2009-03-17 22:05:46 +03:00
|
|
|
PushOnScopeChains(New, S);
|
2010-01-23 03:46:32 +03:00
|
|
|
}
|
2008-12-17 05:04:30 +03:00
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
return New;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-05-16 11:06:02 +04:00
|
|
|
void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc,
|
2010-08-21 13:40:31 +04:00
|
|
|
SourceLocation RBraceLoc, Decl *EnumDeclX,
|
|
|
|
Decl **Elements, unsigned NumElements,
|
2009-08-08 18:36:57 +04:00
|
|
|
Scope *S, AttributeList *Attr) {
|
2010-08-21 13:40:31 +04:00
|
|
|
EnumDecl *Enum = cast<EnumDecl>(EnumDeclX);
|
2009-01-05 22:45:36 +03:00
|
|
|
QualType EnumType = Context.getTypeDeclType(Enum);
|
2009-08-08 18:36:57 +04:00
|
|
|
|
|
|
|
if (Attr)
|
|
|
|
ProcessDeclAttributeList(S, Enum, Attr);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-12-11 04:34:50 +03:00
|
|
|
if (Enum->isDependentType()) {
|
|
|
|
for (unsigned i = 0; i != NumElements; ++i) {
|
|
|
|
EnumConstantDecl *ECD =
|
2010-08-21 13:40:31 +04:00
|
|
|
cast_or_null<EnumConstantDecl>(Elements[i]);
|
2009-12-11 04:34:50 +03:00
|
|
|
if (!ECD) continue;
|
|
|
|
|
|
|
|
ECD->setType(EnumType);
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:49:23 +04:00
|
|
|
Enum->completeDefinition(Context.DependentTy, Context.DependentTy, 0, 0);
|
2009-12-11 04:34:50 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-28 09:10:31 +04:00
|
|
|
// TODO: If the result value doesn't fit in an int, it must be a long or long
|
|
|
|
// long value. ISO C does not support this, but GCC does as an extension,
|
|
|
|
// emit a warning.
|
2008-03-05 21:54:05 +03:00
|
|
|
unsigned IntWidth = Context.Target.getIntWidth();
|
2009-08-08 18:36:57 +04:00
|
|
|
unsigned CharWidth = Context.Target.getCharWidth();
|
|
|
|
unsigned ShortWidth = Context.Target.getShortWidth();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-08-28 10:15:15 +04:00
|
|
|
// Verify that all the values are okay, compute the size of the values, and
|
|
|
|
// reverse the list.
|
|
|
|
unsigned NumNegativeBits = 0;
|
|
|
|
unsigned NumPositiveBits = 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-08-28 10:15:15 +04:00
|
|
|
// Keep track of whether all elements have type int.
|
|
|
|
bool AllElementsInt = true;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
for (unsigned i = 0; i != NumElements; ++i) {
|
|
|
|
EnumConstantDecl *ECD =
|
2010-08-21 13:40:31 +04:00
|
|
|
cast_or_null<EnumConstantDecl>(Elements[i]);
|
2007-07-11 21:01:13 +04:00
|
|
|
if (!ECD) continue; // Already issued a diagnostic.
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-08-28 09:27:00 +04:00
|
|
|
const llvm::APSInt &InitVal = ECD->getInitVal();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-08-28 10:15:15 +04:00
|
|
|
// Keep track of the size of positive and negative values.
|
2008-02-26 03:33:57 +03:00
|
|
|
if (InitVal.isUnsigned() || InitVal.isNonNegative())
|
2008-01-15 00:47:29 +03:00
|
|
|
NumPositiveBits = std::max(NumPositiveBits,
|
|
|
|
(unsigned)InitVal.getActiveBits());
|
2007-08-28 10:15:15 +04:00
|
|
|
else
|
2008-01-15 00:47:29 +03:00
|
|
|
NumNegativeBits = std::max(NumNegativeBits,
|
|
|
|
(unsigned)InitVal.getMinSignedBits());
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-08-28 10:15:15 +04:00
|
|
|
// Keep track of whether every enum element has type int (very commmon).
|
|
|
|
if (AllElementsInt)
|
2009-09-09 19:08:12 +04:00
|
|
|
AllElementsInt = ECD->getType() == Context.IntTy;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-08-28 10:15:15 +04:00
|
|
|
// Figure out the type that should be used for this enum.
|
2009-08-08 18:36:57 +04:00
|
|
|
// FIXME: Support -fshort-enums.
|
2007-08-28 10:15:15 +04:00
|
|
|
QualType BestType;
|
2007-08-29 21:31:48 +04:00
|
|
|
unsigned BestWidth;
|
2009-08-08 18:36:57 +04:00
|
|
|
|
2009-12-09 12:09:27 +03:00
|
|
|
// C++0x N3000 [conv.prom]p3:
|
|
|
|
// An rvalue of an unscoped enumeration type whose underlying
|
|
|
|
// type is not fixed can be converted to an rvalue of the first
|
|
|
|
// of the following types that can represent all the values of
|
|
|
|
// the enumeration: int, unsigned int, long int, unsigned long
|
|
|
|
// int, long long int, or unsigned long long int.
|
|
|
|
// C99 6.4.4.3p2:
|
|
|
|
// An identifier declared as an enumeration constant has type int.
|
|
|
|
// The C99 rule is modified by a gcc extension
|
|
|
|
QualType BestPromotionType;
|
|
|
|
|
2009-08-08 18:36:57 +04:00
|
|
|
bool Packed = Enum->getAttr<PackedAttr>() ? true : false;
|
2010-10-08 04:25:19 +04:00
|
|
|
// -fshort-enums is the equivalent to specifying the packed attribute on all
|
|
|
|
// enum definitions.
|
|
|
|
if (LangOpts.ShortEnums)
|
|
|
|
Packed = true;
|
2009-08-08 18:36:57 +04:00
|
|
|
|
2010-10-09 03:50:27 +04:00
|
|
|
if (Enum->isFixed()) {
|
|
|
|
BestType = BestPromotionType = Enum->getIntegerType();
|
2010-10-12 18:07:59 +04:00
|
|
|
// We don't need to set BestWidth, because BestType is going to be the type
|
|
|
|
// of the enumerators, but we do anyway because otherwise some compilers
|
|
|
|
// warn that it might be used uninitialized.
|
|
|
|
BestWidth = CharWidth;
|
2010-10-09 03:50:27 +04:00
|
|
|
}
|
|
|
|
else if (NumNegativeBits) {
|
2009-09-09 19:08:12 +04:00
|
|
|
// If there is a negative value, figure out the smallest integer type (of
|
2007-08-28 10:15:15 +04:00
|
|
|
// int/long/longlong) that fits.
|
2009-08-08 18:36:57 +04:00
|
|
|
// If it's packed, check also if it fits a char or a short.
|
|
|
|
if (Packed && NumNegativeBits <= CharWidth && NumPositiveBits < CharWidth) {
|
2009-12-09 12:09:27 +03:00
|
|
|
BestType = Context.SignedCharTy;
|
|
|
|
BestWidth = CharWidth;
|
2009-09-09 19:08:12 +04:00
|
|
|
} else if (Packed && NumNegativeBits <= ShortWidth &&
|
2009-08-08 18:36:57 +04:00
|
|
|
NumPositiveBits < ShortWidth) {
|
2009-12-09 12:09:27 +03:00
|
|
|
BestType = Context.ShortTy;
|
|
|
|
BestWidth = ShortWidth;
|
|
|
|
} else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
|
2007-08-28 10:15:15 +04:00
|
|
|
BestType = Context.IntTy;
|
2007-08-29 21:31:48 +04:00
|
|
|
BestWidth = IntWidth;
|
|
|
|
} else {
|
2008-03-05 21:54:05 +03:00
|
|
|
BestWidth = Context.Target.getLongWidth();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-12-09 12:09:27 +03:00
|
|
|
if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) {
|
2007-08-28 10:15:15 +04:00
|
|
|
BestType = Context.LongTy;
|
2009-12-09 12:09:27 +03:00
|
|
|
} else {
|
2008-03-05 21:54:05 +03:00
|
|
|
BestWidth = Context.Target.getLongLongWidth();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-08-29 21:31:48 +04:00
|
|
|
if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth)
|
2007-08-28 10:15:15 +04:00
|
|
|
Diag(Enum->getLocation(), diag::warn_enum_too_large);
|
|
|
|
BestType = Context.LongLongTy;
|
|
|
|
}
|
|
|
|
}
|
2009-12-09 12:09:27 +03:00
|
|
|
BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : BestType);
|
2007-08-28 10:15:15 +04:00
|
|
|
} else {
|
2010-02-02 23:10:50 +03:00
|
|
|
// If there is no negative value, figure out the smallest type that fits
|
|
|
|
// all of the enumerator values.
|
2009-08-08 18:36:57 +04:00
|
|
|
// If it's packed, check also if it fits a char or a short.
|
|
|
|
if (Packed && NumPositiveBits <= CharWidth) {
|
2009-12-09 12:09:27 +03:00
|
|
|
BestType = Context.UnsignedCharTy;
|
|
|
|
BestPromotionType = Context.IntTy;
|
|
|
|
BestWidth = CharWidth;
|
2009-08-08 18:36:57 +04:00
|
|
|
} else if (Packed && NumPositiveBits <= ShortWidth) {
|
2009-12-09 12:09:27 +03:00
|
|
|
BestType = Context.UnsignedShortTy;
|
|
|
|
BestPromotionType = Context.IntTy;
|
|
|
|
BestWidth = ShortWidth;
|
|
|
|
} else if (NumPositiveBits <= IntWidth) {
|
2007-08-28 10:15:15 +04:00
|
|
|
BestType = Context.UnsignedIntTy;
|
2007-08-29 21:31:48 +04:00
|
|
|
BestWidth = IntWidth;
|
2010-02-02 23:10:50 +03:00
|
|
|
BestPromotionType
|
|
|
|
= (NumPositiveBits == BestWidth || !getLangOptions().CPlusPlus)
|
|
|
|
? Context.UnsignedIntTy : Context.IntTy;
|
2007-08-29 21:31:48 +04:00
|
|
|
} else if (NumPositiveBits <=
|
2008-03-05 21:54:05 +03:00
|
|
|
(BestWidth = Context.Target.getLongWidth())) {
|
2007-08-28 10:15:15 +04:00
|
|
|
BestType = Context.UnsignedLongTy;
|
2010-02-02 23:10:50 +03:00
|
|
|
BestPromotionType
|
|
|
|
= (NumPositiveBits == BestWidth || !getLangOptions().CPlusPlus)
|
|
|
|
? Context.UnsignedLongTy : Context.LongTy;
|
2008-03-05 21:54:05 +03:00
|
|
|
} else {
|
|
|
|
BestWidth = Context.Target.getLongLongWidth();
|
2007-08-29 21:31:48 +04:00
|
|
|
assert(NumPositiveBits <= BestWidth &&
|
2007-08-28 10:15:15 +04:00
|
|
|
"How could an initializer get larger than ULL?");
|
|
|
|
BestType = Context.UnsignedLongLongTy;
|
2010-02-02 23:10:50 +03:00
|
|
|
BestPromotionType
|
|
|
|
= (NumPositiveBits == BestWidth || !getLangOptions().CPlusPlus)
|
|
|
|
? Context.UnsignedLongLongTy : Context.LongLongTy;
|
2007-08-28 10:15:15 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-08-29 21:31:48 +04:00
|
|
|
// Loop over all of the enumerator constants, changing their types to match
|
|
|
|
// the type of the enum if needed.
|
|
|
|
for (unsigned i = 0; i != NumElements; ++i) {
|
2010-08-21 13:40:31 +04:00
|
|
|
EnumConstantDecl *ECD = cast_or_null<EnumConstantDecl>(Elements[i]);
|
2007-08-29 21:31:48 +04:00
|
|
|
if (!ECD) continue; // Already issued a diagnostic.
|
|
|
|
|
|
|
|
// Standard C says the enumerators have int type, but we allow, as an
|
|
|
|
// extension, the enumerators to be larger than int size. If each
|
|
|
|
// enumerator value fits in an int, type it as an int, otherwise type it the
|
|
|
|
// same as the enumerator decl itself. This means that in "enum { X = 1U }"
|
|
|
|
// that X has type 'int', not 'unsigned'.
|
|
|
|
|
|
|
|
// Determine whether the value fits into an int.
|
|
|
|
llvm::APSInt InitVal = ECD->getInitVal();
|
|
|
|
|
|
|
|
// If it fits into an integer type, force it. Otherwise force it to match
|
|
|
|
// the enum decl type.
|
|
|
|
QualType NewTy;
|
|
|
|
unsigned NewWidth;
|
|
|
|
bool NewSign;
|
2010-02-02 02:36:03 +03:00
|
|
|
if (!getLangOptions().CPlusPlus &&
|
|
|
|
isRepresentableIntegerValue(Context, InitVal, Context.IntTy)) {
|
2007-08-29 21:31:48 +04:00
|
|
|
NewTy = Context.IntTy;
|
|
|
|
NewWidth = IntWidth;
|
|
|
|
NewSign = true;
|
|
|
|
} else if (ECD->getType() == BestType) {
|
|
|
|
// Already the right type!
|
2008-12-12 05:00:36 +03:00
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
// C++ [dcl.enum]p4: Following the closing brace of an
|
|
|
|
// enum-specifier, each enumerator has the type of its
|
2009-09-09 19:08:12 +04:00
|
|
|
// enumeration.
|
2008-12-12 05:00:36 +03:00
|
|
|
ECD->setType(EnumType);
|
2007-08-29 21:31:48 +04:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
NewTy = BestType;
|
|
|
|
NewWidth = BestWidth;
|
|
|
|
NewSign = BestType->isSignedIntegerType();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the APSInt value.
|
|
|
|
InitVal.extOrTrunc(NewWidth);
|
|
|
|
InitVal.setIsSigned(NewSign);
|
|
|
|
ECD->setInitVal(InitVal);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-08-29 21:31:48 +04:00
|
|
|
// Adjust the Expr initializer and type.
|
2009-01-15 22:19:42 +03:00
|
|
|
if (ECD->getInitExpr())
|
2010-08-07 10:22:56 +04:00
|
|
|
ECD->setInitExpr(ImplicitCastExpr::Create(Context, NewTy,
|
2010-08-25 15:45:40 +04:00
|
|
|
CK_IntegralCast,
|
2010-08-07 10:22:56 +04:00
|
|
|
ECD->getInitExpr(),
|
|
|
|
/*base paths*/ 0,
|
2010-08-25 14:28:54 +04:00
|
|
|
VK_RValue));
|
2008-12-12 05:00:36 +03:00
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
// C++ [dcl.enum]p4: Following the closing brace of an
|
|
|
|
// enum-specifier, each enumerator has the type of its
|
2009-09-09 19:08:12 +04:00
|
|
|
// enumeration.
|
2008-12-12 05:00:36 +03:00
|
|
|
ECD->setType(EnumType);
|
|
|
|
else
|
|
|
|
ECD->setType(NewTy);
|
2007-08-29 21:31:48 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-05-06 12:49:23 +04:00
|
|
|
Enum->completeDefinition(BestType, BestPromotionType,
|
|
|
|
NumPositiveBits, NumNegativeBits);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2010-08-24 03:25:46 +04:00
|
|
|
Decl *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc, Expr *expr) {
|
|
|
|
StringLiteral *AsmString = cast<StringLiteral>(expr);
|
2008-12-13 19:23:55 +03:00
|
|
|
|
2009-05-30 04:08:05 +04:00
|
|
|
FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext,
|
|
|
|
Loc, AsmString);
|
2009-06-30 06:36:12 +04:00
|
|
|
CurContext->addDecl(New);
|
2010-08-21 13:40:31 +04:00
|
|
|
return New;
|
2008-02-08 03:33:21 +03:00
|
|
|
}
|
2009-06-05 06:44:36 +04:00
|
|
|
|
|
|
|
void Sema::ActOnPragmaWeakID(IdentifierInfo* Name,
|
|
|
|
SourceLocation PragmaLoc,
|
|
|
|
SourceLocation NameLoc) {
|
2010-04-16 02:33:43 +04:00
|
|
|
Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, LookupOrdinaryName);
|
2009-06-05 06:44:36 +04:00
|
|
|
|
|
|
|
if (PrevDecl) {
|
2010-08-19 03:23:40 +04:00
|
|
|
PrevDecl->addAttr(::new (Context) WeakAttr(PragmaLoc, Context));
|
2009-07-30 07:15:39 +04:00
|
|
|
} else {
|
|
|
|
(void)WeakUndeclaredIdentifiers.insert(
|
|
|
|
std::pair<IdentifierInfo*,WeakInfo>
|
|
|
|
(Name, WeakInfo((IdentifierInfo*)0, NameLoc)));
|
2009-06-05 06:44:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::ActOnPragmaWeakAlias(IdentifierInfo* Name,
|
|
|
|
IdentifierInfo* AliasName,
|
|
|
|
SourceLocation PragmaLoc,
|
|
|
|
SourceLocation NameLoc,
|
|
|
|
SourceLocation AliasNameLoc) {
|
2010-04-16 02:33:43 +04:00
|
|
|
Decl *PrevDecl = LookupSingleName(TUScope, AliasName, AliasNameLoc,
|
|
|
|
LookupOrdinaryName);
|
2009-07-30 07:15:39 +04:00
|
|
|
WeakInfo W = WeakInfo(Name, NameLoc);
|
2009-06-05 06:44:36 +04:00
|
|
|
|
|
|
|
if (PrevDecl) {
|
2009-07-30 07:15:39 +04:00
|
|
|
if (!PrevDecl->hasAttr<AliasAttr>())
|
|
|
|
if (NamedDecl *ND = dyn_cast<NamedDecl>(PrevDecl))
|
2009-07-31 06:52:19 +04:00
|
|
|
DeclApplyPragmaWeak(TUScope, ND, W);
|
2009-07-30 07:15:39 +04:00
|
|
|
} else {
|
|
|
|
(void)WeakUndeclaredIdentifiers.insert(
|
|
|
|
std::pair<IdentifierInfo*,WeakInfo>(AliasName, W));
|
2009-06-05 06:44:36 +04:00
|
|
|
}
|
|
|
|
}
|