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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
2009-05-14 05:09:04 +04:00
|
|
|
#include "SemaInherit.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"
|
2008-08-11 09:35:13 +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"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/Parse/DeclSpec.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2008-01-31 02:46:05 +03:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
// 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"
|
2008-01-31 02:46:05 +03:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2008-12-11 23:41:00 +03:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-12-24 00:05:05 +03:00
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
2007-07-11 21:01:13 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-03-05 04:25:28 +03:00
|
|
|
/// getDeclName - Return a pretty name for the specified decl if possible, or
|
|
|
|
/// an empty string if not. This is used for pretty crash reporting.
|
2009-03-28 22:18:32 +03:00
|
|
|
std::string Sema::getDeclName(DeclPtrTy d) {
|
|
|
|
Decl *D = d.getAs<Decl>();
|
2009-03-05 04:25:28 +03:00
|
|
|
if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(D))
|
|
|
|
return DN->getQualifiedNameAsString();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2009-03-29 20:50:03 +04:00
|
|
|
Sema::DeclGroupPtrTy Sema::ConvertDeclToDeclGroup(DeclPtrTy Ptr) {
|
|
|
|
return DeclGroupPtrTy::make(DeclGroupRef(Ptr.getAs<Decl>()));
|
|
|
|
}
|
|
|
|
|
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.
|
2009-02-09 18:09:02 +03:00
|
|
|
Sema::TypeTy *Sema::getTypeName(IdentifierInfo &II, SourceLocation NameLoc,
|
2009-02-04 20:00:24 +03:00
|
|
|
Scope *S, const CXXScopeSpec *SS) {
|
Introduce a new expression type, UnresolvedDeclRefExpr, that describes
dependent qualified-ids such as
Fibonacci<N - 1>::value
where N is a template parameter. These references are "unresolved"
because the name is dependent and, therefore, cannot be resolved to a
declaration node (as we would do for a DeclRefExpr or
QualifiedDeclRefExpr). UnresolvedDeclRefExprs instantiate to
DeclRefExprs, QualifiedDeclRefExprs, etc.
Also, be a bit more careful about keeping only a single set of
specializations for a class template, and instantiating from the
definition of that template rather than a previous declaration. In
general, we need a better solution for this for all TagDecls, because
it's too easy to accidentally look at a declaration that isn't the
definition.
We can now process a simple Fibonacci computation described as a
template metaprogram.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67308 91177308-0d34-0410-b5e6-96231b3b80d8
2009-03-19 20:26:29 +03:00
|
|
|
// 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).
|
|
|
|
//
|
2009-05-11 23:58:34 +04:00
|
|
|
// We therefore do not perform any name lookup if the result would
|
|
|
|
// refer to a member of an unknown specialization.
|
|
|
|
if (SS && isUnknownSpecialization(*SS))
|
Introduce a new expression type, UnresolvedDeclRefExpr, that describes
dependent qualified-ids such as
Fibonacci<N - 1>::value
where N is a template parameter. These references are "unresolved"
because the name is dependent and, therefore, cannot be resolved to a
declaration node (as we would do for a DeclRefExpr or
QualifiedDeclRefExpr). UnresolvedDeclRefExprs instantiate to
DeclRefExprs, QualifiedDeclRefExprs, etc.
Also, be a bit more careful about keeping only a single set of
specializations for a class template, and instantiating from the
definition of that template rather than a previous declaration. In
general, we need a better solution for this for all TagDecls, because
it's too easy to accidentally look at a declaration that isn't the
definition.
We can now process a simple Fibonacci computation described as a
template metaprogram.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67308 91177308-0d34-0410-b5e6-96231b3b80d8
2009-03-19 20:26:29 +03:00
|
|
|
return 0;
|
|
|
|
|
2009-05-11 23:58:34 +04:00
|
|
|
LookupResult Result
|
|
|
|
= LookupParsedName(S, SS, &II, LookupOrdinaryName, false, false);
|
|
|
|
|
2009-02-17 01:07:16 +03:00
|
|
|
NamedDecl *IIDecl = 0;
|
2009-01-15 03:26:24 +03:00
|
|
|
switch (Result.getKind()) {
|
2009-02-17 01:07:16 +03:00
|
|
|
case LookupResult::NotFound:
|
|
|
|
case LookupResult::FoundOverloaded:
|
|
|
|
return 0;
|
2009-02-04 20:00:24 +03:00
|
|
|
|
2009-02-17 01:07:16 +03:00
|
|
|
case LookupResult::AmbiguousBaseSubobjectTypes:
|
|
|
|
case LookupResult::AmbiguousBaseSubobjects:
|
2009-04-02 01:51:26 +04:00
|
|
|
case LookupResult::AmbiguousReference: {
|
|
|
|
// 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-04-13 19:14:38 +04:00
|
|
|
if (!IIDecl ||
|
|
|
|
(*Res)->getLocation().getRawEncoding() <
|
|
|
|
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.
|
|
|
|
Result.Destroy();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2009-02-17 01:07:16 +03:00
|
|
|
DiagnoseAmbiguousLookup(Result, DeclarationName(&II), NameLoc);
|
2009-04-02 01:51:26 +04:00
|
|
|
break;
|
|
|
|
}
|
2009-02-04 20:00:24 +03:00
|
|
|
|
2009-02-17 01:07:16 +03:00
|
|
|
case LookupResult::Found:
|
|
|
|
IIDecl = Result.getAsDecl();
|
|
|
|
break;
|
2009-01-15 03:26:24 +03:00
|
|
|
}
|
|
|
|
|
2009-01-29 21:09:31 +03:00
|
|
|
if (IIDecl) {
|
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
|
|
|
QualType T;
|
|
|
|
|
2009-02-17 01:07:16 +03:00
|
|
|
if (TypeDecl *TD = dyn_cast<TypeDecl>(IIDecl)) {
|
2009-02-19 00:56:37 +03:00
|
|
|
// Check whether we can use this type
|
|
|
|
(void)DiagnoseUseOfDecl(IIDecl, NameLoc);
|
2009-05-11 02:57:19 +04:00
|
|
|
|
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
// C++ [temp.local]p2:
|
|
|
|
// Within the scope of a class template specialization or
|
|
|
|
// partial specialization, when the injected-class-name is
|
|
|
|
// not followed by a <, it is equivalent to the
|
|
|
|
// injected-class-name followed by the template-argument s
|
|
|
|
// of the class template specialization or partial
|
|
|
|
// specialization enclosed in <>.
|
|
|
|
if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TD))
|
|
|
|
if (RD->isInjectedClassName())
|
|
|
|
if (ClassTemplateDecl *Template = RD->getDescribedClassTemplate())
|
|
|
|
T = Template->getInjectedClassNameType(Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (T.isNull())
|
|
|
|
T = Context.getTypeDeclType(TD);
|
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
|
|
|
} else if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(IIDecl)) {
|
2009-02-19 00:56:37 +03:00
|
|
|
// Check whether we can use this interface.
|
|
|
|
(void)DiagnoseUseOfDecl(IIDecl, NameLoc);
|
2009-02-17 01:07:16 +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
|
|
|
T = Context.getObjCInterfaceType(IDecl);
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
|
2009-03-19 03:39:20 +03:00
|
|
|
if (SS)
|
|
|
|
T = getQualifiedNameType(*SS, T);
|
2009-02-17 01:07:16 +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
|
|
|
return T.getAsOpaquePtr();
|
2009-01-29 21:09:31 +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
|
|
|
|
2007-09-07 01:24:23 +04:00
|
|
|
return 0;
|
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.
|
|
|
|
LookupResult R = LookupName(S, &II, LookupTagName, false, false);
|
|
|
|
if (R.getKind() == LookupResult::Found)
|
|
|
|
if (const TagDecl *TD = dyn_cast<TagDecl>(R.getAsDecl())) {
|
|
|
|
switch (TD->getTagKind()) {
|
|
|
|
case TagDecl::TK_struct: return DeclSpec::TST_struct;
|
|
|
|
case TagDecl::TK_union: return DeclSpec::TST_union;
|
|
|
|
case TagDecl::TK_class: return DeclSpec::TST_class;
|
|
|
|
case TagDecl::TK_enum: return DeclSpec::TST_enum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DeclSpec::TST_unspecified;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-11-08 20:17:31 +03:00
|
|
|
DeclContext *Sema::getContainingDC(DeclContext *DC) {
|
2008-07-01 14:37:29 +04:00
|
|
|
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) {
|
2008-11-08 20:17:31 +03:00
|
|
|
// A C++ out-of-line method will return to the file declaration context.
|
2008-11-10 02:41:00 +03:00
|
|
|
if (MD->isOutOfLineDefinition())
|
|
|
|
return MD->getLexicalDeclContext();
|
2008-11-08 20:17:31 +03:00
|
|
|
|
2009-02-16 01:43:40 +03:00
|
|
|
// A C++ inline method is parsed *after* the topmost class it was declared
|
|
|
|
// in is fully parsed (it's "complete").
|
2008-11-08 20:17:31 +03:00
|
|
|
// The parsing of a C++ inline method happens at the declaration context of
|
2008-11-19 21:01:13 +03:00
|
|
|
// the topmost (non-nested) class it is lexically declared in.
|
2008-07-01 14:37:29 +04:00
|
|
|
assert(isa<CXXRecordDecl>(MD->getParent()) && "C++ method not in Record.");
|
|
|
|
DC = MD->getParent();
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-10 02:41:00 +03:00
|
|
|
if (isa<ObjCMethodDecl>(DC))
|
|
|
|
return Context.getTranslationUnitDecl();
|
|
|
|
|
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);
|
2008-04-04 10:12:32 +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.
|
|
|
|
static bool AllowOverloadingOfFunction(Decl *PrevDecl, ASTContext &Context) {
|
|
|
|
if (Context.getLangOptions().CPlusPlus)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (isa<OverloadedFunctionDecl>(PrevDecl))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return PrevDecl->getAttr<OverloadableAttr>() != 0;
|
|
|
|
}
|
|
|
|
|
2008-04-12 04:47:19 +04:00
|
|
|
/// Add this decl to the scope shadowed decl chains.
|
|
|
|
void Sema::PushOnScopeChains(NamedDecl *D, Scope *S) {
|
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.
|
|
|
|
while (S->getEntity() &&
|
|
|
|
((DeclContext *)S->getEntity())->isTransparentContext())
|
|
|
|
S = S->getParent();
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
S->AddDecl(DeclPtrTy::make(D));
|
2008-05-10 03:39:43 +04:00
|
|
|
|
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-04-10 01:40:53 +04:00
|
|
|
CurContext->addDecl(Context, D);
|
2008-12-24 00:05:05 +03:00
|
|
|
|
2008-05-10 03:39:43 +04:00
|
|
|
// C++ [basic.scope]p4:
|
|
|
|
// -- exactly one declaration shall declare a class name or
|
|
|
|
// enumeration name that is not a typedef name and the other
|
|
|
|
// declarations shall all refer to the same object or
|
|
|
|
// enumerator, or all refer to functions and function templates;
|
|
|
|
// in this case the class name or enumeration name is hidden.
|
|
|
|
if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
|
|
|
|
// We are pushing the name of a tag (enum or class).
|
2009-01-07 19:34:42 +03:00
|
|
|
if (CurContext->getLookupContext()
|
|
|
|
== TD->getDeclContext()->getLookupContext()) {
|
2008-12-11 19:49:14 +03:00
|
|
|
// We're pushing the tag into the current context, which might
|
|
|
|
// require some reshuffling in the identifier resolver.
|
|
|
|
IdentifierResolver::iterator
|
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
|
|
|
I = IdResolver.begin(TD->getDeclName()),
|
2008-12-24 00:05:05 +03:00
|
|
|
IEnd = IdResolver.end();
|
|
|
|
if (I != IEnd && isDeclInScope(*I, CurContext, S)) {
|
|
|
|
NamedDecl *PrevDecl = *I;
|
|
|
|
for (; I != IEnd && isDeclInScope(*I, CurContext, S);
|
|
|
|
PrevDecl = *I, ++I) {
|
|
|
|
if (TD->declarationReplaces(*I)) {
|
|
|
|
// This is a redeclaration. Remove it from the chain and
|
|
|
|
// break out, so that we'll add in the shadowed
|
|
|
|
// declaration.
|
2009-03-28 22:18:32 +03:00
|
|
|
S->RemoveDecl(DeclPtrTy::make(*I));
|
2008-12-24 00:05:05 +03:00
|
|
|
if (PrevDecl == *I) {
|
|
|
|
IdResolver.RemoveDecl(*I);
|
|
|
|
IdResolver.AddDecl(TD);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
IdResolver.RemoveDecl(*I);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is already a declaration with the same name in the same
|
|
|
|
// scope, which is not a tag declaration. It must be found
|
|
|
|
// before we find the new declaration, so insert the new
|
|
|
|
// declaration at the end of the chain.
|
|
|
|
IdResolver.AddShadowedDecl(TD, PrevDecl);
|
|
|
|
|
|
|
|
return;
|
2008-12-11 19:49:14 +03:00
|
|
|
}
|
2008-10-21 20:13:35 +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
|
|
|
} else if (isa<FunctionDecl>(D) &&
|
|
|
|
AllowOverloadingOfFunction(D, Context)) {
|
2008-10-21 20:13:35 +04:00
|
|
|
// We are pushing the name of a function, which might be an
|
|
|
|
// overloaded name.
|
2008-12-11 19:49:14 +03:00
|
|
|
FunctionDecl *FD = cast<FunctionDecl>(D);
|
2008-12-24 00:05:05 +03:00
|
|
|
IdentifierResolver::iterator Redecl
|
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
|
|
|
= std::find_if(IdResolver.begin(FD->getDeclName()),
|
2008-12-24 00:05:05 +03:00
|
|
|
IdResolver.end(),
|
2009-01-20 04:17:11 +03:00
|
|
|
std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces),
|
2008-12-24 00:05:05 +03:00
|
|
|
FD));
|
2009-03-28 22:18:32 +03:00
|
|
|
if (Redecl != IdResolver.end() &&
|
|
|
|
S->isDeclScope(DeclPtrTy::make(*Redecl))) {
|
2008-12-24 00:05:05 +03:00
|
|
|
// There is already a declaration of a function on our
|
|
|
|
// IdResolver chain. Replace it with this declaration.
|
2009-03-28 22:18:32 +03:00
|
|
|
S->RemoveDecl(DeclPtrTy::make(*Redecl));
|
2008-12-24 00:05:05 +03:00
|
|
|
IdResolver.RemoveDecl(*Redecl);
|
2008-05-10 03:39:43 +04:00
|
|
|
}
|
2009-04-24 06:57:34 +04:00
|
|
|
} else if (isa<ObjCInterfaceDecl>(D)) {
|
|
|
|
// We're pushing an Objective-C interface into the current
|
|
|
|
// context. If there is already an alias declaration, remove it first.
|
|
|
|
for (IdentifierResolver::iterator
|
|
|
|
I = IdResolver.begin(D->getDeclName()), IEnd = IdResolver.end();
|
|
|
|
I != IEnd; ++I) {
|
|
|
|
if (isa<ObjCCompatibleAliasDecl>(*I)) {
|
|
|
|
S->RemoveDecl(DeclPtrTy::make(*I));
|
|
|
|
IdResolver.RemoveDecl(*I);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-05-10 03:39:43 +04:00
|
|
|
}
|
2008-10-21 20:13:35 +04:00
|
|
|
|
2008-05-10 03:39:43 +04:00
|
|
|
IdResolver.AddDecl(D);
|
2008-04-12 04:47:19 +04:00
|
|
|
}
|
|
|
|
|
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)) &&
|
|
|
|
"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) {
|
2009-03-28 22:18:32 +03:00
|
|
|
Decl *TmpD = (*I).getAs<Decl>();
|
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;
|
|
|
|
|
|
|
|
// Remove this name from our lexical scope.
|
|
|
|
IdResolver.RemoveDecl(D);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-02 03:04:06 +04:00
|
|
|
/// getObjCInterfaceDecl - Look up a for a class declaration in the scope.
|
|
|
|
/// return 0 if one not found.
|
|
|
|
ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) {
|
2008-04-02 22:30:49 +04:00
|
|
|
// The third "scope" argument is 0 since we aren't enabling lazy built-in
|
|
|
|
// creation from this context.
|
2009-02-04 20:27:36 +03:00
|
|
|
NamedDecl *IDecl = LookupName(TUScope, Id, LookupOrdinaryName);
|
2007-10-12 23:38:20 +04:00
|
|
|
|
2008-04-02 18:35:35 +04:00
|
|
|
return dyn_cast_or_null<ObjCInterfaceDecl>(IDecl);
|
2007-10-12 23:38:20 +04:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// 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) ||
|
|
|
|
(S->getEntity() &&
|
|
|
|
((DeclContext *)S->getEntity())->isTransparentContext()) ||
|
|
|
|
(S->isClassScope() && !getLangOptions().CPlusPlus))
|
|
|
|
S = S->getParent();
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2008-05-06 02:18:14 +04:00
|
|
|
void Sema::InitBuiltinVaListType() {
|
2007-10-16 00:28:48 +04:00
|
|
|
if (!Context.getBuiltinVaListType().isNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list");
|
2009-02-04 20:27:36 +03:00
|
|
|
NamedDecl *VaDecl = LookupName(TUScope, VaIdent, LookupOrdinaryName);
|
2007-10-19 02:17:45 +04:00
|
|
|
TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl);
|
2007-10-16 00:28:48 +04:00
|
|
|
Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2008-09-28 09:54:29 +04:00
|
|
|
if (Context.BuiltinInfo.hasVAListUse(BID))
|
2007-10-16 00:28:48 +04:00
|
|
|
InitBuiltinVaListType();
|
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
|
|
|
|
2009-02-14 04:52:53 +03:00
|
|
|
Builtin::Context::GetBuiltinTypeError Error;
|
|
|
|
QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context, Error);
|
|
|
|
switch (Error) {
|
|
|
|
case Builtin::Context::GE_None:
|
|
|
|
// Okay
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Builtin::Context::GE_Missing_FILE:
|
|
|
|
if (ForRedeclaration)
|
|
|
|
Diag(Loc, diag::err_implicit_decl_requires_stdio)
|
|
|
|
<< Context.BuiltinInfo.GetName(BID);
|
|
|
|
return 0;
|
|
|
|
}
|
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(),
|
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
|
|
|
Loc, II, R,
|
2009-02-25 19:33:18 +03:00
|
|
|
FunctionDecl::Extern, false,
|
|
|
|
/*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-01-20 04:17:11 +03:00
|
|
|
FT->getArgType(i), VarDecl::None, 0));
|
2009-05-21 13:52:38 +04:00
|
|
|
New->setParams(Context, Params.data(), Params.size());
|
2008-05-06 02:18:14 +04:00
|
|
|
}
|
|
|
|
|
2009-02-14 21:57:46 +03:00
|
|
|
AddKnownFunctionAttributes(New);
|
2008-05-06 02:18:14 +04:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-11 14:37:55 +03:00
|
|
|
/// GetStdNamespace - This method gets the C++ "std" namespace. This is where
|
|
|
|
/// everything from the standard library is defined.
|
|
|
|
NamespaceDecl *Sema::GetStdNamespace() {
|
|
|
|
if (!StdNamespace) {
|
2008-11-20 08:45:14 +03:00
|
|
|
IdentifierInfo *StdIdent = &PP.getIdentifierTable().get("std");
|
2008-11-11 14:37:55 +03:00
|
|
|
DeclContext *Global = Context.getTranslationUnitDecl();
|
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
|
|
|
Decl *Std = LookupQualifiedName(Global, StdIdent, LookupNamespaceName);
|
2008-11-11 14:37:55 +03:00
|
|
|
StdNamespace = dyn_cast_or_null<NamespaceDecl>(Std);
|
|
|
|
}
|
|
|
|
return StdNamespace;
|
|
|
|
}
|
|
|
|
|
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-04-25 12:06:05 +04:00
|
|
|
void Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
|
|
|
|
// If either decl is known invalid already, set the new one to be invalid and
|
|
|
|
// don't bother doing any merging checks.
|
|
|
|
if (New->isInvalidDecl() || OldD->isInvalidDecl())
|
|
|
|
return New->setInvalidDecl();
|
|
|
|
|
2009-01-16 22:58:32 +03:00
|
|
|
bool objc_types = false;
|
2009-04-25 12:06:05 +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;
|
|
|
|
case 2:
|
|
|
|
if (!TypeID->isStr("id"))
|
|
|
|
break;
|
2009-04-24 02:29:11 +04:00
|
|
|
Context.setObjCIdType(Context.getTypeDeclType(New));
|
2009-01-16 22:58:32 +03:00
|
|
|
objc_types = true;
|
|
|
|
break;
|
2008-11-20 08:41:43 +03:00
|
|
|
case 5:
|
|
|
|
if (!TypeID->isStr("Class"))
|
|
|
|
break;
|
2009-04-24 02:29:11 +04:00
|
|
|
Context.setObjCClassType(Context.getTypeDeclType(New));
|
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-04-24 02:29:11 +04:00
|
|
|
Context.setObjCSelType(Context.getTypeDeclType(New));
|
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-01-28 20:15:10 +03:00
|
|
|
// Verify the old decl was also a type.
|
|
|
|
TypeDecl *Old = dyn_cast<TypeDecl>(OldD);
|
2007-07-11 21:01:13 +04:00
|
|
|
if (!Old) {
|
2009-01-28 20:15:10 +03:00
|
|
|
Diag(New->getLocation(), diag::err_redefinition_different_kind)
|
2008-11-24 00:45:46 +03:00
|
|
|
<< New->getDeclName();
|
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-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
|
|
|
|
|
|
|
// Determine the "old" type we'll use for checking and diagnostics.
|
|
|
|
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
|
|
|
|
|
|
|
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-04-25 12:06:05 +04:00
|
|
|
|
|
|
|
if (objc_types || getLangOptions().Microsoft)
|
|
|
|
return;
|
2008-06-11 10:20:39 +04:00
|
|
|
|
2008-11-21 19:29:06 +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 (getLangOptions().CPlusPlus) {
|
|
|
|
if (!isa<CXXRecordDecl>(CurContext))
|
2009-04-25 12:06:05 +04: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-04-27 05:46:12 +04:00
|
|
|
// -Wtypedef-redefinition. If either the original was in a system header,
|
|
|
|
// don't emit this for compatibility with GCC.
|
|
|
|
if (PP.getDiagnostics().getSuppressSystemWarnings() &&
|
|
|
|
Context.getSourceManager().isInSystemHeader(Old->getLocation()))
|
|
|
|
return;
|
|
|
|
|
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.
|
2008-03-03 06:28:21 +03:00
|
|
|
static bool DeclHasAttr(const Decl *decl, const Attr *target) {
|
|
|
|
for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext())
|
|
|
|
if (attr->getKind() == target->getKind())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MergeAttributes - append attributes from the Old decl to the New one.
|
2009-03-04 09:05:19 +03:00
|
|
|
static void MergeAttributes(Decl *New, Decl *Old, ASTContext &C) {
|
2009-04-28 10:37:30 +04:00
|
|
|
for (const Attr *attr = Old->getAttrs(); attr; attr = attr->getNext()) {
|
|
|
|
if (!DeclHasAttr(New, attr) && attr->isMerged()) {
|
|
|
|
Attr *NewAttr = attr->clone(C);
|
|
|
|
NewAttr->setInherited(true);
|
|
|
|
New->addAttr(NewAttr);
|
2008-03-03 06:28:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
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) {
|
2008-10-21 20:13:35 +04:00
|
|
|
assert(!isa<OverloadedFunctionDecl>(OldD) &&
|
|
|
|
"Cannot merge with an overloaded function declaration");
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Verify the old decl was also a function.
|
|
|
|
FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
|
|
|
|
if (!Old) {
|
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;
|
|
|
|
else
|
2008-11-24 02:12:31 +03:00
|
|
|
PrevDiag = diag::note_previous_declaration;
|
2008-04-08 08:40:51 +04:00
|
|
|
|
2008-04-07 03:10:54 +04:00
|
|
|
QualType OldQType = Context.getCanonicalType(Old->getType());
|
|
|
|
QualType NewQType = Context.getCanonicalType(New->getType());
|
2007-11-20 22:04:50 +03:00
|
|
|
|
2009-02-24 04:23:02 +03:00
|
|
|
if (!isa<CXXMethodDecl>(New) && !isa<CXXMethodDecl>(Old) &&
|
|
|
|
New->getStorageClass() == FunctionDecl::Static &&
|
|
|
|
Old->getStorageClass() != FunctionDecl::Static) {
|
|
|
|
Diag(New->getLocation(), diag::err_static_non_static)
|
|
|
|
<< New;
|
|
|
|
Diag(Old->getLocation(), PrevDiag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-21 20:13:35 +04:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
// (C++98 13.1p2):
|
|
|
|
// Certain function declarations cannot be overloaded:
|
|
|
|
// -- Function declarations that differ only in the return type
|
|
|
|
// cannot be overloaded.
|
|
|
|
QualType OldReturnType
|
|
|
|
= cast<FunctionType>(OldQType.getTypePtr())->getResultType();
|
|
|
|
QualType NewReturnType
|
|
|
|
= cast<FunctionType>(NewQType.getTypePtr())->getResultType();
|
|
|
|
if (OldReturnType != NewReturnType) {
|
|
|
|
Diag(New->getLocation(), diag::err_ovl_diff_return_type);
|
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;
|
2008-10-21 20:13:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old);
|
|
|
|
const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New);
|
2009-03-12 02:52:16 +03:00
|
|
|
if (OldMethod && NewMethod &&
|
|
|
|
OldMethod->getLexicalDeclContext() ==
|
|
|
|
NewMethod->getLexicalDeclContext()) {
|
2008-10-21 20:13:35 +04: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);
|
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;
|
2008-10-21 20:13:35 +04:00
|
|
|
}
|
2008-12-16 00:24:18 +03:00
|
|
|
|
|
|
|
// 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.
|
2009-03-12 02:52:16 +03:00
|
|
|
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);
|
|
|
|
Diag(Old->getLocation(), PrevDiag) << Old << Old->getType();
|
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-02-24 04:23:02 +03:00
|
|
|
if (OldQType == NewQType)
|
|
|
|
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-03-07 01:43:54 +03:00
|
|
|
const FunctionType *OldFuncType = OldQType->getAsFunctionType();
|
2009-02-16 21:20:44 +03:00
|
|
|
const FunctionType *NewFuncType = NewQType->getAsFunctionType();
|
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(),
|
|
|
|
OldProto->getTypeQuals());
|
|
|
|
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-02-27 02:50:07 +03:00
|
|
|
for (FunctionProtoType::arg_type_iterator
|
2009-02-16 23:58:07 +03:00
|
|
|
ParamType = OldProto->arg_type_begin(),
|
|
|
|
ParamEnd = OldProto->arg_type_end();
|
|
|
|
ParamType != ParamEnd; ++ParamType) {
|
|
|
|
ParmVarDecl *Param = ParmVarDecl::Create(Context, New,
|
|
|
|
SourceLocation(), 0,
|
|
|
|
*ParamType, VarDecl::None,
|
|
|
|
0);
|
|
|
|
Param->setImplicit();
|
|
|
|
Params.push_back(Param);
|
|
|
|
}
|
|
|
|
|
2009-05-21 13:52:38 +04:00
|
|
|
New->setParams(Context, Params.data(), Params.size());
|
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.
|
|
|
|
if (!getLangOptions().CPlusPlus &&
|
|
|
|
Old->hasPrototype() && !New->hasPrototype() &&
|
|
|
|
New->getType()->getAsFunctionProtoType() &&
|
|
|
|
Old->getNumParams() == New->getNumParams()) {
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTypes;
|
|
|
|
llvm::SmallVector<GNUCompatibleParamWarning, 16> Warnings;
|
|
|
|
const FunctionProtoType *OldProto
|
|
|
|
= Old->getType()->getAsFunctionProtoType();
|
|
|
|
const FunctionProtoType *NewProto
|
|
|
|
= New->getType()->getAsFunctionProtoType();
|
|
|
|
|
|
|
|
// Determine whether this is the GNU C extension.
|
|
|
|
bool GNUCompatible =
|
|
|
|
Context.typesAreCompatible(OldProto->getResultType(),
|
|
|
|
NewProto->getResultType()) &&
|
|
|
|
(OldProto->isVariadic() == NewProto->isVariadic());
|
|
|
|
for (unsigned Idx = 0, End = Old->getNumParams();
|
|
|
|
GNUCompatible && Idx != End; ++Idx) {
|
|
|
|
ParmVarDecl *OldParm = Old->getParamDecl(Idx);
|
|
|
|
ParmVarDecl *NewParm = New->getParamDecl(Idx);
|
|
|
|
if (Context.typesAreCompatible(OldParm->getType(),
|
|
|
|
NewProto->getArgType(Idx))) {
|
|
|
|
ArgTypes.push_back(NewParm->getType());
|
|
|
|
} else if (Context.typesAreCompatible(OldParm->getType(),
|
|
|
|
NewParm->getType())) {
|
|
|
|
GNUCompatibleParamWarning Warn
|
|
|
|
= { OldParm, NewParm, NewProto->getArgType(Idx) };
|
|
|
|
Warnings.push_back(Warn);
|
|
|
|
ArgTypes.push_back(NewParm->getType());
|
|
|
|
} else
|
|
|
|
GNUCompatible = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GNUCompatible) {
|
|
|
|
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();
|
|
|
|
Diag(Warnings[Warn].OldParm->getLocation(),
|
|
|
|
diag::note_previous_declaration);
|
|
|
|
}
|
2009-02-16 23:58:07 +03:00
|
|
|
|
2009-03-07 01:43:54 +03:00
|
|
|
New->setType(Context.getFunctionType(NewProto->getResultType(),
|
|
|
|
&ArgTypes[0], ArgTypes.size(),
|
|
|
|
NewProto->isVariadic(),
|
|
|
|
NewProto->getTypeQuals()));
|
|
|
|
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-02-16 20:45:42 +03:00
|
|
|
if (unsigned BuiltinID = Old->getBuiltinID(Context)) {
|
|
|
|
// 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
|
|
|
|
/// known to be compatible.
|
|
|
|
///
|
|
|
|
/// 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
|
2009-03-04 09:05:19 +03:00
|
|
|
MergeAttributes(New, Old, Context);
|
2009-02-24 04:23:02 +03:00
|
|
|
|
|
|
|
// Merge the storage class.
|
2009-04-28 10:37:30 +04:00
|
|
|
if (Old->getStorageClass() != FunctionDecl::Extern)
|
|
|
|
New->setStorageClass(Old->getStorageClass());
|
2009-02-24 04:23:02 +03:00
|
|
|
|
2009-04-23 22:22:55 +04:00
|
|
|
// Merge "inline"
|
|
|
|
if (Old->isInline())
|
|
|
|
New->setInline(true);
|
|
|
|
|
|
|
|
// If this function declaration by itself qualifies as a C99 inline
|
|
|
|
// definition (C99 6.7.4p6), but the previous definition did not,
|
|
|
|
// then the function is not a C99 inline definition.
|
|
|
|
if (New->isC99InlineDefinition() && !Old->isC99InlineDefinition())
|
|
|
|
New->setC99InlineDefinition(false);
|
|
|
|
else if (Old->isC99InlineDefinition() && !New->isC99InlineDefinition()) {
|
|
|
|
// Mark all preceding definitions as not being C99 inline definitions.
|
|
|
|
for (const FunctionDecl *Prev = Old; Prev;
|
|
|
|
Prev = Prev->getPreviousDeclaration())
|
|
|
|
const_cast<FunctionDecl *>(Prev)->setC99InlineDefinition(false);
|
|
|
|
}
|
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();
|
|
|
|
|
|
|
|
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.
|
|
|
|
///
|
2008-08-08 21:50:35 +04:00
|
|
|
/// Tentative definition rules (C99 6.9.2p2) are checked by
|
|
|
|
/// FinalizeDeclaratorGroup. Unfortunately, we can't analyze tentative
|
|
|
|
/// definitions here, since the initializer hasn't been attached.
|
2007-07-11 21:01:13 +04:00
|
|
|
///
|
2009-04-25 12:06:05 +04:00
|
|
|
void Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
|
|
|
|
// If either decl is invalid, make sure the new one is marked invalid and
|
|
|
|
// don't do any other checking.
|
|
|
|
if (New->isInvalidDecl() || OldD->isInvalidDecl())
|
|
|
|
return New->setInvalidDecl();
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Verify the old decl was also a variable.
|
|
|
|
VarDecl *Old = dyn_cast<VarDecl>(OldD);
|
|
|
|
if (!Old) {
|
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();
|
2008-11-24 02:12:31 +03:00
|
|
|
Diag(OldD->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
|
|
|
|
2009-03-04 09:05:19 +03:00
|
|
|
MergeAttributes(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();
|
|
|
|
} else {
|
|
|
|
MergedT = Context.mergeTypes(New->getType(), Old->getType());
|
|
|
|
}
|
2009-01-25 02:49:55 +03:00
|
|
|
if (MergedT.isNull()) {
|
2009-01-09 22:42:16 +03:00
|
|
|
Diag(New->getLocation(), diag::err_redefinition_different_type)
|
|
|
|
<< 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.
|
|
|
|
if (New->getStorageClass() == VarDecl::Static &&
|
2009-04-14 06:25:56 +04:00
|
|
|
(Old->getStorageClass() == VarDecl::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-03-20 01:01:50 +03:00
|
|
|
// C99 6.2.2p4:
|
|
|
|
// 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 */;
|
|
|
|
else if (New->getStorageClass() != VarDecl::Static &&
|
|
|
|
Old->getStorageClass() == VarDecl::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-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);
|
|
|
|
}
|
|
|
|
|
2009-03-11 02:43:53 +03:00
|
|
|
// Keep a chain of previous declarations.
|
|
|
|
New->setPreviousDeclaration(Old);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
/// CheckParmsForFunctionDef - Check that the parameters of the given
|
|
|
|
/// function are appropriate for the definition of a function. This
|
|
|
|
/// takes care of any checks that cannot be performed on the
|
|
|
|
/// declaration itself, e.g., that the types of each of the function
|
|
|
|
/// parameters are complete.
|
|
|
|
bool Sema::CheckParmsForFunctionDef(FunctionDecl *FD) {
|
|
|
|
bool HasInvalidParm = false;
|
|
|
|
for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
|
|
|
|
ParmVarDecl *Param = FD->getParamDecl(p);
|
|
|
|
|
|
|
|
// C99 6.7.5.3p4: the parameters in a parameter type list in a
|
|
|
|
// function declarator that is part of a function definition of
|
|
|
|
// that function shall not have incomplete type.
|
2009-03-24 22:52:54 +03:00
|
|
|
//
|
|
|
|
// This is also C++ [dcl.fct]p6.
|
2009-01-19 22:26:10 +03:00
|
|
|
if (!Param->isInvalidDecl() &&
|
2009-03-09 19:13:40 +03:00
|
|
|
RequireCompleteType(Param->getLocation(), Param->getType(),
|
2009-01-19 22:26:10 +03:00
|
|
|
diag::err_typecheck_decl_incomplete_type)) {
|
2008-04-08 08:40:51 +04:00
|
|
|
Param->setInvalidDecl();
|
|
|
|
HasInvalidParm = true;
|
|
|
|
}
|
2008-12-17 10:32:46 +03:00
|
|
|
|
|
|
|
// C99 6.9.1p5: If the declarator includes a parameter type list, the
|
|
|
|
// declaration of each parameter shall include an identifier.
|
2009-02-16 23:58:07 +03:00
|
|
|
if (Param->getIdentifier() == 0 &&
|
|
|
|
!Param->isImplicit() &&
|
|
|
|
!getLangOptions().CPlusPlus)
|
2008-12-17 10:32:46 +03:00
|
|
|
Diag(Param->getLocation(), diag::err_parameter_name_omitted);
|
2008-04-08 08:40:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return HasInvalidParm;
|
|
|
|
}
|
|
|
|
|
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.
|
2009-03-28 22:18:32 +03:00
|
|
|
Sema::DeclPtrTy Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
|
2009-04-10 01:26:42 +04:00
|
|
|
// FIXME: Error on auto/register at file scope
|
|
|
|
// FIXME: Error on inline/virtual/explicit
|
|
|
|
// FIXME: Error on invalid restrict
|
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-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) {
|
|
|
|
if (!DS.getTypeRep()) // We probably had an error
|
|
|
|
return DeclPtrTy();
|
|
|
|
|
2009-02-09 18:09:02 +03:00
|
|
|
Tag = dyn_cast<TagDecl>(static_cast<Decl *>(DS.getTypeRep()));
|
2009-05-13 03:25:50 +04:00
|
|
|
}
|
2009-02-09 18:09:02 +03:00
|
|
|
|
2009-01-13 01:49:06 +03:00
|
|
|
if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) {
|
|
|
|
if (!Record->getDeclName() && Record->isDefinition() &&
|
2009-03-07 02:06:59 +03:00
|
|
|
DS.getStorageClassSpec() != DeclSpec::SCS_typedef) {
|
|
|
|
if (getLangOptions().CPlusPlus ||
|
|
|
|
Record->getDeclContext()->isRecord())
|
|
|
|
return BuildAnonymousStructOrUnion(S, DS, Record);
|
|
|
|
|
|
|
|
Diag(DS.getSourceRange().getBegin(), diag::err_no_declarators)
|
|
|
|
<< 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)
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy::make(Tag);
|
2009-01-13 01:49:06 +03:00
|
|
|
}
|
|
|
|
|
2009-02-07 01:42:48 +03:00
|
|
|
if (!DS.isMissingDeclaratorOk() &&
|
|
|
|
DS.getTypeSpecType() != DeclSpec::TST_error) {
|
2009-01-22 19:23:54 +03:00
|
|
|
// Warn about typedefs of enums without names, since this is an
|
|
|
|
// extension in both Microsoft an GNU.
|
2009-01-17 05:55:50 +03:00
|
|
|
if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef &&
|
|
|
|
Tag && isa<EnumDecl>(Tag)) {
|
2009-01-22 19:23:54 +03:00
|
|
|
Diag(DS.getSourceRange().getBegin(), diag::ext_typedef_without_a_name)
|
2009-01-14 02:10:51 +03:00
|
|
|
<< DS.getSourceRange();
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy::make(Tag);
|
2009-01-14 02:10:51 +03:00
|
|
|
}
|
|
|
|
|
2008-12-28 18:28:59 +03:00
|
|
|
Diag(DS.getSourceRange().getBegin(), diag::err_no_declarators)
|
|
|
|
<< DS.getSourceRange();
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy();
|
2008-12-28 18:28:59 +03:00
|
|
|
}
|
2009-01-07 03:43:41 +03:00
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy::make(Tag);
|
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.
|
|
|
|
bool Sema::InjectAnonymousStructOrUnionMembers(Scope *S, DeclContext *Owner,
|
|
|
|
RecordDecl *AnonRecord) {
|
|
|
|
bool Invalid = false;
|
2009-04-10 01:40:53 +04:00
|
|
|
for (RecordDecl::field_iterator F = AnonRecord->field_begin(Context),
|
|
|
|
FEnd = AnonRecord->field_end(Context);
|
2009-01-07 03:43:41 +03:00
|
|
|
F != FEnd; ++F) {
|
|
|
|
if ((*F)->getDeclName()) {
|
2009-02-04 20:27:36 +03:00
|
|
|
NamedDecl *PrevDecl = LookupQualifiedName(Owner, (*F)->getDeclName(),
|
|
|
|
LookupOrdinaryName, true);
|
2009-01-07 03:43:41 +03:00
|
|
|
if (PrevDecl && !isa<TagDecl>(PrevDecl)) {
|
|
|
|
// 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.
|
|
|
|
unsigned diagKind
|
|
|
|
= AnonRecord->isUnion()? diag::err_anonymous_union_member_redecl
|
|
|
|
: diag::err_anonymous_struct_member_redecl;
|
|
|
|
Diag((*F)->getLocation(), diagKind)
|
|
|
|
<< (*F)->getDeclName();
|
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_declaration);
|
|
|
|
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-04-10 01:40:53 +04:00
|
|
|
Owner->makeDeclVisibleInContext(Context, *F);
|
2009-03-28 22:18:32 +03:00
|
|
|
S->AddDecl(DeclPtrTy::make(*F));
|
2009-01-07 03:43:41 +03:00
|
|
|
IdResolver.AddDecl(*F);
|
|
|
|
}
|
|
|
|
} else if (const RecordType *InnerRecordType
|
|
|
|
= (*F)->getType()->getAsRecordType()) {
|
|
|
|
RecordDecl *InnerRecord = InnerRecordType->getDecl();
|
|
|
|
if (InnerRecord->isAnonymousStructOrUnion())
|
|
|
|
Invalid = Invalid ||
|
|
|
|
InjectAnonymousStructOrUnionMembers(S, Owner, InnerRecord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
/// are a GNU C and GNU C++ extension.
|
2009-03-28 22:18:32 +03:00
|
|
|
Sema::DeclPtrTy Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS,
|
|
|
|
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);
|
|
|
|
|
|
|
|
// C and C++ require different kinds of checks for anonymous
|
|
|
|
// structs/unions.
|
|
|
|
bool Invalid = false;
|
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
const char* PrevSpec = 0;
|
|
|
|
// 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) ||
|
|
|
|
(isa<NamespaceDecl>(Owner) &&
|
|
|
|
cast<NamespaceDecl>(Owner)->getDeclName()))) {
|
|
|
|
Diag(Record->getLocation(), diag::err_anonymous_union_not_static);
|
|
|
|
Invalid = true;
|
|
|
|
|
|
|
|
// Recover by adding 'static'.
|
|
|
|
DS.SetStorageClassSpec(DeclSpec::SCS_static, SourceLocation(), PrevSpec);
|
|
|
|
}
|
|
|
|
// 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)) {
|
|
|
|
Diag(DS.getStorageClassSpecLoc(),
|
|
|
|
diag::err_anonymous_union_with_storage_spec);
|
|
|
|
Invalid = true;
|
|
|
|
|
|
|
|
// Recover by removing the storage specifier.
|
|
|
|
DS.SetStorageClassSpec(DeclSpec::SCS_unspecified, SourceLocation(),
|
|
|
|
PrevSpec);
|
|
|
|
}
|
2009-01-07 22:46:03 +03:00
|
|
|
|
|
|
|
// C++ [class.union]p2:
|
|
|
|
// 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-04-10 01:40:53 +04:00
|
|
|
for (DeclContext::decl_iterator Mem = Record->decls_begin(Context),
|
|
|
|
MemEnd = Record->decls_end(Context);
|
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).
|
|
|
|
if (FD->getAccess() == AS_protected || FD->getAccess() == AS_private) {
|
|
|
|
Diag(FD->getLocation(), diag::err_anonymous_record_nonpublic_member)
|
|
|
|
<< (int)Record->isUnion() << (int)(FD->getAccess() == AS_protected);
|
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
} 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()) {
|
|
|
|
// This is a nested type declaration.
|
|
|
|
Diag(MemRecord->getLocation(), diag::err_anonymous_record_with_type)
|
|
|
|
<< (int)Record->isUnion();
|
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
Diag((*Mem)->getLocation(), DK)
|
|
|
|
<< (int)Record->isUnion();
|
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
}
|
2009-03-07 02:06:59 +03: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(),
|
|
|
|
/*IdentifierInfo=*/0,
|
|
|
|
Context.getTypeDeclType(Record),
|
2009-01-20 04:17:11 +03:00
|
|
|
/*BitWidth=*/0, /*Mutable=*/false);
|
2009-01-07 22:46:03 +03:00
|
|
|
Anon->setAccess(AS_public);
|
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
FieldCollector->Add(cast<FieldDecl>(Anon));
|
2009-01-07 03:43:41 +03:00
|
|
|
} else {
|
|
|
|
VarDecl::StorageClass SC;
|
|
|
|
switch (DS.getStorageClassSpec()) {
|
|
|
|
default: assert(0 && "Unknown storage class!");
|
|
|
|
case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
|
|
|
|
case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
|
|
|
|
case DeclSpec::SCS_static: SC = VarDecl::Static; break;
|
|
|
|
case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
|
|
|
|
case DeclSpec::SCS_register: SC = VarDecl::Register; break;
|
|
|
|
case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break;
|
|
|
|
case DeclSpec::SCS_mutable:
|
|
|
|
// 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;
|
|
|
|
SC = VarDecl::None;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Anon = VarDecl::Create(Context, Owner, Record->getLocation(),
|
|
|
|
/*IdentifierInfo=*/0,
|
|
|
|
Context.getTypeDeclType(Record),
|
2009-01-20 04:17:11 +03:00
|
|
|
SC, DS.getSourceRange().getBegin());
|
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-04-10 01:40:53 +04:00
|
|
|
Owner->addDecl(Context, Anon);
|
2008-12-28 18:28:59 +03: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.
|
2009-01-13 01:49:06 +03:00
|
|
|
if (InjectAnonymousStructOrUnionMembers(S, Owner, Record))
|
|
|
|
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
|
|
|
|
// InjectAnonymousStructOrUnionMembers.
|
|
|
|
Record->setAnonymousStructOrUnion(true);
|
|
|
|
|
|
|
|
if (Invalid)
|
|
|
|
Anon->setInvalidDecl();
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy::make(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.
|
|
|
|
DeclarationName Sema::GetNameForDeclarator(Declarator &D) {
|
|
|
|
switch (D.getKind()) {
|
|
|
|
case Declarator::DK_Abstract:
|
|
|
|
assert(D.getIdentifier() == 0 && "abstract declarators have no name");
|
|
|
|
return DeclarationName();
|
|
|
|
|
|
|
|
case Declarator::DK_Normal:
|
|
|
|
assert (D.getIdentifier() != 0 && "normal declarators have an identifier");
|
|
|
|
return DeclarationName(D.getIdentifier());
|
|
|
|
|
|
|
|
case Declarator::DK_Constructor: {
|
2009-02-09 18:09:02 +03:00
|
|
|
QualType Ty = QualType::getFromOpaquePtr(D.getDeclaratorIdType());
|
2008-11-18 01:58:34 +03:00
|
|
|
Ty = Context.getCanonicalType(Ty);
|
|
|
|
return Context.DeclarationNames.getCXXConstructorName(Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
case Declarator::DK_Destructor: {
|
2009-02-09 18:09:02 +03:00
|
|
|
QualType Ty = QualType::getFromOpaquePtr(D.getDeclaratorIdType());
|
2008-11-18 01:58:34 +03:00
|
|
|
Ty = Context.getCanonicalType(Ty);
|
|
|
|
return Context.DeclarationNames.getCXXDestructorName(Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
case Declarator::DK_Conversion: {
|
2009-02-09 18:09:02 +03:00
|
|
|
// FIXME: We'd like to keep the non-canonical type for diagnostics!
|
2008-11-18 01:58:34 +03:00
|
|
|
QualType Ty = QualType::getFromOpaquePtr(D.getDeclaratorIdType());
|
|
|
|
Ty = Context.getCanonicalType(Ty);
|
|
|
|
return Context.DeclarationNames.getCXXConversionFunctionName(Ty);
|
|
|
|
}
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59526 91177308-0d34-0410-b5e6-96231b3b80d8
2008-11-18 17:39:36 +03:00
|
|
|
|
|
|
|
case Declarator::DK_Operator:
|
|
|
|
assert(D.getIdentifier() == 0 && "operator names have no identifier");
|
|
|
|
return Context.DeclarationNames.getCXXOperatorName(
|
|
|
|
D.getOverloadedOperator());
|
2008-11-18 01:58:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(false && "Unknown name kind");
|
|
|
|
return DeclarationName();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
DeclParamTy = Context.getCanonicalType(DeclParamTy.getNonReferenceType());
|
|
|
|
DefParamTy = Context.getCanonicalType(DefParamTy.getNonReferenceType());
|
|
|
|
if (DeclParamTy.getUnqualifiedType() != DefParamTy.getUnqualifiedType())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
Sema::DeclPtrTy
|
2009-03-29 20:50:03 +04:00
|
|
|
Sema::ActOnDeclarator(Scope *S, Declarator &D, bool IsFunctionDefinition) {
|
2008-11-18 01:58:34 +03:00
|
|
|
DeclarationName Name = GetNameForDeclarator(D);
|
|
|
|
|
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();
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy();
|
2007-07-25 04:24:17 +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();
|
|
|
|
|
2008-11-08 20:17:31 +03:00
|
|
|
DeclContext *DC;
|
2009-02-04 20:27:36 +03:00
|
|
|
NamedDecl *PrevDecl;
|
2009-01-20 04:17:11 +03:00
|
|
|
NamedDecl *New;
|
2009-02-16 20:45:42 +03:00
|
|
|
|
2009-02-24 23:03:32 +03:00
|
|
|
QualType R = GetTypeForDeclarator(D, S);
|
|
|
|
|
2008-11-08 20:17:31 +03:00
|
|
|
// See if this is a redefinition of a variable in the same scope.
|
2009-03-06 22:06:37 +03:00
|
|
|
if (D.getCXXScopeSpec().isInvalid()) {
|
|
|
|
DC = CurContext;
|
|
|
|
PrevDecl = 0;
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2009-03-06 22:06:37 +03:00
|
|
|
} else if (!D.getCXXScopeSpec().isSet()) {
|
2009-02-24 23:03:32 +03:00
|
|
|
LookupNameKind NameKind = LookupOrdinaryName;
|
|
|
|
|
|
|
|
// 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()) {
|
|
|
|
if (CurContext->isFunctionOrMethod())
|
|
|
|
NameKind = LookupRedeclarationWithLinkage;
|
|
|
|
} else if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern)
|
|
|
|
NameKind = LookupRedeclarationWithLinkage;
|
|
|
|
|
2008-11-08 20:17:31 +03:00
|
|
|
DC = CurContext;
|
2009-02-24 23:03:32 +03:00
|
|
|
PrevDecl = LookupName(S, Name, NameKind, true,
|
2009-02-17 06:23:10 +03:00
|
|
|
D.getDeclSpec().getStorageClassSpec() !=
|
|
|
|
DeclSpec::SCS_static,
|
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
|
|
|
D.getIdentifierLoc());
|
2008-11-08 20:17:31 +03:00
|
|
|
} else { // Something like "int foo::x;"
|
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
|
|
|
DC = computeDeclContext(D.getCXXScopeSpec());
|
2009-03-11 19:48:53 +03:00
|
|
|
// FIXME: RequireCompleteDeclContext(D.getCXXScopeSpec()); ?
|
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
|
|
|
PrevDecl = LookupQualifiedName(DC, Name, LookupOrdinaryName, true);
|
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
|
|
|
|
// matches.
|
2009-02-06 20:46:57 +03:00
|
|
|
|
|
|
|
// First check whether we named the global scope.
|
|
|
|
if (isa<TranslationUnitDecl>(DC)) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_invalid_declarator_global_scope)
|
|
|
|
<< Name << D.getCXXScopeSpec().getRange();
|
|
|
|
} else if (!CurContext->Encloses(DC)) {
|
2008-11-08 20:17:31 +03:00
|
|
|
// The qualifying scope doesn't enclose the original declaration.
|
|
|
|
// Emit diagnostic based on current scope.
|
|
|
|
SourceLocation L = D.getIdentifierLoc();
|
|
|
|
SourceRange R = D.getCXXScopeSpec().getRange();
|
2009-02-06 20:46:57 +03:00
|
|
|
if (isa<FunctionDecl>(CurContext))
|
2008-11-23 23:28:15 +03:00
|
|
|
Diag(L, diag::err_invalid_declarator_in_function) << Name << R;
|
2009-02-06 20:46:57 +03:00
|
|
|
else
|
2008-11-23 23:28:15 +03:00
|
|
|
Diag(L, diag::err_invalid_declarator_scope)
|
2009-02-06 20:46:57 +03:00
|
|
|
<< Name << cast<NamedDecl>(DC) << R;
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2008-11-08 20:17:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2009-04-25 12:06:05 +04:00
|
|
|
if (!D.isInvalidType())
|
|
|
|
if (DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl))
|
|
|
|
D.setInvalidType();
|
|
|
|
|
2008-12-05 21:15:24 +03:00
|
|
|
// Just pretend that we didn't see the previous declaration.
|
|
|
|
PrevDecl = 0;
|
|
|
|
}
|
|
|
|
|
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).
|
|
|
|
if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag &&
|
|
|
|
D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef)
|
2008-04-14 01:07:44 +04:00
|
|
|
PrevDecl = 0;
|
|
|
|
|
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-04-25 12:06:05 +04:00
|
|
|
New = ActOnTypedefDeclarator(S, D, DC, R, PrevDecl, Redeclaration);
|
2009-02-24 23:03:32 +03:00
|
|
|
} else if (R->isFunctionType()) {
|
2009-03-29 20:50:03 +04:00
|
|
|
New = ActOnFunctionDeclarator(S, D, DC, R, PrevDecl,
|
2009-04-25 12:06:05 +04:00
|
|
|
IsFunctionDefinition, Redeclaration);
|
2007-07-11 21:01:13 +04:00
|
|
|
} else {
|
2009-04-25 12:06:05 +04:00
|
|
|
New = ActOnVariableDeclarator(S, D, DC, R, PrevDecl, Redeclaration);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2009-01-16 04:13:29 +03:00
|
|
|
|
|
|
|
if (New == 0)
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2009-02-16 20:45:42 +03:00
|
|
|
// If this has an identifier and is not an invalid redeclaration,
|
|
|
|
// add it to the scope stack.
|
2009-04-25 12:06:05 +04:00
|
|
|
if (Name && !(Redeclaration && New->isInvalidDecl()))
|
2008-04-12 04:47:19 +04:00
|
|
|
PushOnScopeChains(New, S);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy::make(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,
|
|
|
|
bool &SizeIsNegative) {
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
if (const PointerType* PTy = dyn_cast<PointerType>(T)) {
|
|
|
|
QualType Pointee = PTy->getPointeeType();
|
|
|
|
QualType FixedType =
|
|
|
|
TryToFixInvalidVariablyModifiedType(Pointee, Context, SizeIsNegative);
|
|
|
|
if (FixedType.isNull()) return FixedType;
|
2009-02-21 03:58:02 +03:00
|
|
|
FixedType = Context.getPointerType(FixedType);
|
|
|
|
FixedType.setCVRQualifiers(T.getCVRQualifiers());
|
|
|
|
return 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-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
|
|
|
|
2009-02-21 03:44:51 +03:00
|
|
|
llvm::APSInt &Res = EvalResult.Val.getInt();
|
|
|
|
if (Res >= llvm::APSInt(Res.getBitWidth(), Res.isUnsigned()))
|
|
|
|
return Context.getConstantArrayType(VLATy->getElementType(),
|
|
|
|
Res, ArrayType::Normal, 0);
|
|
|
|
|
|
|
|
SizeIsNegative = true;
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
void
|
|
|
|
Sema::RegisterLocallyScopedExternCDecl(NamedDecl *ND, NamedDecl *PrevDecl,
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!PrevDecl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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.
|
2009-03-28 22:18:32 +03:00
|
|
|
while (S && !S->isDeclScope(DeclPtrTy::make(PrevDecl)))
|
2009-03-02 03:19:53 +03:00
|
|
|
S = S->getParent();
|
|
|
|
|
|
|
|
if (S)
|
2009-03-28 22:18:32 +03:00
|
|
|
S->RemoveDecl(DeclPtrTy::make(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())
|
|
|
|
Diag(D.getDeclSpec().getInlineSpecLoc(),
|
|
|
|
diag::err_inline_non_function);
|
|
|
|
|
|
|
|
if (D.getDeclSpec().isVirtualSpecified())
|
|
|
|
Diag(D.getDeclSpec().getVirtualSpecLoc(),
|
|
|
|
diag::err_virtual_non_function);
|
|
|
|
|
|
|
|
if (D.getDeclSpec().isExplicitSpecified())
|
|
|
|
Diag(D.getDeclSpec().getExplicitSpecLoc(),
|
|
|
|
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-04-25 12:06:05 +04:00
|
|
|
QualType R, Decl* PrevDecl, 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.
|
|
|
|
DC = 0;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2009-03-29 20:50:03 +04:00
|
|
|
TypedefDecl *NewTD = ParseTypedefDecl(S, D, R);
|
2009-01-16 06:34:13 +03:00
|
|
|
if (!NewTD) return 0;
|
2009-04-25 12:06:05 +04:00
|
|
|
|
|
|
|
if (D.isInvalidType())
|
|
|
|
NewTD->setInvalidDecl();
|
2009-01-16 06:34:13 +03:00
|
|
|
|
|
|
|
// Handle attributes prior to checking for duplicates in MergeVarDecl
|
|
|
|
ProcessDeclAttributes(NewTD, D);
|
|
|
|
// Merge the decl with the existing one if appropriate. If the decl is
|
|
|
|
// in an outer scope, it isn't the same thing.
|
|
|
|
if (PrevDecl && isDeclInScope(PrevDecl, DC, S)) {
|
2009-02-16 20:45:42 +03:00
|
|
|
Redeclaration = true;
|
2009-04-25 12:06:05 +04:00
|
|
|
MergeTypeDefDecl(NewTD, PrevDecl);
|
2009-01-16 06:34:13 +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.
|
|
|
|
QualType T = NewTD->getUnderlyingType();
|
|
|
|
if (T->isVariablyModifiedType()) {
|
|
|
|
CurFunctionNeedsScopeChecking = true;
|
|
|
|
|
|
|
|
if (S->getFnParent() == 0) {
|
2009-02-21 03:44:51 +03:00
|
|
|
bool SizeIsNegative;
|
|
|
|
QualType FixedTy =
|
|
|
|
TryToFixInvalidVariablyModifiedType(T, Context, SizeIsNegative);
|
|
|
|
if (!FixedTy.isNull()) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::warn_illegal_constant_array_size);
|
|
|
|
NewTD->setUnderlyingType(FixedTy);
|
|
|
|
} 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);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
///
|
|
|
|
/// \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.
|
|
|
|
static bool
|
|
|
|
isOutOfScopePreviousDeclaration(NamedDecl *PrevDecl, DeclContext *DC,
|
|
|
|
ASTContext &Context) {
|
|
|
|
if (!PrevDecl)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// FIXME: PrevDecl could be an OverloadedFunctionDecl, in which
|
|
|
|
// case we need to check each of the overloaded functions.
|
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.
|
|
|
|
DeclContext *OuterContext = DC->getLookupContext();
|
|
|
|
if (!OuterContext->isFunctionOrMethod())
|
|
|
|
// This rule only applies to block-scope declarations.
|
|
|
|
return false;
|
|
|
|
else {
|
|
|
|
DeclContext *PrevOuterContext = PrevDecl->getDeclContext();
|
|
|
|
if (PrevOuterContext->isRecord())
|
|
|
|
// We found a member function: ignore it.
|
|
|
|
return false;
|
|
|
|
else {
|
|
|
|
// Find the innermost enclosing namespace for the new and
|
|
|
|
// previous declarations.
|
|
|
|
while (!OuterContext->isFileContext())
|
|
|
|
OuterContext = OuterContext->getParent();
|
|
|
|
while (!PrevOuterContext->isFileContext())
|
|
|
|
PrevOuterContext = PrevOuterContext->getParent();
|
|
|
|
|
|
|
|
// The previous declaration is in a different namespace, so it
|
|
|
|
// isn't the same function.
|
|
|
|
if (OuterContext->getPrimaryContext() !=
|
|
|
|
PrevOuterContext->getPrimaryContext())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-20 04:17:11 +03:00
|
|
|
NamedDecl*
|
2009-01-16 05:36:34 +03:00
|
|
|
Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC,
|
2009-04-25 12:06:05 +04:00
|
|
|
QualType R,NamedDecl* PrevDecl,
|
2009-02-16 20:45:42 +03:00
|
|
|
bool &Redeclaration) {
|
2009-01-16 05:36:34 +03:00
|
|
|
DeclarationName Name = GetNameForDeclarator(D);
|
|
|
|
|
|
|
|
// Check that there are no default arguments (C++ only).
|
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
CheckExtraCXXDefaultArguments(D);
|
|
|
|
|
|
|
|
VarDecl *NewVD;
|
|
|
|
VarDecl::StorageClass SC;
|
|
|
|
switch (D.getDeclSpec().getStorageClassSpec()) {
|
|
|
|
default: assert(0 && "Unknown storage class!");
|
|
|
|
case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
|
|
|
|
case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
|
|
|
|
case DeclSpec::SCS_static: SC = VarDecl::Static; break;
|
|
|
|
case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
|
|
|
|
case DeclSpec::SCS_register: SC = VarDecl::Register; break;
|
|
|
|
case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break;
|
|
|
|
case DeclSpec::SCS_mutable:
|
|
|
|
// 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();
|
2009-01-16 05:36:34 +03:00
|
|
|
SC = VarDecl::None;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (SC == VarDecl::Auto || SC == VarDecl::Register) {
|
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.
|
|
|
|
if (SC == VarDecl::Register && D.getAsmLabel())
|
|
|
|
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.
|
|
|
|
if (SC == VarDecl::Static) {
|
|
|
|
Diag(D.getDeclSpec().getStorageClassSpecLoc(),
|
|
|
|
diag::err_static_out_of_line)
|
|
|
|
<< CodeModificationHint::CreateRemoval(
|
|
|
|
SourceRange(D.getDeclSpec().getStorageClassSpecLoc()));
|
|
|
|
} else if (SC == VarDecl::None)
|
|
|
|
SC = VarDecl::Static;
|
|
|
|
}
|
2009-03-22 23:18:17 +03:00
|
|
|
|
|
|
|
// The variable can not
|
2009-03-11 23:22:50 +03:00
|
|
|
NewVD = VarDecl::Create(Context, DC, D.getIdentifierLoc(),
|
|
|
|
II, R, SC,
|
|
|
|
// FIXME: Move to DeclGroup...
|
|
|
|
D.getDeclSpec().getSourceRange().getBegin());
|
2009-04-20 00:27:55 +04:00
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
if (D.isInvalidType())
|
|
|
|
NewVD->setInvalidDecl();
|
|
|
|
|
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
|
|
|
|
ProcessDeclAttributes(NewVD, D);
|
|
|
|
|
|
|
|
// Handle GNU asm-label extension (encoded as an attribute).
|
|
|
|
if (Expr *E = (Expr*) D.getAsmLabel()) {
|
|
|
|
// The parser guarantees this is a string.
|
|
|
|
StringLiteral *SE = cast<StringLiteral>(E);
|
2009-03-04 09:34:08 +03:00
|
|
|
NewVD->addAttr(::new (Context) AsmLabelAttr(std::string(SE->getStrData(),
|
|
|
|
SE->getByteLength())));
|
2009-01-16 05:36:34 +03:00
|
|
|
}
|
|
|
|
|
2009-03-26 02:32:15 +03:00
|
|
|
// If name lookup finds a previous declaration that is not in the
|
|
|
|
// same scope as the new declaration, this may still be an
|
|
|
|
// acceptable redeclaration.
|
|
|
|
if (PrevDecl && !isDeclInScope(PrevDecl, DC, S) &&
|
|
|
|
!(NewVD->hasLinkage() &&
|
|
|
|
isOutOfScopePreviousDeclaration(PrevDecl, DC, Context)))
|
|
|
|
PrevDecl = 0;
|
|
|
|
|
|
|
|
// Merge the decl with the existing one if appropriate.
|
|
|
|
if (PrevDecl) {
|
|
|
|
if (isa<FieldDecl>(PrevDecl) && D.getCXXScopeSpec().isSet()) {
|
|
|
|
// 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();
|
|
|
|
PrevDecl = 0;
|
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.
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_typecheck_no_member)
|
|
|
|
<< Name << D.getCXXScopeSpec().getRange();
|
2009-04-25 12:06:05 +04:00
|
|
|
NewVD->setInvalidDecl();
|
2009-03-26 02:32:15 +03:00
|
|
|
}
|
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
CheckVariableDeclaration(NewVD, PrevDecl, Redeclaration);
|
2009-03-26 02:32:15 +03:00
|
|
|
|
|
|
|
// If this is a locally-scoped extern C variable, update the map of
|
|
|
|
// such variables.
|
|
|
|
if (CurContext->isFunctionOrMethod() && NewVD->isExternC(Context) &&
|
2009-04-25 12:06:05 +04:00
|
|
|
!NewVD->isInvalidDecl())
|
2009-03-26 02:32:15 +03:00
|
|
|
RegisterLocallyScopedExternCDecl(NewVD, PrevDecl, S);
|
|
|
|
|
|
|
|
return NewVD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \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.
|
|
|
|
void Sema::CheckVariableDeclaration(VarDecl *NewVD, NamedDecl *PrevDecl,
|
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-03-26 02:32:15 +03:00
|
|
|
QualType T = NewVD->getType();
|
|
|
|
|
|
|
|
if (T->isObjCInterfaceType()) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// The variable can not have an abstract class type.
|
|
|
|
if (RequireNonAbstractType(NewVD->getLocation(), T,
|
|
|
|
diag::err_abstract_type_in_decl,
|
|
|
|
AbstractVariableType))
|
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
|
|
|
// 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
|
2009-03-26 02:32:15 +03:00
|
|
|
if (NewVD->hasLocalStorage() && (T.getAddressSpace() != 0)) {
|
|
|
|
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-04-14 06:45:29 +04:00
|
|
|
&& !NewVD->hasAttr<BlocksAttr>())
|
2009-03-26 02:32:15 +03:00
|
|
|
Diag(NewVD->getLocation(), diag::warn_attribute_weak_on_local);
|
2009-02-21 22:44:02 +03:00
|
|
|
|
2009-04-19 09:21:20 +04:00
|
|
|
bool isVM = T->isVariablyModifiedType();
|
|
|
|
if (isVM || NewVD->hasAttr<CleanupAttr>())
|
|
|
|
CurFunctionNeedsScopeChecking = true;
|
|
|
|
|
|
|
|
if ((isVM && NewVD->hasLinkage()) ||
|
|
|
|
(T->isVariableArrayType() && NewVD->hasGlobalStorage())) {
|
2009-03-01 00:56:50 +03:00
|
|
|
bool SizeIsNegative;
|
|
|
|
QualType FixedTy =
|
2009-03-26 02:32:15 +03:00
|
|
|
TryToFixInvalidVariablyModifiedType(T, Context, SizeIsNegative);
|
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-03-01 00:56:50 +03:00
|
|
|
// FIXME: This won't give the correct result for
|
|
|
|
// int a[10][n];
|
|
|
|
SourceRange SizeRange = VAT->getSizeExpr()->getSourceRange();
|
2009-04-25 12:06:05 +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;
|
2009-03-01 00:56:50 +03:00
|
|
|
else if (NewVD->getStorageClass() == VarDecl::Static)
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-03-02 03:19:53 +03:00
|
|
|
if (!PrevDecl && NewVD->isExternC(Context)) {
|
|
|
|
// 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())
|
|
|
|
PrevDecl = Pos->second;
|
|
|
|
}
|
|
|
|
|
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-04-30 04:19:40 +04:00
|
|
|
if (!NewVD->hasLocalStorage() && NewVD->hasAttr<BlocksAttr>()) {
|
|
|
|
Diag(NewVD->getLocation(), diag::err_block_on_nonlocal);
|
|
|
|
return NewVD->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
2009-05-02 03:41:47 +04:00
|
|
|
if (isVM && NewVD->hasAttr<BlocksAttr>()) {
|
|
|
|
Diag(NewVD->getLocation(), diag::err_block_on_vm);
|
|
|
|
return NewVD->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
2009-03-26 02:32:15 +03:00
|
|
|
if (PrevDecl) {
|
2009-02-16 20:45:42 +03:00
|
|
|
Redeclaration = true;
|
2009-04-25 12:06:05 +04:00
|
|
|
MergeVarDecl(NewVD, PrevDecl);
|
2009-03-11 00:58:27 +03:00
|
|
|
}
|
2009-01-16 05:36:34 +03:00
|
|
|
}
|
|
|
|
|
2009-01-20 04:17:11 +03:00
|
|
|
NamedDecl*
|
2009-01-16 04:13:29 +03:00
|
|
|
Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
|
2009-03-29 20:50:03 +04:00
|
|
|
QualType R, NamedDecl* PrevDecl,
|
2009-04-25 12:06:05 +04:00
|
|
|
bool IsFunctionDefinition, bool &Redeclaration) {
|
2009-01-16 04:13:29 +03:00
|
|
|
assert(R.getTypePtr()->isFunctionType());
|
|
|
|
|
|
|
|
DeclarationName Name = GetNameForDeclarator(D);
|
|
|
|
FunctionDecl::StorageClass SC = FunctionDecl::None;
|
|
|
|
switch (D.getDeclSpec().getStorageClassSpec()) {
|
|
|
|
default: assert(0 && "Unknown storage class!");
|
|
|
|
case DeclSpec::SCS_auto:
|
|
|
|
case DeclSpec::SCS_register:
|
|
|
|
case DeclSpec::SCS_mutable:
|
2009-02-24 04:23:02 +03:00
|
|
|
Diag(D.getDeclSpec().getStorageClassSpecLoc(),
|
|
|
|
diag::err_typecheck_sclass_func);
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2009-01-16 04:13:29 +03:00
|
|
|
break;
|
|
|
|
case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
|
|
|
|
case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
|
2009-02-24 04:23:02 +03:00
|
|
|
case DeclSpec::SCS_static: {
|
2009-03-12 02:52:16 +03:00
|
|
|
if (CurContext->getLookupContext()->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).
|
|
|
|
Diag(D.getDeclSpec().getStorageClassSpecLoc(),
|
|
|
|
diag::err_static_block_func);
|
|
|
|
SC = FunctionDecl::None;
|
|
|
|
} else
|
|
|
|
SC = FunctionDecl::Static;
|
|
|
|
break;
|
|
|
|
}
|
2009-01-16 04:13:29 +03:00
|
|
|
case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break;
|
|
|
|
}
|
|
|
|
|
2009-04-20 00:27:55 +04:00
|
|
|
if (D.getDeclSpec().isThreadSpecified())
|
|
|
|
Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
|
|
|
|
|
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();
|
|
|
|
|
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
|
|
|
|
// the class has been completely parsed.
|
|
|
|
if (!DC->isRecord() &&
|
|
|
|
RequireNonAbstractType(D.getIdentifierLoc(),
|
2009-03-22 23:18:17 +03:00
|
|
|
R->getAsFunctionType()->getResultType(),
|
2009-03-23 20:49:10 +03: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-04-11 23:17:25 +04:00
|
|
|
|
|
|
|
// Do not allow returning a objc interface by-value.
|
|
|
|
if (R->getAsFunctionType()->getResultType()->isObjCInterfaceType()) {
|
|
|
|
Diag(D.getIdentifierLoc(),
|
|
|
|
diag::err_object_cannot_be_passed_returned_by_value) << 0
|
|
|
|
<< R->getAsFunctionType()->getResultType();
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2009-04-11 23:17:25 +04:00
|
|
|
}
|
2009-03-22 23:18:17 +03:00
|
|
|
|
2009-03-12 02:00:04 +03:00
|
|
|
bool isVirtualOkay = false;
|
2009-01-16 04:13:29 +03:00
|
|
|
FunctionDecl *NewFD;
|
|
|
|
if (D.getKind() == Declarator::DK_Constructor) {
|
|
|
|
// 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
|
|
|
|
NewFD = CXXConstructorDecl::Create(Context,
|
|
|
|
cast<CXXRecordDecl>(DC),
|
|
|
|
D.getIdentifierLoc(), Name, R,
|
|
|
|
isExplicit, isInline,
|
|
|
|
/*isImplicitlyDeclared=*/false);
|
|
|
|
} else if (D.getKind() == Declarator::DK_Destructor) {
|
|
|
|
// This is a C++ destructor declaration.
|
|
|
|
if (DC->isRecord()) {
|
2009-04-25 12:28:21 +04:00
|
|
|
R = CheckDestructorDeclarator(D, SC);
|
2009-04-25 12:06:05 +04:00
|
|
|
|
2009-01-16 04:13:29 +03:00
|
|
|
NewFD = CXXDestructorDecl::Create(Context,
|
|
|
|
cast<CXXRecordDecl>(DC),
|
|
|
|
D.getIdentifierLoc(), Name, R,
|
|
|
|
isInline,
|
|
|
|
/*isImplicitlyDeclared=*/false);
|
|
|
|
|
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(),
|
2009-01-20 04:17:11 +03:00
|
|
|
Name, R, SC, isInline,
|
2009-02-25 19:33:18 +03:00
|
|
|
/*hasPrototype=*/true,
|
2009-01-16 04:13:29 +03:00
|
|
|
// FIXME: Move to DeclGroup...
|
|
|
|
D.getDeclSpec().getSourceRange().getBegin());
|
2009-04-25 12:06:05 +04:00
|
|
|
D.setInvalidType();
|
2009-01-16 04:13:29 +03:00
|
|
|
}
|
|
|
|
} else if (D.getKind() == Declarator::DK_Conversion) {
|
|
|
|
if (!DC->isRecord()) {
|
|
|
|
Diag(D.getIdentifierLoc(),
|
|
|
|
diag::err_conv_function_not_member);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-04-25 12:35:12 +04:00
|
|
|
|
|
|
|
CheckConversionDeclarator(D, R, SC);
|
|
|
|
NewFD = CXXConversionDecl::Create(Context, cast<CXXRecordDecl>(DC),
|
|
|
|
D.getIdentifierLoc(), Name, R,
|
|
|
|
isInline, isExplicit);
|
|
|
|
|
|
|
|
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
|
|
|
|
// must be an invalid constructor that has a return type.
|
|
|
|
// (The parser checks for a return type and makes the declarator a
|
|
|
|
// constructor if it has no return type).
|
|
|
|
// must have an invalid constructor that has a return type
|
|
|
|
if (Name.getAsIdentifierInfo() == cast<CXXRecordDecl>(DC)->getIdentifier()){
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_constructor_return_type)
|
|
|
|
<< SourceRange(D.getDeclSpec().getTypeSpecTypeLoc())
|
|
|
|
<< SourceRange(D.getIdentifierLoc());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-16 04:13:29 +03:00
|
|
|
// This is a C++ method declaration.
|
|
|
|
NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(DC),
|
|
|
|
D.getIdentifierLoc(), Name, R,
|
2009-01-20 04:17:11 +03:00
|
|
|
(SC == FunctionDecl::Static), isInline);
|
2009-03-12 02:00:04 +03:00
|
|
|
|
|
|
|
isVirtualOkay = (SC != FunctionDecl::Static);
|
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-03-19 21:14:46 +03: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-03-18 02:17:04 +03:00
|
|
|
|
2009-01-16 04:13:29 +03:00
|
|
|
NewFD = FunctionDecl::Create(Context, DC,
|
|
|
|
D.getIdentifierLoc(),
|
2009-03-18 02:17:04 +03:00
|
|
|
Name, R, SC, isInline, HasPrototype,
|
2009-01-16 04:13:29 +03:00
|
|
|
// FIXME: Move to DeclGroup...
|
|
|
|
D.getDeclSpec().getSourceRange().getBegin());
|
|
|
|
}
|
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
if (D.isInvalidType())
|
2009-04-25 09:44:12 +04:00
|
|
|
NewFD->setInvalidDecl();
|
|
|
|
|
2009-01-16 04:13:29 +03:00
|
|
|
// Set the lexical context. If the declarator has a C++
|
|
|
|
// scope specifier, the lexical context will be different
|
|
|
|
// from the semantic context.
|
|
|
|
NewFD->setLexicalDeclContext(CurContext);
|
|
|
|
|
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) {
|
|
|
|
Diag(D.getDeclSpec().getVirtualSpecLoc(),
|
|
|
|
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)
|
|
|
|
<< CodeModificationHint::CreateRemoval(
|
|
|
|
SourceRange(D.getDeclSpec().getVirtualSpecLoc()));
|
|
|
|
} else {
|
|
|
|
// Okay: Add virtual to the method.
|
2009-05-15 02:15:41 +04:00
|
|
|
cast<CXXMethodDecl>(NewFD)->setVirtualAsWritten(true);
|
2009-03-12 02:00:04 +03:00
|
|
|
CXXRecordDecl *CurClass = cast<CXXRecordDecl>(DC);
|
|
|
|
CurClass->setAggregate(false);
|
|
|
|
CurClass->setPOD(false);
|
|
|
|
CurClass->setPolymorphic(true);
|
2009-04-16 04:08:20 +04:00
|
|
|
CurClass->setHasTrivialConstructor(false);
|
2009-03-12 02:00:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-14 05:09:04 +04:00
|
|
|
if (CXXMethodDecl *NewMD = dyn_cast<CXXMethodDecl>(NewFD)) {
|
|
|
|
// Look for virtual methods in base classes that this method might override.
|
|
|
|
|
|
|
|
BasePaths Paths;
|
|
|
|
if (LookupInBases(cast<CXXRecordDecl>(DC),
|
2009-05-30 04:52:53 +04:00
|
|
|
MemberLookupCriteria(NewMD), Paths)) {
|
2009-05-14 05:09:04 +04:00
|
|
|
for (BasePaths::decl_iterator I = Paths.found_decls_begin(),
|
|
|
|
E = Paths.found_decls_end(); I != E; ++I) {
|
|
|
|
if (CXXMethodDecl *OldMD = dyn_cast<CXXMethodDecl>(*I)) {
|
2009-05-17 03:59:08 +04:00
|
|
|
if (!CheckOverridingFunctionReturnType(NewMD, OldMD))
|
|
|
|
NewMD->addOverriddenMethod(OldMD);
|
2009-05-14 05:09:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-12 02:52:16 +03:00
|
|
|
if (SC == FunctionDecl::Static && isa<CXXMethodDecl>(NewFD) &&
|
|
|
|
!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.
|
|
|
|
Diag(D.getDeclSpec().getStorageClassSpecLoc(),
|
|
|
|
diag::err_static_out_of_line)
|
|
|
|
<< CodeModificationHint::CreateRemoval(
|
|
|
|
SourceRange(D.getDeclSpec().getStorageClassSpecLoc()));
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
StringLiteral *SE = cast<StringLiteral>(E);
|
2009-03-04 09:34:08 +03:00
|
|
|
NewFD->addAttr(::new (Context) AsmLabelAttr(std::string(SE->getStrData(),
|
|
|
|
SE->getByteLength())));
|
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 &&
|
2009-03-28 22:18:32 +03:00
|
|
|
FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType()) {
|
2009-04-25 10:12:16 +04:00
|
|
|
// Empty arg list, don't push any params.
|
2009-03-28 22:18:32 +03:00
|
|
|
ParmVarDecl *Param = FTI.ArgInfo[0].Param.getAs<ParmVarDecl>();
|
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-03-28 22:18:32 +03:00
|
|
|
for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
|
|
|
|
Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>());
|
2009-01-16 04:13:29 +03:00
|
|
|
}
|
|
|
|
|
2009-04-25 10:03:53 +04:00
|
|
|
} else if (const FunctionProtoType *FT = R->getAsFunctionProtoType()) {
|
|
|
|
// 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-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) {
|
|
|
|
ParmVarDecl *Param = ParmVarDecl::Create(Context, DC,
|
|
|
|
SourceLocation(), 0,
|
|
|
|
*AI, VarDecl::None, 0);
|
|
|
|
Param->setImplicit();
|
|
|
|
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.
|
2009-05-21 13:52:38 +04:00
|
|
|
NewFD->setParams(Context, Params.data(), Params.size());
|
2009-04-25 22:38:18 +04:00
|
|
|
|
2009-04-25 10:12:16 +04:00
|
|
|
|
2009-01-16 04:13:29 +03:00
|
|
|
|
2009-02-24 22:23:27 +03:00
|
|
|
// If name lookup finds a previous declaration that is not in the
|
|
|
|
// same scope as the new declaration, this may still be an
|
|
|
|
// acceptable redeclaration.
|
|
|
|
if (PrevDecl && !isDeclInScope(PrevDecl, DC, S) &&
|
2009-02-24 23:03:32 +03:00
|
|
|
!(NewFD->hasLinkage() &&
|
|
|
|
isOutOfScopePreviousDeclaration(PrevDecl, DC, Context)))
|
2009-02-24 22:23:27 +03:00
|
|
|
PrevDecl = 0;
|
2009-02-24 04:23:02 +03:00
|
|
|
|
2009-03-24 02:06:20 +03:00
|
|
|
// Perform semantic checking on the function declaration.
|
|
|
|
bool OverloadableAttrRequired = false; // FIXME: HACK!
|
2009-04-25 12:06:05 +04:00
|
|
|
CheckFunctionDeclaration(NewFD, PrevDecl, Redeclaration,
|
|
|
|
/*FIXME:*/OverloadableAttrRequired);
|
2009-03-24 02:06:20 +03:00
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
if (D.getCXXScopeSpec().isSet() && !NewFD->isInvalidDecl()) {
|
2009-03-24 02:06:20 +03:00
|
|
|
// An out-of-line member function declaration must also be a
|
|
|
|
// definition (C++ [dcl.meaning]p1).
|
|
|
|
if (!IsFunctionDefinition) {
|
|
|
|
Diag(NewFD->getLocation(), diag::err_out_of_line_declaration)
|
|
|
|
<< D.getCXXScopeSpec().getRange();
|
2009-04-25 12:06:05 +04:00
|
|
|
NewFD->setInvalidDecl();
|
2009-03-24 02:06:20 +03:00
|
|
|
} else if (!Redeclaration) {
|
|
|
|
// 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)
|
|
|
|
<< cast<NamedDecl>(DC) << D.getCXXScopeSpec().getRange();
|
2009-04-25 12:06:05 +04:00
|
|
|
NewFD->setInvalidDecl();
|
2009-03-24 02:06:20 +03:00
|
|
|
|
|
|
|
LookupResult Prev = LookupQualifiedName(DC, Name, LookupOrdinaryName,
|
|
|
|
true);
|
|
|
|
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(Context, cast<FunctionDecl>(*Func), NewFD))
|
|
|
|
Diag((*Func)->getLocation(), diag::note_member_def_close_match);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrevDecl = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
ProcessDeclAttributes(NewFD, D);
|
|
|
|
AddKnownFunctionAttributes(NewFD);
|
|
|
|
|
|
|
|
if (OverloadableAttrRequired && !NewFD->getAttr<OverloadableAttr>()) {
|
|
|
|
// 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;
|
|
|
|
if (PrevDecl)
|
|
|
|
Diag(PrevDecl->getLocation(),
|
|
|
|
diag::note_attribute_overloadable_prev_overload);
|
|
|
|
NewFD->addAttr(::new (Context) OverloadableAttr());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a locally-scoped extern C function, update the
|
|
|
|
// map of such names.
|
|
|
|
if (CurContext->isFunctionOrMethod() && NewFD->isExternC(Context)
|
2009-04-25 12:06:05 +04:00
|
|
|
&& !NewFD->isInvalidDecl())
|
2009-03-24 02:06:20 +03:00
|
|
|
RegisterLocallyScopedExternCDecl(NewFD, PrevDecl, S);
|
|
|
|
|
|
|
|
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-04-25 12:06:05 +04:00
|
|
|
/// This sets NewFD->isInvalidDecl() to true if there was an error.
|
|
|
|
void Sema::CheckFunctionDeclaration(FunctionDecl *NewFD, NamedDecl *&PrevDecl,
|
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.
|
|
|
|
if (NewFD->isInvalidDecl())
|
|
|
|
return;
|
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-03-24 02:06:20 +03:00
|
|
|
// Semantic checking for this function declaration (in isolation).
|
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
// C++-specific checks.
|
2009-04-25 12:06:05 +04:00
|
|
|
if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(NewFD)) {
|
2009-04-25 12:35:12 +04:00
|
|
|
CheckConstructor(Constructor);
|
2009-04-25 12:06:05 +04:00
|
|
|
} else if (isa<CXXDestructorDecl>(NewFD)) {
|
2009-03-24 02:06:20 +03:00
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(NewFD->getParent());
|
|
|
|
Record->setUserDeclaredDestructor(true);
|
|
|
|
// C++ [class]p4: A POD-struct is an aggregate class that has [...] no
|
|
|
|
// user-defined destructor.
|
|
|
|
Record->setPOD(false);
|
2009-04-17 06:34:54 +04:00
|
|
|
|
|
|
|
// C++ [class.dtor]p3: A destructor is trivial if it is an implicitly-
|
|
|
|
// declared destructor.
|
|
|
|
Record->setHasTrivialDestructor(false);
|
2009-03-24 02:06:20 +03:00
|
|
|
} else if (CXXConversionDecl *Conversion
|
|
|
|
= dyn_cast<CXXConversionDecl>(NewFD))
|
|
|
|
ActOnConversionDeclarator(Conversion);
|
|
|
|
|
|
|
|
// Extra checking for C++ overloaded operators (C++ [over.oper]).
|
|
|
|
if (NewFD->isOverloadedOperator() &&
|
|
|
|
CheckOverloadedOperatorDeclaration(NewFD))
|
2009-04-25 12:06:05 +04:00
|
|
|
return NewFD->setInvalidDecl();
|
2009-03-24 02:06:20 +03:00
|
|
|
}
|
|
|
|
|
2009-04-23 22:22:55 +04:00
|
|
|
// C99 6.7.4p6:
|
|
|
|
// [... ] For a function with external linkage, the following
|
|
|
|
// restrictions apply: [...] If all of the file scope declarations
|
|
|
|
// for a function in a translation unit include the inline
|
|
|
|
// function specifier without extern, then the definition in that
|
|
|
|
// translation unit is an inline definition. An inline definition
|
|
|
|
// does not provide an external definition for the function, and
|
|
|
|
// does not forbid an external definition in another translation
|
|
|
|
// unit.
|
|
|
|
//
|
|
|
|
// Here we determine whether this function, in isolation, would be a
|
|
|
|
// C99 inline definition. MergeCompatibleFunctionDecls looks at
|
|
|
|
// previous declarations.
|
2009-04-28 10:37:30 +04:00
|
|
|
if (NewFD->isInline() && getLangOptions().C99 &&
|
|
|
|
NewFD->getStorageClass() == FunctionDecl::None &&
|
|
|
|
NewFD->getDeclContext()->getLookupContext()->isTranslationUnit())
|
|
|
|
NewFD->setC99InlineDefinition(true);
|
2009-04-23 22:22:55 +04:00
|
|
|
|
2009-03-24 02:06:20 +03:00
|
|
|
// Check for a previous declaration of this name.
|
2009-03-02 03:19:53 +03:00
|
|
|
if (!PrevDecl && NewFD->isExternC(Context)) {
|
|
|
|
// 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())
|
|
|
|
PrevDecl = Pos->second;
|
|
|
|
}
|
|
|
|
|
2009-02-24 04:23:02 +03:00
|
|
|
// Merge or overload the declaration with an existing declaration of
|
|
|
|
// the same name, if appropriate.
|
|
|
|
if (PrevDecl) {
|
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.
|
|
|
|
OverloadedFunctionDecl::function_iterator MatchedDecl;
|
2009-02-13 03:26:38 +03:00
|
|
|
|
|
|
|
if (!getLangOptions().CPlusPlus &&
|
2009-02-18 09:34:51 +03:00
|
|
|
AllowOverloadingOfFunction(PrevDecl, Context)) {
|
2009-02-13 03:26:38 +03:00
|
|
|
OverloadableAttrRequired = true;
|
|
|
|
|
2009-02-18 09:34:51 +03:00
|
|
|
// Functions marked "overloadable" must have a prototype (that
|
|
|
|
// we can't get through declaration merging).
|
2009-03-24 02:06:20 +03:00
|
|
|
if (!NewFD->getType()->getAsFunctionProtoType()) {
|
2009-02-18 09:34:51 +03:00
|
|
|
Diag(NewFD->getLocation(), diag::err_attribute_overloadable_no_prototype)
|
|
|
|
<< NewFD;
|
|
|
|
Redeclaration = true;
|
|
|
|
|
|
|
|
// Turn this into a variadic function with no parameters.
|
2009-03-24 02:06:20 +03:00
|
|
|
QualType R = Context.getFunctionType(
|
|
|
|
NewFD->getType()->getAsFunctionType()->getResultType(),
|
|
|
|
0, 0, true, 0);
|
2009-02-18 09:34:51 +03:00
|
|
|
NewFD->setType(R);
|
2009-04-25 12:06:05 +04:00
|
|
|
return NewFD->setInvalidDecl();
|
2009-02-18 09:34:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PrevDecl &&
|
|
|
|
(!AllowOverloadingOfFunction(PrevDecl, Context) ||
|
|
|
|
!IsOverload(NewFD, PrevDecl, MatchedDecl))) {
|
2009-02-16 20:45:42 +03:00
|
|
|
Redeclaration = true;
|
2009-01-16 04:13:29 +03:00
|
|
|
Decl *OldDecl = PrevDecl;
|
|
|
|
|
|
|
|
// If PrevDecl was an overloaded function, extract the
|
|
|
|
// FunctionDecl that matched.
|
|
|
|
if (isa<OverloadedFunctionDecl>(PrevDecl))
|
|
|
|
OldDecl = *MatchedDecl;
|
|
|
|
|
2009-03-24 02:06:20 +03:00
|
|
|
// NewFD and OldDecl represent declarations that need to be
|
2009-01-16 04:13:29 +03: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-04-25 12:06:05 +04:00
|
|
|
NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl));
|
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-04-25 12:06:05 +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 (getLangOptions().CPlusPlus && !CurContext->isRecord())
|
2009-03-24 02:06:20 +03:00
|
|
|
CheckCXXDefaultArguments(NewFD);
|
2009-01-16 04:13:29 +03: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.)
|
2009-02-25 00:54:33 +03:00
|
|
|
if (Init->isConstantInitializer(Context))
|
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
|
|
|
}
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
void Sema::AddInitializerToDecl(DeclPtrTy dcl, ExprArg init) {
|
2009-01-14 18:45:31 +03:00
|
|
|
AddInitializerToDecl(dcl, move(init), /*DirectInit=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AddInitializerToDecl - Adds the initializer Init to the
|
|
|
|
/// declaration dcl. If DirectInit is true, this is C++ direct
|
|
|
|
/// initialization rather than copy initialization.
|
2009-03-28 22:18:32 +03:00
|
|
|
void Sema::AddInitializerToDecl(DeclPtrTy dcl, ExprArg init, bool DirectInit) {
|
|
|
|
Decl *RealDecl = dcl.getAs<Decl>();
|
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;
|
2007-09-12 18:07:44 +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;
|
|
|
|
Expr *Init = static_cast<Expr *>(init.get());
|
|
|
|
if ((IL = dyn_cast<IntegerLiteral>(Init)) && IL->getValue() == 0 &&
|
|
|
|
Context.getCanonicalType(IL->getType()) == Context.IntTy) {
|
2009-05-15 02:15:41 +04:00
|
|
|
if (Method->isVirtualAsWritten()) {
|
2009-03-12 02:00:04 +03:00
|
|
|
Method->setPure();
|
2009-03-22 04:52:17 +03:00
|
|
|
|
|
|
|
// A class is abstract if at least one function is pure virtual.
|
|
|
|
cast<CXXRecordDecl>(CurContext)->setAbstract(true);
|
2009-03-27 07:38:56 +03:00
|
|
|
} else if (!Method->isInvalidDecl()) {
|
2009-03-12 02:00:04 +03:00
|
|
|
Diag(Method->getLocation(), diag::err_non_virtual_pure)
|
|
|
|
<< Method->getDeclName() << Init->getSourceRange();
|
|
|
|
Method->setInvalidDecl();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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))
|
|
|
|
Diag(RealDecl->getLocation(), diag::err_member_initialization)
|
|
|
|
<< cast<NamedDecl>(RealDecl)->getDeclName();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-04-14 01:28:54 +04:00
|
|
|
if (!VDecl->getType()->isArrayType() &&
|
|
|
|
RequireCompleteType(VDecl->getLocation(), VDecl->getType(),
|
|
|
|
diag::err_typecheck_decl_incomplete_type)) {
|
|
|
|
RealDecl->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-11 02:43:53 +03:00
|
|
|
const VarDecl *Def = 0;
|
|
|
|
if (VDecl->getDefinition(Def)) {
|
|
|
|
Diag(VDecl->getLocation(), diag::err_redefinition)
|
|
|
|
<< VDecl->getDeclName();
|
|
|
|
Diag(Def->getLocation(), diag::note_previous_definition);
|
|
|
|
VDecl->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-02-27 07:17:12 +03:00
|
|
|
// Take ownership of the expression, now that we're sure we have somewhere
|
|
|
|
// to put it.
|
2009-05-01 23:30:39 +04:00
|
|
|
Expr *Init = init.takeAs<Expr>();
|
2009-02-27 07:17:12 +03:00
|
|
|
assert(Init && "missing initializer");
|
|
|
|
|
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()) {
|
2008-11-05 18:29:30 +03:00
|
|
|
if (CheckInitializerTypes(Init, DclT, VDecl->getLocation(),
|
2009-05-27 20:10:08 +04:00
|
|
|
VDecl->getDeclName(), DirectInit, VDecl))
|
2008-04-16 02:42:06 +04:00
|
|
|
VDecl->setInvalidDecl();
|
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()) {
|
2009-04-14 06:25:56 +04:00
|
|
|
if (VDecl->getStorageClass() == VarDecl::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-03-12 02:00:04 +03:00
|
|
|
} else if (VDecl->isStaticDataMember() &&
|
|
|
|
VDecl->getLexicalDeclContext()->isRecord()) {
|
|
|
|
// This is an in-class initialization for a static data member, e.g.,
|
|
|
|
//
|
|
|
|
// struct S {
|
|
|
|
// static const int value = 17;
|
|
|
|
// };
|
|
|
|
|
|
|
|
// Attach the initializer
|
2009-05-26 22:54:04 +04:00
|
|
|
VDecl->setInit(Context, Init);
|
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();
|
|
|
|
if (!T->isDependentType() &&
|
|
|
|
(!Context.getCanonicalType(T).isConstQualified() ||
|
|
|
|
!T->isIntegralType())) {
|
|
|
|
Diag(VDecl->getLocation(), diag::err_member_initialization)
|
|
|
|
<< VDecl->getDeclName() << Init->getSourceRange();
|
|
|
|
VDecl->setInvalidDecl();
|
|
|
|
} else {
|
|
|
|
// 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).
|
|
|
|
if (!Init->isTypeDependent() &&
|
|
|
|
!Init->getType()->isIntegralType()) {
|
|
|
|
// We have a non-dependent, non-integral or enumeration type.
|
|
|
|
Diag(Init->getSourceRange().getBegin(),
|
|
|
|
diag::err_in_class_initializer_non_integral_type)
|
|
|
|
<< Init->getType() << Init->getSourceRange();
|
|
|
|
VDecl->setInvalidDecl();
|
|
|
|
} else if (!Init->isTypeDependent() && !Init->isValueDependent()) {
|
|
|
|
// 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();
|
2009-03-26 02:32:15 +03:00
|
|
|
} else if (!VDecl->getType()->isDependentType())
|
|
|
|
ImpCastExprToType(Init, VDecl->getType());
|
2009-03-12 02:00:04 +03:00
|
|
|
}
|
|
|
|
}
|
2008-04-16 02:42:06 +04:00
|
|
|
} else if (VDecl->isFileVarDecl()) {
|
2009-04-15 19:20:03 +04:00
|
|
|
if (VDecl->getStorageClass() == VarDecl::Extern)
|
2007-09-13 00:13:48 +04:00
|
|
|
Diag(VDecl->getLocation(), diag::warn_extern_init);
|
2008-04-16 02:42:06 +04:00
|
|
|
if (!VDecl->isInvalidDecl())
|
2008-11-05 18:29:30 +03:00
|
|
|
if (CheckInitializerTypes(Init, DclT, VDecl->getLocation(),
|
2009-01-14 18:45:31 +03:00
|
|
|
VDecl->getDeclName(), DirectInit))
|
2008-04-16 02:42:06 +04:00
|
|
|
VDecl->setInvalidDecl();
|
2008-01-11 01:15:12 +03: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
|
|
|
|
// completed by the initializer. For example:
|
|
|
|
// 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);
|
|
|
|
}
|
2007-09-12 18:07:44 +04:00
|
|
|
|
|
|
|
// Attach the initializer to the decl.
|
2009-05-26 22:54:04 +04:00
|
|
|
VDecl->setInit(Context, Init);
|
2009-04-21 21:11:58 +04:00
|
|
|
|
|
|
|
// If the previous declaration of VDecl was a tentative definition,
|
|
|
|
// remove it from the set of tentative definitions.
|
|
|
|
if (VDecl->getPreviousDeclaration() &&
|
|
|
|
VDecl->getPreviousDeclaration()->isTentativeDefinition(Context)) {
|
|
|
|
llvm::DenseMap<DeclarationName, VarDecl *>::iterator Pos
|
|
|
|
= TentativeDefinitions.find(VDecl->getDeclName());
|
|
|
|
assert(Pos != TentativeDefinitions.end() &&
|
|
|
|
"Unrecorded tentative definition?");
|
|
|
|
TentativeDefinitions.erase(Pos);
|
|
|
|
}
|
|
|
|
|
2007-09-12 18:07:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
void Sema::ActOnUninitializedDecl(DeclPtrTy dcl) {
|
|
|
|
Decl *RealDecl = dcl.getAs<Decl>();
|
2008-10-29 03:13:59 +03:00
|
|
|
|
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
|
|
|
|
|
|
|
// Record tentative definitions.
|
|
|
|
if (Var->isTentativeDefinition(Context))
|
|
|
|
TentativeDefinitions[Var->getDeclName()] = Var;
|
|
|
|
|
2008-10-29 03:13:59 +03:00
|
|
|
// C++ [dcl.init.ref]p3:
|
|
|
|
// The initializer can be omitted for a reference only in a
|
|
|
|
// parameter declaration (8.3.5), in the declaration of a
|
|
|
|
// function return type, in the declaration of a class member
|
|
|
|
// within its class declaration (9.2), and where the extern
|
|
|
|
// specifier is explicitly used.
|
2009-04-14 06:25:56 +04:00
|
|
|
if (Type->isReferenceType() && !Var->hasExternalStorage()) {
|
2008-11-20 09:06:08 +03:00
|
|
|
Diag(Var->getLocation(), diag::err_reference_var_requires_init)
|
2008-11-24 08:29:24 +03:00
|
|
|
<< Var->getDeclName()
|
|
|
|
<< SourceRange(Var->getLocation(), Var->getLocation());
|
2008-11-03 23:45:27 +03:00
|
|
|
Var->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++ [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.
|
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
QualType InitType = Type;
|
|
|
|
if (const ArrayType *Array = Context.getAsArrayType(Type))
|
|
|
|
InitType = Array->getElementType();
|
2009-05-17 02:05:23 +04:00
|
|
|
if ((!Var->hasExternalStorage() && !Var->isExternC(Context)) &&
|
2009-05-29 18:25:00 +04:00
|
|
|
InitType->isRecordType() && !InitType->isDependentType()) {
|
2009-04-17 03:50:50 +04:00
|
|
|
CXXRecordDecl *RD =
|
|
|
|
cast<CXXRecordDecl>(InitType->getAsRecordType()->getDecl());
|
|
|
|
CXXConstructorDecl *Constructor = 0;
|
2009-03-09 19:13:40 +03:00
|
|
|
if (!RequireCompleteType(Var->getLocation(), InitType,
|
2009-03-03 07:44:36 +03:00
|
|
|
diag::err_invalid_incomplete_type_use))
|
|
|
|
Constructor
|
|
|
|
= PerformInitializationByConstructor(InitType, 0, 0,
|
|
|
|
Var->getLocation(),
|
2008-11-05 18:29:30 +03:00
|
|
|
SourceRange(Var->getLocation(),
|
|
|
|
Var->getLocation()),
|
2009-03-03 07:44:36 +03:00
|
|
|
Var->getDeclName(),
|
|
|
|
IK_Default);
|
2008-11-03 23:45:27 +03:00
|
|
|
if (!Constructor)
|
|
|
|
Var->setInvalidDecl();
|
2009-04-17 03:50:50 +04:00
|
|
|
else if (!RD->hasTrivialConstructor())
|
|
|
|
InitializeVarWithConstructor(Var, Constructor, InitType, 0, 0);
|
2008-11-03 23:45:27 +03:00
|
|
|
}
|
|
|
|
}
|
2008-10-29 03:13:59 +03:00
|
|
|
|
2008-10-29 16:50:18 +03:00
|
|
|
#if 0
|
|
|
|
// FIXME: Temporarily disabled because we are not properly parsing
|
|
|
|
// linkage specifications on declarations, e.g.,
|
|
|
|
//
|
|
|
|
// extern "C" const CGPoint CGPointerZero;
|
|
|
|
//
|
2008-10-29 03:13:59 +03:00
|
|
|
// C++ [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
|
|
|
|
// an 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.
|
|
|
|
//
|
|
|
|
// This isn't technically an error in C, so we don't diagnose it.
|
|
|
|
//
|
|
|
|
// FIXME: Actually perform the POD/user-defined default
|
|
|
|
// constructor check.
|
|
|
|
if (getLangOptions().CPlusPlus &&
|
2008-10-29 16:50:18 +03:00
|
|
|
Context.getCanonicalType(Type).isConstQualified() &&
|
2009-04-14 06:25:56 +04:00
|
|
|
!Var->hasExternalStorage())
|
2008-11-20 09:38:18 +03:00
|
|
|
Diag(Var->getLocation(), diag::err_const_var_requires_init)
|
|
|
|
<< Var->getName()
|
|
|
|
<< SourceRange(Var->getLocation(), Var->getLocation());
|
2008-10-29 16:50:18 +03:00
|
|
|
#endif
|
2008-10-29 03:13:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-29 05:49:24 +04:00
|
|
|
Sema::DeclGroupPtrTy Sema::FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS,
|
|
|
|
DeclPtrTy *Group,
|
2009-03-29 20:50:03 +04:00
|
|
|
unsigned NumDecls) {
|
|
|
|
llvm::SmallVector<Decl*, 8> Decls;
|
2009-05-29 05:49:24 +04:00
|
|
|
|
|
|
|
if (DS.isTypeSpecOwned())
|
|
|
|
Decls.push_back((Decl*)DS.getTypeRep());
|
|
|
|
|
2009-03-29 20:50:03 +04:00
|
|
|
for (unsigned i = 0; i != NumDecls; ++i)
|
|
|
|
if (Decl *D = Group[i].getAs<Decl>())
|
|
|
|
Decls.push_back(D);
|
2007-09-14 03:52:58 +04:00
|
|
|
|
2007-09-12 18:07:44 +04:00
|
|
|
// Perform semantic analysis that depends on having fully processed both
|
|
|
|
// the declarator and initializer.
|
2009-03-29 20:50:03 +04:00
|
|
|
for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
|
|
|
|
VarDecl *IDecl = dyn_cast<VarDecl>(Decls[i]);
|
2007-09-12 18:07:44 +04:00
|
|
|
if (!IDecl)
|
|
|
|
continue;
|
|
|
|
QualType T = IDecl->getType();
|
2009-03-29 20:50:03 +04:00
|
|
|
|
2007-09-12 18:07:44 +04:00
|
|
|
// 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...
|
2009-04-14 06:25:56 +04:00
|
|
|
if (IDecl->isBlockVarDecl() && !IDecl->hasExternalStorage()) {
|
2009-01-19 22:26:10 +03:00
|
|
|
if (!IDecl->isInvalidDecl() &&
|
2009-03-09 19:13:40 +03:00
|
|
|
RequireCompleteType(IDecl->getLocation(), T,
|
2009-03-29 20:50:03 +04:00
|
|
|
diag::err_typecheck_decl_incomplete_type))
|
2007-09-12 18:07:44 +04:00
|
|
|
IDecl->setInvalidDecl();
|
|
|
|
}
|
|
|
|
// File scope. C99 6.9.2p2: A declaration of an identifier for and
|
|
|
|
// 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).
|
2009-03-11 02:43:53 +03:00
|
|
|
if (IDecl->isTentativeDefinition(Context)) {
|
2009-03-11 00:58:27 +03:00
|
|
|
QualType CheckType = T;
|
|
|
|
unsigned DiagID = diag::err_typecheck_decl_incomplete_type;
|
2009-03-29 20:50:03 +04:00
|
|
|
|
2009-03-11 00:58:27 +03:00
|
|
|
const IncompleteArrayType *ArrayT = Context.getAsIncompleteArrayType(T);
|
|
|
|
if (ArrayT) {
|
|
|
|
CheckType = ArrayT->getElementType();
|
|
|
|
DiagID = diag::err_illegal_decl_array_incomplete_type;
|
|
|
|
}
|
2009-03-29 20:50:03 +04:00
|
|
|
|
2009-03-11 00:58:27 +03:00
|
|
|
if (IDecl->isInvalidDecl()) {
|
|
|
|
// Do nothing with invalid declarations
|
|
|
|
} else if ((ArrayT || IDecl->getStorageClass() == VarDecl::Static) &&
|
|
|
|
RequireCompleteType(IDecl->getLocation(), CheckType, DiagID)) {
|
2008-01-18 23:40:52 +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.
|
2007-09-12 18:07:44 +04:00
|
|
|
IDecl->setInvalidDecl();
|
2009-03-11 00:58:27 +03:00
|
|
|
}
|
2007-09-12 18:07:44 +04:00
|
|
|
}
|
2007-07-11 21:01:13 +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.
|
2009-03-28 22:18:32 +03:00
|
|
|
Sema::DeclPtrTy
|
2008-04-08 08:40:51 +04:00
|
|
|
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'.
|
2008-09-04 01:54:21 +04:00
|
|
|
VarDecl::StorageClass StorageClass = VarDecl::None;
|
|
|
|
if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
|
|
|
|
StorageClass = VarDecl::Register;
|
|
|
|
} 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);
|
|
|
|
|
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;
|
|
|
|
QualType parmDeclType = GetTypeForDeclarator(D, S, /*Skip=*/0, &OwnedDecl);
|
2008-04-08 08:40:51 +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);
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
|
|
|
|
// Can this happen for params? We already checked that they don't conflict
|
|
|
|
// among each other. Here they can only shadow globals, which is ok.
|
2008-04-08 08:40:51 +04:00
|
|
|
IdentifierInfo *II = D.getIdentifier();
|
2009-01-21 05:38:50 +03:00
|
|
|
if (II) {
|
2009-02-04 20:27:36 +03:00
|
|
|
if (NamedDecl *PrevDecl = LookupName(S, II, LookupOrdinaryName)) {
|
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;
|
2009-03-28 22:18:32 +03:00
|
|
|
} else if (S->isDeclScope(DeclPtrTy::make(PrevDecl))) {
|
2009-01-21 05:38:50 +03:00
|
|
|
Diag(D.getIdentifierLoc(), diag::err_param_redefinition) << II;
|
|
|
|
|
|
|
|
// Recover by removing the name
|
|
|
|
II = 0;
|
|
|
|
D.SetIdentifier(0, D.getIdentifierLoc());
|
|
|
|
}
|
2008-04-08 08:40:51 +04:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-08-08 02:44:21 +04:00
|
|
|
|
2009-03-23 22:10:31 +03:00
|
|
|
// Parameters can not be abstract class types.
|
2009-03-24 04:19:16 +03:00
|
|
|
// For record types, this is done by the AbstractClassUsageDiagnoser once
|
|
|
|
// the class has been completely parsed.
|
|
|
|
if (!CurContext->isRecord() &&
|
|
|
|
RequireNonAbstractType(D.getIdentifierLoc(), parmDeclType,
|
2009-03-23 22:10:31 +03:00
|
|
|
diag::err_abstract_type_in_decl,
|
2009-03-24 04:19:16 +03:00
|
|
|
AbstractParamType))
|
2009-03-23 22:10:31 +03:00
|
|
|
D.setInvalidType(true);
|
2009-03-24 02:06:20 +03:00
|
|
|
|
|
|
|
QualType T = adjustParameterType(parmDeclType);
|
2009-03-23 22:10:31 +03:00
|
|
|
|
2009-03-24 02:06:20 +03:00
|
|
|
ParmVarDecl *New;
|
|
|
|
if (T == parmDeclType) // parameter type did not need adjustment
|
|
|
|
New = ParmVarDecl::Create(Context, CurContext,
|
|
|
|
D.getIdentifierLoc(), II,
|
|
|
|
parmDeclType, StorageClass,
|
|
|
|
0);
|
|
|
|
else // keep track of both the adjusted and unadjusted types
|
|
|
|
New = OriginalParmVarDecl::Create(Context, CurContext,
|
|
|
|
D.getIdentifierLoc(), II, T,
|
|
|
|
parmDeclType, StorageClass, 0);
|
2008-02-15 10:04:12 +03:00
|
|
|
|
2009-04-25 12:06:05 +04:00
|
|
|
if (D.isInvalidType())
|
2007-08-28 22:45:29 +04:00
|
|
|
New->setInvalidDecl();
|
2008-12-11 19:49:14 +03:00
|
|
|
|
2009-02-21 01:59:16 +03:00
|
|
|
// Parameter declarators cannot be interface types. All ObjC objects are
|
|
|
|
// passed by reference.
|
2009-03-24 02:06:20 +03:00
|
|
|
if (T->isObjCInterfaceType()) {
|
2009-04-11 23:08:56 +04:00
|
|
|
Diag(D.getIdentifierLoc(),
|
|
|
|
diag::err_object_cannot_be_passed_returned_by_value) << 1 << T;
|
2009-02-21 01:59:16 +03: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();
|
|
|
|
}
|
2008-12-16 02:53:10 +03:00
|
|
|
|
2008-12-11 19:49:14 +03:00
|
|
|
// Add the parameter declaration into this scope.
|
2009-03-28 22:18:32 +03:00
|
|
|
S->AddDecl(DeclPtrTy::make(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
|
|
|
|
2008-06-29 04:02:00 +04:00
|
|
|
ProcessDeclAttributes(New, D);
|
2009-04-30 04:19:40 +04:00
|
|
|
|
|
|
|
if (New->hasAttr<BlocksAttr>()) {
|
|
|
|
Diag(New->getLocation(), diag::err_block_on_nonlocal);
|
|
|
|
}
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy::make(New);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-11-09 02:49:49 +03:00
|
|
|
|
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-04-02 07:14:12 +04:00
|
|
|
std::string Code = " int ";
|
2009-04-02 03:51:29 +04:00
|
|
|
Code += FTI.ArgInfo[i].Ident->getName();
|
2009-04-02 07:14:12 +04:00
|
|
|
Code += ";\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
|
|
|
|
<< CodeModificationHint::CreateInsertion(LocAfterDecls, Code);
|
|
|
|
|
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
|
|
|
|
DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc,
|
|
|
|
PrevSpec);
|
|
|
|
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-01-23 19:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
Sema::DeclPtrTy 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) {
|
2008-04-08 08:40:51 +04:00
|
|
|
// FIXME: Diagnose arguments without names in C.
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-12-16 02:53:10 +03:00
|
|
|
Scope *ParentScope = FnBodyScope->getParent();
|
2008-01-14 23:51:29 +03:00
|
|
|
|
2009-03-29 20:50:03 +04:00
|
|
|
DeclPtrTy DP = ActOnDeclarator(ParentScope, D, /*IsFunctionDefinition=*/true);
|
|
|
|
return ActOnStartOfFunctionDef(FnBodyScope, DP);
|
2008-07-01 14:37:29 +04:00
|
|
|
}
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
Sema::DeclPtrTy Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclPtrTy D) {
|
|
|
|
FunctionDecl *FD = cast<FunctionDecl>(D.getAs<Decl>());
|
2008-10-29 18:10:40 +03:00
|
|
|
|
2009-04-19 09:21:20 +04:00
|
|
|
CurFunctionNeedsScopeChecking = false;
|
|
|
|
|
2008-10-29 18:10:40 +03:00
|
|
|
// See if this is a redefinition.
|
|
|
|
const FunctionDecl *Definition;
|
2009-04-18 04:02:19 +04:00
|
|
|
if (FD->getBody(Context, Definition)) {
|
2008-11-24 00:45:46 +03:00
|
|
|
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.
|
|
|
|
if (unsigned BuiltinID = FD->getBuiltinID(Context)) {
|
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-04-08 19:21:36 +04:00
|
|
|
if (!FD->isInvalidDecl() && FD->isGlobal() && !isa<CXXMethodDecl>(FD) &&
|
|
|
|
!FD->isMain()) {
|
2009-03-31 20:35:03 +04: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MissingPrototype)
|
|
|
|
Diag(FD->getLocation(), diag::warn_missing_prototype) << FD;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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.
|
2009-05-15 21:59:04 +04:00
|
|
|
if (Param->getIdentifier() && FnBodyScope)
|
2008-04-12 04:47:19 +04:00
|
|
|
PushOnScopeChains(Param, FnBodyScope);
|
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.
|
|
|
|
if (FD->getAttr<DLLImportAttr>() && (!FD->getAttr<DLLExportAttr>())) {
|
|
|
|
// dllimport attribute cannot be applied to definition.
|
|
|
|
if (!(FD->getAttr<DLLImportAttr>())->isInherited()) {
|
|
|
|
Diag(FD->getLocation(),
|
|
|
|
diag::err_attribute_can_be_applied_only_to_symbol_declaration)
|
|
|
|
<< "dllimport";
|
|
|
|
FD->setInvalidDecl();
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy::make(FD);
|
2008-12-26 03:52:02 +03:00
|
|
|
} else {
|
|
|
|
// 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)
|
|
|
|
<< FD->getNameAsCString() << "dllimport";
|
|
|
|
}
|
|
|
|
}
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy::make(FD);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
Sema::DeclPtrTy Sema::ActOnFinishFunctionBody(DeclPtrTy D, StmtArg BodyArg) {
|
2009-05-15 21:59:04 +04:00
|
|
|
return ActOnFinishFunctionBody(D, move(BodyArg), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sema::DeclPtrTy Sema::ActOnFinishFunctionBody(DeclPtrTy D, StmtArg BodyArg,
|
|
|
|
bool IsInstantiation) {
|
2009-03-28 22:18:32 +03:00
|
|
|
Decl *dcl = D.getAs<Decl>();
|
2009-04-27 00:35:05 +04:00
|
|
|
Stmt *Body = BodyArg.takeAs<Stmt>();
|
2008-07-25 21:57:26 +04:00
|
|
|
if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(dcl)) {
|
2009-04-18 13:36:27 +04:00
|
|
|
FD->setBody(Body);
|
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-02-07 04:47:29 +03:00
|
|
|
} else {
|
|
|
|
Body->Destroy(Context);
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy();
|
2009-02-07 04:47:29 +03:00
|
|
|
}
|
2009-05-15 21:59:04 +04:00
|
|
|
if (!IsInstantiation)
|
|
|
|
PopDeclContext();
|
|
|
|
|
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
|
|
|
|
2009-04-19 00:05:34 +04:00
|
|
|
assert(&getLabelMap() == &FunctionLabelMap && "Didn't pop block right?");
|
2009-04-19 00:01:55 +04:00
|
|
|
|
2007-11-10 19:31:34 +03:00
|
|
|
// Check goto/label use.
|
2009-03-13 18:38:40 +03:00
|
|
|
for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator
|
2009-04-19 00:01:55 +04:00
|
|
|
I = FunctionLabelMap.begin(), E = FunctionLabelMap.end(); I != E; ++I) {
|
2009-04-18 23:30:02 +04:00
|
|
|
LabelStmt *L = I->second;
|
|
|
|
|
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.
|
2009-04-18 23:30:02 +04:00
|
|
|
if (L->getSubStmt() != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Emit error.
|
|
|
|
Diag(L->getIdentLoc(), diag::err_undeclared_label_use) << L->getName();
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
// The whole function wasn't parsed correctly, just delete this.
|
|
|
|
L->Destroy(Context);
|
|
|
|
continue;
|
2007-11-10 19:31:34 +03: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);
|
|
|
|
std::vector<Stmt*> Elements(Compound->body_begin(), Compound->body_end());
|
2009-04-18 23:30:02 +04:00
|
|
|
Elements.push_back(L);
|
2009-04-27 00:35:05 +04:00
|
|
|
Compound->setStmts(Context, &Elements[0], Elements.size());
|
2007-11-10 19:31:34 +03:00
|
|
|
}
|
2009-04-19 00:01:55 +04:00
|
|
|
FunctionLabelMap.clear();
|
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
|
|
|
|
|
|
|
if (!Body) return D;
|
|
|
|
|
2009-04-19 01:00:42 +04:00
|
|
|
// Verify that that gotos and switch cases don't jump into scopes illegally.
|
2009-04-19 09:21:20 +04:00
|
|
|
if (CurFunctionNeedsScopeChecking)
|
|
|
|
DiagnoseInvalidJumps(Body);
|
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
|
|
|
|
2009-04-28 01:33:24 +04:00
|
|
|
// 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 (isa<CXXConstructorDecl>(dcl) && isa<CXXTryStmt>(Body))
|
|
|
|
DiagnoseReturnInConstructorExceptionHandler(cast<CXXTryStmt>(Body));
|
|
|
|
|
2007-11-12 02:20:51 +03:00
|
|
|
return D;
|
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-01-20 04:17:11 +03:00
|
|
|
NamedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc,
|
|
|
|
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.
|
|
|
|
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;
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// FIXME: handle stuff like:
|
|
|
|
// void foo() { extern float X(); }
|
|
|
|
// void bar() { X(); } <-- implicit decl for X in another scope.
|
|
|
|
|
|
|
|
// Set a Declarator for the implicit definition: int foo();
|
|
|
|
const char *Dummy;
|
|
|
|
DeclSpec DS;
|
|
|
|
bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
|
|
|
|
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-29 22:02:33 +04:00
|
|
|
0, 0, false, false, 0,0,0, 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();
|
|
|
|
|
2008-04-04 18:32:09 +04:00
|
|
|
FunctionDecl *FD =
|
2009-03-28 22:18:32 +03:00
|
|
|
dyn_cast<FunctionDecl>(ActOnDeclarator(TUScope, D, DeclPtrTy()).getAs<Decl>());
|
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.
|
|
|
|
if (unsigned BuiltinID = FD->getBuiltinID(Context)) {
|
|
|
|
// Handle printf-formatting attributes.
|
|
|
|
unsigned FormatIdx;
|
|
|
|
bool HasVAListArg;
|
|
|
|
if (Context.BuiltinInfo.isPrintfLike(BuiltinID, FormatIdx, HasVAListArg)) {
|
|
|
|
if (!FD->getAttr<FormatAttr>())
|
2009-03-04 09:34:08 +03:00
|
|
|
FD->addAttr(::new (Context) FormatAttr("printf", FormatIdx + 1,
|
|
|
|
FormatIdx + 2));
|
2009-02-14 21:57:46 +03:00
|
|
|
}
|
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)) {
|
|
|
|
if (!FD->getAttr<ConstAttr>())
|
2009-03-04 09:34:08 +03:00
|
|
|
FD->addAttr(::new (Context) ConstAttr());
|
2009-02-17 01:43:43 +03:00
|
|
|
}
|
2009-02-14 21:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
IdentifierInfo *Name = FD->getIdentifier();
|
|
|
|
if (!Name)
|
|
|
|
return;
|
|
|
|
if ((!getLangOptions().CPlusPlus &&
|
|
|
|
FD->getDeclContext()->isTranslationUnit()) ||
|
|
|
|
(isa<LinkageSpecDecl>(FD->getDeclContext()) &&
|
|
|
|
cast<LinkageSpecDecl>(FD->getDeclContext())->getLanguage() ==
|
|
|
|
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-02-14 21:57:46 +03:00
|
|
|
if (const FormatAttr *Format = FD->getAttr<FormatAttr>()) {
|
|
|
|
// FIXME: We known better than our headers.
|
|
|
|
const_cast<FormatAttr *>(Format)->setType("printf");
|
|
|
|
} else
|
2009-03-04 09:34:08 +03:00
|
|
|
FD->addAttr(::new (Context) FormatAttr("printf", 1, 2));
|
2009-04-23 00:56:09 +04:00
|
|
|
} else if (Name->isStr("asprintf") || Name->isStr("vasprintf")) {
|
2009-02-14 21:57:46 +03:00
|
|
|
if (!FD->getAttr<FormatAttr>())
|
2009-03-04 09:34:08 +03:00
|
|
|
FD->addAttr(::new (Context) FormatAttr("printf", 2, 3));
|
2009-02-14 21:57:46 +03:00
|
|
|
}
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2009-03-29 20:50:03 +04:00
|
|
|
TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T) {
|
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");
|
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(),
|
2008-03-15 09:12:44 +03:00
|
|
|
D.getIdentifier(),
|
2009-01-20 04:17:11 +03:00
|
|
|
T);
|
2009-03-10 20:07:44 +03:00
|
|
|
|
|
|
|
if (TagType *TT = dyn_cast<TagType>(T)) {
|
|
|
|
TagDecl *TD = TT->getDecl();
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
bool Sema::isAcceptableTagRedeclaration(const TagDecl *Previous,
|
|
|
|
TagDecl::TagKind NewTag,
|
|
|
|
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
|
|
|
|
// declaration to which the name in theelaborated-type-specifier
|
|
|
|
// 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
|
|
|
|
// refer to an enumeration (7.2), the union class-keyshall be
|
|
|
|
// 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.
|
|
|
|
TagDecl::TagKind OldTag = Previous->getTagKind();
|
|
|
|
if (OldTag == NewTag)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ((OldTag == TagDecl::TK_struct || OldTag == TagDecl::TK_class) &&
|
|
|
|
(NewTag == TagDecl::TK_struct || NewTag == TagDecl::TK_class)) {
|
|
|
|
// 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)
|
|
|
|
<< (NewTag == TagDecl::TK_class)
|
|
|
|
<< isTemplate << &Name
|
|
|
|
<< CodeModificationHint::CreateReplacement(SourceRange(NewTagLoc),
|
|
|
|
OldTag == TagDecl::TK_class? "class" : "struct");
|
|
|
|
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-01-17 03:42:38 +03:00
|
|
|
/// TagSpec indicates what kind of tag this is. TK indicates whether this is a
|
2007-07-11 21:01:13 +04:00
|
|
|
/// reference/declaration/definition of a tag.
|
2009-03-28 22:18:32 +03:00
|
|
|
Sema::DeclPtrTy Sema::ActOnTag(Scope *S, unsigned TagSpec, TagKind TK,
|
|
|
|
SourceLocation KWLoc, const CXXScopeSpec &SS,
|
|
|
|
IdentifierInfo *Name, SourceLocation NameLoc,
|
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
|
|
|
AttributeList *Attr, AccessSpecifier AS,
|
|
|
|
bool &OwnedDecl) {
|
2008-12-15 19:32:14 +03:00
|
|
|
// If this is not a definition, it must have a name.
|
2007-07-11 21:01:13 +04:00
|
|
|
assert((Name != 0 || TK == TK_Definition) &&
|
|
|
|
"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;
|
2008-06-10 03:19:58 +04:00
|
|
|
TagDecl::TagKind Kind;
|
2009-01-17 03:42:38 +03:00
|
|
|
switch (TagSpec) {
|
2007-07-11 21:01:13 +04:00
|
|
|
default: assert(0 && "Unknown tag type!");
|
2008-06-10 03:19:58 +04:00
|
|
|
case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
|
|
|
|
case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
|
|
|
|
case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
|
|
|
|
case DeclSpec::TST_enum: Kind = TagDecl::TK_enum; break;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-11-10 01:09:58 +03:00
|
|
|
|
2009-01-13 01:49:06 +03:00
|
|
|
DeclContext *SearchDC = CurContext;
|
2008-11-10 01:53:32 +03:00
|
|
|
DeclContext *DC = CurContext;
|
2009-02-04 20:27:36 +03:00
|
|
|
NamedDecl *PrevDecl = 0;
|
2008-11-08 20:17:31 +03:00
|
|
|
|
2009-01-17 03:42:38 +03:00
|
|
|
bool Invalid = false;
|
|
|
|
|
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-05-14 04:28:11 +04:00
|
|
|
if (RequireCompleteDeclContext(SS))
|
|
|
|
return DeclPtrTy::make((Decl *)0);
|
|
|
|
|
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
|
|
|
DC = computeDeclContext(SS);
|
2009-02-03 03:34:39 +03:00
|
|
|
SearchDC = DC;
|
2008-11-10 01:53:32 +03:00
|
|
|
// Look-up name inside 'foo::'.
|
2009-05-14 04:28:11 +04:00
|
|
|
PrevDecl
|
|
|
|
= dyn_cast_or_null<TagDecl>(
|
|
|
|
LookupQualifiedName(DC, Name, LookupTagName, true).getAsDecl());
|
2008-11-10 01:09:58 +03:00
|
|
|
|
|
|
|
// A tag 'foo::bar' must already exist.
|
2008-11-08 20:17:31 +03:00
|
|
|
if (PrevDecl == 0) {
|
2008-11-19 11:23:25 +03:00
|
|
|
Diag(NameLoc, diag::err_not_tag_in_scope) << Name << 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-02-03 22:26:08 +03:00
|
|
|
LookupResult R = LookupName(S, Name, LookupTagName,
|
|
|
|
/*RedeclarationOnly=*/(TK != TK_Reference));
|
2009-02-03 22:21:40 +03:00
|
|
|
if (R.isAmbiguous()) {
|
|
|
|
DiagnoseAmbiguousLookup(R, Name, NameLoc);
|
|
|
|
// FIXME: This is not best way to recover from case like:
|
|
|
|
//
|
|
|
|
// struct S s;
|
|
|
|
//
|
2009-04-02 01:51:26 +04:00
|
|
|
// causes needless "incomplete type" error later.
|
2009-02-03 22:21:40 +03:00
|
|
|
Name = 0;
|
|
|
|
PrevDecl = 0;
|
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
else
|
2009-02-04 20:27:36 +03:00
|
|
|
PrevDecl = R;
|
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 (!getLangOptions().CPlusPlus && TK != TK_Reference) {
|
|
|
|
// 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
|
|
|
}
|
2008-11-08 20:17:31 +03:00
|
|
|
}
|
|
|
|
|
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(NameLoc, PrevDecl);
|
|
|
|
// Just pretend that we didn't see the previous declaration.
|
|
|
|
PrevDecl = 0;
|
|
|
|
}
|
|
|
|
|
2009-02-17 01:07:16 +03:00
|
|
|
if (PrevDecl) {
|
2009-02-19 00:56:37 +03:00
|
|
|
// Check whether the previous declaration is usable.
|
|
|
|
(void)DiagnoseUseOfDecl(PrevDecl, NameLoc);
|
2009-02-17 01:07:16 +03: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-01-13 01:49:06 +03:00
|
|
|
if (TK == TK_Reference || 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-04-02 03:51:29 +04:00
|
|
|
bool SafeToContinue
|
|
|
|
= (PrevTagDecl->getTagKind() != TagDecl::TK_enum &&
|
|
|
|
Kind != TagDecl::TK_enum);
|
|
|
|
if (SafeToContinue)
|
|
|
|
Diag(KWLoc, diag::err_use_with_wrong_tag)
|
|
|
|
<< Name
|
|
|
|
<< CodeModificationHint::CreateReplacement(SourceRange(KWLoc),
|
|
|
|
PrevTagDecl->getKindName());
|
|
|
|
else
|
|
|
|
Diag(KWLoc, diag::err_use_with_wrong_tag) << Name;
|
2008-11-24 02:12:31 +03:00
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_use);
|
2009-04-02 03:51:29 +04:00
|
|
|
|
|
|
|
if (SafeToContinue)
|
|
|
|
Kind = PrevTagDecl->getTagKind();
|
|
|
|
else {
|
|
|
|
// Recover by making this an anonymous redefinition.
|
|
|
|
Name = 0;
|
|
|
|
PrevDecl = 0;
|
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (TK == TK_Reference)
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy::make(PrevDecl);
|
2009-02-04 22:02:06 +03:00
|
|
|
|
2008-07-03 07:30:58 +04:00
|
|
|
// Diagnose attempts to redefine a tag.
|
2008-12-15 19:32:14 +03:00
|
|
|
if (TK == TK_Definition) {
|
|
|
|
if (TagDecl *Def = PrevTagDecl->getDefinition(Context)) {
|
|
|
|
Diag(NameLoc, diag::err_redefinition) << Name;
|
|
|
|
Diag(Def->getLocation(), diag::note_previous_definition);
|
2009-01-17 03:42:38 +03:00
|
|
|
// If this is a redefinition, recover by making this
|
|
|
|
// struct be anonymous, which will make any later
|
|
|
|
// references get the previous definition.
|
2008-12-15 19:32:14 +03:00
|
|
|
Name = 0;
|
|
|
|
PrevDecl = 0;
|
2009-01-17 03:42:38 +03:00
|
|
|
Invalid = true;
|
|
|
|
} 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;
|
|
|
|
Diag(PrevTagDecl->getLocation(),
|
|
|
|
diag::note_previous_definition);
|
|
|
|
Name = 0;
|
|
|
|
PrevDecl = 0;
|
|
|
|
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
|
|
|
|
// have a definition. Just create a new decl.
|
|
|
|
} else {
|
|
|
|
// If we get here, this is a definition of a new tag type in a nested
|
|
|
|
// scope, e.g. "struct foo; void bar() { struct foo; }", just create a
|
|
|
|
// new decl/type. We set PrevDecl to NULL so that the entities
|
|
|
|
// have distinct types.
|
|
|
|
PrevDecl = 0;
|
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.
|
2008-04-27 17:50:30 +04:00
|
|
|
} else {
|
2009-01-28 20:15:10 +03:00
|
|
|
// PrevDecl is a namespace, template, or anything else
|
|
|
|
// that lives in the IDNS_Tag identifier namespace.
|
2009-01-13 01:49:06 +03:00
|
|
|
if (isDeclInScope(PrevDecl, SearchDC, S)) {
|
2008-09-03 22:03:35 +04:00
|
|
|
// The tag name clashes with a namespace name, 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;
|
2008-12-15 19:32:14 +03:00
|
|
|
PrevDecl = 0;
|
2009-01-17 03:42:38 +03:00
|
|
|
Invalid = true;
|
2008-12-15 19:32:14 +03:00
|
|
|
} else {
|
|
|
|
// The existing declaration isn't relevant to us; we're in a
|
|
|
|
// new scope, so clear out the previous declaration.
|
|
|
|
PrevDecl = 0;
|
2008-07-16 11:45:46 +04:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2009-01-10 01:42:13 +03:00
|
|
|
} else if (TK == TK_Reference && SS.isEmpty() && Name &&
|
2009-03-06 21:34:03 +03:00
|
|
|
(Kind != TagDecl::TK_enum || !getLangOptions().CPlusPlus)) {
|
2009-05-11 23:58:34 +04:00
|
|
|
// C++ [basic.scope.pdecl]p5:
|
2009-01-10 01:42:13 +03:00
|
|
|
// -- 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.
|
2009-03-06 21:34:03 +03:00
|
|
|
//
|
|
|
|
// GNU C also supports this behavior as part of its incomplete
|
|
|
|
// enum types extension, while GNU C++ does not.
|
|
|
|
//
|
2009-01-10 01:42:13 +03:00
|
|
|
// Find the context where we'll be declaring the tag.
|
2009-01-13 01:49:06 +03:00
|
|
|
// FIXME: We would like to maintain the current DeclContext as the
|
|
|
|
// lexical context,
|
2009-02-03 03:34:39 +03:00
|
|
|
while (SearchDC->isRecord())
|
|
|
|
SearchDC = SearchDC->getParent();
|
2009-01-10 01:42:13 +03:00
|
|
|
|
|
|
|
// Find the scope where we'll be declaring the tag.
|
|
|
|
while (S->isClassScope() ||
|
|
|
|
(getLangOptions().CPlusPlus && S->isFunctionPrototypeScope()) ||
|
2009-01-12 21:45:55 +03:00
|
|
|
((S->getFlags() & Scope::DeclScope) == 0) ||
|
|
|
|
(S->getEntity() &&
|
|
|
|
((DeclContext *)S->getEntity())->isTransparentContext()))
|
2009-01-10 01:42:13 +03:00
|
|
|
S = S->getParent();
|
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:
|
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;
|
|
|
|
|
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
|
|
|
|
2008-06-10 03:19:58 +04:00
|
|
|
if (Kind == TagDecl::TK_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-02-03 03:34:39 +03:00
|
|
|
New = EnumDecl::Create(Context, SearchDC, Loc, Name,
|
2008-12-15 19:32:14 +03:00
|
|
|
cast_or_null<EnumDecl>(PrevDecl));
|
2007-07-11 21:01:13 +04:00
|
|
|
// If this is an undefined enum, warn.
|
2009-03-06 21:34:03 +03:00
|
|
|
if (TK != TK_Definition && !Invalid) {
|
|
|
|
unsigned DK = getLangOptions().CPlusPlus? diag::err_forward_ref_enum
|
|
|
|
: diag::ext_forward_ref_enum;
|
|
|
|
Diag(Loc, DK);
|
|
|
|
}
|
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-02-07 01:42:48 +03:00
|
|
|
if (getLangOptions().CPlusPlus)
|
2008-09-05 21:39:33 +04:00
|
|
|
// FIXME: Look for a way to use RecordDecl for simple structs.
|
2009-02-03 03:34:39 +03:00
|
|
|
New = CXXRecordDecl::Create(Context, Kind, SearchDC, Loc, Name,
|
2008-12-15 19:32:14 +03:00
|
|
|
cast_or_null<CXXRecordDecl>(PrevDecl));
|
2009-02-07 01:42:48 +03:00
|
|
|
else
|
2009-02-03 03:34:39 +03:00
|
|
|
New = RecordDecl::Create(Context, Kind, SearchDC, Loc, Name,
|
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
|
|
|
|
|
|
|
if (Kind != TagDecl::TK_enum) {
|
|
|
|
// Handle #pragma pack: if the #pragma pack stack has non-default
|
|
|
|
// alignment, make up a packed attribute for this decl. These
|
|
|
|
// attributes are checked when the ASTContext lays out the
|
|
|
|
// structure.
|
|
|
|
//
|
|
|
|
// 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).
|
2009-02-17 04:09:29 +03:00
|
|
|
if (unsigned Alignment = getPragmaPackAlignment())
|
2009-03-04 09:34:08 +03:00
|
|
|
New->addAttr(::new (Context) PackedAttr(Alignment * 8));
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-12-15 19:32:14 +03:00
|
|
|
|
2009-01-28 20:15:10 +03:00
|
|
|
if (getLangOptions().CPlusPlus && SS.isEmpty() && Name && !Invalid) {
|
|
|
|
// C++ [dcl.typedef]p3:
|
|
|
|
// [...] Similarly, in a given scope, a class or enumeration
|
|
|
|
// shall not be declared with the same name as a typedef-name
|
|
|
|
// that is declared in that scope and refers to a type other
|
|
|
|
// than the class or enumeration itself.
|
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
|
|
|
LookupResult Lookup = LookupName(S, Name, LookupOrdinaryName, true);
|
2009-01-28 20:15:10 +03:00
|
|
|
TypedefDecl *PrevTypedef = 0;
|
|
|
|
if (Lookup.getKind() == LookupResult::Found)
|
|
|
|
PrevTypedef = dyn_cast<TypedefDecl>(Lookup.getAsDecl());
|
|
|
|
|
|
|
|
if (PrevTypedef && isDeclInScope(PrevTypedef, SearchDC, S) &&
|
|
|
|
Context.getCanonicalType(Context.getTypeDeclType(PrevTypedef)) !=
|
|
|
|
Context.getCanonicalType(Context.getTypeDeclType(New))) {
|
|
|
|
Diag(Loc, diag::err_tag_definition_of_typedef)
|
|
|
|
<< Context.getTypeDeclType(New)
|
|
|
|
<< PrevTypedef->getUnderlyingType();
|
|
|
|
Diag(PrevTypedef->getLocation(), diag::note_previous_definition);
|
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-17 03:42:38 +03:00
|
|
|
if (Invalid)
|
|
|
|
New->setInvalidDecl();
|
|
|
|
|
2008-06-29 03:58:55 +04:00
|
|
|
if (Attr)
|
|
|
|
ProcessDeclAttributeList(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-03-26 04:19:02 +03:00
|
|
|
// Set the access specifier.
|
2009-05-27 21:30:49 +04:00
|
|
|
if (!Invalid)
|
|
|
|
SetMemberAccessSpecifier(New, PrevDecl, AS);
|
2009-03-26 01:00:53 +03:00
|
|
|
|
2009-01-17 03:42:38 +03:00
|
|
|
if (TK == TK_Definition)
|
|
|
|
New->startDefinition();
|
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.
|
2008-12-15 19:32:14 +03:00
|
|
|
if (Name) {
|
2009-01-12 21:45:55 +03:00
|
|
|
S = getNonFieldDeclScope(S);
|
2009-02-03 03:34:39 +03:00
|
|
|
PushOnScopeChains(New, S);
|
2009-01-13 01:49:06 +03:00
|
|
|
} else {
|
2009-04-10 01:40:53 +04:00
|
|
|
CurContext->addDecl(Context, 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
|
|
|
|
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;
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy::make(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
|
|
|
}
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
void Sema::ActOnTagStartDefinition(Scope *S, DeclPtrTy TagD) {
|
2009-02-04 22:02:06 +03:00
|
|
|
AdjustDeclIfTemplate(TagD);
|
2009-03-28 22:18:32 +03:00
|
|
|
TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>());
|
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);
|
|
|
|
|
|
|
|
if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Tag)) {
|
|
|
|
FieldCollector->StartClass();
|
|
|
|
|
|
|
|
if (Record->getIdentifier()) {
|
|
|
|
// 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.
|
2009-03-26 00:17:03 +03:00
|
|
|
CXXRecordDecl *InjectedClassName
|
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
|
|
|
= CXXRecordDecl::Create(Context, Record->getTagKind(),
|
|
|
|
CurContext, Record->getLocation(),
|
|
|
|
Record->getIdentifier(), Record);
|
|
|
|
InjectedClassName->setImplicit();
|
2009-03-26 00:17:03 +03:00
|
|
|
InjectedClassName->setAccess(AS_public);
|
2009-03-26 03:10:35 +03:00
|
|
|
if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate())
|
|
|
|
InjectedClassName->setDescribedClassTemplate(Template);
|
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
|
|
|
PushOnScopeChains(InjectedClassName, S);
|
2009-03-25 18:59:44 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
void Sema::ActOnTagFinishDefinition(Scope *S, DeclPtrTy TagD) {
|
2009-02-04 22:02:06 +03:00
|
|
|
AdjustDeclIfTemplate(TagD);
|
2009-03-28 22:18:32 +03:00
|
|
|
TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>());
|
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();
|
|
|
|
|
|
|
|
// Notify the consumer that we've defined a tag.
|
|
|
|
Consumer.HandleTagDeclDefinition(Tag);
|
|
|
|
}
|
2008-06-21 23:39:06 +04:00
|
|
|
|
2009-04-20 21:29:38 +04:00
|
|
|
// Note that FieldName may be null for anonymous bitfields.
|
2008-12-06 23:33:04 +03:00
|
|
|
bool Sema::VerifyBitField(SourceLocation FieldLoc, IdentifierInfo *FieldName,
|
2008-12-12 07:56:04 +03:00
|
|
|
QualType FieldTy, const Expr *BitWidth) {
|
2009-04-20 21:29:38 +04:00
|
|
|
|
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.
|
2009-03-11 21:59:21 +03:00
|
|
|
if (!FieldTy->isDependentType() && !FieldTy->isIntegralType()) {
|
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;
|
|
|
|
|
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-04-20 21:29:38 +04:00
|
|
|
if (Value.isSigned() && Value.isNegative()) {
|
|
|
|
if (FieldName)
|
|
|
|
return Diag(FieldLoc, diag::err_bitfield_has_negative_width)
|
|
|
|
<< 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) {
|
|
|
|
if (FieldName)
|
|
|
|
return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_size)
|
|
|
|
<< FieldName << (unsigned)TypeSize;
|
|
|
|
return Diag(FieldLoc, diag::err_anon_bitfield_width_exceeds_type_size)
|
|
|
|
<< (unsigned)TypeSize;
|
|
|
|
}
|
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.
|
2009-03-28 22:18:32 +03:00
|
|
|
Sema::DeclPtrTy Sema::ActOnField(Scope *S, DeclPtrTy TagD,
|
|
|
|
SourceLocation DeclStart,
|
|
|
|
Declarator &D, ExprTy *BitfieldWidth) {
|
|
|
|
FieldDecl *Res = HandleField(S, cast_or_null<RecordDecl>(TagD.getAs<Decl>()),
|
|
|
|
DeclStart, D, static_cast<Expr*>(BitfieldWidth),
|
|
|
|
AS_public);
|
|
|
|
return DeclPtrTy::make(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-03-11 21:59:21 +03:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
QualType T = GetTypeForDeclarator(D, S);
|
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);
|
|
|
|
|
2009-03-11 21:59:21 +03:00
|
|
|
NamedDecl *PrevDecl = LookupName(S, II, LookupMemberName, true);
|
|
|
|
if (PrevDecl && !isDeclInScope(PrevDecl, Record, S))
|
|
|
|
PrevDecl = 0;
|
|
|
|
|
|
|
|
FieldDecl *NewFD
|
|
|
|
= CheckFieldDecl(II, T, Record, Loc,
|
|
|
|
D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_mutable,
|
2009-03-11 23:50:30 +03:00
|
|
|
BitWidth, AS, PrevDecl, &D);
|
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-04-10 01:40:53 +04:00
|
|
|
Record->addDecl(Context, 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.
|
|
|
|
///
|
|
|
|
/// \todo The Declarator argument is a hack. It will be removed once
|
|
|
|
FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T,
|
|
|
|
RecordDecl *Record, SourceLocation Loc,
|
|
|
|
bool Mutable, Expr *BitWidth,
|
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;
|
|
|
|
}
|
|
|
|
|
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.
|
2008-02-15 15:53:51 +03:00
|
|
|
if (T->isVariablyModifiedType()) {
|
2009-02-21 03:44:51 +03:00
|
|
|
bool SizeIsNegative;
|
|
|
|
QualType FixedTy = TryToFixInvalidVariablyModifiedType(T, Context,
|
|
|
|
SizeIsNegative);
|
|
|
|
if (!FixedTy.isNull()) {
|
|
|
|
Diag(Loc, diag::warn_illegal_constant_array_size);
|
|
|
|
T = FixedTy;
|
|
|
|
} else {
|
|
|
|
if (SizeIsNegative)
|
|
|
|
Diag(Loc, diag::err_typecheck_negative_array_size);
|
|
|
|
else
|
|
|
|
Diag(Loc, diag::err_typecheck_field_variable_size);
|
|
|
|
T = Context.IntTy;
|
|
|
|
InvalidDecl = true;
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-12-06 23:33:04 +03:00
|
|
|
|
2009-03-22 23:18:17 +03:00
|
|
|
// Fields can not have abstract class types
|
2009-03-23 20:49:10 +03:00
|
|
|
if (RequireNonAbstractType(Loc, T, diag::err_abstract_type_in_decl,
|
2009-03-24 04:19:16 +03:00
|
|
|
AbstractFieldType))
|
2009-03-22 23:18:17 +03:00
|
|
|
InvalidDecl = true;
|
|
|
|
|
2009-03-11 21:59:21 +03:00
|
|
|
// If this is declared as a bit-field, check the bit-field.
|
|
|
|
if (BitWidth && VerifyBitField(Loc, II, T, BitWidth)) {
|
|
|
|
InvalidDecl = true;
|
|
|
|
DeleteExpr(BitWidth);
|
|
|
|
BitWidth = 0;
|
2008-12-06 23:33:04 +03:00
|
|
|
}
|
|
|
|
|
2009-03-11 21:59:21 +03:00
|
|
|
FieldDecl *NewFD = FieldDecl::Create(Context, Record, Loc, II, T, 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
|
|
|
}
|
|
|
|
|
2009-03-11 21:59:21 +03:00
|
|
|
if (getLangOptions().CPlusPlus && !T->isPODType())
|
|
|
|
cast<CXXRecordDecl>(Record)->setPOD(false);
|
|
|
|
|
|
|
|
// FIXME: We need to pass in the attributes given an AST
|
|
|
|
// representation, not a parser representation.
|
|
|
|
if (D)
|
|
|
|
ProcessDeclAttributes(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);
|
|
|
|
|
|
|
|
// C++ [dcl.init.aggr]p1:
|
|
|
|
// An aggregate is an array or a class (clause 9) with [...] no
|
|
|
|
// private or protected non-static data members (clause 11).
|
|
|
|
// A POD must be an aggregate.
|
|
|
|
if (getLangOptions().CPlusPlus &&
|
|
|
|
(AS == AS_private || AS == AS_protected)) {
|
|
|
|
CXXRecordDecl *CXXRecord = cast<CXXRecordDecl>(Record);
|
|
|
|
CXXRecord->setAggregate(false);
|
|
|
|
CXXRecord->setPOD(false);
|
|
|
|
}
|
|
|
|
|
2007-08-29 00:14:24 +04:00
|
|
|
return NewFD;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2007-10-01 20:53:59 +04:00
|
|
|
/// TranslateIvarVisibility - Translate visibility from a token ID to an
|
|
|
|
/// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-11 20:55:42 +04:00
|
|
|
/// ActOnIvar - Each ivar field of an objective-c class is passed into this
|
|
|
|
/// in order to create an IvarDecl object for it.
|
2009-03-28 22:18:32 +03:00
|
|
|
Sema::DeclPtrTy Sema::ActOnIvar(Scope *S,
|
|
|
|
SourceLocation DeclStart,
|
|
|
|
Declarator &D, ExprTy *BitfieldWidth,
|
|
|
|
tok::ObjCKeywordKind Visibility) {
|
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
|
|
|
|
2008-04-11 03:32:45 +04:00
|
|
|
IdentifierInfo *II = D.getIdentifier();
|
|
|
|
Expr *BitWidth = (Expr*)BitfieldWidth;
|
|
|
|
SourceLocation Loc = DeclStart;
|
|
|
|
if (II) Loc = D.getIdentifierLoc();
|
|
|
|
|
|
|
|
// FIXME: Unnamed fields can be handled in various different ways, for
|
|
|
|
// example, unnamed unions inject all members into the struct namespace!
|
|
|
|
|
2008-12-06 23:33:04 +03:00
|
|
|
QualType T = GetTypeForDeclarator(D, S);
|
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
|
|
|
DeleteExpr(BitWidth);
|
|
|
|
BitWidth = 0;
|
|
|
|
}
|
2008-04-11 03:32:45 +04:00
|
|
|
} else {
|
|
|
|
// Not a bitfield.
|
|
|
|
|
|
|
|
// validate II.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// C99 6.7.2.1p8: A member of a structure or union may have any type other
|
|
|
|
// than a variably modified type.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-07-23 22:04:17 +04:00
|
|
|
// Get the visibility (access control) for this ivar.
|
|
|
|
ObjCIvarDecl::AccessControl ac =
|
|
|
|
Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility)
|
|
|
|
: ObjCIvarDecl::None;
|
|
|
|
|
|
|
|
// Construct the decl.
|
2009-02-17 23:20:37 +03:00
|
|
|
ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, CurContext, Loc, II, T,ac,
|
2008-07-16 22:22:22 +04:00
|
|
|
(Expr *)BitfieldWidth);
|
2008-04-11 03:32:45 +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) {
|
2009-02-04 20:27:36 +03:00
|
|
|
NamedDecl *PrevDecl = LookupName(S, II, LookupMemberName, true);
|
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 (PrevDecl && isDeclInScope(PrevDecl, CurContext, S)
|
|
|
|
&& !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.
|
2008-06-29 04:02:00 +04:00
|
|
|
ProcessDeclAttributes(NewID, D);
|
2008-04-11 03:32:45 +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.
|
2009-03-28 22:18:32 +03:00
|
|
|
S->AddDecl(DeclPtrTy::make(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);
|
|
|
|
}
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy::make(NewID);
|
2008-04-11 03:32:45 +04:00
|
|
|
}
|
|
|
|
|
2007-09-29 04:54:24 +04:00
|
|
|
void Sema::ActOnFields(Scope* S,
|
2009-03-28 22:18:32 +03:00
|
|
|
SourceLocation RecLoc, DeclPtrTy RecDecl,
|
|
|
|
DeclPtrTy *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) {
|
2009-03-28 22:18:32 +03:00
|
|
|
Decl *EnclosingDecl = RecDecl.getAs<Decl>();
|
2007-09-15 02:20:54 +04:00
|
|
|
assert(EnclosingDecl && "missing record or interface decl");
|
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;
|
|
|
|
}
|
|
|
|
|
2007-09-15 02:20:54 +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) {
|
2009-03-28 22:18:32 +03:00
|
|
|
FieldDecl *FD = cast<FieldDecl>(Fields[i].getAs<Decl>());
|
2007-09-15 02:20:54 +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-03-06 01:45:59 +03:00
|
|
|
|
|
|
|
// If the field is already invalid for some reason, don't emit more
|
|
|
|
// diagnostics about it.
|
|
|
|
if (FD->isInvalidDecl())
|
|
|
|
continue;
|
2007-09-15 03:09:53 +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;
|
2009-03-24 22:52:54 +03:00
|
|
|
} else if (FDTy->isIncompleteArrayType() && i == NumFields - 1 &&
|
|
|
|
Record && Record->isStruct()) {
|
|
|
|
// Flexible array member.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
// 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() &&
|
|
|
|
RequireCompleteType(FD->getLocation(), FD->getType(),
|
|
|
|
diag::err_field_incomplete)) {
|
|
|
|
// Incomplete type
|
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
|
|
|
continue;
|
|
|
|
} else if (const RecordType *FDTTy = FDTy->getAsRecordType()) {
|
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-03-24 22:52:54 +03:00
|
|
|
} else if (FDTy->isObjCInterfaceType()) {
|
|
|
|
/// 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;
|
|
|
|
}
|
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) {
|
2008-12-11 19:49:14 +03:00
|
|
|
Record->completeDefinition(Context);
|
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->setIVarList(ClsFields, RecFields.size(), Context);
|
|
|
|
ID->setLocEnd(RBrac);
|
|
|
|
|
2008-12-16 04:08:35 +03:00
|
|
|
// Must enforce the rule that ivars in the base classes may not be
|
|
|
|
// duplicates.
|
2008-12-18 01:21:44 +03:00
|
|
|
if (ID->getSuperClass()) {
|
|
|
|
for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
|
|
|
|
IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
|
|
|
|
ObjCIvarDecl* Ivar = (*IVI);
|
2009-05-03 05:08:28 +04:00
|
|
|
|
|
|
|
if (IdentifierInfo *II = Ivar->getIdentifier()) {
|
|
|
|
ObjCIvarDecl* prevIvar =
|
|
|
|
ID->getSuperClass()->lookupInstanceVariable(Context, II);
|
|
|
|
if (prevIvar) {
|
|
|
|
Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
|
|
|
|
Diag(prevIvar->getLocation(), diag::note_previous_declaration);
|
|
|
|
}
|
2008-12-16 04:08:35 +03:00
|
|
|
}
|
2008-12-18 01:21:44 +03:00
|
|
|
}
|
2008-12-16 04:08:35 +03:00
|
|
|
}
|
2009-02-24 01:00:08 +03:00
|
|
|
} else if (ObjCImplementationDecl *IMPDecl =
|
|
|
|
dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
|
2008-01-07 22:49:32 +03:00
|
|
|
assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl");
|
2009-04-23 07:23:08 +04:00
|
|
|
for (unsigned I = 0, N = RecFields.size(); I != N; ++I) {
|
|
|
|
// FIXME: Set the DeclContext correctly when we build the
|
|
|
|
// declarations.
|
|
|
|
ClsFields[I]->setLexicalDeclContext(IMPDecl);
|
|
|
|
IMPDecl->addDecl(Context, ClsFields[I]);
|
|
|
|
}
|
2007-10-31 21:48:14 +03:00
|
|
|
CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac);
|
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)
|
|
|
|
ProcessDeclAttributeList(Record, Attr);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-03-17 22:05:46 +03:00
|
|
|
EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum,
|
|
|
|
EnumConstantDecl *LastEnumConst,
|
|
|
|
SourceLocation IdLoc,
|
|
|
|
IdentifierInfo *Id,
|
|
|
|
ExprArg val) {
|
|
|
|
Expr *Val = (Expr *)val.get();
|
|
|
|
|
|
|
|
llvm::APSInt EnumVal(32);
|
|
|
|
QualType EltTy;
|
|
|
|
if (Val && !Val->isTypeDependent()) {
|
|
|
|
// Make sure to promote the operand type to int.
|
|
|
|
UsualUnaryConversions(Val);
|
|
|
|
if (Val != val.get()) {
|
|
|
|
val.release();
|
|
|
|
val = Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// C99 6.7.2.2p2: Make sure we have an integer constant expression.
|
|
|
|
SourceLocation ExpLoc;
|
|
|
|
if (!Val->isValueDependent() &&
|
|
|
|
VerifyIntegerConstantExpression(Val, &EnumVal)) {
|
|
|
|
Val = 0;
|
|
|
|
} else {
|
|
|
|
EltTy = Val->getType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Val) {
|
|
|
|
if (LastEnumConst) {
|
|
|
|
// Assign the last value + 1.
|
|
|
|
EnumVal = LastEnumConst->getInitVal();
|
|
|
|
++EnumVal;
|
|
|
|
|
|
|
|
// Check for overflow on increment.
|
|
|
|
if (EnumVal < LastEnumConst->getInitVal())
|
|
|
|
Diag(IdLoc, diag::warn_enum_value_overflow);
|
|
|
|
|
|
|
|
EltTy = LastEnumConst->getType();
|
|
|
|
} else {
|
|
|
|
// First value, set to zero.
|
|
|
|
EltTy = Context.IntTy;
|
|
|
|
EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val.release();
|
|
|
|
return EnumConstantDecl::Create(Context, Enum, IdLoc, Id, EltTy,
|
|
|
|
Val, EnumVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
Sema::DeclPtrTy Sema::ActOnEnumConstant(Scope *S, DeclPtrTy theEnumDecl,
|
|
|
|
DeclPtrTy lastEnumConst,
|
|
|
|
SourceLocation IdLoc,
|
|
|
|
IdentifierInfo *Id,
|
|
|
|
SourceLocation EqualLoc, ExprTy *val) {
|
|
|
|
EnumDecl *TheEnumDecl = cast<EnumDecl>(theEnumDecl.getAs<Decl>());
|
2007-07-11 21:01:13 +04:00
|
|
|
EnumConstantDecl *LastEnumConst =
|
2009-03-28 22:18:32 +03:00
|
|
|
cast_or_null<EnumConstantDecl>(lastEnumConst.getAs<Decl>());
|
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);
|
2007-08-26 10:24:45 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Verify that there isn't already something declared with this name in this
|
|
|
|
// scope.
|
2009-02-04 20:27:36 +03:00
|
|
|
NamedDecl *PrevDecl = LookupName(S, Id, LookupOrdinaryName);
|
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);
|
2009-02-28 22:37:57 +03:00
|
|
|
if (Val) Val->Destroy(Context);
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy();
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-17 22:05:46 +03:00
|
|
|
EnumConstantDecl *New = CheckEnumConstant(TheEnumDecl, LastEnumConst,
|
|
|
|
IdLoc, Id, Owned(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.
|
2009-03-17 22:05:46 +03:00
|
|
|
if (New)
|
|
|
|
PushOnScopeChains(New, S);
|
2008-12-17 05:04:30 +03:00
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
return DeclPtrTy::make(New);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-05-16 11:06:02 +04:00
|
|
|
void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc,
|
|
|
|
SourceLocation RBraceLoc, DeclPtrTy EnumDeclX,
|
2009-03-28 22:18:32 +03:00
|
|
|
DeclPtrTy *Elements, unsigned NumElements) {
|
|
|
|
EnumDecl *Enum = cast<EnumDecl>(EnumDeclX.getAs<Decl>());
|
2009-01-05 22:45:36 +03:00
|
|
|
QualType EnumType = Context.getTypeDeclType(Enum);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
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();
|
2007-08-28 09:10:31 +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;
|
|
|
|
|
|
|
|
// Keep track of whether all elements have type int.
|
|
|
|
bool AllElementsInt = true;
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
for (unsigned i = 0; i != NumElements; ++i) {
|
|
|
|
EnumConstantDecl *ECD =
|
2009-03-28 22:18:32 +03:00
|
|
|
cast_or_null<EnumConstantDecl>(Elements[i].getAs<Decl>());
|
2007-07-11 21:01:13 +04:00
|
|
|
if (!ECD) continue; // Already issued a diagnostic.
|
2007-08-28 09:27:00 +04:00
|
|
|
|
|
|
|
// If the enum value doesn't fit in an int, emit an extension warning.
|
|
|
|
const llvm::APSInt &InitVal = ECD->getInitVal();
|
2008-02-26 03:33:57 +03:00
|
|
|
assert(InitVal.getBitWidth() >= IntWidth &&
|
|
|
|
"Should have promoted value to int");
|
2007-08-28 09:27:00 +04:00
|
|
|
if (InitVal.getBitWidth() > IntWidth) {
|
|
|
|
llvm::APSInt V(InitVal);
|
|
|
|
V.trunc(IntWidth);
|
|
|
|
V.extend(InitVal.getBitWidth());
|
|
|
|
if (V != InitVal)
|
2008-11-20 09:38:18 +03:00
|
|
|
Diag(ECD->getLocation(), diag::ext_enum_value_not_int)
|
|
|
|
<< InitVal.toString(10);
|
2007-08-28 09:27:00 +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)
|
|
|
|
AllElementsInt = ECD->getType() == Context.IntTy;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2007-08-28 10:15:15 +04:00
|
|
|
// Figure out the type that should be used for this enum.
|
|
|
|
// FIXME: Support attribute(packed) on enums and -fshort-enums.
|
|
|
|
QualType BestType;
|
2007-08-29 21:31:48 +04:00
|
|
|
unsigned BestWidth;
|
2007-08-28 10:15:15 +04:00
|
|
|
|
|
|
|
if (NumNegativeBits) {
|
|
|
|
// If there is a negative value, figure out the smallest integer type (of
|
|
|
|
// int/long/longlong) that fits.
|
2007-08-29 21:31:48 +04:00
|
|
|
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();
|
2007-12-13 01:39:36 +03:00
|
|
|
|
2007-08-29 21:31:48 +04:00
|
|
|
if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth)
|
2007-08-28 10:15:15 +04:00
|
|
|
BestType = Context.LongTy;
|
|
|
|
else {
|
2008-03-05 21:54:05 +03:00
|
|
|
BestWidth = Context.Target.getLongLongWidth();
|
2007-12-13 01:39:36 +03: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If there is no negative value, figure out which of uint, ulong, ulonglong
|
|
|
|
// fits.
|
2007-08-29 21:31:48 +04:00
|
|
|
if (NumPositiveBits <= IntWidth) {
|
2007-08-28 10:15:15 +04:00
|
|
|
BestType = Context.UnsignedIntTy;
|
2007-08-29 21:31:48 +04:00
|
|
|
BestWidth = IntWidth;
|
|
|
|
} 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;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
EnumConstantDecl *ECD =
|
2009-03-28 22:18:32 +03:00
|
|
|
cast_or_null<EnumConstantDecl>(Elements[i].getAs<Decl>());
|
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'.
|
2008-02-26 03:33:57 +03:00
|
|
|
if (ECD->getType() == Context.IntTy) {
|
|
|
|
// Make sure the init value is signed.
|
|
|
|
llvm::APSInt IV = ECD->getInitVal();
|
|
|
|
IV.setIsSigned(true);
|
|
|
|
ECD->setInitVal(IV);
|
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
|
|
|
|
// enumeration.
|
|
|
|
ECD->setType(EnumType);
|
2007-08-29 21:31:48 +04:00
|
|
|
continue; // Already int type.
|
2008-02-26 03:33:57 +03:00
|
|
|
}
|
2007-08-29 21:31:48 +04:00
|
|
|
|
|
|
|
// Determine whether the value fits into an int.
|
|
|
|
llvm::APSInt InitVal = ECD->getInitVal();
|
|
|
|
bool FitsInInt;
|
|
|
|
if (InitVal.isUnsigned() || !InitVal.isNegative())
|
|
|
|
FitsInInt = InitVal.getActiveBits() < IntWidth;
|
|
|
|
else
|
|
|
|
FitsInInt = InitVal.getMinSignedBits() <= IntWidth;
|
|
|
|
|
|
|
|
// If it fits into an integer type, force it. Otherwise force it to match
|
|
|
|
// the enum decl type.
|
|
|
|
QualType NewTy;
|
|
|
|
unsigned NewWidth;
|
|
|
|
bool NewSign;
|
|
|
|
if (FitsInInt) {
|
|
|
|
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
|
|
|
|
// enumeration.
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Adjust the Expr initializer and type.
|
2009-01-15 22:19:42 +03:00
|
|
|
if (ECD->getInitExpr())
|
2009-02-07 04:47:29 +03:00
|
|
|
ECD->setInitExpr(new (Context) ImplicitCastExpr(NewTy, ECD->getInitExpr(),
|
|
|
|
/*isLvalue=*/false));
|
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
|
|
|
|
// enumeration.
|
|
|
|
ECD->setType(EnumType);
|
|
|
|
else
|
|
|
|
ECD->setType(NewTy);
|
2007-08-29 21:31:48 +04:00
|
|
|
}
|
2007-08-28 10:15:15 +04:00
|
|
|
|
2008-12-11 19:49:14 +03:00
|
|
|
Enum->completeDefinition(Context, BestType);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-03-28 22:18:32 +03:00
|
|
|
Sema::DeclPtrTy Sema::ActOnFileScopeAsmDecl(SourceLocation Loc,
|
|
|
|
ExprArg expr) {
|
2009-05-01 23:30:39 +04:00
|
|
|
StringLiteral *AsmString = cast<StringLiteral>(expr.takeAs<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);
|
|
|
|
CurContext->addDecl(Context, New);
|
|
|
|
return DeclPtrTy::make(New);
|
2008-02-08 03:33:21 +03:00
|
|
|
}
|