Skip to content

Commit 4d29c58

Browse files
committed
ocaml raw type formatted as a sequence of bytes
1 parent 788ad7b commit 4d29c58

File tree

21 files changed

+153
-0
lines changed

21 files changed

+153
-0
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
239239
mutable llvm::FoldingSet<PipeType> PipeTypes;
240240
mutable llvm::FoldingSet<BitIntType> BitIntTypes;
241241
mutable llvm::FoldingSet<DependentBitIntType> DependentBitIntTypes;
242+
mutable llvm::FoldingSet<OCamlRawType> OCamlRawTypes;
242243
llvm::FoldingSet<BTFTagAttributedType> BTFTagAttributedTypes;
243244

244245
mutable llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames;
@@ -1368,6 +1369,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
13681369
/// and bit count.
13691370
QualType getDependentBitIntType(bool Unsigned, Expr *BitsExpr) const;
13701371

1372+
/// Return an OCaml raw data type with the specified bit count.
1373+
QualType getOCamlRawType(unsigned NumBits) const;
1374+
13711375
/// Gets the struct used to keep track of the extended descriptor for
13721376
/// pointer to blocks.
13731377
QualType getBlockDescriptorExtendedType() const;

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,7 @@ DEF_TRAVERSE_TYPE(PipeType, { TRY_TO(TraverseType(T->getElementType())); })
11561156
DEF_TRAVERSE_TYPE(BitIntType, {})
11571157
DEF_TRAVERSE_TYPE(DependentBitIntType,
11581158
{ TRY_TO(TraverseStmt(T->getNumBitsExpr())); })
1159+
DEF_TRAVERSE_TYPE(OCamlRawType, {})
11591160

11601161
#undef DEF_TRAVERSE_TYPE
11611162

@@ -1456,6 +1457,7 @@ DEF_TRAVERSE_TYPELOC(BitIntType, {})
14561457
DEF_TRAVERSE_TYPELOC(DependentBitIntType, {
14571458
TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr()));
14581459
})
1460+
DEF_TRAVERSE_TYPELOC(OCamlRawType, {})
14591461

14601462
#undef DEF_TRAVERSE_TYPELOC
14611463

clang/include/clang/AST/Type.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6541,6 +6541,33 @@ class BitIntType final : public Type, public llvm::FoldingSetNode {
65416541
static bool classof(const Type *T) { return T->getTypeClass() == BitInt; }
65426542
};
65436543

6544+
/// OCaml raw data type with specified bit width for LLDB debugging
6545+
class OCamlRawType final : public Type, public llvm::FoldingSetNode {
6546+
friend class ASTContext;
6547+
unsigned NumBits : 24;
6548+
6549+
protected:
6550+
OCamlRawType(unsigned NumBits);
6551+
6552+
public:
6553+
unsigned getNumBits() const { return NumBits; }
6554+
6555+
bool isSugared() const { return false; }
6556+
QualType desugar() const { return QualType(this, 0); }
6557+
6558+
void Profile(llvm::FoldingSetNodeID &ID) {
6559+
Profile(ID, getNumBits());
6560+
}
6561+
6562+
static void Profile(llvm::FoldingSetNodeID &ID, unsigned NumBits) {
6563+
ID.AddInteger(NumBits);
6564+
}
6565+
6566+
static bool classof(const Type *T) {
6567+
return T->getTypeClass() == OCamlRaw;
6568+
}
6569+
};
6570+
65446571
class DependentBitIntType final : public Type, public llvm::FoldingSetNode {
65456572
friend class ASTContext;
65466573
const ASTContext &Context;

clang/include/clang/AST/TypeLoc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,6 +2655,10 @@ class DependentBitIntTypeLoc final
26552655
: public InheritingConcreteTypeLoc<TypeSpecTypeLoc, DependentBitIntTypeLoc,
26562656
DependentBitIntType> {};
26572657

2658+
class OCamlRawTypeLoc final
2659+
: public InheritingConcreteTypeLoc<TypeSpecTypeLoc, OCamlRawTypeLoc,
2660+
OCamlRawType> {};
2661+
26582662
class ObjCProtocolLoc {
26592663
ObjCProtocolDecl *Protocol = nullptr;
26602664
SourceLocation Loc = SourceLocation();

clang/include/clang/AST/TypeProperties.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,3 +946,13 @@ let Class = DependentBitIntType in {
946946
return ctx.getDependentBitIntType(isUnsigned, numBitsExpr);
947947
}]>;
948948
}
949+
950+
let Class = OCamlRawType in {
951+
def : Property<"numBits", UInt32> {
952+
let Read = [{ node->getNumBits() }];
953+
}
954+
955+
def : Creator<[{
956+
return ctx.getOCamlRawType(numBits);
957+
}]>;
958+
}

