Skip to content

Commit d86cc73

Browse files
committed
[NFC] [Serialization] Avoid using DeclID directly as much as possible
This patch tries to remove all the direct use of DeclID except the real low level reading and writing. All the use of DeclID is converted to the use of LocalDeclID or GlobalDeclID. This is helpful to increase the readability and type safety.
1 parent 72b5814 commit d86cc73

38 files changed

+583
-509
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
455455
/// initialization of another module).
456456
struct PerModuleInitializers {
457457
llvm::SmallVector<Decl*, 4> Initializers;
458-
llvm::SmallVector<DeclID, 4> LazyInitializers;
458+
llvm::SmallVector<GlobalDeclID, 4> LazyInitializers;
459459

460460
void resolve(ASTContext &Ctx);
461461
};
@@ -1059,7 +1059,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
10591059
/// or an ImportDecl nominating another module that has initializers.
10601060
void addModuleInitializer(Module *M, Decl *Init);
10611061

1062-
void addLazyModuleInitializers(Module *M, ArrayRef<DeclID> IDs);
1062+
void addLazyModuleInitializers(Module *M, ArrayRef<GlobalDeclID> IDs);
10631063

10641064
/// Get the initializations to perform when importing a module, if any.
10651065
ArrayRef<Decl*> getModuleInitializers(Module *M);

clang/include/clang/AST/Decl.h

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class PragmaCommentDecl final
157157
SourceLocation CommentLoc,
158158
PragmaMSCommentKind CommentKind,
159159
StringRef Arg);
160-
static PragmaCommentDecl *CreateDeserialized(ASTContext &C, DeclID ID,
160+
static PragmaCommentDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID,
161161
unsigned ArgSize);
162162

163163
PragmaMSCommentKind getCommentKind() const { return CommentKind; }
@@ -192,7 +192,7 @@ class PragmaDetectMismatchDecl final
192192
SourceLocation Loc, StringRef Name,
193193
StringRef Value);
194194
static PragmaDetectMismatchDecl *
195-
CreateDeserialized(ASTContext &C, DeclID ID, unsigned NameValueSize);
195+
CreateDeserialized(ASTContext &C, GlobalDeclID ID, unsigned NameValueSize);
196196

197197
StringRef getName() const { return getTrailingObjects<char>(); }
198198
StringRef getValue() const { return getTrailingObjects<char>() + ValueStart; }
@@ -518,7 +518,7 @@ class LabelDecl : public NamedDecl {
518518
static LabelDecl *Create(ASTContext &C, DeclContext *DC,
519519
SourceLocation IdentL, IdentifierInfo *II,
520520
SourceLocation GnuLabelL);
521-
static LabelDecl *CreateDeserialized(ASTContext &C, DeclID ID);
521+
static LabelDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
522522

523523
LabelStmt *getStmt() const { return TheStmt; }
524524
void setStmt(LabelStmt *T) { TheStmt = T; }
@@ -581,7 +581,7 @@ class NamespaceDecl : public NamedDecl, public DeclContext,
581581
IdentifierInfo *Id, NamespaceDecl *PrevDecl,
582582
bool Nested);
583583

584-
static NamespaceDecl *CreateDeserialized(ASTContext &C, DeclID ID);
584+
static NamespaceDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
585585

586586
using redecl_range = redeclarable_base::redecl_range;
587587
using redecl_iterator = redeclarable_base::redecl_iterator;
@@ -1146,7 +1146,7 @@ class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
11461146
const IdentifierInfo *Id, QualType T,
11471147
TypeSourceInfo *TInfo, StorageClass S);
11481148

1149-
static VarDecl *CreateDeserialized(ASTContext &C, DeclID ID);
1149+
static VarDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
11501150

11511151
SourceRange getSourceRange() const override LLVM_READONLY;
11521152

