return CVR;
}
- bool isConstant(ASTContext& Ctx) const;
+ bool isConstant(ASTContext& Ctx) const {
+ return QualType::isConstant(*this, Ctx);
+ }
// Don't promise in the API that anything besides 'const' can be
// easily added.
/// concrete.
///
/// Qualifiers are left in place.
- QualType getDesugaredType(bool ForDisplay = false) const;
+ QualType getDesugaredType() const {
+ return QualType::getDesugaredType(*this);
+ }
/// operator==/!= - Indicate whether the specified types and qualifiers are
/// identical.
/// getNoReturnAttr - Returns true if the type has the noreturn attribute,
/// false otherwise.
bool getNoReturnAttr() const;
+
+private:
+ // These methods are implemented in a separate translation unit;
+ // "static"-ize them to avoid creating temporary QualTypes in the
+ // caller.
+ static bool isConstant(QualType T, ASTContext& Ctx);
+ static QualType getDesugaredType(QualType T);
};
} // end clang.
/// pointer, this returns the respective pointee.
QualType getPointeeType() const;
- /// getDesugaredType - Return the specified type with any "sugar" removed from
- /// the type. This takes off typedefs, typeof's etc. If the outer level of
- /// the type is already concrete, it returns it unmodified. This is similar
- /// to getting the canonical type, but it doesn't remove *all* typedefs. For
- /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
- /// concrete.
- QualType getDesugaredType(bool ForDisplay = false) const;
+ /// getUnqualifiedDesugaredType() - Return the specified type with
+ /// any "sugar" removed from the type, removing any typedefs,
+ /// typeofs, etc., as well as any qualifiers.
+ const Type *getUnqualifiedDesugaredType() const;
/// More type predicates useful for type checking/promotion
bool isPromotableIntegerType() const; // C99 6.3.1.1p2
Kind getKind() const { return TypeKind; }
const char *getName(const LangOptions &LO) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
bool isSigned() const { return Signed; }
const char *getName() const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getElementType());
}
QualType getPointeeType() const { return PointeeType; }
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getPointeeType());
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getPointeeType());
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
static bool classof(const Type *T) {
return T->getTypeClass() == LValueReference;
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
static bool classof(const Type *T) {
return T->getTypeClass() == RValueReference;
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getPointeeType(), getClass());
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getElementType(), getSize(),
getSizeModifier(), getIndexTypeCVRQualifiers());
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
static bool classof(const Type *T) {
return T->getTypeClass() == ConstantArrayWithExpr;
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
static bool classof(const Type *T) {
return T->getTypeClass() == ConstantArrayWithoutExpr;
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
static bool classof(const Type *T) {
return T->getTypeClass() == IncompleteArray;
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
static bool classof(const Type *T) {
return T->getTypeClass() == VariableArray;
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
static bool classof(const Type *T) {
return T->getTypeClass() == DependentSizedArray;
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
static bool classof(const Type *T) {
return T->getTypeClass() == DependentSizedExtVector;
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getElementType(), getNumElements(), getTypeClass());
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
static bool classof(const Type *T) {
return T->getTypeClass() == ExtVector;
}
QualType getResultType() const { return ResultType; }
bool getNoReturnAttr() const { return NoReturn; }
-
static bool classof(const Type *T) {
return T->getTypeClass() == FunctionNoProto ||
T->getTypeClass() == FunctionProto;
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getResultType(), getNoReturnAttr());
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
static bool classof(const Type *T) {
return T->getTypeClass() == FunctionProto;
}
/// looking through the typedefs for B will give you "const volatile A".
QualType LookThroughTypedefs() const;
+ bool isSugared() const { return true; }
+ QualType desugar() const;
+
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
public:
Expr *getUnderlyingExpr() const { return TOExpr; }
+ /// \brief Remove a single level of sugar.
+ QualType desugar() const;
+
+ /// \brief Returns whether this type directly provides sugar.
+ bool isSugared() const { return true; }
+
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
DependentTypeOfExprType(ASTContext &Context, Expr *E)
: TypeOfExprType(E), Context(Context) { }
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, Context, getUnderlyingExpr());
}
public:
QualType getUnderlyingType() const { return TOType; }
+ /// \brief Remove a single level of sugar.
+ QualType desugar() const { return getUnderlyingType(); }
+
+ /// \brief Returns whether this type directly provides sugar.
+ bool isSugared() const { return true; }
+
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
Expr *getUnderlyingExpr() const { return E; }
QualType getUnderlyingType() const { return UnderlyingType; }
+ /// \brief Remove a single level of sugar.
+ QualType desugar() const { return getUnderlyingType(); }
+
+ /// \brief Returns whether this type directly provides sugar.
+ bool isSugared() const { return !isDependentType(); }
+
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
public:
DependentDecltypeType(ASTContext &Context, Expr *E);
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, Context, getUnderlyingExpr());
}
// the same address space, and return that.
unsigned getAddressSpace() const { return 0; }
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
static bool classof(const TagType *T);
static bool classof(const Type *T) {
return isa<TagType>(T) && classof(cast<TagType>(T));
return reinterpret_cast<EnumDecl*>(TagType::getDecl());
}
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
static bool classof(const TagType *T);
static bool classof(const Type *T) {
return isa<TagType>(T) && classof(cast<TagType>(T));
TagKind getTagKind() const { return Tag; }
QualType getUnderlyingType() const { return UnderlyingType; }
+ /// \brief Remove a single level of sugar.
+ QualType desugar() const { return getUnderlyingType(); }
+
+ /// \brief Returns whether this type directly provides sugar.
+ bool isSugared() const { return true; }
+
static const char *getNameForTagKind(TagKind Kind) {
switch (Kind) {
default: assert(0 && "Unknown TagKind!");
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, Depth, Index, ParameterPack, Name);
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return !isDependentType(); }
+ QualType desugar() const { return getCanonicalTypeInternal(); }
+
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, Template, getArgs(), NumArgs, Context);
}
/// \brief Retrieve the type named by the qualified-id.
QualType getNamedType() const { return NamedType; }
+ /// \brief Remove a single level of sugar.
+ QualType desugar() const { return getNamedType(); }
+
+ /// \brief Returns whether this type directly provides sugar.
+ bool isSugared() const { return true; }
+
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, NNS, Name);
}
virtual void getAsStringInternal(std::string &InnerString,
const PrintingPolicy &Policy) const;
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID);
static void Profile(llvm::FoldingSetNodeID &ID,
const ObjCInterfaceDecl *Decl,
/// interface type, or 0 if there are none.
unsigned getNumProtocols() const { return Protocols.size(); }
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID);
static void Profile(llvm::FoldingSetNodeID &ID, QualType T,
ObjCProtocolDecl **protocols, unsigned NumProtocols);
/// \brief Return the number of qualifying protocols.
unsigned getNumProtocols() const { return Protocols.size(); }
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
+
void Profile(llvm::FoldingSetNodeID &ID);
static void Profile(llvm::FoldingSetNodeID &ID, QualType T,
ObjCProtocolDecl **protocols, unsigned NumProtocols);
// If this is a typedef for the type, strip the typedef off without
// losing all typedef information.
- return cast<T>(getDesugaredType());
+ return cast<T>(getUnqualifiedDesugaredType());
}
} // end namespace clang
#include "llvm/Support/raw_ostream.h"
using namespace clang;
-bool QualType::isConstant(ASTContext &Ctx) const {
- if (isConstQualified())
+bool QualType::isConstant(QualType T, ASTContext &Ctx) {
+ if (T.isConstQualified())
return true;
- if (getTypePtr()->isArrayType())
- return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
+ if (const ArrayType *AT = Ctx.getAsArrayType(T))
+ return AT->getElementType().isConstant(Ctx);
return false;
}
// If this is a typedef for an array type, strip the typedef off without
// losing all typedef information.
- return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
+ return cast<ArrayType>(getUnqualifiedDesugaredType())
+ ->getElementType().getTypePtr();
}
/// getDesugaredType - Return the specified type with any "sugar" removed from
/// to getting the canonical type, but it doesn't remove *all* typedefs. For
/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
/// concrete.
-///
-/// \param ForDisplay When true, the desugaring is provided for
-/// display purposes only. In this case, we apply more heuristics to
-/// decide whether it is worth providing a desugared form of the type
-/// or not.
-QualType QualType::getDesugaredType(bool ForDisplay) const {
+QualType QualType::getDesugaredType(QualType T) {
QualifierCollector Qs;
- return Qs.apply(Qs.strip(*this)->getDesugaredType(ForDisplay));
-}
-/// getDesugaredType - Return the specified type with any "sugar" removed from
-/// type type. This takes off typedefs, typeof's etc. If the outer level of
-/// the type is already concrete, it returns it unmodified. This is similar
-/// to getting the canonical type, but it doesn't remove *all* typedefs. For
-/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
-/// concrete.
-///
-/// \param ForDisplay When true, the desugaring is provided for
-/// display purposes only. In this case, we apply more heuristics to
-/// decide whether it is worth providing a desugared form of the type
-/// or not.
-QualType Type::getDesugaredType(bool ForDisplay) const {
- if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
- return TDT->LookThroughTypedefs().getDesugaredType();
- if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(this))
- return ET->getUnderlyingType().getDesugaredType();
- if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
- return TOE->getUnderlyingExpr()->getType().getDesugaredType();
- if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
- return TOT->getUnderlyingType().getDesugaredType();
- if (const DecltypeType *DTT = dyn_cast<DecltypeType>(this)) {
- if (!DTT->getUnderlyingType()->isDependentType())
- return DTT->getUnderlyingType().getDesugaredType();
- }
- if (const TemplateSpecializationType *Spec
- = dyn_cast<TemplateSpecializationType>(this)) {
- if (ForDisplay)
- return QualType(this, 0);
-
- QualType Canon = Spec->getCanonicalTypeInternal();
- if (Canon->getAs<TemplateSpecializationType>())
- return QualType(this, 0);
- return Canon->getDesugaredType();
- }
- if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
- if (ForDisplay) {
- // If desugaring the type that the qualified name is referring to
- // produces something interesting, that's our desugared type.
- QualType NamedType = QualName->getNamedType().getDesugaredType();
- if (NamedType != QualName->getNamedType())
- return NamedType;
- } else
- return QualName->getNamedType().getDesugaredType();
+ QualType Cur = T;
+ while (true) {
+ const Type *CurTy = Qs.strip(Cur);
+ switch (CurTy->getTypeClass()) {
+#define ABSTRACT_TYPE(Class, Parent)
+#define TYPE(Class, Parent) \
+ case Type::Class: { \
+ const Class##Type *Ty = cast<Class##Type>(CurTy); \
+ if (!Ty->isSugared()) \
+ return Qs.apply(Cur); \
+ Cur = Ty->desugar(); \
+ break; \
+ }
+#include "clang/AST/TypeNodes.def"
+ }
}
+}
- return QualType(this, 0);
+/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
+/// sugar off the given type. This should produce an object of the
+/// same dynamic type as the canonical type.
+const Type *Type::getUnqualifiedDesugaredType() const {
+ const Type *Cur = this;
+
+ while (true) {
+ switch (Cur->getTypeClass()) {
+#define ABSTRACT_TYPE(Class, Parent)
+#define TYPE(Class, Parent) \
+ case Class: { \
+ const Class##Type *Ty = cast<Class##Type>(Cur); \
+ if (!Ty->isSugared()) return Cur; \
+ Cur = Ty->desugar().getTypePtr(); \
+ break; \
+ }
+#include "clang/AST/TypeNodes.def"
+ }
+ }
}
/// isVoidType - Helper method to determine if this is the 'void' type.
// If this is a typedef for a structure type, strip the typedef off without
// losing all typedef information.
- return cast<RecordType>(getDesugaredType());
+ return cast<RecordType>(getUnqualifiedDesugaredType());
}
return 0;
}
// If this is a typedef for a union type, strip the typedef off without
// losing all typedef information.
- return cast<RecordType>(getDesugaredType());
+ return cast<RecordType>(getUnqualifiedDesugaredType());
}
return 0;
return Qs.apply(CurType);
}
+QualType TypedefType::desugar() const {
+ return getDecl()->getUnderlyingType();
+}
+
TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
: Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
}
+QualType TypeOfExprType::desugar() const {
+ return getUnderlyingExpr()->getType();
+}
+
void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
ASTContext &Context, Expr *E) {
E->Profile(ID, Context, true);