|
| 1 | +//===--- DeclID.h - ID number for deserialized declarations ----*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file defines DeclID class family to describe the deserialized |
| 10 | +// declarations. The DeclID is widely used in AST via LazyDeclPtr, or calls to |
| 11 | +// `ExternalASTSource::getExternalDecl`. It will be helpful for type safety to |
| 12 | +// require the use of `DeclID` to explicit. |
| 13 | +// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +#ifndef LLVM_CLANG_AST_DECLID_H |
| 17 | +#define LLVM_CLANG_AST_DECLID_H |
| 18 | + |
| 19 | +#include "llvm/ADT/iterator.h" |
| 20 | + |
| 21 | +namespace clang { |
| 22 | + |
| 23 | +/// Predefined declaration IDs. |
| 24 | +/// |
| 25 | +/// These declaration IDs correspond to predefined declarations in the AST |
| 26 | +/// context, such as the NULL declaration ID. Such declarations are never |
| 27 | +/// actually serialized, since they will be built by the AST context when |
| 28 | +/// it is created. |
| 29 | +enum PredefinedDeclIDs { |
| 30 | + /// The NULL declaration. |
| 31 | + PREDEF_DECL_NULL_ID = 0, |
| 32 | + |
| 33 | + /// The translation unit. |
| 34 | + PREDEF_DECL_TRANSLATION_UNIT_ID = 1, |
| 35 | + |
| 36 | + /// The Objective-C 'id' type. |
| 37 | + PREDEF_DECL_OBJC_ID_ID = 2, |
| 38 | + |
| 39 | + /// The Objective-C 'SEL' type. |
| 40 | + PREDEF_DECL_OBJC_SEL_ID = 3, |
| 41 | + |
| 42 | + /// The Objective-C 'Class' type. |
| 43 | + PREDEF_DECL_OBJC_CLASS_ID = 4, |
| 44 | + |
| 45 | + /// The Objective-C 'Protocol' type. |
| 46 | + PREDEF_DECL_OBJC_PROTOCOL_ID = 5, |
| 47 | + |
| 48 | + /// The signed 128-bit integer type. |
| 49 | + PREDEF_DECL_INT_128_ID = 6, |
| 50 | + |
| 51 | + /// The unsigned 128-bit integer type. |
| 52 | + PREDEF_DECL_UNSIGNED_INT_128_ID = 7, |
| 53 | + |
| 54 | + /// The internal 'instancetype' typedef. |
| 55 | + PREDEF_DECL_OBJC_INSTANCETYPE_ID = 8, |
| 56 | + |
| 57 | + /// The internal '__builtin_va_list' typedef. |
| 58 | + PREDEF_DECL_BUILTIN_VA_LIST_ID = 9, |
| 59 | + |
| 60 | + /// The internal '__va_list_tag' struct, if any. |
| 61 | + PREDEF_DECL_VA_LIST_TAG = 10, |
| 62 | + |
| 63 | + /// The internal '__builtin_ms_va_list' typedef. |
| 64 | + PREDEF_DECL_BUILTIN_MS_VA_LIST_ID = 11, |
| 65 | + |
| 66 | + /// The predeclared '_GUID' struct. |
| 67 | + PREDEF_DECL_BUILTIN_MS_GUID_ID = 12, |
| 68 | + |
| 69 | + /// The extern "C" context. |
| 70 | + PREDEF_DECL_EXTERN_C_CONTEXT_ID = 13, |
| 71 | + |
| 72 | + /// The internal '__make_integer_seq' template. |
| 73 | + PREDEF_DECL_MAKE_INTEGER_SEQ_ID = 14, |
| 74 | + |
| 75 | + /// The internal '__NSConstantString' typedef. |
| 76 | + PREDEF_DECL_CF_CONSTANT_STRING_ID = 15, |
| 77 | + |
| 78 | + /// The internal '__NSConstantString' tag type. |
| 79 | + PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID = 16, |
| 80 | + |
| 81 | + /// The internal '__type_pack_element' template. |
| 82 | + PREDEF_DECL_TYPE_PACK_ELEMENT_ID = 17, |
| 83 | +}; |
| 84 | + |
| 85 | +/// The number of declaration IDs that are predefined. |
| 86 | +/// |
| 87 | +/// For more information about predefined declarations, see the |
| 88 | +/// \c PredefinedDeclIDs type and the PREDEF_DECL_*_ID constants. |
| 89 | +const unsigned int NUM_PREDEF_DECL_IDS = 18; |
| 90 | + |
| 91 | +/// An ID number that refers to a declaration in an AST file. |
| 92 | +/// |
| 93 | +/// The ID numbers of declarations are consecutive (in order of |
| 94 | +/// discovery), with values below NUM_PREDEF_DECL_IDS being reserved. |
| 95 | +/// At the start of a chain of precompiled headers, declaration ID 1 is |
| 96 | +/// used for the translation unit declaration. |
| 97 | +using DeclID = uint32_t; |
| 98 | + |
| 99 | +class LocalDeclID { |
| 100 | +public: |
| 101 | + explicit LocalDeclID(DeclID ID) : ID(ID) {} |
| 102 | + |
| 103 | + DeclID get() const { return ID; } |
| 104 | + |
| 105 | +private: |
| 106 | + DeclID ID; |
| 107 | +}; |
| 108 | + |
| 109 | +/// Wrapper class for DeclID. This is helpful to not mix the use of LocalDeclID |
| 110 | +/// and GlobalDeclID to improve the type safety. |
| 111 | +class GlobalDeclID { |
| 112 | +public: |
| 113 | + GlobalDeclID() : ID(PREDEF_DECL_NULL_ID) {} |
| 114 | + explicit GlobalDeclID(DeclID ID) : ID(ID) {} |
| 115 | + |
| 116 | + DeclID get() const { return ID; } |
| 117 | + |
| 118 | + explicit operator DeclID() const { return ID; } |
| 119 | + |
| 120 | + friend bool operator==(const GlobalDeclID &LHS, const GlobalDeclID &RHS) { |
| 121 | + return LHS.ID == RHS.ID; |
| 122 | + } |
| 123 | + friend bool operator!=(const GlobalDeclID &LHS, const GlobalDeclID &RHS) { |
| 124 | + return LHS.ID != RHS.ID; |
| 125 | + } |
| 126 | + // We may sort the global decl ID. |
| 127 | + friend bool operator<(const GlobalDeclID &LHS, const GlobalDeclID &RHS) { |
| 128 | + return LHS.ID < RHS.ID; |
| 129 | + } |
| 130 | + friend bool operator>(const GlobalDeclID &LHS, const GlobalDeclID &RHS) { |
| 131 | + return LHS.ID > RHS.ID; |
| 132 | + } |
| 133 | + friend bool operator<=(const GlobalDeclID &LHS, const GlobalDeclID &RHS) { |
| 134 | + return LHS.ID <= RHS.ID; |
| 135 | + } |
| 136 | + friend bool operator>=(const GlobalDeclID &LHS, const GlobalDeclID &RHS) { |
| 137 | + return LHS.ID >= RHS.ID; |
| 138 | + } |
| 139 | + |
| 140 | +private: |
| 141 | + DeclID ID; |
| 142 | +}; |
| 143 | + |
| 144 | +/// A helper iterator adaptor to convert the iterators to `SmallVector<DeclID>` |
| 145 | +/// to the iterators to `SmallVector<GlobalDeclID>`. |
| 146 | +class GlobalDeclIDIterator |
| 147 | + : public llvm::iterator_adaptor_base<GlobalDeclIDIterator, const DeclID *, |
| 148 | + std::forward_iterator_tag, |
| 149 | + GlobalDeclID> { |
| 150 | +public: |
| 151 | + GlobalDeclIDIterator() : iterator_adaptor_base(nullptr) {} |
| 152 | + |
| 153 | + GlobalDeclIDIterator(const DeclID *ID) : iterator_adaptor_base(ID) {} |
| 154 | + |
| 155 | + value_type operator*() const { return GlobalDeclID(*I); } |
| 156 | + |
| 157 | + bool operator==(const GlobalDeclIDIterator &RHS) const { return I == RHS.I; } |
| 158 | +}; |
| 159 | + |
| 160 | +/// A helper iterator adaptor to convert the iterators to |
| 161 | +/// `SmallVector<GlobalDeclID>` to the iterators to `SmallVector<DeclID>`. |
| 162 | +class DeclIDIterator |
| 163 | + : public llvm::iterator_adaptor_base<DeclIDIterator, const GlobalDeclID *, |
| 164 | + std::forward_iterator_tag, DeclID> { |
| 165 | +public: |
| 166 | + DeclIDIterator() : iterator_adaptor_base(nullptr) {} |
| 167 | + |
| 168 | + DeclIDIterator(const GlobalDeclID *ID) : iterator_adaptor_base(ID) {} |
| 169 | + |
| 170 | + value_type operator*() const { return DeclID(*I); } |
| 171 | + |
| 172 | + bool operator==(const DeclIDIterator &RHS) const { return I == RHS.I; } |
| 173 | +}; |
| 174 | + |
| 175 | +} // namespace clang |
| 176 | + |
| 177 | +#endif |
0 commit comments