2012-12-18 22:30:41 +08:00
|
|
|
//===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines common functions that both ASTReader and ASTWriter use.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ASTCommon.h"
|
2014-08-28 09:33:39 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2013-01-21 23:25:38 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2012-12-18 22:30:41 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "clang/Serialization/ASTDeserializationListener.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
// Give ASTDeserializationListener's VTable a home.
|
2015-10-20 21:23:58 +08:00
|
|
|
ASTDeserializationListener::~ASTDeserializationListener() { }
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
serialization::TypeIdx
|
|
|
|
serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
|
|
|
|
unsigned ID = 0;
|
|
|
|
switch (BT->getKind()) {
|
2015-09-15 20:18:29 +08:00
|
|
|
case BuiltinType::Void:
|
|
|
|
ID = PREDEF_TYPE_VOID_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Bool:
|
|
|
|
ID = PREDEF_TYPE_BOOL_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
ID = PREDEF_TYPE_CHAR_U_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
ID = PREDEF_TYPE_UCHAR_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::UShort:
|
|
|
|
ID = PREDEF_TYPE_USHORT_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::UInt:
|
|
|
|
ID = PREDEF_TYPE_UINT_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::ULong:
|
|
|
|
ID = PREDEF_TYPE_ULONG_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::ULongLong:
|
|
|
|
ID = PREDEF_TYPE_ULONGLONG_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::UInt128:
|
|
|
|
ID = PREDEF_TYPE_UINT128_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
ID = PREDEF_TYPE_CHAR_S_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
ID = PREDEF_TYPE_SCHAR_ID;
|
|
|
|
break;
|
2012-12-18 22:30:41 +08:00
|
|
|
case BuiltinType::WChar_S:
|
2015-09-15 20:18:29 +08:00
|
|
|
case BuiltinType::WChar_U:
|
|
|
|
ID = PREDEF_TYPE_WCHAR_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Short:
|
|
|
|
ID = PREDEF_TYPE_SHORT_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Int:
|
|
|
|
ID = PREDEF_TYPE_INT_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Long:
|
|
|
|
ID = PREDEF_TYPE_LONG_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::LongLong:
|
|
|
|
ID = PREDEF_TYPE_LONGLONG_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Int128:
|
|
|
|
ID = PREDEF_TYPE_INT128_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Half:
|
|
|
|
ID = PREDEF_TYPE_HALF_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Float:
|
|
|
|
ID = PREDEF_TYPE_FLOAT_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Double:
|
|
|
|
ID = PREDEF_TYPE_DOUBLE_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::LongDouble:
|
|
|
|
ID = PREDEF_TYPE_LONGDOUBLE_ID;
|
|
|
|
break;
|
2016-05-09 16:52:33 +08:00
|
|
|
case BuiltinType::Float128:
|
|
|
|
ID = PREDEF_TYPE_FLOAT128_ID;
|
|
|
|
break;
|
2015-09-15 20:18:29 +08:00
|
|
|
case BuiltinType::NullPtr:
|
|
|
|
ID = PREDEF_TYPE_NULLPTR_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Char16:
|
|
|
|
ID = PREDEF_TYPE_CHAR16_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Char32:
|
|
|
|
ID = PREDEF_TYPE_CHAR32_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Overload:
|
|
|
|
ID = PREDEF_TYPE_OVERLOAD_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::BoundMember:
|
|
|
|
ID = PREDEF_TYPE_BOUND_MEMBER;
|
|
|
|
break;
|
|
|
|
case BuiltinType::PseudoObject:
|
|
|
|
ID = PREDEF_TYPE_PSEUDO_OBJECT;
|
|
|
|
break;
|
|
|
|
case BuiltinType::Dependent:
|
|
|
|
ID = PREDEF_TYPE_DEPENDENT_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::UnknownAny:
|
|
|
|
ID = PREDEF_TYPE_UNKNOWN_ANY;
|
|
|
|
break;
|
2012-12-18 22:30:41 +08:00
|
|
|
case BuiltinType::ARCUnbridgedCast:
|
2015-09-15 20:18:29 +08:00
|
|
|
ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;
|
|
|
|
break;
|
|
|
|
case BuiltinType::ObjCId:
|
|
|
|
ID = PREDEF_TYPE_OBJC_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::ObjCClass:
|
|
|
|
ID = PREDEF_TYPE_OBJC_CLASS;
|
|
|
|
break;
|
|
|
|
case BuiltinType::ObjCSel:
|
|
|
|
ID = PREDEF_TYPE_OBJC_SEL;
|
|
|
|
break;
|
2016-04-08 21:40:33 +08:00
|
|
|
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
|
|
|
|
case BuiltinType::Id: \
|
|
|
|
ID = PREDEF_TYPE_##Id##_ID; \
|
2015-09-15 20:18:29 +08:00
|
|
|
break;
|
2016-04-13 16:33:41 +08:00
|
|
|
#include "clang/Basic/OpenCLImageTypes.def"
|
2015-09-15 20:18:29 +08:00
|
|
|
case BuiltinType::OCLSampler:
|
|
|
|
ID = PREDEF_TYPE_SAMPLER_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::OCLEvent:
|
|
|
|
ID = PREDEF_TYPE_EVENT_ID;
|
|
|
|
break;
|
2015-09-15 19:18:52 +08:00
|
|
|
case BuiltinType::OCLClkEvent:
|
|
|
|
ID = PREDEF_TYPE_CLK_EVENT_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::OCLQueue:
|
|
|
|
ID = PREDEF_TYPE_QUEUE_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::OCLNDRange:
|
|
|
|
ID = PREDEF_TYPE_NDRANGE_ID;
|
|
|
|
break;
|
|
|
|
case BuiltinType::OCLReserveID:
|
|
|
|
ID = PREDEF_TYPE_RESERVE_ID_ID;
|
|
|
|
break;
|
2012-12-18 22:30:41 +08:00
|
|
|
case BuiltinType::BuiltinFn:
|
2015-09-15 20:18:29 +08:00
|
|
|
ID = PREDEF_TYPE_BUILTIN_FN;
|
|
|
|
break;
|
|
|
|
case BuiltinType::OMPArraySection:
|
|
|
|
ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
|
|
|
|
break;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TypeIdx(ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned serialization::ComputeHash(Selector Sel) {
|
|
|
|
unsigned N = Sel.getNumArgs();
|
|
|
|
if (N == 0)
|
|
|
|
++N;
|
|
|
|
unsigned R = 5381;
|
|
|
|
for (unsigned I = 0; I != N; ++I)
|
|
|
|
if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
|
|
|
|
R = llvm::HashString(II->getName(), R);
|
|
|
|
return R;
|
|
|
|
}
|
2013-01-21 23:25:38 +08:00
|
|
|
|
2013-01-23 01:08:30 +08:00
|
|
|
const DeclContext *
|
|
|
|
serialization::getDefinitiveDeclContext(const DeclContext *DC) {
|
2013-01-21 23:25:38 +08:00
|
|
|
switch (DC->getDeclKind()) {
|
|
|
|
// These entities may have multiple definitions.
|
|
|
|
case Decl::TranslationUnit:
|
2015-03-07 08:04:49 +08:00
|
|
|
case Decl::ExternCContext:
|
2013-01-21 23:25:38 +08:00
|
|
|
case Decl::Namespace:
|
|
|
|
case Decl::LinkageSpec:
|
2016-09-09 07:14:54 +08:00
|
|
|
case Decl::Export:
|
2014-05-22 13:54:18 +08:00
|
|
|
return nullptr;
|
2013-01-21 23:25:38 +08:00
|
|
|
|
|
|
|
// C/C++ tag types can only be defined in one place.
|
|
|
|
case Decl::Enum:
|
|
|
|
case Decl::Record:
|
|
|
|
if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
|
|
|
|
return Def;
|
2014-05-22 13:54:18 +08:00
|
|
|
return nullptr;
|
2013-01-21 23:25:38 +08:00
|
|
|
|
|
|
|
// FIXME: These can be defined in one place... except special member
|
|
|
|
// functions and out-of-line definitions.
|
|
|
|
case Decl::CXXRecord:
|
|
|
|
case Decl::ClassTemplateSpecialization:
|
|
|
|
case Decl::ClassTemplatePartialSpecialization:
|
2014-05-22 13:54:18 +08:00
|
|
|
return nullptr;
|
2013-01-21 23:25:38 +08:00
|
|
|
|
|
|
|
// Each function, method, and block declaration is its own DeclContext.
|
|
|
|
case Decl::Function:
|
|
|
|
case Decl::CXXMethod:
|
|
|
|
case Decl::CXXConstructor:
|
|
|
|
case Decl::CXXDestructor:
|
|
|
|
case Decl::CXXConversion:
|
|
|
|
case Decl::ObjCMethod:
|
|
|
|
case Decl::Block:
|
2013-04-17 03:37:38 +08:00
|
|
|
case Decl::Captured:
|
2013-01-21 23:25:38 +08:00
|
|
|
// Objective C categories, category implementations, and class
|
|
|
|
// implementations can only be defined in one place.
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
case Decl::ObjCImplementation:
|
2013-01-23 01:08:30 +08:00
|
|
|
return DC;
|
2013-01-21 23:25:38 +08:00
|
|
|
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
if (const ObjCProtocolDecl *Def
|
|
|
|
= cast<ObjCProtocolDecl>(DC)->getDefinition())
|
|
|
|
return Def;
|
2014-05-22 13:54:18 +08:00
|
|
|
return nullptr;
|
2013-01-21 23:25:38 +08:00
|
|
|
|
|
|
|
// FIXME: These are defined in one place, but properties in class extensions
|
|
|
|
// end up being back-patched into the main interface. See
|
|
|
|
// Sema::HandlePropertyInClassExtension for the offending code.
|
|
|
|
case Decl::ObjCInterface:
|
2014-05-22 13:54:18 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2013-01-21 23:25:38 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unhandled DeclContext in AST reader");
|
|
|
|
}
|
|
|
|
|
2013-01-23 01:08:30 +08:00
|
|
|
llvm_unreachable("Unhandled decl kind");
|
2013-01-22 00:16:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool serialization::isRedeclarableDeclKind(unsigned Kind) {
|
|
|
|
switch (static_cast<Decl::Kind>(Kind)) {
|
2015-03-07 08:04:49 +08:00
|
|
|
case Decl::TranslationUnit:
|
|
|
|
case Decl::ExternCContext:
|
|
|
|
// Special case of a "merged" declaration.
|
|
|
|
return true;
|
|
|
|
|
2013-01-22 00:16:40 +08:00
|
|
|
case Decl::Namespace:
|
2014-09-04 07:11:22 +08:00
|
|
|
case Decl::NamespaceAlias:
|
2013-01-22 00:16:40 +08:00
|
|
|
case Decl::Typedef:
|
|
|
|
case Decl::TypeAlias:
|
|
|
|
case Decl::Enum:
|
|
|
|
case Decl::Record:
|
|
|
|
case Decl::CXXRecord:
|
|
|
|
case Decl::ClassTemplateSpecialization:
|
|
|
|
case Decl::ClassTemplatePartialSpecialization:
|
2013-08-06 09:03:05 +08:00
|
|
|
case Decl::VarTemplateSpecialization:
|
|
|
|
case Decl::VarTemplatePartialSpecialization:
|
2013-01-22 00:16:40 +08:00
|
|
|
case Decl::Function:
|
|
|
|
case Decl::CXXMethod:
|
|
|
|
case Decl::CXXConstructor:
|
|
|
|
case Decl::CXXDestructor:
|
|
|
|
case Decl::CXXConversion:
|
2013-10-23 10:17:46 +08:00
|
|
|
case Decl::UsingShadow:
|
P0136R1, DR1573, DR1645, DR1715, DR1736, DR1903, DR1941, DR1959, DR1991:
Replace inheriting constructors implementation with new approach, voted into
C++ last year as a DR against C++11.
Instead of synthesizing a set of derived class constructors for each inherited
base class constructor, we make the constructors of the base class visible to
constructor lookup in the derived class, using the normal rules for
using-declarations.
For constructors, UsingShadowDecl now has a ConstructorUsingShadowDecl derived
class that tracks the requisite additional information. We create shadow
constructors (not found by name lookup) in the derived class to model the
actual initialization, and have a new expression node,
CXXInheritedCtorInitExpr, to model the initialization of a base class from such
a constructor. (This initialization is special because it performs real perfect
forwarding of arguments.)
In cases where argument forwarding is not possible (for inalloca calls,
variadic calls, and calls with callee parameter cleanup), the shadow inheriting
constructor is not emitted and instead we directly emit the initialization code
into the caller of the inherited constructor.
Note that this new model is not perfectly compatible with the old model in some
corner cases. In particular:
* if B inherits a private constructor from A, and C uses that constructor to
construct a B, then we previously required that A befriends B and B
befriends C, but the new rules require A to befriend C directly, and
* if a derived class has its own constructors (and so its implicit default
constructor is suppressed), it may still inherit a default constructor from
a base class
llvm-svn: 274049
2016-06-29 03:03:57 +08:00
|
|
|
case Decl::ConstructorUsingShadow:
|
2013-01-22 00:16:40 +08:00
|
|
|
case Decl::Var:
|
|
|
|
case Decl::FunctionTemplate:
|
|
|
|
case Decl::ClassTemplate:
|
2013-08-06 09:03:05 +08:00
|
|
|
case Decl::VarTemplate:
|
2013-01-22 00:16:40 +08:00
|
|
|
case Decl::TypeAliasTemplate:
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
case Decl::ObjCInterface:
|
2013-02-23 01:15:32 +08:00
|
|
|
case Decl::Empty:
|
2013-01-22 00:16:40 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Never redeclarable.
|
|
|
|
case Decl::UsingDirective:
|
|
|
|
case Decl::Label:
|
|
|
|
case Decl::UnresolvedUsingTypename:
|
|
|
|
case Decl::TemplateTypeParm:
|
|
|
|
case Decl::EnumConstant:
|
|
|
|
case Decl::UnresolvedUsingValue:
|
|
|
|
case Decl::IndirectField:
|
|
|
|
case Decl::Field:
|
2013-04-16 15:28:30 +08:00
|
|
|
case Decl::MSProperty:
|
2013-01-22 00:16:40 +08:00
|
|
|
case Decl::ObjCIvar:
|
|
|
|
case Decl::ObjCAtDefsField:
|
|
|
|
case Decl::NonTypeTemplateParm:
|
|
|
|
case Decl::TemplateTemplateParm:
|
|
|
|
case Decl::Using:
|
2016-12-21 05:35:28 +08:00
|
|
|
case Decl::UsingPack:
|
2013-01-22 00:16:40 +08:00
|
|
|
case Decl::ObjCMethod:
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
case Decl::ObjCImplementation:
|
|
|
|
case Decl::ObjCProperty:
|
|
|
|
case Decl::ObjCCompatibleAlias:
|
|
|
|
case Decl::LinkageSpec:
|
2016-09-09 07:14:54 +08:00
|
|
|
case Decl::Export:
|
2013-01-22 00:16:40 +08:00
|
|
|
case Decl::ObjCPropertyImpl:
|
2016-03-03 01:28:48 +08:00
|
|
|
case Decl::PragmaComment:
|
2016-03-03 03:28:54 +08:00
|
|
|
case Decl::PragmaDetectMismatch:
|
2013-01-22 00:16:40 +08:00
|
|
|
case Decl::FileScopeAsm:
|
|
|
|
case Decl::AccessSpec:
|
|
|
|
case Decl::Friend:
|
|
|
|
case Decl::FriendTemplate:
|
|
|
|
case Decl::StaticAssert:
|
|
|
|
case Decl::Block:
|
2013-04-17 03:37:38 +08:00
|
|
|
case Decl::Captured:
|
2013-01-22 00:16:40 +08:00
|
|
|
case Decl::ClassScopeFunctionSpecialization:
|
|
|
|
case Decl::Import:
|
2013-03-22 14:34:35 +08:00
|
|
|
case Decl::OMPThreadPrivate:
|
2016-02-11 13:35:55 +08:00
|
|
|
case Decl::OMPCapturedExpr:
|
2016-03-03 13:21:39 +08:00
|
|
|
case Decl::OMPDeclareReduction:
|
2015-11-04 11:40:30 +08:00
|
|
|
case Decl::BuiltinTemplate:
|
2016-07-23 07:36:59 +08:00
|
|
|
case Decl::Decomposition:
|
|
|
|
case Decl::Binding:
|
2013-01-22 00:16:40 +08:00
|
|
|
return false;
|
2014-09-04 07:11:22 +08:00
|
|
|
|
|
|
|
// These indirectly derive from Redeclarable<T> but are not actually
|
|
|
|
// redeclarable.
|
|
|
|
case Decl::ImplicitParam:
|
|
|
|
case Decl::ParmVar:
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
case Decl::ObjCTypeParam:
|
2014-09-04 07:11:22 +08:00
|
|
|
return false;
|
2013-01-22 00:16:40 +08:00
|
|
|
}
|
2013-01-21 23:25:38 +08:00
|
|
|
|
2013-01-22 00:16:40 +08:00
|
|
|
llvm_unreachable("Unhandled declaration kind");
|
2013-01-21 23:25:38 +08:00
|
|
|
}
|
2014-08-28 09:33:39 +08:00
|
|
|
|
|
|
|
bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
|
2015-02-07 11:11:11 +08:00
|
|
|
// Friend declarations in dependent contexts aren't anonymous in the usual
|
|
|
|
// sense, but they cannot be found by name lookup in their semantic context
|
|
|
|
// (or indeed in any context), so we treat them as anonymous.
|
|
|
|
//
|
|
|
|
// This doesn't apply to friend tag decls; Sema makes those available to name
|
|
|
|
// lookup in the surrounding context.
|
|
|
|
if (D->getFriendObjectKind() &&
|
|
|
|
D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
|
|
|
|
// For function templates and class templates, the template is numbered and
|
|
|
|
// not its pattern.
|
|
|
|
if (auto *FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
return !FD->getDescribedFunctionTemplate();
|
|
|
|
if (auto *RD = dyn_cast<CXXRecordDecl>(D))
|
|
|
|
return !RD->getDescribedClassTemplate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we only care about anonymous class members.
|
2014-08-28 09:33:39 +08:00
|
|
|
if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
|
|
|
|
return false;
|
|
|
|
return isa<TagDecl>(D) || isa<FieldDecl>(D);
|
|
|
|
}
|
|
|
|
|