Basic support for representing elaborated type specifiers
directly in the AST. The current thinking is to create these
only in C++ mode for efficiency. But for now, they're not being
created at all; patch to follow.
This will let us do things like verify that tags match during
template instantation, as well as signal that an elaborated type
specifier was used for clients that actually care.
Optimally, the TypeLoc hierarchy should be adjusted to carry tag
location information as well.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@81057 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index 1143b30..780b1fd 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -733,6 +733,10 @@
break;
}
+ case Type::Elaborated: {
+ return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType().getTypePtr());
+ }
+
case Type::Typedef: {
const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
@@ -1901,6 +1905,25 @@
return QualType(T, 0);
}
+QualType
+ASTContext::getElaboratedType(QualType UnderlyingType,
+ ElaboratedType::TagKind Tag) {
+ llvm::FoldingSetNodeID ID;
+ ElaboratedType::Profile(ID, UnderlyingType, Tag);
+
+ void *InsertPos = 0;
+ ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
+ if (T)
+ return QualType(T, 0);
+
+ QualType Canon = getCanonicalType(UnderlyingType);
+
+ T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
+ Types.push_back(T);
+ ElaboratedTypes.InsertNode(T, InsertPos);
+ return QualType(T, 0);
+}
+
/// CmpProtocolNames - Comparison predicate for sorting protocols
/// alphabetically.
static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp
index c6ea357..f4dad13 100644
--- a/lib/AST/Type.cpp
+++ b/lib/AST/Type.cpp
@@ -1674,6 +1674,16 @@
InnerString = ObjCQIString + InnerString;
}
+void ElaboratedType::getAsStringInternal(std::string &InnerString,
+ const PrintingPolicy &Policy) const {
+ std::string TypeStr;
+ PrintingPolicy InnerPolicy(Policy);
+ InnerPolicy.SuppressTagKind = true;
+ UnderlyingType.getAsStringInternal(InnerString, InnerPolicy);
+
+ InnerString = std::string(getNameForTagKind(getTagKind())) + ' ' + InnerString;
+}
+
void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
if (Policy.SuppressTag)
return;
diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp
index c4759b9..e53f1fa 100644
--- a/lib/CodeGen/CGDebugInfo.cpp
+++ b/lib/CodeGen/CGDebugInfo.cpp
@@ -792,6 +792,9 @@
case Type::FunctionProto:
case Type::FunctionNoProto:
return Slot = CreateType(cast<FunctionType>(Ty), Unit);
+ case Type::Elaborated:
+ return Slot = getOrCreateType(cast<ElaboratedType>(Ty)->getUnderlyingType(),
+ Unit);
case Type::ConstantArray:
case Type::ConstantArrayWithExpr:
diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp
index 4fcf026..a7e6c0c 100644
--- a/lib/Frontend/PCHReader.cpp
+++ b/lib/Frontend/PCHReader.cpp
@@ -1941,6 +1941,13 @@
assert(Record.size() == 1 && "incorrect encoding of enum type");
return Context->getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
+ case pch::TYPE_ELABORATED: {
+ assert(Record.size() == 2 && "incorrect encoding of elaborated type");
+ unsigned Tag = Record[1];
+ return Context->getElaboratedType(GetType(Record[0]),
+ (ElaboratedType::TagKind) Tag);
+ }
+
case pch::TYPE_OBJC_INTERFACE: {
unsigned Idx = 0;
ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
diff --git a/lib/Frontend/PCHWriter.cpp b/lib/Frontend/PCHWriter.cpp
index 4b8847c..985d99a 100644
--- a/lib/Frontend/PCHWriter.cpp
+++ b/lib/Frontend/PCHWriter.cpp
@@ -225,6 +225,12 @@
Code = pch::TYPE_ENUM;
}
+void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
+ Writer.AddTypeRef(T->getUnderlyingType(), Record);
+ Record.push_back(T->getTagKind());
+ Code = pch::TYPE_ELABORATED;
+}
+
void
PCHTypeWriter::VisitTemplateSpecializationType(
const TemplateSpecializationType *T) {
diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h
index 22e01ab..b49ed54 100644
--- a/lib/Sema/TreeTransform.h
+++ b/lib/Sema/TreeTransform.h
@@ -441,6 +441,11 @@
QualType RebuildEnumType(EnumDecl *Enum) {
return SemaRef.Context.getTypeDeclType(Enum);
}
+
+ /// \brief Build a new elaborated type.
+ QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag) {
+ return SemaRef.Context.getElaboratedType(T, Tag);
+ }
/// \brief Build a new typeof(expr) type.
///
@@ -2328,6 +2333,21 @@
return getDerived().RebuildEnumType(Enum);
}
+
+template <typename Derived>
+QualType TreeTransform<Derived>::TransformElaboratedType(
+ const ElaboratedType *T) {
+ QualType Underlying = getDerived().TransformType(T->getUnderlyingType());
+ if (Underlying.isNull())
+ return QualType();
+
+ if (!getDerived().AlwaysRebuild() &&
+ Underlying == T->getUnderlyingType())
+ return QualType(T, 0);
+
+ return getDerived().RebuildElaboratedType(Underlying, T->getTagKind());
+}
+
template<typename Derived>
QualType TreeTransform<Derived>::TransformTemplateTypeParmType(