clang/include/clang/Basic/TypeNodes.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,4 @@ def PipeType : TypeNode<Type>;
111111
def AtomicType : TypeNode<Type>;
112112
def BitIntType : TypeNode<Type>;
113113
def DependentBitIntType : TypeNode<Type>, AlwaysDependent;
114+
def OCamlRawType : TypeNode<Type>;

clang/lib/AST/ASTContext.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,6 +2326,12 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
23262326
Width = llvm::alignTo(EIT->getNumBits(), Align);
23272327
break;
23282328
}
2329+
case Type::OCamlRaw: {
2330+
const auto *ORT = cast<OCamlRawType>(T);
2331+
Align = std::min(8U, static_cast<unsigned>(llvm::PowerOf2Ceil(ORT->getNumBits())));
2332+
Width = ORT->getNumBits();
2333+
break;
2334+
}
23292335
case Type::Record:
23302336
case Type::Enum: {
23312337
const auto *TT = cast<TagType>(T);
@@ -4587,6 +4593,20 @@ QualType ASTContext::getBitIntType(bool IsUnsigned, unsigned NumBits) const {
45874593
return QualType(New, 0);
45884594
}
45894595

4596+
QualType ASTContext::getOCamlRawType(unsigned NumBits) const {
4597+
llvm::FoldingSetNodeID ID;
4598+
OCamlRawType::Profile(ID, NumBits);
4599+
4600+
void *InsertPos = nullptr;
4601+
if (OCamlRawType *ORT = OCamlRawTypes.FindNodeOrInsertPos(ID, InsertPos))
4602+
return QualType(ORT, 0);
4603+
4604+
auto *New = new (*this, TypeAlignment) OCamlRawType(NumBits);
4605+
OCamlRawTypes.InsertNode(New, InsertPos);
4606+
Types.push_back(New);
4607+
return QualType(New, 0);
4608+
}
4609+
45904610
QualType ASTContext::getDependentBitIntType(bool IsUnsigned,
45914611
Expr *NumBitsExpr) const {
45924612
assert(NumBitsExpr->isInstantiationDependent() && "Only good for dependent");

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4053,6 +4053,11 @@ void CXXNameMangler::mangleType(const DependentBitIntType *T) {
40534053
Out << "_";
40544054
}
40554055

4056+
void CXXNameMangler::mangleType(const OCamlRawType *T) {
4057+
// This should never be called in practice for lldb usage
4058+
llvm_unreachable("OCamlRaw types should not be mangled");
4059+
}
4060+
40564061
void CXXNameMangler::mangleIntegerLiteral(QualType T,
40574062
const llvm::APSInt &Value) {
40584063
// <expr-primary> ::= L <type> <value number> E # integer literal

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,6 +3409,12 @@ void MicrosoftCXXNameMangler::mangleType(const DependentBitIntType *T,
34093409
Diags.Report(Range.getBegin(), DiagID) << Range;
34103410
}
34113411

3412+
void MicrosoftCXXNameMangler::mangleType(const OCamlRawType *T,
3413+
Qualifiers, SourceRange Range) {
3414+
// This should never be called in practice for lldb usage
3415+
llvm_unreachable("OCamlRaw types should not be mangled");
3416+
}
3417+
34123418
// <this-adjustment> ::= <no-adjustment> | <static-adjustment> |
34133419
// <virtual-adjustment>
34143420
// <no-adjustment> ::= A # private near

clang/lib/AST/Type.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ BitIntType::BitIntType(bool IsUnsigned, unsigned NumBits)
342342
: Type(BitInt, QualType{}, TypeDependence::None), IsUnsigned(IsUnsigned),
343343
NumBits(NumBits) {}
344344

345+
OCamlRawType::OCamlRawType(unsigned NumBits)
346+
: Type(OCamlRaw, QualType{}, TypeDependence::None), NumBits(NumBits) {}
347+
345348
DependentBitIntType::DependentBitIntType(const ASTContext &Context,
346349
bool IsUnsigned, Expr *NumBitsExpr)
347350
: Type(DependentBitInt, QualType{},

0 commit comments

Comments
 (0)