@@ -1728,7 +1728,7 @@ class ImplicitParamDecl : public VarDecl {
17281728
static ImplicitParamDecl *Create(ASTContext &C, QualType T,
17291729
ImplicitParamKind ParamKind);
17301730

1731-
static ImplicitParamDecl *CreateDeserialized(ASTContext &C, DeclID ID);
1731+
static ImplicitParamDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
17321732

17331733
ImplicitParamDecl(ASTContext &C, DeclContext *DC, SourceLocation IdLoc,
17341734
const IdentifierInfo *Id, QualType Type,
@@ -1782,7 +1782,7 @@ class ParmVarDecl : public VarDecl {
17821782
TypeSourceInfo *TInfo, StorageClass S,
17831783
Expr *DefArg);
17841784

1785-
static ParmVarDecl *CreateDeserialized(ASTContext &C, DeclID ID);
1785+
static ParmVarDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
17861786

17871787
SourceRange getSourceRange() const override LLVM_READONLY;
17881788

@@ -2178,7 +2178,7 @@ class FunctionDecl : public DeclaratorDecl,
21782178
bool hasWrittenPrototype, ConstexprSpecKind ConstexprKind,
21792179
Expr *TrailingRequiresClause);
21802180

2181-
static FunctionDecl *CreateDeserialized(ASTContext &C, DeclID ID);
2181+
static FunctionDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
21822182

21832183
DeclarationNameInfo getNameInfo() const {
21842184
return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
@@ -3136,7 +3136,7 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
31363136
TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
31373137
InClassInitStyle InitStyle);
31383138

3139-
static FieldDecl *CreateDeserialized(ASTContext &C, DeclID ID);
3139+
static FieldDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
31403140

31413141
/// Returns the index of this field within its record,
31423142
/// as appropriate for passing to ASTRecordLayout::getFieldOffset.
@@ -3311,7 +3311,7 @@ class EnumConstantDecl : public ValueDecl,
33113311
SourceLocation L, IdentifierInfo *Id,
33123312
QualType T, Expr *E,
33133313
const llvm::APSInt &V);
3314-
static EnumConstantDecl *CreateDeserialized(ASTContext &C, DeclID ID);
3314+
static EnumConstantDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
33153315

33163316
const Expr *getInitExpr() const { return (const Expr*) Init; }
33173317
Expr *getInitExpr() { return (Expr*) Init; }
@@ -3357,7 +3357,7 @@ class IndirectFieldDecl : public ValueDecl,
33573357
QualType T,
33583358
llvm::MutableArrayRef<NamedDecl *> CH);
33593359

3360-
static IndirectFieldDecl *CreateDeserialized(ASTContext &C, DeclID ID);
3360+
static IndirectFieldDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
33613361

33623362
using chain_iterator = ArrayRef<NamedDecl *>::const_iterator;
33633363

