Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// and bit count.
QualType getDependentBitIntType(bool Unsigned, Expr *BitsExpr) const;

QualType getPredefinedSugarType(uint32_t KD, QualType UnderlyingType) const;

/// Gets the struct used to keep track of the extended descriptor for
/// pointer to blocks.
QualType getBlockDescriptorExtendedType() const;
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/AST/RecursiveASTVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,8 @@ DEF_TRAVERSE_TYPE(BitIntType, {})
DEF_TRAVERSE_TYPE(DependentBitIntType,
{ TRY_TO(TraverseStmt(T->getNumBitsExpr())); })

DEF_TRAVERSE_TYPE(PredefinedSugarType, {})

#undef DEF_TRAVERSE_TYPE

// ----------------- TypeLoc traversal -----------------
Expand Down Expand Up @@ -1526,6 +1528,8 @@ DEF_TRAVERSE_TYPELOC(DependentBitIntType, {
TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr()));
})

DEF_TRAVERSE_TYPELOC(PredefinedSugarType, {})

#undef DEF_TRAVERSE_TYPELOC

// ----------------- Decl traversal -----------------
Expand Down
35 changes: 35 additions & 0 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -8054,6 +8054,41 @@ class DependentBitIntType final : public Type, public llvm::FoldingSetNode {
}
};

class PredefinedSugarType final : public Type {
public:
enum Kind { SizeT, SignedSizeT, PtrdiffT };
friend class ASTContext;

private:
Kind K;
QualType Underlying;
PredefinedSugarType(Kind KD, QualType UnderlyingType)
: Type(PredefinedSugar, UnderlyingType->getCanonicalTypeInternal(),
TypeDependence::None),
K(KD), Underlying(UnderlyingType) {}

public:
bool isSugared() const { return true; }
QualType desugar() const { return Underlying; }
Kind getKind() const { return K; }

StringRef getName() const {
switch (K) {
case SizeT:
return "__size_t";
case SignedSizeT:
return "__signed_size_t";
case PtrdiffT:
return "__ptrdiff_t";
}
llvm_unreachable("");
}

static bool classof(const Type *T) {
return T->getTypeClass() == PredefinedSugar;
}
};

/// A qualifier set is used to build a set of qualifiers.
class QualifierCollector : public Qualifiers {
public:
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/AST/TypeLoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2767,6 +2767,10 @@ class DependentBitIntTypeLoc final
: public InheritingConcreteTypeLoc<TypeSpecTypeLoc, DependentBitIntTypeLoc,
DependentBitIntType> {};

class PredefinedSugarTypeLoc final
: public InheritingConcreteTypeLoc<TypeSpecTypeLoc, PredefinedSugarTypeLoc,
PredefinedSugarType> {};

class ObjCProtocolLoc {
ObjCProtocolDecl *Protocol = nullptr;
SourceLocation Loc = SourceLocation();
Expand Down
12 changes: 12 additions & 0 deletions clang/include/clang/AST/TypeProperties.td
Original file line number Diff line number Diff line change
Expand Up @@ -1028,3 +1028,15 @@ let Class = DependentBitIntType in {
return ctx.getDependentBitIntType(isUnsigned, numBitsExpr);
}]>;
}

let Class = PredefinedSugarType in {
def : Property<"underlying", QualType> {
let Read = [{ node->desugar() }];
}
def : Property<"k", UInt32> {
let Read = [{ static_cast<uint32_t>(node->getKind()) }];
}
def : Creator<[{
return ctx.getPredefinedSugarType(k, underlying);
}]>;
}
1 change: 1 addition & 0 deletions clang/include/clang/Basic/TypeNodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,4 @@ def PipeType : TypeNode<Type>;
def AtomicType : TypeNode<Type>;
def BitIntType : TypeNode<Type>;
def DependentBitIntType : TypeNode<Type>, AlwaysDependent;
def PredefinedSugarType : TypeNode<Type>, NeverCanonical;
1 change: 1 addition & 0 deletions clang/include/clang/Serialization/TypeBitCodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ TYPE_BIT_CODE(CountAttributed, COUNT_ATTRIBUTED, 57)
TYPE_BIT_CODE(ArrayParameter, ARRAY_PARAMETER, 58)
TYPE_BIT_CODE(HLSLAttributedResource, HLSLRESOURCE_ATTRIBUTED, 59)
TYPE_BIT_CODE(HLSLInlineSpirv, HLSL_INLINE_SPIRV, 60)
TYPE_BIT_CODE(PredefinedSugar, PREDEFINED_SUGAR, 61)

