summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/AST/Decl.cpp56
-rw-r--r--clang/lib/AST/DeclTemplate.cpp64
-rw-r--r--clang/lib/Sema/SemaTemplateDeduction.cpp19
3 files changed, 71 insertions, 68 deletions
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index ab96094ba82..c3c2ac49f0b 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -3108,33 +3108,35 @@ FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
const UnresolvedSetImpl &Templates,
const TemplateArgumentListInfo &TemplateArgs) {
assert(TemplateOrSpecialization.isNull());
- size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
- Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
- Size += Templates.size() * sizeof(FunctionTemplateDecl *);
- void *Buffer = Context.Allocate(Size);
DependentFunctionTemplateSpecializationInfo *Info =
- new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
- TemplateArgs);
+ DependentFunctionTemplateSpecializationInfo::Create(Context, Templates,
+ TemplateArgs);
TemplateOrSpecialization = Info;
}
+DependentFunctionTemplateSpecializationInfo *
+DependentFunctionTemplateSpecializationInfo::Create(
+ ASTContext &Context, const UnresolvedSetImpl &Ts,
+ const TemplateArgumentListInfo &TArgs) {
+ void *Buffer = Context.Allocate(
+ totalSizeToAlloc<TemplateArgumentLoc, FunctionTemplateDecl *>(
+ TArgs.size(), Ts.size()));
+ return new (Buffer) DependentFunctionTemplateSpecializationInfo(Ts, TArgs);
+}
+
DependentFunctionTemplateSpecializationInfo::
DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
const TemplateArgumentListInfo &TArgs)
: AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
- static_assert(sizeof(*this) % llvm::AlignOf<void *>::Alignment == 0,
- "Trailing data is unaligned!");
NumTemplates = Ts.size();
NumArgs = TArgs.size();
- FunctionTemplateDecl **TsArray =
- const_cast<FunctionTemplateDecl**>(getTemplates());
+ FunctionTemplateDecl **TsArray = getTrailingObjects<FunctionTemplateDecl *>();
for (unsigned I = 0, E = Ts.size(); I != E; ++I)
TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
- TemplateArgumentLoc *ArgsArray =
- const_cast<TemplateArgumentLoc*>(getTemplateArgs());
+ TemplateArgumentLoc *ArgsArray = getTrailingObjects<TemplateArgumentLoc>();
for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
}
@@ -3864,13 +3866,13 @@ BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
unsigned NumParams) {
- return new (C, DC, NumParams * sizeof(ImplicitParamDecl *))
+ return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams))
CapturedDecl(DC, NumParams);
}
CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID,
unsigned NumParams) {
- return new (C, ID, NumParams * sizeof(ImplicitParamDecl *))
+ return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams))
CapturedDecl(nullptr, NumParams);
}
@@ -4015,9 +4017,9 @@ ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
NextLocalImport()
{
assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
- SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
- memcpy(StoredLocs, IdentifierLocs.data(),
- IdentifierLocs.size() * sizeof(SourceLocation));
+ SourceLocation *StoredLocs = getTrailingObjects<SourceLocation>();
+ std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(),
+ StoredLocs);
}
ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
@@ -4025,13 +4027,14 @@ ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
: Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
NextLocalImport()
{
- *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
+ *getTrailingObjects<SourceLocation>() = EndLoc;
}
ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, Module *Imported,
ArrayRef<SourceLocation> IdentifierLocs) {
- return new (C, DC, IdentifierLocs.size() * sizeof(SourceLocation))
+ return new (C, DC,
+ additionalSizeToAlloc<SourceLocation>(IdentifierLocs.size()))
ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
}
@@ -4039,16 +4042,15 @@ ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
SourceLocation StartLoc,
Module *Imported,
SourceLocation EndLoc) {
- ImportDecl *Import =
- new (C, DC, sizeof(SourceLocation)) ImportDecl(DC, StartLoc,
- Imported, EndLoc);
+ ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(1))
+ ImportDecl(DC, StartLoc, Imported, EndLoc);
Import->setImplicit();
return Import;
}
ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
unsigned NumLocations) {
- return new (C, ID, NumLocations * sizeof(SourceLocation))
+ return new (C, ID, additionalSizeToAlloc<SourceLocation>(NumLocations))
ImportDecl(EmptyShell());
}
@@ -4056,16 +4058,14 @@ ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
if (!ImportedAndComplete.getInt())
return None;
- const SourceLocation *StoredLocs
- = reinterpret_cast<const SourceLocation *>(this + 1);
+ const SourceLocation *StoredLocs = getTrailingObjects<SourceLocation>();
return llvm::makeArrayRef(StoredLocs,
getNumModuleIdentifiers(getImportedModule()));
}
SourceRange ImportDecl::getSourceRange() const {
if (!ImportedAndComplete.getInt())
- return SourceRange(getLocation(),
- *reinterpret_cast<const SourceLocation *>(this + 1));
-
+ return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>());
+
return SourceRange(getLocation(), getIdentifierLocs().back());
}
diff --git a/clang/lib/AST/DeclTemplate.cpp b/clang/lib/AST/DeclTemplate.cpp
index c1379c0d4d3..dd3baef893e 100644
--- a/clang/lib/AST/DeclTemplate.cpp
+++ b/clang/lib/AST/DeclTemplate.cpp
@@ -57,11 +57,8 @@ TemplateParameterList *
TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
SourceLocation LAngleLoc, NamedDecl **Params,
unsigned NumParams, SourceLocation RAngleLoc) {
- unsigned Size = sizeof(TemplateParameterList)
- + sizeof(NamedDecl *) * NumParams;
- unsigned Align = std::max(llvm::alignOf<TemplateParameterList>(),
- llvm::alignOf<NamedDecl*>());
- void *Mem = C.Allocate(Size, Align);
+ void *Mem = C.Allocate(totalSizeToAlloc<NamedDecl *>(NumParams),
+ llvm::alignOf<TemplateParameterList>());
return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
NumParams, RAngleLoc);
}
@@ -552,10 +549,11 @@ NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(DeclContext *DC,
TemplateParmPosition(D, P), ParameterPack(true),
ExpandedParameterPack(true), NumExpandedTypes(NumExpandedTypes) {
if (ExpandedTypes && ExpandedTInfos) {
- void **TypesAndInfos = reinterpret_cast<void **>(this + 1);
+ auto TypesAndInfos =
+ getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
for (unsigned I = 0; I != NumExpandedTypes; ++I) {
- TypesAndInfos[2*I] = ExpandedTypes[I].getAsOpaquePtr();
- TypesAndInfos[2*I + 1] = ExpandedTInfos[I];
+ new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]);
+ TypesAndInfos[I].second = ExpandedTInfos[I];
}
}
}
@@ -579,10 +577,11 @@ NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
const QualType *ExpandedTypes,
unsigned NumExpandedTypes,
TypeSourceInfo **ExpandedTInfos) {
- unsigned Extra = NumExpandedTypes * 2 * sizeof(void*);
- return new (C, DC, Extra) NonTypeTemplateParmDecl(
- DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
- ExpandedTypes, NumExpandedTypes, ExpandedTInfos);
+ return new (C, DC,
+ additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>(
+ NumExpandedTypes))
+ NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
+ ExpandedTypes, NumExpandedTypes, ExpandedTInfos);
}
NonTypeTemplateParmDecl *
@@ -595,10 +594,12 @@ NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
NonTypeTemplateParmDecl *
NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
unsigned NumExpandedTypes) {
- unsigned Extra = NumExpandedTypes * 2 * sizeof(void*);
- return new (C, ID, Extra) NonTypeTemplateParmDecl(
- nullptr, SourceLocation(), SourceLocation(), 0, 0, nullptr, QualType(),
- nullptr, nullptr, NumExpandedTypes, nullptr);
+ return new (C, ID,
+ additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>(
+ NumExpandedTypes))
+ NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(), 0, 0,
+ nullptr, QualType(), nullptr, nullptr,
+ NumExpandedTypes, nullptr);
}
SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
@@ -628,8 +629,8 @@ TemplateTemplateParmDecl::TemplateTemplateParmDecl(
TemplateParmPosition(D, P), ParameterPack(true),
ExpandedParameterPack(true), NumExpandedParams(NumExpansions) {
if (Expansions)
- std::memcpy(reinterpret_cast<void*>(this + 1), Expansions,
- sizeof(TemplateParameterList*) * NumExpandedParams);
+ std::uninitialized_copy(Expansions, Expansions + NumExpandedParams,
+ getTrailingObjects<TemplateParameterList *>());
}
TemplateTemplateParmDecl *
@@ -647,9 +648,10 @@ TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
IdentifierInfo *Id,
TemplateParameterList *Params,
ArrayRef<TemplateParameterList *> Expansions) {
- return new (C, DC, sizeof(TemplateParameterList*) * Expansions.size())
- TemplateTemplateParmDecl(DC, L, D, P, Id, Params,
- Expansions.size(), Expansions.data());
+ return new (C, DC,
+ additionalSizeToAlloc<TemplateParameterList *>(Expansions.size()))
+ TemplateTemplateParmDecl(DC, L, D, P, Id, Params, Expansions.size(),
+ Expansions.data());
}
TemplateTemplateParmDecl *
@@ -661,7 +663,8 @@ TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
TemplateTemplateParmDecl *
TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
unsigned NumExpansions) {
- return new (C, ID, sizeof(TemplateParameterList*) * NumExpansions)
+ return new (C, ID,
+ additionalSizeToAlloc<TemplateParameterList *>(NumExpansions))
TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr,
nullptr, NumExpansions, nullptr);
}
@@ -682,18 +685,19 @@ void TemplateTemplateParmDecl::setDefaultArgument(
//===----------------------------------------------------------------------===//
// TemplateArgumentList Implementation
//===----------------------------------------------------------------------===//
+TemplateArgumentList::TemplateArgumentList(const TemplateArgument *Args,
+ unsigned NumArgs)
+ : Arguments(getTrailingObjects<TemplateArgument>()), NumArguments(NumArgs) {
+ std::uninitialized_copy(Args, Args + NumArgs,
+ getTrailingObjects<TemplateArgument>());
+}
+
TemplateArgumentList *
TemplateArgumentList::CreateCopy(ASTContext &Context,
const TemplateArgument *Args,
unsigned NumArgs) {
- std::size_t Size = sizeof(TemplateArgumentList)
- + NumArgs * sizeof(TemplateArgument);
- void *Mem = Context.Allocate(Size);
- TemplateArgument *StoredArgs
- = reinterpret_cast<TemplateArgument *>(
- static_cast<TemplateArgumentList *>(Mem) + 1);
- std::uninitialized_copy(Args, Args + NumArgs, StoredArgs);
- return new (Mem) TemplateArgumentList(StoredArgs, NumArgs);
+ void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumArgs));
+ return new (Mem) TemplateArgumentList(Args, NumArgs);
}
FunctionTemplateSpecializationInfo *
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index cc17ab84201..8b2e7db8a24 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -3989,8 +3989,8 @@ Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result) {
nullptr, false, false);
QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0);
NamedDecl *TemplParamPtr = TemplParam;
- FixedSizeTemplateParameterList<1> TemplateParams(Loc, Loc, &TemplParamPtr,
- Loc);
+ FixedSizeTemplateParameterListStorage<1> TemplateParamsSt(
+ Loc, Loc, &TemplParamPtr, Loc);
QualType FuncParam = SubstituteAutoTransform(*this, TemplArg).Apply(Type);
assert(!FuncParam.isNull() &&
@@ -4007,20 +4007,19 @@ Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result) {
InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
if (InitList) {
for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) {
- if (DeduceTemplateArgumentByListElement(*this, &TemplateParams,
- TemplArg,
- InitList->getInit(i),
+ if (DeduceTemplateArgumentByListElement(*this, TemplateParamsSt.get(),
+ TemplArg, InitList->getInit(i),
Info, Deduced, TDF))
return DAR_Failed;
}
} else {
- if (AdjustFunctionParmAndArgTypesForDeduction(*this, &TemplateParams,
- FuncParam, InitType, Init,
- TDF))
+ if (AdjustFunctionParmAndArgTypesForDeduction(
+ *this, TemplateParamsSt.get(), FuncParam, InitType, Init, TDF))
return DAR_Failed;
- if (DeduceTemplateArgumentsByTypeMatch(*this, &TemplateParams, FuncParam,
- InitType, Info, Deduced, TDF))
+ if (DeduceTemplateArgumentsByTypeMatch(*this, TemplateParamsSt.get(),
+ FuncParam, InitType, Info, Deduced,
+ TDF))
return DAR_Failed;
}
OpenPOWER on IntegriCloud