@@ -3542,7 +3542,7 @@ class TypedefDecl : public TypedefNameDecl {
35423542
static TypedefDecl *Create(ASTContext &C, DeclContext *DC,
35433543
SourceLocation StartLoc, SourceLocation IdLoc,
35443544
const IdentifierInfo *Id, TypeSourceInfo *TInfo);
3545-
static TypedefDecl *CreateDeserialized(ASTContext &C, DeclID ID);
3545+
static TypedefDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
35463546

35473547
SourceRange getSourceRange() const override LLVM_READONLY;
35483548

@@ -3567,7 +3567,7 @@ class TypeAliasDecl : public TypedefNameDecl {
35673567
static TypeAliasDecl *Create(ASTContext &C, DeclContext *DC,
35683568
SourceLocation StartLoc, SourceLocation IdLoc,
35693569
const IdentifierInfo *Id, TypeSourceInfo *TInfo);
3570-
static TypeAliasDecl *CreateDeserialized(ASTContext &C, DeclID ID);
3570+
static TypeAliasDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
35713571

35723572
SourceRange getSourceRange() const override LLVM_READONLY;
35733573

@@ -3977,7 +3977,7 @@ class EnumDecl : public TagDecl {
39773977
IdentifierInfo *Id, EnumDecl *PrevDecl,
39783978
bool IsScoped, bool IsScopedUsingClassTag,
39793979
bool IsFixed);
3980-
static EnumDecl *CreateDeserialized(ASTContext &C, DeclID ID);
3980+
static EnumDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
39813981

39823982
/// Overrides to provide correct range when there's an enum-base specifier
39833983
/// with forward declarations.
@@ -4182,7 +4182,7 @@ class RecordDecl : public TagDecl {
41824182
static RecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC,
41834183
SourceLocation StartLoc, SourceLocation IdLoc,
41844184
IdentifierInfo *Id, RecordDecl* PrevDecl = nullptr);
4185-
static RecordDecl *CreateDeserialized(const ASTContext &C, DeclID ID);
4185+
static RecordDecl *CreateDeserialized(const ASTContext &C, GlobalDeclID ID);
41864186

41874187
RecordDecl *getPreviousDecl() {
41884188
return cast_or_null<RecordDecl>(
@@ -4433,7 +4433,7 @@ class FileScopeAsmDecl : public Decl {
44334433
StringLiteral *Str, SourceLocation AsmLoc,
44344434
SourceLocation RParenLoc);
44354435

4436-
static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, DeclID ID);
4436+
static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
44374437

44384438
SourceLocation getAsmLoc() const { return getLocation(); }
44394439
SourceLocation getRParenLoc() const { return RParenLoc; }
@@ -4469,7 +4469,7 @@ class TopLevelStmtDecl : public Decl, public DeclContext {
44694469

44704470
public:
44714471
static TopLevelStmtDecl *Create(ASTContext &C, Stmt *Statement);
4472-
static TopLevelStmtDecl *CreateDeserialized(ASTContext &C, DeclID ID);
4472+
static TopLevelStmtDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
44734473

44744474
SourceRange getSourceRange() const override LLVM_READONLY;
44754475
Stmt *getStmt() { return Statement; }
@@ -4563,7 +4563,7 @@ class BlockDecl : public Decl, public DeclContext {
45634563

45644564
public:
45654565
static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L);
4566-
static BlockDecl *CreateDeserialized(ASTContext &C, DeclID ID);
4566+
static BlockDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
45674567

45684568
SourceLocation getCaretLocation() const { return getLocation(); }
45694569

@@ -4717,7 +4717,7 @@ class CapturedDecl final
47174717

47184718
static CapturedDecl *Create(ASTContext &C, DeclContext *DC,
47194719
unsigned NumParams);
4720-
static CapturedDecl *CreateDeserialized(ASTContext &C, DeclID ID,
4720+
static CapturedDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID,
47214721
unsigned NumParams);
47224722

47234723
Stmt *getBody() const override;
@@ -4851,7 +4851,7 @@ class ImportDecl final : public Decl,
48514851
SourceLocation EndLoc);
48524852

48534853
/// Create a new, deserialized module import declaration.
4854-
static ImportDecl *CreateDeserialized(ASTContext &C, DeclID ID,
4854+
static ImportDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID,
48554855
unsigned NumLocations);
48564856

48574857
/// Retrieve the module that was imported by the import declaration.
@@ -4892,7 +4892,7 @@ class ExportDecl final : public Decl, public DeclContext {
48924892
public:
48934893
static ExportDecl *Create(ASTContext &C, DeclContext *DC,
48944894
SourceLocation ExportLoc);
4895-
static ExportDecl *CreateDeserialized(ASTContext &C, DeclID ID);
4895+
static ExportDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
48964896

48974897
SourceLocation getExportLoc() const { return getLocation(); }
48984898
SourceLocation getRBraceLoc() const { return RBraceLoc; }
@@ -4931,7 +4931,7 @@ class EmptyDecl : public Decl {
49314931
public:
49324932
static EmptyDecl *Create(ASTContext &C, DeclContext *DC,
49334933
SourceLocation L);
4934-
static EmptyDecl *CreateDeserialized(ASTContext &C, DeclID ID);
4934+
static EmptyDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
49354935

49364936
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
49374937
static bool classofKind(Kind K) { return K == Empty; }
@@ -4957,7 +4957,7 @@ class HLSLBufferDecl final : public NamedDecl, public DeclContext {
49574957
bool CBuffer, SourceLocation KwLoc,
49584958
IdentifierInfo *ID, SourceLocation IDLoc,
49594959
SourceLocation LBrace);
4960-
static HLSLBufferDecl *CreateDeserialized(ASTContext &C, DeclID ID);
4960+
static HLSLBufferDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
49614961

49624962
SourceRange getSourceRange() const override LLVM_READONLY {
49634963
return SourceRange(getLocStart(), RBraceLoc);

clang/include/clang/AST/DeclBase.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class alignas(8) Decl {
359359
/// \param Ctx The context in which we will allocate memory.
360360
/// \param ID The global ID of the deserialized declaration.
361361
/// \param Extra The amount of extra space to allocate after the object.
362-
void *operator new(std::size_t Size, const ASTContext &Ctx, DeclID ID,
362+
void *operator new(std::size_t Size, const ASTContext &Ctx, GlobalDeclID ID,
363363
std::size_t Extra = 0);
364364

365365
/// Allocate memory for a non-deserialized declaration.
@@ -777,10 +777,10 @@ class alignas(8) Decl {
777777

778778
/// Retrieve the global declaration ID associated with this
779779
/// declaration, which specifies where this Decl was loaded from.
780-
DeclID getGlobalID() const {
780+
GlobalDeclID getGlobalID() const {
781781
if (isFromASTFile())
782-
return *((const DeclID *)this - 1);
783-
return 0;
782+
return (*((const GlobalDeclID *)this - 1));
783+
return GlobalDeclID();
784784
}
785785

786786
/// Retrieve the global ID of the module that owns this particular

0 commit comments

Comments
 (0)