#undef TYPE_BIT_CODE
21 changes: 19 additions & 2 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2528,8 +2528,13 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
Align = static_cast<unsigned>(Width);
}
}

break;

case Type::PredefinedSugar:
return getTypeInfo(cast<PredefinedSugarType>(T)->desugar().getTypePtr());
break;

case Type::Pipe:
Width = Target->getPointerWidth(LangAS::opencl_global);
Align = Target->getPointerAlign(LangAS::opencl_global);
Expand Down Expand Up @@ -5148,6 +5153,14 @@ QualType ASTContext::getDependentBitIntType(bool IsUnsigned,
return QualType(New, 0);
}

QualType ASTContext::getPredefinedSugarType(uint32_t KD,
QualType UnderlyingType) const {
auto *New = new (*this, alignof(PredefinedSugarType)) PredefinedSugarType(
static_cast<PredefinedSugarType::Kind>(KD), UnderlyingType);
Types.push_back(New);
return QualType(New, 0);
}

#ifndef NDEBUG
static bool NeedsInjectedClassNameType(const RecordDecl *D) {
if (!isa<CXXRecordDecl>(D)) return false;
Expand Down Expand Up @@ -6743,8 +6756,9 @@ QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
QualType ASTContext::getSizeType() const {
if (SizeType.isNull()) {
if (!getLangOpts().HLSL)
SizeType = getTypedefType(buildImplicitTypedef(
getFromTargetType(Target->getSizeType()), "__size_t"));
SizeType =
getPredefinedSugarType(PredefinedSugarType::Kind::SizeT,
getFromTargetType(Target->getSizeType()));
else
SizeType = getFromTargetType(Target->getSizeType());
}
Expand Down Expand Up @@ -14573,6 +14587,9 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X,
DX->isCountInBytes(), DX->isOrNull(),
CDX);
}
case Type::PredefinedSugar: {
return QualType();
}
}
llvm_unreachable("Unhandled Type Class");
}
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,12 @@ ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType(
*ToNumBitsExprOrErr);
}

ExpectedType clang::ASTNodeImporter::VisitPredefinedSugarType(
const clang::PredefinedSugarType *T) {
return Importer.getToContext().getPredefinedSugarType(T->getKind(),
T->desugar());
}

ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType(
const clang::DependentSizedMatrixType *T) {
Error Err = Error::success();
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/AST/ASTStructuralEquivalence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,14 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
return false;
break;
}
case Type::PredefinedSugar: {
const auto *TP1 = cast<PredefinedSugarType>(T1);
const auto *TP2 = cast<PredefinedSugarType>(T2);

if (TP1->getKind() != TP2->getKind())
return false;
break;
}
} // end switch

