1 //===-- BitcodeReader.cpp - ClangDoc Bitcode Reader ------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "BitcodeReader.h"
10 #include "llvm/ADT/IndexedMap.h"
11 #include "llvm/Support/Error.h"
12 #include "llvm/Support/raw_ostream.h"
18 using Record
= llvm::SmallVector
<uint64_t, 1024>;
20 // This implements decode for SmallString.
21 llvm::Error
decodeRecord(const Record
&R
, llvm::SmallVectorImpl
<char> &Field
,
22 llvm::StringRef Blob
) {
23 Field
.assign(Blob
.begin(), Blob
.end());
24 return llvm::Error::success();
27 llvm::Error
decodeRecord(const Record
&R
, SymbolID
&Field
,
28 llvm::StringRef Blob
) {
29 if (R
[0] != BitCodeConstants::USRHashSize
)
30 return llvm::createStringError(llvm::inconvertibleErrorCode(),
31 "incorrect USR size");
33 // First position in the record is the length of the following array, so we
34 // copy the following elements to the field.
35 for (int I
= 0, E
= R
[0]; I
< E
; ++I
)
37 return llvm::Error::success();
40 llvm::Error
decodeRecord(const Record
&R
, bool &Field
, llvm::StringRef Blob
) {
42 return llvm::Error::success();
45 llvm::Error
decodeRecord(const Record
&R
, int &Field
, llvm::StringRef Blob
) {
47 return llvm::createStringError(llvm::inconvertibleErrorCode(),
48 "integer too large to parse");
50 return llvm::Error::success();
53 llvm::Error
decodeRecord(const Record
&R
, AccessSpecifier
&Field
,
54 llvm::StringRef Blob
) {
60 Field
= (AccessSpecifier
)R
[0];
61 return llvm::Error::success();
63 return llvm::createStringError(llvm::inconvertibleErrorCode(),
64 "invalid value for AccessSpecifier");
68 llvm::Error
decodeRecord(const Record
&R
, TagTypeKind
&Field
,
69 llvm::StringRef Blob
) {
70 switch (static_cast<TagTypeKind
>(R
[0])) {
71 case TagTypeKind::Struct
:
72 case TagTypeKind::Interface
:
73 case TagTypeKind::Union
:
74 case TagTypeKind::Class
:
75 case TagTypeKind::Enum
:
76 Field
= static_cast<TagTypeKind
>(R
[0]);
77 return llvm::Error::success();
79 return llvm::createStringError(llvm::inconvertibleErrorCode(),
80 "invalid value for TagTypeKind");
83 llvm::Error
decodeRecord(const Record
&R
, std::optional
<Location
> &Field
,
84 llvm::StringRef Blob
) {
86 return llvm::createStringError(llvm::inconvertibleErrorCode(),
87 "integer too large to parse");
88 Field
.emplace((int)R
[0], Blob
, (bool)R
[1]);
89 return llvm::Error::success();
92 llvm::Error
decodeRecord(const Record
&R
, InfoType
&Field
,
93 llvm::StringRef Blob
) {
94 switch (auto IT
= static_cast<InfoType
>(R
[0])) {
95 case InfoType::IT_namespace
:
96 case InfoType::IT_record
:
97 case InfoType::IT_function
:
98 case InfoType::IT_default
:
99 case InfoType::IT_enum
:
100 case InfoType::IT_typedef
:
102 return llvm::Error::success();
104 return llvm::createStringError(llvm::inconvertibleErrorCode(),
105 "invalid value for InfoType");
108 llvm::Error
decodeRecord(const Record
&R
, FieldId
&Field
,
109 llvm::StringRef Blob
) {
110 switch (auto F
= static_cast<FieldId
>(R
[0])) {
111 case FieldId::F_namespace
:
112 case FieldId::F_parent
:
113 case FieldId::F_vparent
:
114 case FieldId::F_type
:
115 case FieldId::F_child_namespace
:
116 case FieldId::F_child_record
:
117 case FieldId::F_default
:
119 return llvm::Error::success();
121 return llvm::createStringError(llvm::inconvertibleErrorCode(),
122 "invalid value for FieldId");
125 llvm::Error
decodeRecord(const Record
&R
,
126 llvm::SmallVectorImpl
<llvm::SmallString
<16>> &Field
,
127 llvm::StringRef Blob
) {
128 Field
.push_back(Blob
);
129 return llvm::Error::success();
132 llvm::Error
decodeRecord(const Record
&R
,
133 llvm::SmallVectorImpl
<Location
> &Field
,
134 llvm::StringRef Blob
) {
136 return llvm::createStringError(llvm::inconvertibleErrorCode(),
137 "integer too large to parse");
138 Field
.emplace_back((int)R
[0], Blob
, (bool)R
[1]);
139 return llvm::Error::success();
142 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
143 const unsigned VersionNo
) {
144 if (ID
== VERSION
&& R
[0] == VersionNo
)
145 return llvm::Error::success();
146 return llvm::createStringError(llvm::inconvertibleErrorCode(),
147 "mismatched bitcode version number");
150 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
154 return decodeRecord(R
, I
->USR
, Blob
);
156 return decodeRecord(R
, I
->Name
, Blob
);
158 return decodeRecord(R
, I
->Path
, Blob
);
160 return llvm::createStringError(llvm::inconvertibleErrorCode(),
161 "invalid field for NamespaceInfo");
165 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
169 return decodeRecord(R
, I
->USR
, Blob
);
171 return decodeRecord(R
, I
->Name
, Blob
);
173 return decodeRecord(R
, I
->Path
, Blob
);
174 case RECORD_DEFLOCATION
:
175 return decodeRecord(R
, I
->DefLoc
, Blob
);
176 case RECORD_LOCATION
:
177 return decodeRecord(R
, I
->Loc
, Blob
);
178 case RECORD_TAG_TYPE
:
179 return decodeRecord(R
, I
->TagType
, Blob
);
180 case RECORD_IS_TYPE_DEF
:
181 return decodeRecord(R
, I
->IsTypeDef
, Blob
);
183 return llvm::createStringError(llvm::inconvertibleErrorCode(),
184 "invalid field for RecordInfo");
188 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
191 case BASE_RECORD_USR
:
192 return decodeRecord(R
, I
->USR
, Blob
);
193 case BASE_RECORD_NAME
:
194 return decodeRecord(R
, I
->Name
, Blob
);
195 case BASE_RECORD_PATH
:
196 return decodeRecord(R
, I
->Path
, Blob
);
197 case BASE_RECORD_TAG_TYPE
:
198 return decodeRecord(R
, I
->TagType
, Blob
);
199 case BASE_RECORD_IS_VIRTUAL
:
200 return decodeRecord(R
, I
->IsVirtual
, Blob
);
201 case BASE_RECORD_ACCESS
:
202 return decodeRecord(R
, I
->Access
, Blob
);
203 case BASE_RECORD_IS_PARENT
:
204 return decodeRecord(R
, I
->IsParent
, Blob
);
206 return llvm::createStringError(llvm::inconvertibleErrorCode(),
207 "invalid field for BaseRecordInfo");
211 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
215 return decodeRecord(R
, I
->USR
, Blob
);
217 return decodeRecord(R
, I
->Name
, Blob
);
218 case ENUM_DEFLOCATION
:
219 return decodeRecord(R
, I
->DefLoc
, Blob
);
221 return decodeRecord(R
, I
->Loc
, Blob
);
223 return decodeRecord(R
, I
->Scoped
, Blob
);
225 return llvm::createStringError(llvm::inconvertibleErrorCode(),
226 "invalid field for EnumInfo");
230 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
234 return decodeRecord(R
, I
->USR
, Blob
);
236 return decodeRecord(R
, I
->Name
, Blob
);
237 case TYPEDEF_DEFLOCATION
:
238 return decodeRecord(R
, I
->DefLoc
, Blob
);
239 case TYPEDEF_IS_USING
:
240 return decodeRecord(R
, I
->IsUsing
, Blob
);
242 return llvm::createStringError(llvm::inconvertibleErrorCode(),
243 "invalid field for TypedefInfo");
247 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
250 case ENUM_VALUE_NAME
:
251 return decodeRecord(R
, I
->Name
, Blob
);
252 case ENUM_VALUE_VALUE
:
253 return decodeRecord(R
, I
->Value
, Blob
);
254 case ENUM_VALUE_EXPR
:
255 return decodeRecord(R
, I
->ValueExpr
, Blob
);
257 return llvm::createStringError(llvm::inconvertibleErrorCode(),
258 "invalid field for EnumValueInfo");
262 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
266 return decodeRecord(R
, I
->USR
, Blob
);
268 return decodeRecord(R
, I
->Name
, Blob
);
269 case FUNCTION_DEFLOCATION
:
270 return decodeRecord(R
, I
->DefLoc
, Blob
);
271 case FUNCTION_LOCATION
:
272 return decodeRecord(R
, I
->Loc
, Blob
);
273 case FUNCTION_ACCESS
:
274 return decodeRecord(R
, I
->Access
, Blob
);
275 case FUNCTION_IS_METHOD
:
276 return decodeRecord(R
, I
->IsMethod
, Blob
);
278 return llvm::createStringError(llvm::inconvertibleErrorCode(),
279 "invalid field for FunctionInfo");
283 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
285 return llvm::Error::success();
288 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
291 case FIELD_TYPE_NAME
:
292 return decodeRecord(R
, I
->Name
, Blob
);
293 case FIELD_DEFAULT_VALUE
:
294 return decodeRecord(R
, I
->DefaultValue
, Blob
);
296 return llvm::createStringError(llvm::inconvertibleErrorCode(),
297 "invalid field for TypeInfo");
301 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
304 case MEMBER_TYPE_NAME
:
305 return decodeRecord(R
, I
->Name
, Blob
);
306 case MEMBER_TYPE_ACCESS
:
307 return decodeRecord(R
, I
->Access
, Blob
);
309 return llvm::createStringError(llvm::inconvertibleErrorCode(),
310 "invalid field for MemberTypeInfo");
314 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
318 return decodeRecord(R
, I
->Kind
, Blob
);
320 return decodeRecord(R
, I
->Text
, Blob
);
322 return decodeRecord(R
, I
->Name
, Blob
);
323 case COMMENT_DIRECTION
:
324 return decodeRecord(R
, I
->Direction
, Blob
);
325 case COMMENT_PARAMNAME
:
326 return decodeRecord(R
, I
->ParamName
, Blob
);
327 case COMMENT_CLOSENAME
:
328 return decodeRecord(R
, I
->CloseName
, Blob
);
329 case COMMENT_ATTRKEY
:
330 return decodeRecord(R
, I
->AttrKeys
, Blob
);
331 case COMMENT_ATTRVAL
:
332 return decodeRecord(R
, I
->AttrValues
, Blob
);
334 return decodeRecord(R
, I
->Args
, Blob
);
335 case COMMENT_SELFCLOSING
:
336 return decodeRecord(R
, I
->SelfClosing
, Blob
);
337 case COMMENT_EXPLICIT
:
338 return decodeRecord(R
, I
->Explicit
, Blob
);
340 return llvm::createStringError(llvm::inconvertibleErrorCode(),
341 "invalid field for CommentInfo");
345 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
346 Reference
*I
, FieldId
&F
) {
349 return decodeRecord(R
, I
->USR
, Blob
);
351 return decodeRecord(R
, I
->Name
, Blob
);
352 case REFERENCE_QUAL_NAME
:
353 return decodeRecord(R
, I
->QualName
, Blob
);
355 return decodeRecord(R
, I
->RefType
, Blob
);
357 return decodeRecord(R
, I
->Path
, Blob
);
358 case REFERENCE_FIELD
:
359 return decodeRecord(R
, F
, Blob
);
361 return llvm::createStringError(llvm::inconvertibleErrorCode(),
362 "invalid field for Reference");
366 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
368 // Currently there are no child records of TemplateInfo (only child blocks).
369 return llvm::createStringError(llvm::inconvertibleErrorCode(),
370 "invalid field for TemplateParamInfo");
373 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
374 TemplateSpecializationInfo
*I
) {
375 if (ID
== TEMPLATE_SPECIALIZATION_OF
)
376 return decodeRecord(R
, I
->SpecializationOf
, Blob
);
377 return llvm::createStringError(llvm::inconvertibleErrorCode(),
378 "invalid field for TemplateParamInfo");
381 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
382 TemplateParamInfo
*I
) {
383 if (ID
== TEMPLATE_PARAM_CONTENTS
)
384 return decodeRecord(R
, I
->Contents
, Blob
);
385 return llvm::createStringError(llvm::inconvertibleErrorCode(),
386 "invalid field for TemplateParamInfo");
389 template <typename T
> llvm::Expected
<CommentInfo
*> getCommentInfo(T I
) {
390 return llvm::createStringError(llvm::inconvertibleErrorCode(),
391 "invalid type cannot contain CommentInfo");
394 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(FunctionInfo
*I
) {
395 return &I
->Description
.emplace_back();
398 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(NamespaceInfo
*I
) {
399 return &I
->Description
.emplace_back();
402 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(RecordInfo
*I
) {
403 return &I
->Description
.emplace_back();
406 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(MemberTypeInfo
*I
) {
407 return &I
->Description
.emplace_back();
410 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(EnumInfo
*I
) {
411 return &I
->Description
.emplace_back();
414 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(TypedefInfo
*I
) {
415 return &I
->Description
.emplace_back();
418 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(EnumValueInfo
*I
) {
419 return &I
->Description
.emplace_back();
422 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(CommentInfo
*I
) {
423 I
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
424 return I
->Children
.back().get();
428 llvm::Expected
<CommentInfo
*> getCommentInfo(std::unique_ptr
<CommentInfo
> &I
) {
429 return getCommentInfo(I
.get());
432 // When readSubBlock encounters a TypeInfo sub-block, it calls addTypeInfo on
433 // the parent block to set it. The template specializations define what to do
434 // for each supported parent block.
435 template <typename T
, typename TTypeInfo
>
436 llvm::Error
addTypeInfo(T I
, TTypeInfo
&&TI
) {
437 return llvm::createStringError(llvm::inconvertibleErrorCode(),
438 "invalid type cannot contain TypeInfo");
441 template <> llvm::Error
addTypeInfo(RecordInfo
*I
, MemberTypeInfo
&&T
) {
442 I
->Members
.emplace_back(std::move(T
));
443 return llvm::Error::success();
446 template <> llvm::Error
addTypeInfo(BaseRecordInfo
*I
, MemberTypeInfo
&&T
) {
447 I
->Members
.emplace_back(std::move(T
));
448 return llvm::Error::success();
451 template <> llvm::Error
addTypeInfo(FunctionInfo
*I
, TypeInfo
&&T
) {
452 I
->ReturnType
= std::move(T
);
453 return llvm::Error::success();
456 template <> llvm::Error
addTypeInfo(FunctionInfo
*I
, FieldTypeInfo
&&T
) {
457 I
->Params
.emplace_back(std::move(T
));
458 return llvm::Error::success();
461 template <> llvm::Error
addTypeInfo(EnumInfo
*I
, TypeInfo
&&T
) {
462 I
->BaseType
= std::move(T
);
463 return llvm::Error::success();
466 template <> llvm::Error
addTypeInfo(TypedefInfo
*I
, TypeInfo
&&T
) {
467 I
->Underlying
= std::move(T
);
468 return llvm::Error::success();
471 template <typename T
> llvm::Error
addReference(T I
, Reference
&&R
, FieldId F
) {
472 return llvm::createStringError(llvm::inconvertibleErrorCode(),
473 "invalid type cannot contain Reference");
476 template <> llvm::Error
addReference(TypeInfo
*I
, Reference
&&R
, FieldId F
) {
478 case FieldId::F_type
:
479 I
->Type
= std::move(R
);
480 return llvm::Error::success();
482 return llvm::createStringError(llvm::inconvertibleErrorCode(),
483 "invalid type cannot contain Reference");
488 llvm::Error
addReference(FieldTypeInfo
*I
, Reference
&&R
, FieldId F
) {
490 case FieldId::F_type
:
491 I
->Type
= std::move(R
);
492 return llvm::Error::success();
494 return llvm::createStringError(llvm::inconvertibleErrorCode(),
495 "invalid type cannot contain Reference");
500 llvm::Error
addReference(MemberTypeInfo
*I
, Reference
&&R
, FieldId F
) {
502 case FieldId::F_type
:
503 I
->Type
= std::move(R
);
504 return llvm::Error::success();
506 return llvm::createStringError(llvm::inconvertibleErrorCode(),
507 "invalid type cannot contain Reference");
511 template <> llvm::Error
addReference(EnumInfo
*I
, Reference
&&R
, FieldId F
) {
513 case FieldId::F_namespace
:
514 I
->Namespace
.emplace_back(std::move(R
));
515 return llvm::Error::success();
517 return llvm::createStringError(llvm::inconvertibleErrorCode(),
518 "invalid type cannot contain Reference");
522 template <> llvm::Error
addReference(TypedefInfo
*I
, Reference
&&R
, FieldId F
) {
524 case FieldId::F_namespace
:
525 I
->Namespace
.emplace_back(std::move(R
));
526 return llvm::Error::success();
528 return llvm::createStringError(llvm::inconvertibleErrorCode(),
529 "invalid type cannot contain Reference");
534 llvm::Error
addReference(NamespaceInfo
*I
, Reference
&&R
, FieldId F
) {
536 case FieldId::F_namespace
:
537 I
->Namespace
.emplace_back(std::move(R
));
538 return llvm::Error::success();
539 case FieldId::F_child_namespace
:
540 I
->Children
.Namespaces
.emplace_back(std::move(R
));
541 return llvm::Error::success();
542 case FieldId::F_child_record
:
543 I
->Children
.Records
.emplace_back(std::move(R
));
544 return llvm::Error::success();
546 return llvm::createStringError(llvm::inconvertibleErrorCode(),
547 "invalid type cannot contain Reference");
552 llvm::Error
addReference(FunctionInfo
*I
, Reference
&&R
, FieldId F
) {
554 case FieldId::F_namespace
:
555 I
->Namespace
.emplace_back(std::move(R
));
556 return llvm::Error::success();
557 case FieldId::F_parent
:
558 I
->Parent
= std::move(R
);
559 return llvm::Error::success();
561 return llvm::createStringError(llvm::inconvertibleErrorCode(),
562 "invalid type cannot contain Reference");
566 template <> llvm::Error
addReference(RecordInfo
*I
, Reference
&&R
, FieldId F
) {
568 case FieldId::F_namespace
:
569 I
->Namespace
.emplace_back(std::move(R
));
570 return llvm::Error::success();
571 case FieldId::F_parent
:
572 I
->Parents
.emplace_back(std::move(R
));
573 return llvm::Error::success();
574 case FieldId::F_vparent
:
575 I
->VirtualParents
.emplace_back(std::move(R
));
576 return llvm::Error::success();
577 case FieldId::F_child_record
:
578 I
->Children
.Records
.emplace_back(std::move(R
));
579 return llvm::Error::success();
581 return llvm::createStringError(llvm::inconvertibleErrorCode(),
582 "invalid type cannot contain Reference");
586 template <typename T
, typename ChildInfoType
>
587 void addChild(T I
, ChildInfoType
&&R
) {
588 llvm::errs() << "invalid child type for info";
592 // Namespace children:
593 template <> void addChild(NamespaceInfo
*I
, FunctionInfo
&&R
) {
594 I
->Children
.Functions
.emplace_back(std::move(R
));
596 template <> void addChild(NamespaceInfo
*I
, EnumInfo
&&R
) {
597 I
->Children
.Enums
.emplace_back(std::move(R
));
599 template <> void addChild(NamespaceInfo
*I
, TypedefInfo
&&R
) {
600 I
->Children
.Typedefs
.emplace_back(std::move(R
));
604 template <> void addChild(RecordInfo
*I
, FunctionInfo
&&R
) {
605 I
->Children
.Functions
.emplace_back(std::move(R
));
607 template <> void addChild(RecordInfo
*I
, EnumInfo
&&R
) {
608 I
->Children
.Enums
.emplace_back(std::move(R
));
610 template <> void addChild(RecordInfo
*I
, TypedefInfo
&&R
) {
611 I
->Children
.Typedefs
.emplace_back(std::move(R
));
614 // Other types of children:
615 template <> void addChild(EnumInfo
*I
, EnumValueInfo
&&R
) {
616 I
->Members
.emplace_back(std::move(R
));
618 template <> void addChild(RecordInfo
*I
, BaseRecordInfo
&&R
) {
619 I
->Bases
.emplace_back(std::move(R
));
621 template <> void addChild(BaseRecordInfo
*I
, FunctionInfo
&&R
) {
622 I
->Children
.Functions
.emplace_back(std::move(R
));
625 // TemplateParam children. These go into either a TemplateInfo (for template
626 // parameters) or TemplateSpecializationInfo (for the specialization's
628 template <typename T
> void addTemplateParam(T I
, TemplateParamInfo
&&P
) {
629 llvm::errs() << "invalid container for template parameter";
632 template <> void addTemplateParam(TemplateInfo
*I
, TemplateParamInfo
&&P
) {
633 I
->Params
.emplace_back(std::move(P
));
636 void addTemplateParam(TemplateSpecializationInfo
*I
, TemplateParamInfo
&&P
) {
637 I
->Params
.emplace_back(std::move(P
));
640 // Template info. These apply to either records or functions.
641 template <typename T
> void addTemplate(T I
, TemplateInfo
&&P
) {
642 llvm::errs() << "invalid container for template info";
645 template <> void addTemplate(RecordInfo
*I
, TemplateInfo
&&P
) {
646 I
->Template
.emplace(std::move(P
));
648 template <> void addTemplate(FunctionInfo
*I
, TemplateInfo
&&P
) {
649 I
->Template
.emplace(std::move(P
));
652 // Template specializations go only into template records.
653 template <typename T
>
654 void addTemplateSpecialization(T I
, TemplateSpecializationInfo
&&TSI
) {
655 llvm::errs() << "invalid container for template specialization info";
659 void addTemplateSpecialization(TemplateInfo
*I
,
660 TemplateSpecializationInfo
&&TSI
) {
661 I
->Specialization
.emplace(std::move(TSI
));
664 // Read records from bitcode into a given info.
665 template <typename T
>
666 llvm::Error
ClangDocBitcodeReader::readRecord(unsigned ID
, T I
) {
668 llvm::StringRef Blob
;
669 llvm::Expected
<unsigned> MaybeRecID
= Stream
.readRecord(ID
, R
, &Blob
);
671 return MaybeRecID
.takeError();
672 return parseRecord(R
, MaybeRecID
.get(), Blob
, I
);
676 llvm::Error
ClangDocBitcodeReader::readRecord(unsigned ID
, Reference
*I
) {
678 llvm::StringRef Blob
;
679 llvm::Expected
<unsigned> MaybeRecID
= Stream
.readRecord(ID
, R
, &Blob
);
681 return MaybeRecID
.takeError();
682 return parseRecord(R
, MaybeRecID
.get(), Blob
, I
, CurrentReferenceField
);
685 // Read a block of records into a single info.
686 template <typename T
>
687 llvm::Error
ClangDocBitcodeReader::readBlock(unsigned ID
, T I
) {
688 if (llvm::Error Err
= Stream
.EnterSubBlock(ID
))
692 unsigned BlockOrCode
= 0;
693 Cursor Res
= skipUntilRecordOrBlock(BlockOrCode
);
696 case Cursor::BadBlock
:
697 return llvm::createStringError(llvm::inconvertibleErrorCode(),
699 case Cursor::BlockEnd
:
700 return llvm::Error::success();
701 case Cursor::BlockBegin
:
702 if (llvm::Error Err
= readSubBlock(BlockOrCode
, I
)) {
703 if (llvm::Error Skipped
= Stream
.SkipBlock())
704 return joinErrors(std::move(Err
), std::move(Skipped
));
711 if (auto Err
= readRecord(BlockOrCode
, I
))
716 template <typename T
>
717 llvm::Error
ClangDocBitcodeReader::readSubBlock(unsigned ID
, T I
) {
719 // Blocks can only have certain types of sub blocks.
720 case BI_COMMENT_BLOCK_ID
: {
721 auto Comment
= getCommentInfo(I
);
723 return Comment
.takeError();
724 if (auto Err
= readBlock(ID
, Comment
.get()))
726 return llvm::Error::success();
728 case BI_TYPE_BLOCK_ID
: {
730 if (auto Err
= readBlock(ID
, &TI
))
732 if (auto Err
= addTypeInfo(I
, std::move(TI
)))
734 return llvm::Error::success();
736 case BI_FIELD_TYPE_BLOCK_ID
: {
738 if (auto Err
= readBlock(ID
, &TI
))
740 if (auto Err
= addTypeInfo(I
, std::move(TI
)))
742 return llvm::Error::success();
744 case BI_MEMBER_TYPE_BLOCK_ID
: {
746 if (auto Err
= readBlock(ID
, &TI
))
748 if (auto Err
= addTypeInfo(I
, std::move(TI
)))
750 return llvm::Error::success();
752 case BI_REFERENCE_BLOCK_ID
: {
754 if (auto Err
= readBlock(ID
, &R
))
756 if (auto Err
= addReference(I
, std::move(R
), CurrentReferenceField
))
758 return llvm::Error::success();
760 case BI_FUNCTION_BLOCK_ID
: {
762 if (auto Err
= readBlock(ID
, &F
))
764 addChild(I
, std::move(F
));
765 return llvm::Error::success();
767 case BI_BASE_RECORD_BLOCK_ID
: {
769 if (auto Err
= readBlock(ID
, &BR
))
771 addChild(I
, std::move(BR
));
772 return llvm::Error::success();
774 case BI_ENUM_BLOCK_ID
: {
776 if (auto Err
= readBlock(ID
, &E
))
778 addChild(I
, std::move(E
));
779 return llvm::Error::success();
781 case BI_ENUM_VALUE_BLOCK_ID
: {
783 if (auto Err
= readBlock(ID
, &EV
))
785 addChild(I
, std::move(EV
));
786 return llvm::Error::success();
788 case BI_TEMPLATE_BLOCK_ID
: {
790 if (auto Err
= readBlock(ID
, &TI
))
792 addTemplate(I
, std::move(TI
));
793 return llvm::Error::success();
795 case BI_TEMPLATE_SPECIALIZATION_BLOCK_ID
: {
796 TemplateSpecializationInfo TSI
;
797 if (auto Err
= readBlock(ID
, &TSI
))
799 addTemplateSpecialization(I
, std::move(TSI
));
800 return llvm::Error::success();
802 case BI_TEMPLATE_PARAM_BLOCK_ID
: {
803 TemplateParamInfo TPI
;
804 if (auto Err
= readBlock(ID
, &TPI
))
806 addTemplateParam(I
, std::move(TPI
));
807 return llvm::Error::success();
809 case BI_TYPEDEF_BLOCK_ID
: {
811 if (auto Err
= readBlock(ID
, &TI
))
813 addChild(I
, std::move(TI
));
814 return llvm::Error::success();
817 return llvm::createStringError(llvm::inconvertibleErrorCode(),
818 "invalid subblock type");
822 ClangDocBitcodeReader::Cursor
823 ClangDocBitcodeReader::skipUntilRecordOrBlock(unsigned &BlockOrRecordID
) {
826 while (!Stream
.AtEndOfStream()) {
827 Expected
<unsigned> MaybeCode
= Stream
.ReadCode();
829 // FIXME this drops the error on the floor.
830 consumeError(MaybeCode
.takeError());
831 return Cursor::BadBlock
;
834 unsigned Code
= MaybeCode
.get();
835 if (Code
>= static_cast<unsigned>(llvm::bitc::FIRST_APPLICATION_ABBREV
)) {
836 BlockOrRecordID
= Code
;
837 return Cursor::Record
;
839 switch (static_cast<llvm::bitc::FixedAbbrevIDs
>(Code
)) {
840 case llvm::bitc::ENTER_SUBBLOCK
:
841 if (Expected
<unsigned> MaybeID
= Stream
.ReadSubBlockID())
842 BlockOrRecordID
= MaybeID
.get();
844 // FIXME this drops the error on the floor.
845 consumeError(MaybeID
.takeError());
847 return Cursor::BlockBegin
;
848 case llvm::bitc::END_BLOCK
:
849 if (Stream
.ReadBlockEnd())
850 return Cursor::BadBlock
;
851 return Cursor::BlockEnd
;
852 case llvm::bitc::DEFINE_ABBREV
:
853 if (llvm::Error Err
= Stream
.ReadAbbrevRecord()) {
854 // FIXME this drops the error on the floor.
855 consumeError(std::move(Err
));
858 case llvm::bitc::UNABBREV_RECORD
:
859 return Cursor::BadBlock
;
860 case llvm::bitc::FIRST_APPLICATION_ABBREV
:
861 llvm_unreachable("Unexpected abbrev id.");
864 llvm_unreachable("Premature stream end.");
867 llvm::Error
ClangDocBitcodeReader::validateStream() {
868 if (Stream
.AtEndOfStream())
869 return llvm::createStringError(llvm::inconvertibleErrorCode(),
870 "premature end of stream");
872 // Sniff for the signature.
873 for (int Idx
= 0; Idx
!= 4; ++Idx
) {
874 Expected
<llvm::SimpleBitstreamCursor::word_t
> MaybeRead
= Stream
.Read(8);
876 return MaybeRead
.takeError();
877 else if (MaybeRead
.get() != BitCodeConstants::Signature
[Idx
])
878 return llvm::createStringError(llvm::inconvertibleErrorCode(),
879 "invalid bitcode signature");
881 return llvm::Error::success();
884 llvm::Error
ClangDocBitcodeReader::readBlockInfoBlock() {
885 Expected
<std::optional
<llvm::BitstreamBlockInfo
>> MaybeBlockInfo
=
886 Stream
.ReadBlockInfoBlock();
888 return MaybeBlockInfo
.takeError();
890 BlockInfo
= MaybeBlockInfo
.get();
892 return llvm::createStringError(llvm::inconvertibleErrorCode(),
893 "unable to parse BlockInfoBlock");
894 Stream
.setBlockInfo(&*BlockInfo
);
895 return llvm::Error::success();
898 template <typename T
>
899 llvm::Expected
<std::unique_ptr
<Info
>>
900 ClangDocBitcodeReader::createInfo(unsigned ID
) {
901 std::unique_ptr
<Info
> I
= std::make_unique
<T
>();
902 if (auto Err
= readBlock(ID
, static_cast<T
*>(I
.get())))
903 return std::move(Err
);
904 return std::unique_ptr
<Info
>{std::move(I
)};
907 llvm::Expected
<std::unique_ptr
<Info
>>
908 ClangDocBitcodeReader::readBlockToInfo(unsigned ID
) {
910 case BI_NAMESPACE_BLOCK_ID
:
911 return createInfo
<NamespaceInfo
>(ID
);
912 case BI_RECORD_BLOCK_ID
:
913 return createInfo
<RecordInfo
>(ID
);
914 case BI_ENUM_BLOCK_ID
:
915 return createInfo
<EnumInfo
>(ID
);
916 case BI_TYPEDEF_BLOCK_ID
:
917 return createInfo
<TypedefInfo
>(ID
);
918 case BI_FUNCTION_BLOCK_ID
:
919 return createInfo
<FunctionInfo
>(ID
);
921 return llvm::createStringError(llvm::inconvertibleErrorCode(),
922 "cannot create info");
927 llvm::Expected
<std::vector
<std::unique_ptr
<Info
>>>
928 ClangDocBitcodeReader::readBitcode() {
929 std::vector
<std::unique_ptr
<Info
>> Infos
;
930 if (auto Err
= validateStream())
931 return std::move(Err
);
933 // Read the top level blocks.
934 while (!Stream
.AtEndOfStream()) {
935 Expected
<unsigned> MaybeCode
= Stream
.ReadCode();
937 return MaybeCode
.takeError();
938 if (MaybeCode
.get() != llvm::bitc::ENTER_SUBBLOCK
)
939 return llvm::createStringError(llvm::inconvertibleErrorCode(),
940 "no blocks in input");
941 Expected
<unsigned> MaybeID
= Stream
.ReadSubBlockID();
943 return MaybeID
.takeError();
944 unsigned ID
= MaybeID
.get();
946 // NamedType and Comment blocks should not appear at the top level
947 case BI_TYPE_BLOCK_ID
:
948 case BI_FIELD_TYPE_BLOCK_ID
:
949 case BI_MEMBER_TYPE_BLOCK_ID
:
950 case BI_COMMENT_BLOCK_ID
:
951 case BI_REFERENCE_BLOCK_ID
:
952 return llvm::createStringError(llvm::inconvertibleErrorCode(),
953 "invalid top level block");
954 case BI_NAMESPACE_BLOCK_ID
:
955 case BI_RECORD_BLOCK_ID
:
956 case BI_ENUM_BLOCK_ID
:
957 case BI_TYPEDEF_BLOCK_ID
:
958 case BI_FUNCTION_BLOCK_ID
: {
959 auto InfoOrErr
= readBlockToInfo(ID
);
961 return InfoOrErr
.takeError();
962 Infos
.emplace_back(std::move(InfoOrErr
.get()));
965 case BI_VERSION_BLOCK_ID
:
966 if (auto Err
= readBlock(ID
, VersionNumber
))
967 return std::move(Err
);
969 case llvm::bitc::BLOCKINFO_BLOCK_ID
:
970 if (auto Err
= readBlockInfoBlock())
971 return std::move(Err
);
974 if (llvm::Error Err
= Stream
.SkipBlock()) {
975 // FIXME this drops the error on the floor.
976 consumeError(std::move(Err
));
981 return std::move(Infos
);