| 1 | //===----------------------------------------------------------------------===// |
| 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 | #ifndef LLVM_CLANG_LIB_APINOTES_APINOTESFORMAT_H |
| 10 | #define LLVM_CLANG_LIB_APINOTES_APINOTESFORMAT_H |
| 11 | |
| 12 | #include "clang/APINotes/Types.h" |
| 13 | #include "llvm/ADT/ArrayRef.h" |
| 14 | #include "llvm/ADT/PointerEmbeddedInt.h" |
| 15 | #include "llvm/ADT/SmallVector.h" |
| 16 | #include "llvm/Bitcode/BitcodeConvenience.h" |
| 17 | |
| 18 | #include <optional> |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace api_notes { |
| 22 | /// Magic number for API notes files. |
| 23 | const unsigned char API_NOTES_SIGNATURE[] = {0xE2, 0x9C, 0xA8, 0x01}; |
| 24 | |
| 25 | /// API notes file major version number. |
| 26 | const uint16_t VERSION_MAJOR = 0; |
| 27 | |
| 28 | /// API notes file minor version number. |
| 29 | /// |
| 30 | /// When the format changes IN ANY WAY, this number should be incremented. |
| 31 | const uint16_t VERSION_MINOR = 41; // 39 for BoundsSafety; |
| 32 | // 40 for UnsafeBufferUsageAttr |
| 33 | // 41 for FunctionTableKey parameters |
| 34 | |
| 35 | const uint8_t kSwiftConforms = 1; |
| 36 | const uint8_t kSwiftDoesNotConform = 2; |
| 37 | |
| 38 | using IdentifierIDField = llvm::BCVBR<16>; |
| 39 | |
| 40 | using SelectorID = llvm::PointerEmbeddedInt<unsigned, 31>; |
| 41 | using SelectorIDField = llvm::BCVBR<16>; |
| 42 | |
| 43 | /// The various types of blocks that can occur within a API notes file. |
| 44 | /// |
| 45 | /// These IDs must \em not be renumbered or reordered without incrementing |
| 46 | /// VERSION_MAJOR. |
| 47 | enum BlockID { |
| 48 | /// The control block, which contains all of the information that needs to |
| 49 | /// be validated prior to committing to loading the API notes file. |
| 50 | /// |
| 51 | /// \sa control_block |
| 52 | CONTROL_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID, |
| 53 | |
| 54 | /// The identifier data block, which maps identifier strings to IDs. |
| 55 | IDENTIFIER_BLOCK_ID, |
| 56 | |
| 57 | /// The Objective-C context data block, which contains information about |
| 58 | /// Objective-C classes and protocols. |
| 59 | OBJC_CONTEXT_BLOCK_ID, |
| 60 | |
| 61 | /// The Objective-C property data block, which maps Objective-C |
| 62 | /// (class name, property name) pairs to information about the |
| 63 | /// property. |
| 64 | OBJC_PROPERTY_BLOCK_ID, |
| 65 | |
| 66 | /// The Objective-C property data block, which maps Objective-C |
| 67 | /// (class name, selector, is_instance_method) tuples to information |
| 68 | /// about the method. |
| 69 | OBJC_METHOD_BLOCK_ID, |
| 70 | |
| 71 | /// The C++ method data block, which maps C++ (context id, method name) pairs |
| 72 | /// to information about the method. |
| 73 | CXX_METHOD_BLOCK_ID, |
| 74 | |
| 75 | /// The Objective-C selector data block, which maps Objective-C |
| 76 | /// selector names (# of pieces, identifier IDs) to the selector ID |
| 77 | /// used in other tables. |
| 78 | OBJC_SELECTOR_BLOCK_ID, |
| 79 | |
| 80 | /// The fields data block, which maps names fields of C records to |
| 81 | /// information about the field. |
| 82 | FIELD_BLOCK_ID, |
| 83 | |
| 84 | /// The global variables data block, which maps global variable names to |
| 85 | /// information about the global variable. |
| 86 | GLOBAL_VARIABLE_BLOCK_ID, |
| 87 | |
| 88 | /// The (global) functions data block, which maps global function names to |
| 89 | /// information about the global function. |
| 90 | GLOBAL_FUNCTION_BLOCK_ID, |
| 91 | |
| 92 | /// The tag data block, which maps tag names to information about |
| 93 | /// the tags. |
| 94 | TAG_BLOCK_ID, |
| 95 | |
| 96 | /// The typedef data block, which maps typedef names to information about |
| 97 | /// the typedefs. |
| 98 | TYPEDEF_BLOCK_ID, |
| 99 | |
| 100 | /// The enum constant data block, which maps enumerator names to |
| 101 | /// information about the enumerators. |
| 102 | ENUM_CONSTANT_BLOCK_ID, |
| 103 | }; |
| 104 | |
| 105 | namespace control_block { |
| 106 | // These IDs must \em not be renumbered or reordered without incrementing |
| 107 | // VERSION_MAJOR. |
| 108 | enum { |
| 109 | METADATA = 1, |
| 110 | MODULE_NAME = 2, |
| 111 | MODULE_OPTIONS = 3, |
| 112 | SOURCE_FILE = 4, |
| 113 | }; |
| 114 | |
| 115 | using MetadataLayout = |
| 116 | llvm::BCRecordLayout<METADATA, // ID |
| 117 | llvm::BCFixed<16>, // Module format major version |
| 118 | llvm::BCFixed<16> // Module format minor version |
| 119 | >; |
| 120 | |
| 121 | using ModuleNameLayout = llvm::BCRecordLayout<MODULE_NAME, |
| 122 | llvm::BCBlob // Module name |
| 123 | >; |
| 124 | |
| 125 | using ModuleOptionsLayout = |
| 126 | llvm::BCRecordLayout<MODULE_OPTIONS, |
| 127 | llvm::BCFixed<1> // SwiftInferImportAsMember |
| 128 | >; |
| 129 | |
| 130 | using SourceFileLayout = llvm::BCRecordLayout<SOURCE_FILE, |
| 131 | llvm::BCVBR<16>, // file size |
| 132 | llvm::BCVBR<16> // creation time |
| 133 | >; |
| 134 | } // namespace control_block |
| 135 | |
| 136 | namespace identifier_block { |
| 137 | enum { |
| 138 | IDENTIFIER_DATA = 1, |
| 139 | }; |
| 140 | |
| 141 | using IdentifierDataLayout = llvm::BCRecordLayout< |
| 142 | IDENTIFIER_DATA, // record ID |
| 143 | llvm::BCVBR<16>, // table offset within the blob (see below) |
| 144 | llvm::BCBlob // map from identifier strings to decl kinds / decl IDs |
| 145 | >; |
| 146 | } // namespace identifier_block |
| 147 | |
| 148 | namespace context_block { |
| 149 | enum { |
| 150 | CONTEXT_ID_DATA = 1, |
| 151 | CONTEXT_INFO_DATA = 2, |
| 152 | }; |
| 153 | |
| 154 | using ContextIDLayout = |
| 155 | llvm::BCRecordLayout<CONTEXT_ID_DATA, // record ID |
| 156 | llvm::BCVBR<16>, // table offset within the blob (see |
| 157 | // below) |
| 158 | llvm::BCBlob // map from ObjC class names/protocol (as |
| 159 | // IDs) to context IDs |
| 160 | >; |
| 161 | |
| 162 | using ContextInfoLayout = llvm::BCRecordLayout< |
| 163 | CONTEXT_INFO_DATA, // record ID |
| 164 | llvm::BCVBR<16>, // table offset within the blob (see below) |
| 165 | llvm::BCBlob // map from ObjC context IDs to context information. |
| 166 | >; |
| 167 | } // namespace context_block |
| 168 | |
| 169 | namespace objc_property_block { |
| 170 | enum { |
| 171 | OBJC_PROPERTY_DATA = 1, |
| 172 | }; |
| 173 | |
| 174 | using ObjCPropertyDataLayout = llvm::BCRecordLayout< |
| 175 | OBJC_PROPERTY_DATA, // record ID |
| 176 | llvm::BCVBR<16>, // table offset within the blob (see below) |
| 177 | llvm::BCBlob // map from ObjC (class name, property name) pairs to |
| 178 | // ObjC property information |
| 179 | >; |
| 180 | } // namespace objc_property_block |
| 181 | |
| 182 | namespace objc_method_block { |
| 183 | enum { |
| 184 | OBJC_METHOD_DATA = 1, |
| 185 | }; |
| 186 | |
| 187 | using ObjCMethodDataLayout = |
| 188 | llvm::BCRecordLayout<OBJC_METHOD_DATA, // record ID |
| 189 | llvm::BCVBR<16>, // table offset within the blob (see |
| 190 | // below) |
| 191 | llvm::BCBlob // map from ObjC (class names, selector, |
| 192 | // is-instance-method) tuples to ObjC |
| 193 | // method information |
| 194 | >; |
| 195 | } // namespace objc_method_block |
| 196 | |
| 197 | namespace cxx_method_block { |
| 198 | enum { |
| 199 | CXX_METHOD_DATA = 1, |
| 200 | }; |
| 201 | |
| 202 | using CXXMethodDataLayout = |
| 203 | llvm::BCRecordLayout<CXX_METHOD_DATA, // record ID |
| 204 | llvm::BCVBR<16>, // table offset within the blob (see |
| 205 | // below) |
| 206 | llvm::BCBlob // map from C++ (context id, name) |
| 207 | // tuples to C++ method information |
| 208 | >; |
| 209 | } // namespace cxx_method_block |
| 210 | |
| 211 | namespace field_block { |
| 212 | enum { |
| 213 | FIELD_DATA = 1, |
| 214 | }; |
| 215 | |
| 216 | using FieldDataLayout = |
| 217 | llvm::BCRecordLayout<FIELD_DATA, // record ID |
| 218 | llvm::BCVBR<16>, // table offset within the blob (see |
| 219 | // below) |
| 220 | llvm::BCBlob // map from C (context id, name) |
| 221 | // tuples to C field information |
| 222 | >; |
| 223 | } // namespace field_block |
| 224 | |
| 225 | namespace objc_selector_block { |
| 226 | enum { |
| 227 | OBJC_SELECTOR_DATA = 1, |
| 228 | }; |
| 229 | |
| 230 | using ObjCSelectorDataLayout = |
| 231 | llvm::BCRecordLayout<OBJC_SELECTOR_DATA, // record ID |
| 232 | llvm::BCVBR<16>, // table offset within the blob (see |
| 233 | // below) |
| 234 | llvm::BCBlob // map from (# pieces, identifier IDs) to |
| 235 | // Objective-C selector ID. |
| 236 | >; |
| 237 | } // namespace objc_selector_block |
| 238 | |
| 239 | namespace global_variable_block { |
| 240 | enum { GLOBAL_VARIABLE_DATA = 1 }; |
| 241 | |
| 242 | using GlobalVariableDataLayout = llvm::BCRecordLayout< |
| 243 | GLOBAL_VARIABLE_DATA, // record ID |
| 244 | llvm::BCVBR<16>, // table offset within the blob (see below) |
| 245 | llvm::BCBlob // map from name to global variable information |
| 246 | >; |
| 247 | } // namespace global_variable_block |
| 248 | |
| 249 | namespace global_function_block { |
| 250 | enum { GLOBAL_FUNCTION_DATA = 1 }; |
| 251 | |
| 252 | using GlobalFunctionDataLayout = llvm::BCRecordLayout< |
| 253 | GLOBAL_FUNCTION_DATA, // record ID |
| 254 | llvm::BCVBR<16>, // table offset within the blob (see below) |
| 255 | llvm::BCBlob // map from name to global function information |
| 256 | >; |
| 257 | } // namespace global_function_block |
| 258 | |
| 259 | namespace tag_block { |
| 260 | enum { TAG_DATA = 1 }; |
| 261 | |
| 262 | using TagDataLayout = |
| 263 | llvm::BCRecordLayout<TAG_DATA, // record ID |
| 264 | llvm::BCVBR<16>, // table offset within the blob (see |
| 265 | // below) |
| 266 | llvm::BCBlob // map from name to tag information |
| 267 | >; |
| 268 | } // namespace tag_block |
| 269 | |
| 270 | namespace typedef_block { |
| 271 | enum { TYPEDEF_DATA = 1 }; |
| 272 | |
| 273 | using TypedefDataLayout = |
| 274 | llvm::BCRecordLayout<TYPEDEF_DATA, // record ID |
| 275 | llvm::BCVBR<16>, // table offset within the blob (see |
| 276 | // below) |
| 277 | llvm::BCBlob // map from name to typedef information |
| 278 | >; |
| 279 | } // namespace typedef_block |
| 280 | |
| 281 | namespace enum_constant_block { |
| 282 | enum { ENUM_CONSTANT_DATA = 1 }; |
| 283 | |
| 284 | using EnumConstantDataLayout = |
| 285 | llvm::BCRecordLayout<ENUM_CONSTANT_DATA, // record ID |
| 286 | llvm::BCVBR<16>, // table offset within the blob (see |
| 287 | // below) |
| 288 | llvm::BCBlob // map from name to enumerator information |
| 289 | >; |
| 290 | } // namespace enum_constant_block |
| 291 | |
| 292 | /// A stored Objective-C selector. |
| 293 | struct StoredObjCSelector { |
| 294 | unsigned NumArgs; |
| 295 | llvm::SmallVector<IdentifierID, 2> Identifiers; |
| 296 | }; |
| 297 | |
| 298 | /// A stored Objective-C or C++ context, represented by the ID of its parent |
| 299 | /// context, the kind of this context (Objective-C class / C++ namespace / etc), |
| 300 | /// and the ID of this context. |
| 301 | struct ContextTableKey { |
| 302 | uint32_t parentContextID; |
| 303 | uint8_t contextKind; |
| 304 | uint32_t contextID; |
| 305 | |
| 306 | ContextTableKey() : parentContextID(-1), contextKind(-1), contextID(-1) {} |
| 307 | |
| 308 | ContextTableKey(uint32_t parentContextID, uint8_t contextKind, |
| 309 | uint32_t contextID) |
| 310 | : parentContextID(parentContextID), contextKind(contextKind), |
| 311 | contextID(contextID) {} |
| 312 | |
| 313 | ContextTableKey(std::optional<ContextID> ParentContextID, ContextKind Kind, |
| 314 | uint32_t ContextID) |
| 315 | : parentContextID(ParentContextID ? ParentContextID->Value : -1), |
| 316 | contextKind(static_cast<uint8_t>(Kind)), contextID(ContextID) {} |
| 317 | |
| 318 | ContextTableKey(std::optional<Context> ParentContext, ContextKind Kind, |
| 319 | uint32_t ContextID) |
| 320 | : ContextTableKey(ParentContext ? std::make_optional(t&: ParentContext->id) |
| 321 | : std::nullopt, |
| 322 | Kind, ContextID) {} |
| 323 | |
| 324 | llvm::hash_code hashValue() const { |
| 325 | return llvm::hash_value( |
| 326 | arg: std::tuple{parentContextID, contextKind, contextID}); |
| 327 | } |
| 328 | }; |
| 329 | |
| 330 | inline bool operator==(const ContextTableKey &lhs, const ContextTableKey &rhs) { |
| 331 | return lhs.parentContextID == rhs.parentContextID && |
| 332 | lhs.contextKind == rhs.contextKind && lhs.contextID == rhs.contextID; |
| 333 | } |
| 334 | |
| 335 | /// A stored Objective-C or C++ declaration, represented by the ID of its parent |
| 336 | /// context, and the name of the declaration. |
| 337 | struct SingleDeclTableKey { |
| 338 | uint32_t parentContextID; |
| 339 | uint32_t nameID; |
| 340 | |
| 341 | SingleDeclTableKey() : parentContextID(-1), nameID(-1) {} |
| 342 | |
| 343 | SingleDeclTableKey(uint32_t ParentContextID, uint32_t NameID) |
| 344 | : parentContextID(ParentContextID), nameID(NameID) {} |
| 345 | |
| 346 | SingleDeclTableKey(std::optional<Context> ParentCtx, IdentifierID NameID) |
| 347 | : parentContextID(ParentCtx ? ParentCtx->id.Value |
| 348 | : static_cast<uint32_t>(-1)), |
| 349 | nameID(NameID) {} |
| 350 | |
| 351 | llvm::hash_code hashValue() const { |
| 352 | return llvm::hash_value(arg: std::make_pair(x: parentContextID, y: nameID)); |
| 353 | } |
| 354 | }; |
| 355 | |
| 356 | inline bool operator==(const SingleDeclTableKey &lhs, |
| 357 | const SingleDeclTableKey &rhs) { |
| 358 | return lhs.parentContextID == rhs.parentContextID && lhs.nameID == rhs.nameID; |
| 359 | } |
| 360 | |
| 361 | /// A stored C or C++ function declaration, represented by the ID of its parent |
| 362 | /// context, the name of the declaration, and optional exact parameter types. |
| 363 | constexpr uint8_t FunctionKeyHasParameterSelector = 0x01; |
| 364 | constexpr unsigned FunctionTableKeyBaseLength = |
| 365 | sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint16_t); |
| 366 | |
| 367 | template <typename GetIdentifierFn> |
| 368 | std::optional<FunctionTableKey> |
| 369 | getFunctionKeyImpl(uint32_t ParentContextID, llvm::StringRef Name, |
| 370 | GetIdentifierFn GetIdentifier) { |
| 371 | std::optional<IdentifierID> NameID = GetIdentifier(Name); |
| 372 | if (!NameID) |
| 373 | return std::nullopt; |
| 374 | |
| 375 | return FunctionTableKey(ParentContextID, *NameID); |
| 376 | } |
| 377 | |
| 378 | template <typename ParameterT, typename GetIdentifierFn> |
| 379 | std::optional<FunctionTableKey> |
| 380 | getFunctionKeyImpl(uint32_t ParentContextID, llvm::StringRef Name, |
| 381 | llvm::ArrayRef<ParameterT> Parameters, |
| 382 | GetIdentifierFn GetIdentifier) { |
| 383 | std::optional<IdentifierID> NameID = GetIdentifier(Name); |
| 384 | if (!NameID) |
| 385 | return std::nullopt; |
| 386 | |
| 387 | llvm::SmallVector<IdentifierID, 2> ParameterTypeIDs; |
| 388 | ParameterTypeIDs.reserve(N: Parameters.size()); |
| 389 | for (const ParameterT &Parameter : Parameters) { |
| 390 | std::optional<IdentifierID> ParameterID = |
| 391 | GetIdentifier(llvm::StringRef(Parameter)); |
| 392 | if (!ParameterID) |
| 393 | return std::nullopt; |
| 394 | ParameterTypeIDs.push_back(Elt: *ParameterID); |
| 395 | } |
| 396 | return FunctionTableKey(ParentContextID, *NameID, ParameterTypeIDs); |
| 397 | } |
| 398 | |
| 399 | } // namespace api_notes |
| 400 | } // namespace clang |
| 401 | |
| 402 | namespace llvm { |
| 403 | template <> struct DenseMapInfo<clang::api_notes::StoredObjCSelector> { |
| 404 | typedef DenseMapInfo<unsigned> UnsignedInfo; |
| 405 | |
| 406 | static unsigned |
| 407 | getHashValue(const clang::api_notes::StoredObjCSelector &Selector) { |
| 408 | auto hash = llvm::hash_value(value: Selector.NumArgs); |
| 409 | hash = hash_combine(args: hash, args: Selector.Identifiers.size()); |
| 410 | for (auto piece : Selector.Identifiers) |
| 411 | hash = hash_combine(args: hash, args: static_cast<unsigned>(piece)); |
| 412 | // FIXME: Mix upper/lower 32-bit values together to produce |
| 413 | // unsigned rather than truncating. |
| 414 | return hash; |
| 415 | } |
| 416 | |
| 417 | static bool isEqual(const clang::api_notes::StoredObjCSelector &LHS, |
| 418 | const clang::api_notes::StoredObjCSelector &RHS) { |
| 419 | return LHS.NumArgs == RHS.NumArgs && LHS.Identifiers == RHS.Identifiers; |
| 420 | } |
| 421 | }; |
| 422 | |
| 423 | template <> struct DenseMapInfo<clang::api_notes::ContextTableKey> { |
| 424 | static unsigned getHashValue(const clang::api_notes::ContextTableKey &value) { |
| 425 | return value.hashValue(); |
| 426 | } |
| 427 | |
| 428 | static bool isEqual(const clang::api_notes::ContextTableKey &lhs, |
| 429 | const clang::api_notes::ContextTableKey &rhs) { |
| 430 | return lhs == rhs; |
| 431 | } |
| 432 | }; |
| 433 | |
| 434 | template <> struct DenseMapInfo<clang::api_notes::SingleDeclTableKey> { |
| 435 | static unsigned |
| 436 | getHashValue(const clang::api_notes::SingleDeclTableKey &value) { |
| 437 | return value.hashValue(); |
| 438 | } |
| 439 | |
| 440 | static bool isEqual(const clang::api_notes::SingleDeclTableKey &lhs, |
| 441 | const clang::api_notes::SingleDeclTableKey &rhs) { |
| 442 | return lhs == rhs; |
| 443 | } |
| 444 | }; |
| 445 | |
| 446 | } // namespace llvm |
| 447 | |
| 448 | #endif |
| 449 | |