return true;
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/ItaniumMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2523,6 +2523,10 @@ bool CXXNameMangler::mangleUnresolvedTypeOrSimpleId(QualType Ty,
mangleSourceNameWithAbiTags(cast<TypedefType>(Ty)->getDecl());
break;

case Type::PredefinedSugar:
mangleType(cast<PredefinedSugarType>(Ty)->desugar());
break;

case Type::UnresolvedUsing:
mangleSourceNameWithAbiTags(
cast<UnresolvedUsingType>(Ty)->getDecl());
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/AST/TypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ bool TypePrinter::canPrefixQualifiers(const Type *T,
case Type::BTFTagAttributed:
case Type::HLSLAttributedResource:
case Type::HLSLInlineSpirv:
case Type::PredefinedSugar:
CanPrefixQualifiers = true;
break;

Expand Down Expand Up @@ -1417,6 +1418,15 @@ void TypePrinter::printDependentBitIntBefore(const DependentBitIntType *T,
void TypePrinter::printDependentBitIntAfter(const DependentBitIntType *T,
raw_ostream &OS) {}

void TypePrinter::printPredefinedSugarBefore(const PredefinedSugarType *T,
raw_ostream &OS) {
OS << T->getName();
spaceBeforePlaceHolder(OS);
}

void TypePrinter::printPredefinedSugarAfter(const PredefinedSugarType *T,
raw_ostream &OS) {}

/// Appends the given scope to the end of a string.
void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS,
DeclarationName NameInScope) {
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/CodeGen/CGDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4047,7 +4047,8 @@ llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
return CreateType(cast<HLSLAttributedResourceType>(Ty), Unit);
case Type::HLSLInlineSpirv:
return CreateType(cast<HLSLInlineSpirvType>(Ty), Unit);

case Type::PredefinedSugar:
return getOrCreateType(cast<PredefinedSugarType>(Ty)->desugar(), Unit);
case Type::CountAttributed:
case Type::Auto:
case Type::Attributed:
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ static bool matchesStlAllocatorFn(const Decl *D, const ASTContext &Ctx) {
(MD->getNumParams() != 1 && MD->getNumParams() != 2))
return false;

if (MD->parameters()[0]->getType().getCanonicalType() != Ctx.getSizeType())
if (MD->parameters()[0]->getType().getCanonicalType() != Ctx.getCanonicalSizeType())
return false;

if (MD->getNumParams() == 2) {
Expand Down Expand Up @@ -2485,6 +2485,7 @@ void CodeGenFunction::EmitVariablyModifiedType(QualType type) {
case Type::ObjCObjectPointer:
case Type::BitInt:
case Type::HLSLInlineSpirv:
case Type::PredefinedSugar:
llvm_unreachable("type class is never variably-modified!");

case Type::Elaborated:
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4646,6 +4646,9 @@ static void captureVariablyModifiedType(ASTContext &Context, QualType T,
case Type::Atomic:
T = cast<AtomicType>(Ty)->getValueType();
break;
case Type::PredefinedSugar:
T = cast<PredefinedSugarType>(Ty)->desugar();
break;
}
} while (!T.isNull() && T->isVariablyModifiedType());
}
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3404,7 +3404,8 @@ void Sema::DeclareGlobalNewDelete() {
GlobalNewDeleteDeclared = true;

QualType VoidPtr = Context.getPointerType(Context.VoidTy);
QualType SizeT = Context.getSizeType();
// FIXME: Why is 'Canonical'SizeType needed here?
QualType SizeT = Context.getCanonicalSizeType();

auto DeclareGlobalAllocationFunctions = [&](OverloadedOperatorKind Kind,
QualType Return, QualType Param) {
Expand Down
28 changes: 28 additions & 0 deletions clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "clang/AST/StmtOpenACC.h"
#include "clang/AST/StmtOpenMP.h"
#include "clang/AST/StmtSYCL.h"
#include "clang/AST/TypeLoc.h"
#include "clang/Basic/DiagnosticParse.h"
#include "clang/Basic/OpenMPKinds.h"
#include "clang/Sema/Designator.h"
Expand Down Expand Up @@ -1278,6 +1279,9 @@ class TreeTransform {
QualType RebuildDependentBitIntType(bool IsUnsigned, Expr *NumBitsExpr,
SourceLocation Loc);

QualType RebuildPredefinedSugarType(uint32_t K, QualType Underlying,
SourceLocation Loc);

/// Build a new template name given a nested name specifier, a flag
/// indicating whether the "template" keyword was provided, and the template
/// that the template name refers to.
Expand Down Expand Up @@ -7246,6 +7250,24 @@ QualType TreeTransform<Derived>::TransformDependentBitIntType(
return Result;
}

template <typename Derived>
QualType TreeTransform<Derived>::TransformPredefinedSugarType(
TypeLocBuilder &TLB, PredefinedSugarTypeLoc TL) {
const PredefinedSugarType *EIT = TL.getTypePtr();
QualType Result = TL.getType();

if (getDerived().AlwaysRebuild()) {
Result = getDerived().RebuildPredefinedSugarType(
EIT->getKind(), EIT->desugar(), TL.getNameLoc());
if (Result.isNull())
return QualType();
}

PredefinedSugarTypeLoc NewTL = TLB.push<PredefinedSugarTypeLoc>(Result);
NewTL.setNameLoc(TL.getNameLoc());
return Result;
}

/// Simple iterator that traverses the template arguments in a
/// container that provides a \c getArgLoc() member function.
///
Expand Down Expand Up @@ -17432,6 +17454,12 @@ QualType TreeTransform<Derived>::RebuildDependentBitIntType(
return SemaRef.BuildBitIntType(IsUnsigned, NumBitsExpr, Loc);
}

template <typename Derived>
QualType TreeTransform<Derived>::RebuildPredefinedSugarType(
uint32_t K, QualType Underlying, SourceLocation Loc) {
return SemaRef.Context.getPredefinedSugarType(K, Underlying);
}

template<typename Derived>
TemplateName
TreeTransform<Derived>::RebuildTemplateName(CXXScopeSpec &SS,
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7453,11 +7453,16 @@ void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
TL.setNameLoc(readSourceLocation());
}

void TypeLocReader::VisitDependentBitIntTypeLoc(
clang::DependentBitIntTypeLoc TL) {
TL.setNameLoc(readSourceLocation());
}

void TypeLocReader::VisitPredefinedSugarTypeLoc(PredefinedSugarTypeLoc TL) {
TL.setNameLoc(readSourceLocation());
}

void ASTRecordReader::readTypeLoc(TypeLoc TL, LocSeq *ParentSeq) {
LocSeq::State Seq(ParentSeq);
TypeLocReader TLR(*this, Seq);
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,17 @@ void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
void TypeLocWriter::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
addSourceLocation(TL.getNameLoc());
}

void TypeLocWriter::VisitDependentBitIntTypeLoc(
clang::DependentBitIntTypeLoc TL) {
addSourceLocation(TL.getNameLoc());
}

void TypeLocWriter::VisitPredefinedSugarTypeLoc(
clang::PredefinedSugarTypeLoc TL) {
addSourceLocation(TL.getNameLoc());
}

void ASTWriter::WriteTypeAbbrevs() {
using namespace llvm;

Expand Down
9 changes: 3 additions & 6 deletions clang/test/AST/ast-dump-expr-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -3912,8 +3912,7 @@ void PrimaryExpressions(int a) {
// CHECK-NEXT: },
// CHECK-NEXT: "type": {
// CHECK-NEXT: "desugaredQualType": "unsigned long",
// CHECK-NEXT: "qualType": "__size_t",
// CHECK-NEXT: "typeAliasDeclId": "0x{{.*}}"
// CHECK-NEXT: "qualType": "__size_t"
// CHECK-NEXT: },
// CHECK-NEXT: "valueCategory": "prvalue",
// CHECK-NEXT: "name": "sizeof",
Expand Down Expand Up @@ -3967,8 +3966,7 @@ void PrimaryExpressions(int a) {
// CHECK-NEXT: },
// CHECK-NEXT: "type": {
// CHECK-NEXT: "desugaredQualType": "unsigned long",
// CHECK-NEXT: "qualType": "__size_t",
// CHECK-NEXT: "typeAliasDeclId": "0x{{.*}}"
// CHECK-NEXT: "qualType": "__size_t"
// CHECK-NEXT: },
// CHECK-NEXT: "valueCategory": "prvalue",
// CHECK-NEXT: "name": "sizeof",
Expand All @@ -3994,8 +3992,7 @@ void PrimaryExpressions(int a) {
// CHECK-NEXT: },
// CHECK-NEXT: "type": {
// CHECK-NEXT: "desugaredQualType": "unsigned long",
// CHECK-NEXT: "qualType": "__size_t",
// CHECK-NEXT: "typeAliasDeclId": "0x{{.*}}"
// CHECK-NEXT: "qualType": "__size_t"
// CHECK-NEXT: },
// CHECK-NEXT: "valueCategory": "prvalue",
// CHECK-NEXT: "name": "alignof",
Expand Down
Loading