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
) {
76 Field
= (TagTypeKind
)R
[0];
77 return llvm::Error::success();
79 return llvm::createStringError(llvm::inconvertibleErrorCode(),
80 "invalid value for TagTypeKind");
84 llvm::Error
decodeRecord(const Record
&R
, std::optional
<Location
> &Field
,
85 llvm::StringRef Blob
) {
87 return llvm::createStringError(llvm::inconvertibleErrorCode(),
88 "integer too large to parse");
89 Field
.emplace((int)R
[0], Blob
, (bool)R
[1]);
90 return llvm::Error::success();
93 llvm::Error
decodeRecord(const Record
&R
, InfoType
&Field
,
94 llvm::StringRef Blob
) {
95 switch (auto IT
= static_cast<InfoType
>(R
[0])) {
96 case InfoType::IT_namespace
:
97 case InfoType::IT_record
:
98 case InfoType::IT_function
:
99 case InfoType::IT_default
:
100 case InfoType::IT_enum
:
101 case InfoType::IT_typedef
:
103 return llvm::Error::success();
105 return llvm::createStringError(llvm::inconvertibleErrorCode(),
106 "invalid value for InfoType");
109 llvm::Error
decodeRecord(const Record
&R
, FieldId
&Field
,
110 llvm::StringRef Blob
) {
111 switch (auto F
= static_cast<FieldId
>(R
[0])) {
112 case FieldId::F_namespace
:
113 case FieldId::F_parent
:
114 case FieldId::F_vparent
:
115 case FieldId::F_type
:
116 case FieldId::F_child_namespace
:
117 case FieldId::F_child_record
:
118 case FieldId::F_default
:
120 return llvm::Error::success();
122 return llvm::createStringError(llvm::inconvertibleErrorCode(),
123 "invalid value for FieldId");
126 llvm::Error
decodeRecord(const Record
&R
,
127 llvm::SmallVectorImpl
<llvm::SmallString
<16>> &Field
,
128 llvm::StringRef Blob
) {
129 Field
.push_back(Blob
);
130 return llvm::Error::success();
133 llvm::Error
decodeRecord(const Record
&R
,
134 llvm::SmallVectorImpl
<Location
> &Field
,
135 llvm::StringRef Blob
) {
137 return llvm::createStringError(llvm::inconvertibleErrorCode(),
138 "integer too large to parse");
139 Field
.emplace_back((int)R
[0], Blob
, (bool)R
[1]);
140 return llvm::Error::success();
143 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
144 const unsigned VersionNo
) {
145 if (ID
== VERSION
&& R
[0] == VersionNo
)
146 return llvm::Error::success();
147 return llvm::createStringError(llvm::inconvertibleErrorCode(),
148 "mismatched bitcode version number");
151 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
155 return decodeRecord(R
, I
->USR
, Blob
);
157 return decodeRecord(R
, I
->Name
, Blob
);
159 return decodeRecord(R
, I
->Path
, Blob
);
161 return llvm::createStringError(llvm::inconvertibleErrorCode(),
162 "invalid field for NamespaceInfo");
166 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
170 return decodeRecord(R
, I
->USR
, Blob
);
172 return decodeRecord(R
, I
->Name
, Blob
);
174 return decodeRecord(R
, I
->Path
, Blob
);
175 case RECORD_DEFLOCATION
:
176 return decodeRecord(R
, I
->DefLoc
, Blob
);
177 case RECORD_LOCATION
:
178 return decodeRecord(R
, I
->Loc
, Blob
);
179 case RECORD_TAG_TYPE
:
180 return decodeRecord(R
, I
->TagType
, Blob
);
181 case RECORD_IS_TYPE_DEF
:
182 return decodeRecord(R
, I
->IsTypeDef
, Blob
);
184 return llvm::createStringError(llvm::inconvertibleErrorCode(),
185 "invalid field for RecordInfo");
189 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
192 case BASE_RECORD_USR
:
193 return decodeRecord(R
, I
->USR
, Blob
);
194 case BASE_RECORD_NAME
:
195 return decodeRecord(R
, I
->Name
, Blob
);
196 case BASE_RECORD_PATH
:
197 return decodeRecord(R
, I
->Path
, Blob
);
198 case BASE_RECORD_TAG_TYPE
:
199 return decodeRecord(R
, I
->TagType
, Blob
);
200 case BASE_RECORD_IS_VIRTUAL
:
201 return decodeRecord(R
, I
->IsVirtual
, Blob
);
202 case BASE_RECORD_ACCESS
:
203 return decodeRecord(R
, I
->Access
, Blob
);
204 case BASE_RECORD_IS_PARENT
:
205 return decodeRecord(R
, I
->IsParent
, Blob
);
207 return llvm::createStringError(llvm::inconvertibleErrorCode(),
208 "invalid field for BaseRecordInfo");
212 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
216 return decodeRecord(R
, I
->USR
, Blob
);
218 return decodeRecord(R
, I
->Name
, Blob
);
219 case ENUM_DEFLOCATION
:
220 return decodeRecord(R
, I
->DefLoc
, Blob
);
222 return decodeRecord(R
, I
->Loc
, Blob
);
224 return decodeRecord(R
, I
->Scoped
, Blob
);
226 return llvm::createStringError(llvm::inconvertibleErrorCode(),
227 "invalid field for EnumInfo");
231 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
235 return decodeRecord(R
, I
->USR
, Blob
);
237 return decodeRecord(R
, I
->Name
, Blob
);
238 case TYPEDEF_DEFLOCATION
:
239 return decodeRecord(R
, I
->DefLoc
, Blob
);
240 case TYPEDEF_IS_USING
:
241 return decodeRecord(R
, I
->IsUsing
, Blob
);
243 return llvm::createStringError(llvm::inconvertibleErrorCode(),
244 "invalid field for TypedefInfo");
248 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
251 case ENUM_VALUE_NAME
:
252 return decodeRecord(R
, I
->Name
, Blob
);
253 case ENUM_VALUE_VALUE
:
254 return decodeRecord(R
, I
->Value
, Blob
);
255 case ENUM_VALUE_EXPR
:
256 return decodeRecord(R
, I
->ValueExpr
, Blob
);
258 return llvm::createStringError(llvm::inconvertibleErrorCode(),
259 "invalid field for EnumValueInfo");
263 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
267 return decodeRecord(R
, I
->USR
, Blob
);
269 return decodeRecord(R
, I
->Name
, Blob
);
270 case FUNCTION_DEFLOCATION
:
271 return decodeRecord(R
, I
->DefLoc
, Blob
);
272 case FUNCTION_LOCATION
:
273 return decodeRecord(R
, I
->Loc
, Blob
);
274 case FUNCTION_ACCESS
:
275 return decodeRecord(R
, I
->Access
, Blob
);
276 case FUNCTION_IS_METHOD
:
277 return decodeRecord(R
, I
->IsMethod
, Blob
);
279 return llvm::createStringError(llvm::inconvertibleErrorCode(),
280 "invalid field for FunctionInfo");
284 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
286 return llvm::Error::success();
289 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
292 case FIELD_TYPE_NAME
:
293 return decodeRecord(R
, I
->Name
, Blob
);
294 case FIELD_DEFAULT_VALUE
:
295 return decodeRecord(R
, I
->DefaultValue
, Blob
);
297 return llvm::createStringError(llvm::inconvertibleErrorCode(),
298 "invalid field for TypeInfo");
302 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
305 case MEMBER_TYPE_NAME
:
306 return decodeRecord(R
, I
->Name
, Blob
);
307 case MEMBER_TYPE_ACCESS
:
308 return decodeRecord(R
, I
->Access
, Blob
);
310 return llvm::createStringError(llvm::inconvertibleErrorCode(),
311 "invalid field for MemberTypeInfo");
315 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
319 return decodeRecord(R
, I
->Kind
, Blob
);
321 return decodeRecord(R
, I
->Text
, Blob
);
323 return decodeRecord(R
, I
->Name
, Blob
);
324 case COMMENT_DIRECTION
:
325 return decodeRecord(R
, I
->Direction
, Blob
);
326 case COMMENT_PARAMNAME
:
327 return decodeRecord(R
, I
->ParamName
, Blob
);
328 case COMMENT_CLOSENAME
:
329 return decodeRecord(R
, I
->CloseName
, Blob
);
330 case COMMENT_ATTRKEY
:
331 return decodeRecord(R
, I
->AttrKeys
, Blob
);
332 case COMMENT_ATTRVAL
:
333 return decodeRecord(R
, I
->AttrValues
, Blob
);
335 return decodeRecord(R
, I
->Args
, Blob
);
336 case COMMENT_SELFCLOSING
:
337 return decodeRecord(R
, I
->SelfClosing
, Blob
);
338 case COMMENT_EXPLICIT
:
339 return decodeRecord(R
, I
->Explicit
, Blob
);
341 return llvm::createStringError(llvm::inconvertibleErrorCode(),
342 "invalid field for CommentInfo");
346 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
347 Reference
*I
, FieldId
&F
) {
350 return decodeRecord(R
, I
->USR
, Blob
);
352 return decodeRecord(R
, I
->Name
, Blob
);
353 case REFERENCE_QUAL_NAME
:
354 return decodeRecord(R
, I
->QualName
, Blob
);
356 return decodeRecord(R
, I
->RefType
, Blob
);
358 return decodeRecord(R
, I
->Path
, Blob
);
359 case REFERENCE_FIELD
:
360 return decodeRecord(R
, F
, Blob
);
362 return llvm::createStringError(llvm::inconvertibleErrorCode(),
363 "invalid field for Reference");
367 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
369 // Currently there are no child records of TemplateInfo (only child blocks).
370 return llvm::createStringError(llvm::inconvertibleErrorCode(),
371 "invalid field for TemplateParamInfo");
374 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
375 TemplateSpecializationInfo
*I
) {
376 if (ID
== TEMPLATE_SPECIALIZATION_OF
)
377 return decodeRecord(R
, I
->SpecializationOf
, Blob
);
378 return llvm::createStringError(llvm::inconvertibleErrorCode(),
379 "invalid field for TemplateParamInfo");
382 llvm::Error
parseRecord(const Record
&R
, unsigned ID
, llvm::StringRef Blob
,
383 TemplateParamInfo
*I
) {
384 if (ID
== TEMPLATE_PARAM_CONTENTS
)
385 return decodeRecord(R
, I
->Contents
, Blob
);
386 return llvm::createStringError(llvm::inconvertibleErrorCode(),
387 "invalid field for TemplateParamInfo");
390 template <typename T
> llvm::Expected
<CommentInfo
*> getCommentInfo(T I
) {
391 return llvm::createStringError(llvm::inconvertibleErrorCode(),
392 "invalid type cannot contain CommentInfo");
395 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(FunctionInfo
*I
) {
396 return &I
->Description
.emplace_back();
399 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(NamespaceInfo
*I
) {
400 return &I
->Description
.emplace_back();
403 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(RecordInfo
*I
) {
404 return &I
->Description
.emplace_back();
407 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(MemberTypeInfo
*I
) {
408 return &I
->Description
.emplace_back();
411 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(EnumInfo
*I
) {
412 return &I
->Description
.emplace_back();
415 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(TypedefInfo
*I
) {
416 return &I
->Description
.emplace_back();
419 template <> llvm::Expected
<CommentInfo
*> getCommentInfo(CommentInfo
*I
) {
420 I
->Children
.emplace_back(std::make_unique
<CommentInfo
>());
421 return I
->Children
.back().get();
425 llvm::Expected
<CommentInfo
*> getCommentInfo(std::unique_ptr
<CommentInfo
> &I
) {
426 return getCommentInfo(I
.get());
429 // When readSubBlock encounters a TypeInfo sub-block, it calls addTypeInfo on
430 // the parent block to set it. The template specializations define what to do
431 // for each supported parent block.
432 template <typename T
, typename TTypeInfo
>
433 llvm::Error
addTypeInfo(T I
, TTypeInfo
&&TI
) {
434 return llvm::createStringError(llvm::inconvertibleErrorCode(),
435 "invalid type cannot contain TypeInfo");
438 template <> llvm::Error
addTypeInfo(RecordInfo
*I
, MemberTypeInfo
&&T
) {
439 I
->Members
.emplace_back(std::move(T
));
440 return llvm::Error::success();
443 template <> llvm::Error
addTypeInfo(BaseRecordInfo
*I
, MemberTypeInfo
&&T
) {
444 I
->Members
.emplace_back(std::move(T
));
445 return llvm::Error::success();
448 template <> llvm::Error
addTypeInfo(FunctionInfo
*I
, TypeInfo
&&T
) {
449 I
->ReturnType
= std::move(T
);
450 return llvm::Error::success();
453 template <> llvm::Error
addTypeInfo(FunctionInfo
*I
, FieldTypeInfo
&&T
) {
454 I
->Params
.emplace_back(std::move(T
));
455 return llvm::Error::success();
458 template <> llvm::Error
addTypeInfo(EnumInfo
*I
, TypeInfo
&&T
) {
459 I
->BaseType
= std::move(T
);
460 return llvm::Error::success();
463 template <> llvm::Error
addTypeInfo(TypedefInfo
*I
, TypeInfo
&&T
) {
464 I
->Underlying
= std::move(T
);
465 return llvm::Error::success();
468 template <typename T
> llvm::Error
addReference(T I
, Reference
&&R
, FieldId F
) {
469 return llvm::createStringError(llvm::inconvertibleErrorCode(),
470 "invalid type cannot contain Reference");
473 template <> llvm::Error
addReference(TypeInfo
*I
, Reference
&&R
, FieldId F
) {
475 case FieldId::F_type
:
476 I
->Type
= std::move(R
);
477 return llvm::Error::success();
479 return llvm::createStringError(llvm::inconvertibleErrorCode(),
480 "invalid type cannot contain Reference");
485 llvm::Error
addReference(FieldTypeInfo
*I
, Reference
&&R
, FieldId F
) {
487 case FieldId::F_type
:
488 I
->Type
= std::move(R
);
489 return llvm::Error::success();
491 return llvm::createStringError(llvm::inconvertibleErrorCode(),
492 "invalid type cannot contain Reference");
497 llvm::Error
addReference(MemberTypeInfo
*I
, Reference
&&R
, FieldId F
) {
499 case FieldId::F_type
:
500 I
->Type
= std::move(R
);
501 return llvm::Error::success();
503 return llvm::createStringError(llvm::inconvertibleErrorCode(),
504 "invalid type cannot contain Reference");
508 template <> llvm::Error
addReference(EnumInfo
*I
, Reference
&&R
, FieldId F
) {
510 case FieldId::F_namespace
:
511 I
->Namespace
.emplace_back(std::move(R
));
512 return llvm::Error::success();
514 return llvm::createStringError(llvm::inconvertibleErrorCode(),
515 "invalid type cannot contain Reference");
519 template <> llvm::Error
addReference(TypedefInfo
*I
, Reference
&&R
, FieldId F
) {
521 case FieldId::F_namespace
:
522 I
->Namespace
.emplace_back(std::move(R
));
523 return llvm::Error::success();
525 return llvm::createStringError(llvm::inconvertibleErrorCode(),
526 "invalid type cannot contain Reference");
531 llvm::Error
addReference(NamespaceInfo
*I
, Reference
&&R
, FieldId F
) {
533 case FieldId::F_namespace
:
534 I
->Namespace
.emplace_back(std::move(R
));
535 return llvm::Error::success();
536 case FieldId::F_child_namespace
:
537 I
->Children
.Namespaces
.emplace_back(std::move(R
));
538 return llvm::Error::success();
539 case FieldId::F_child_record
:
540 I
->Children
.Records
.emplace_back(std::move(R
));
541 return llvm::Error::success();
543 return llvm::createStringError(llvm::inconvertibleErrorCode(),
544 "invalid type cannot contain Reference");
549 llvm::Error
addReference(FunctionInfo
*I
, Reference
&&R
, FieldId F
) {
551 case FieldId::F_namespace
:
552 I
->Namespace
.emplace_back(std::move(R
));
553 return llvm::Error::success();
554 case FieldId::F_parent
:
555 I
->Parent
= std::move(R
);
556 return llvm::Error::success();
558 return llvm::createStringError(llvm::inconvertibleErrorCode(),
559 "invalid type cannot contain Reference");
563 template <> llvm::Error
addReference(RecordInfo
*I
, Reference
&&R
, FieldId F
) {
565 case FieldId::F_namespace
:
566 I
->Namespace
.emplace_back(std::move(R
));
567 return llvm::Error::success();
568 case FieldId::F_parent
:
569 I
->Parents
.emplace_back(std::move(R
));
570 return llvm::Error::success();
571 case FieldId::F_vparent
:
572 I
->VirtualParents
.emplace_back(std::move(R
));
573 return llvm::Error::success();
574 case FieldId::F_child_record
:
575 I
->Children
.Records
.emplace_back(std::move(R
));
576 return llvm::Error::success();
578 return llvm::createStringError(llvm::inconvertibleErrorCode(),
579 "invalid type cannot contain Reference");
583 template <typename T
, typename ChildInfoType
>
584 void addChild(T I
, ChildInfoType
&&R
) {
585 llvm::errs() << "invalid child type for info";
589 // Namespace children:
590 template <> void addChild(NamespaceInfo
*I
, FunctionInfo
&&R
) {
591 I
->Children
.Functions
.emplace_back(std::move(R
));
593 template <> void addChild(NamespaceInfo
*I
, EnumInfo
&&R
) {
594 I
->Children
.Enums
.emplace_back(std::move(R
));
596 template <> void addChild(NamespaceInfo
*I
, TypedefInfo
&&R
) {
597 I
->Children
.Typedefs
.emplace_back(std::move(R
));
601 template <> void addChild(RecordInfo
*I
, FunctionInfo
&&R
) {
602 I
->Children
.Functions
.emplace_back(std::move(R
));
604 template <> void addChild(RecordInfo
*I
, EnumInfo
&&R
) {
605 I
->Children
.Enums
.emplace_back(std::move(R
));
607 template <> void addChild(RecordInfo
*I
, TypedefInfo
&&R
) {
608 I
->Children
.Typedefs
.emplace_back(std::move(R
));
611 // Other types of children:
612 template <> void addChild(EnumInfo
*I
, EnumValueInfo
&&R
) {
613 I
->Members
.emplace_back(std::move(R
));
615 template <> void addChild(RecordInfo
*I
, BaseRecordInfo
&&R
) {
616 I
->Bases
.emplace_back(std::move(R
));
618 template <> void addChild(BaseRecordInfo
*I
, FunctionInfo
&&R
) {
619 I
->Children
.Functions
.emplace_back(std::move(R
));
622 // TemplateParam children. These go into either a TemplateInfo (for template
623 // parameters) or TemplateSpecializationInfo (for the specialization's
625 template <typename T
> void addTemplateParam(T I
, TemplateParamInfo
&&P
) {
626 llvm::errs() << "invalid container for template parameter";
629 template <> void addTemplateParam(TemplateInfo
*I
, TemplateParamInfo
&&P
) {
630 I
->Params
.emplace_back(std::move(P
));
633 void addTemplateParam(TemplateSpecializationInfo
*I
, TemplateParamInfo
&&P
) {
634 I
->Params
.emplace_back(std::move(P
));
637 // Template info. These apply to either records or functions.
638 template <typename T
> void addTemplate(T I
, TemplateInfo
&&P
) {
639 llvm::errs() << "invalid container for template info";
642 template <> void addTemplate(RecordInfo
*I
, TemplateInfo
&&P
) {
643 I
->Template
.emplace(std::move(P
));
645 template <> void addTemplate(FunctionInfo
*I
, TemplateInfo
&&P
) {
646 I
->Template
.emplace(std::move(P
));
649 // Template specializations go only into template records.
650 template <typename T
>
651 void addTemplateSpecialization(T I
, TemplateSpecializationInfo
&&TSI
) {
652 llvm::errs() << "invalid container for template specialization info";
656 void addTemplateSpecialization(TemplateInfo
*I
,
657 TemplateSpecializationInfo
&&TSI
) {
658 I
->Specialization
.emplace(std::move(TSI
));
661 // Read records from bitcode into a given info.
662 template <typename T
>
663 llvm::Error
ClangDocBitcodeReader::readRecord(unsigned ID
, T I
) {
665 llvm::StringRef Blob
;
666 llvm::Expected
<unsigned> MaybeRecID
= Stream
.readRecord(ID
, R
, &Blob
);
668 return MaybeRecID
.takeError();
669 return parseRecord(R
, MaybeRecID
.get(), Blob
, I
);
673 llvm::Error
ClangDocBitcodeReader::readRecord(unsigned ID
, Reference
*I
) {
675 llvm::StringRef Blob
;
676 llvm::Expected
<unsigned> MaybeRecID
= Stream
.readRecord(ID
, R
, &Blob
);
678 return MaybeRecID
.takeError();
679 return parseRecord(R
, MaybeRecID
.get(), Blob
, I
, CurrentReferenceField
);
682 // Read a block of records into a single info.
683 template <typename T
>
684 llvm::Error
ClangDocBitcodeReader::readBlock(unsigned ID
, T I
) {
685 if (llvm::Error Err
= Stream
.EnterSubBlock(ID
))
689 unsigned BlockOrCode
= 0;
690 Cursor Res
= skipUntilRecordOrBlock(BlockOrCode
);
693 case Cursor::BadBlock
:
694 return llvm::createStringError(llvm::inconvertibleErrorCode(),
696 case Cursor::BlockEnd
:
697 return llvm::Error::success();
698 case Cursor::BlockBegin
:
699 if (llvm::Error Err
= readSubBlock(BlockOrCode
, I
)) {
700 if (llvm::Error Skipped
= Stream
.SkipBlock())
701 return joinErrors(std::move(Err
), std::move(Skipped
));
708 if (auto Err
= readRecord(BlockOrCode
, I
))
713 template <typename T
>
714 llvm::Error
ClangDocBitcodeReader::readSubBlock(unsigned ID
, T I
) {
716 // Blocks can only have certain types of sub blocks.
717 case BI_COMMENT_BLOCK_ID
: {
718 auto Comment
= getCommentInfo(I
);
720 return Comment
.takeError();
721 if (auto Err
= readBlock(ID
, Comment
.get()))
723 return llvm::Error::success();
725 case BI_TYPE_BLOCK_ID
: {
727 if (auto Err
= readBlock(ID
, &TI
))
729 if (auto Err
= addTypeInfo(I
, std::move(TI
)))
731 return llvm::Error::success();
733 case BI_FIELD_TYPE_BLOCK_ID
: {
735 if (auto Err
= readBlock(ID
, &TI
))
737 if (auto Err
= addTypeInfo(I
, std::move(TI
)))
739 return llvm::Error::success();
741 case BI_MEMBER_TYPE_BLOCK_ID
: {
743 if (auto Err
= readBlock(ID
, &TI
))
745 if (auto Err
= addTypeInfo(I
, std::move(TI
)))
747 return llvm::Error::success();
749 case BI_REFERENCE_BLOCK_ID
: {
751 if (auto Err
= readBlock(ID
, &R
))
753 if (auto Err
= addReference(I
, std::move(R
), CurrentReferenceField
))
755 return llvm::Error::success();
757 case BI_FUNCTION_BLOCK_ID
: {
759 if (auto Err
= readBlock(ID
, &F
))
761 addChild(I
, std::move(F
));
762 return llvm::Error::success();
764 case BI_BASE_RECORD_BLOCK_ID
: {
766 if (auto Err
= readBlock(ID
, &BR
))
768 addChild(I
, std::move(BR
));
769 return llvm::Error::success();
771 case BI_ENUM_BLOCK_ID
: {
773 if (auto Err
= readBlock(ID
, &E
))
775 addChild(I
, std::move(E
));
776 return llvm::Error::success();
778 case BI_ENUM_VALUE_BLOCK_ID
: {
780 if (auto Err
= readBlock(ID
, &EV
))
782 addChild(I
, std::move(EV
));
783 return llvm::Error::success();
785 case BI_TEMPLATE_BLOCK_ID
: {
787 if (auto Err
= readBlock(ID
, &TI
))
789 addTemplate(I
, std::move(TI
));
790 return llvm::Error::success();
792 case BI_TEMPLATE_SPECIALIZATION_BLOCK_ID
: {
793 TemplateSpecializationInfo TSI
;
794 if (auto Err
= readBlock(ID
, &TSI
))
796 addTemplateSpecialization(I
, std::move(TSI
));
797 return llvm::Error::success();
799 case BI_TEMPLATE_PARAM_BLOCK_ID
: {
800 TemplateParamInfo TPI
;
801 if (auto Err
= readBlock(ID
, &TPI
))
803 addTemplateParam(I
, std::move(TPI
));
804 return llvm::Error::success();
806 case BI_TYPEDEF_BLOCK_ID
: {
808 if (auto Err
= readBlock(ID
, &TI
))
810 addChild(I
, std::move(TI
));
811 return llvm::Error::success();
814 return llvm::createStringError(llvm::inconvertibleErrorCode(),
815 "invalid subblock type");
819 ClangDocBitcodeReader::Cursor
820 ClangDocBitcodeReader::skipUntilRecordOrBlock(unsigned &BlockOrRecordID
) {
823 while (!Stream
.AtEndOfStream()) {
824 Expected
<unsigned> MaybeCode
= Stream
.ReadCode();
826 // FIXME this drops the error on the floor.
827 consumeError(MaybeCode
.takeError());
828 return Cursor::BadBlock
;
831 unsigned Code
= MaybeCode
.get();
832 if (Code
>= static_cast<unsigned>(llvm::bitc::FIRST_APPLICATION_ABBREV
)) {
833 BlockOrRecordID
= Code
;
834 return Cursor::Record
;
836 switch (static_cast<llvm::bitc::FixedAbbrevIDs
>(Code
)) {
837 case llvm::bitc::ENTER_SUBBLOCK
:
838 if (Expected
<unsigned> MaybeID
= Stream
.ReadSubBlockID())
839 BlockOrRecordID
= MaybeID
.get();
841 // FIXME this drops the error on the floor.
842 consumeError(MaybeID
.takeError());
844 return Cursor::BlockBegin
;
845 case llvm::bitc::END_BLOCK
:
846 if (Stream
.ReadBlockEnd())
847 return Cursor::BadBlock
;
848 return Cursor::BlockEnd
;
849 case llvm::bitc::DEFINE_ABBREV
:
850 if (llvm::Error Err
= Stream
.ReadAbbrevRecord()) {
851 // FIXME this drops the error on the floor.
852 consumeError(std::move(Err
));
855 case llvm::bitc::UNABBREV_RECORD
:
856 return Cursor::BadBlock
;
857 case llvm::bitc::FIRST_APPLICATION_ABBREV
:
858 llvm_unreachable("Unexpected abbrev id.");
861 llvm_unreachable("Premature stream end.");
864 llvm::Error
ClangDocBitcodeReader::validateStream() {
865 if (Stream
.AtEndOfStream())
866 return llvm::createStringError(llvm::inconvertibleErrorCode(),
867 "premature end of stream");
869 // Sniff for the signature.
870 for (int Idx
= 0; Idx
!= 4; ++Idx
) {
871 Expected
<llvm::SimpleBitstreamCursor::word_t
> MaybeRead
= Stream
.Read(8);
873 return MaybeRead
.takeError();
874 else if (MaybeRead
.get() != BitCodeConstants::Signature
[Idx
])
875 return llvm::createStringError(llvm::inconvertibleErrorCode(),
876 "invalid bitcode signature");
878 return llvm::Error::success();
881 llvm::Error
ClangDocBitcodeReader::readBlockInfoBlock() {
882 Expected
<std::optional
<llvm::BitstreamBlockInfo
>> MaybeBlockInfo
=
883 Stream
.ReadBlockInfoBlock();
885 return MaybeBlockInfo
.takeError();
887 BlockInfo
= MaybeBlockInfo
.get();
889 return llvm::createStringError(llvm::inconvertibleErrorCode(),
890 "unable to parse BlockInfoBlock");
891 Stream
.setBlockInfo(&*BlockInfo
);
892 return llvm::Error::success();
895 template <typename T
>
896 llvm::Expected
<std::unique_ptr
<Info
>>
897 ClangDocBitcodeReader::createInfo(unsigned ID
) {
898 std::unique_ptr
<Info
> I
= std::make_unique
<T
>();
899 if (auto Err
= readBlock(ID
, static_cast<T
*>(I
.get())))
900 return std::move(Err
);
901 return std::unique_ptr
<Info
>{std::move(I
)};
904 llvm::Expected
<std::unique_ptr
<Info
>>
905 ClangDocBitcodeReader::readBlockToInfo(unsigned ID
) {
907 case BI_NAMESPACE_BLOCK_ID
:
908 return createInfo
<NamespaceInfo
>(ID
);
909 case BI_RECORD_BLOCK_ID
:
910 return createInfo
<RecordInfo
>(ID
);
911 case BI_ENUM_BLOCK_ID
:
912 return createInfo
<EnumInfo
>(ID
);
913 case BI_TYPEDEF_BLOCK_ID
:
914 return createInfo
<TypedefInfo
>(ID
);
915 case BI_FUNCTION_BLOCK_ID
:
916 return createInfo
<FunctionInfo
>(ID
);
918 return llvm::createStringError(llvm::inconvertibleErrorCode(),
919 "cannot create info");
924 llvm::Expected
<std::vector
<std::unique_ptr
<Info
>>>
925 ClangDocBitcodeReader::readBitcode() {
926 std::vector
<std::unique_ptr
<Info
>> Infos
;
927 if (auto Err
= validateStream())
928 return std::move(Err
);
930 // Read the top level blocks.
931 while (!Stream
.AtEndOfStream()) {
932 Expected
<unsigned> MaybeCode
= Stream
.ReadCode();
934 return MaybeCode
.takeError();
935 if (MaybeCode
.get() != llvm::bitc::ENTER_SUBBLOCK
)
936 return llvm::createStringError(llvm::inconvertibleErrorCode(),
937 "no blocks in input");
938 Expected
<unsigned> MaybeID
= Stream
.ReadSubBlockID();
940 return MaybeID
.takeError();
941 unsigned ID
= MaybeID
.get();
943 // NamedType and Comment blocks should not appear at the top level
944 case BI_TYPE_BLOCK_ID
:
945 case BI_FIELD_TYPE_BLOCK_ID
:
946 case BI_MEMBER_TYPE_BLOCK_ID
:
947 case BI_COMMENT_BLOCK_ID
:
948 case BI_REFERENCE_BLOCK_ID
:
949 return llvm::createStringError(llvm::inconvertibleErrorCode(),
950 "invalid top level block");
951 case BI_NAMESPACE_BLOCK_ID
:
952 case BI_RECORD_BLOCK_ID
:
953 case BI_ENUM_BLOCK_ID
:
954 case BI_TYPEDEF_BLOCK_ID
:
955 case BI_FUNCTION_BLOCK_ID
: {
956 auto InfoOrErr
= readBlockToInfo(ID
);
958 return InfoOrErr
.takeError();
959 Infos
.emplace_back(std::move(InfoOrErr
.get()));
962 case BI_VERSION_BLOCK_ID
:
963 if (auto Err
= readBlock(ID
, VersionNumber
))
964 return std::move(Err
);
966 case llvm::bitc::BLOCKINFO_BLOCK_ID
:
967 if (auto Err
= readBlockInfoBlock())
968 return std::move(Err
);
971 if (llvm::Error Err
= Stream
.SkipBlock()) {
972 // FIXME this drops the error on the floor.
973 consumeError(std::move(Err
));
978 return std::move(Infos
);