56#include "llvm/ADT/APSInt.h"
57#include "llvm/ADT/ArrayRef.h"
58#include "llvm/ADT/DenseMap.h"
59#include "llvm/ADT/STLExtras.h"
60#include "llvm/ADT/ScopeExit.h"
61#include "llvm/ADT/SmallVector.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
64#include "llvm/Support/MemoryBuffer.h"
75 using llvm::make_error;
90 return "NameConflict";
92 return "UnsupportedConstruct";
94 return "Unknown error";
96 llvm_unreachable(
"Invalid error code.");
97 return "Invalid error code.";
103 llvm_unreachable(
"Function not implemented.");
112 for (
auto *R :
D->getFirstDecl()->
redecls()) {
113 if (R !=
D->getFirstDecl())
114 Redecls.push_back(R);
116 Redecls.push_back(
D->getFirstDecl());
117 std::reverse(Redecls.begin(), Redecls.end());
122 if (
auto *FD = dyn_cast<FunctionDecl>(
D))
123 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
124 if (
auto *VD = dyn_cast<VarDecl>(
D))
125 return getCanonicalForwardRedeclChain<VarDecl>(VD);
126 if (
auto *TD = dyn_cast<TagDecl>(
D))
127 return getCanonicalForwardRedeclChain<TagDecl>(TD);
128 llvm_unreachable(
"Bad declaration kind");
149 bool const IgnoreChildErrors;
153 : FromDC(FromDC), IgnoreChildErrors(!
isa<
TagDecl>(FromDC)) {}
163 if (ChildErr && !IgnoreChildErrors)
164 ResultErr = joinErrors(std::move(ResultErr), std::move(ChildErr));
166 consumeError(std::move(ChildErr));
172 if (!IgnoreChildErrors || !FromDC)
180 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
184 template <
typename ImportT>
185 [[nodiscard]] Error importInto(ImportT &To,
const ImportT &From) {
190 template <
typename ImportT>
191 [[nodiscard]] Error importInto(ImportT *&To, ImportT *From) {
192 auto ToOrErr = Importer.
Import(From);
194 To = cast_or_null<ImportT>(*ToOrErr);
195 return ToOrErr.takeError();
200 template <
typename T>
204 auto ToOrErr = Importer.
Import(From);
206 return ToOrErr.takeError();
207 return cast_or_null<T>(*ToOrErr);
210 template <
typename T>
211 auto import(
const T *From) {
212 return import(
const_cast<T *
>(From));
216 template <
typename T>
218 return Importer.
Import(From);
222 template <
typename T>
226 return import(*From);
233 template <
typename ToDeclT>
struct CallOverloadedCreateFun {
234 template <
typename... Args>
decltype(
auto)
operator()(Args &&... args) {
235 return ToDeclT::Create(std::forward<Args>(args)...);
245 template <
typename ToDeclT,
typename FromDeclT,
typename... Args>
246 [[nodiscard]]
bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
251 CallOverloadedCreateFun<ToDeclT> OC;
252 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
253 std::forward<Args>(args)...);
260 template <
typename NewDeclT,
typename ToDeclT,
typename FromDeclT,
262 [[nodiscard]]
bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
264 CallOverloadedCreateFun<NewDeclT> OC;
265 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
266 std::forward<Args>(args)...);
270 template <
typename ToDeclT,
typename CreateFunT,
typename FromDeclT,
273 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
274 FromDeclT *FromD, Args &&...args) {
282 ToD = CreateFun(std::forward<Args>(args)...);
285 Importer.SharedState->markAsNewDecl(ToD);
286 InitializeImportedDecl(FromD, ToD);
290 void InitializeImportedDecl(
Decl *FromD,
Decl *ToD) {
294 if (FromD->isImplicit())
302 if (
D->doesThisDeclarationHaveABody() &&
308 void addDeclToContexts(
Decl *FromD,
Decl *ToD) {
313 if (!FromD->getDescribedTemplate() &&
320 DeclContext *FromLexicalDC = FromD->getLexicalDeclContext();
324 bool Visible =
false;
337 if (
auto *FromNamed = dyn_cast<NamedDecl>(FromD)) {
338 auto *ToNamed = cast<NamedDecl>(ToD);
340 FromDC->
lookup(FromNamed->getDeclName());
341 if (llvm::is_contained(FromLookup, FromNamed))
358 updateLookupTableForTemplateParameters(
362 template <
typename TemplateParmDeclT>
363 void tryUpdateTemplateParmDeclInheritedFrom(
NamedDecl *RecentParm,
365 if (
auto *ParmT = dyn_cast<TemplateParmDeclT>(RecentParm)) {
366 if (ParmT->hasDefaultArgument()) {
367 auto *
P = cast<TemplateParmDeclT>(NewParm);
368 P->removeDefaultArgument();
369 P->setInheritedDefaultArgument(Importer.ToContext, ParmT);
397 void updateTemplateParametersInheritedFrom(
400 for (
auto [Idx, Param] : enumerate(RecentParams)) {
401 tryUpdateTemplateParmDeclInheritedFrom<NonTypeTemplateParmDecl>(
403 tryUpdateTemplateParmDeclInheritedFrom<TemplateTypeParmDecl>(
405 tryUpdateTemplateParmDeclInheritedFrom<TemplateTemplateParmDecl>(
419#define TYPE(Class, Base) \
420 ExpectedType Visit##Class##Type(const Class##Type *T);
421#include "clang/AST/TypeNodes.inc"
479 template <
typename InContainerTy>
483 template<
typename InContainerTy>
490 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
495 template <
typename DeclTy>
508 template <
typename T>
512 bool IgnoreTemplateParmDepth =
false);
704 Err = MaybeVal.takeError();
710 template<
typename IIter,
typename OIter>
712 using ItemT = std::remove_reference_t<
decltype(*Obegin)>;
713 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
716 return ToOrErr.takeError();
719 return Error::success();
726 template<
typename InContainerTy,
typename OutContainerTy>
728 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
730 InContainer.begin(), InContainer.end(), OutContainer.begin());
733 template<
typename InContainerTy,
typename OIter>
750template <
typename InContainerTy>
754 auto ToLAngleLocOrErr =
import(FromLAngleLoc);
755 if (!ToLAngleLocOrErr)
756 return ToLAngleLocOrErr.takeError();
757 auto ToRAngleLocOrErr =
import(FromRAngleLoc);
758 if (!ToRAngleLocOrErr)
759 return ToRAngleLocOrErr.takeError();
765 return Error::success();
769Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
780 return ImportTemplateArgumentListInfo(
781 From.LAngleLoc, From.RAngleLoc, From.arguments(),
Result);
793 if (Error Err = importInto(std::get<0>(
Result), FTSInfo->getTemplate()))
794 return std::move(Err);
799 return std::move(Err);
809 return std::move(Err);
812 if (!ToRequiresClause)
813 return ToRequiresClause.takeError();
816 if (!ToTemplateLocOrErr)
817 return ToTemplateLocOrErr.takeError();
819 if (!ToLAngleLocOrErr)
820 return ToLAngleLocOrErr.takeError();
822 if (!ToRAngleLocOrErr)
823 return ToRAngleLocOrErr.takeError();
844 return ToTypeOrErr.takeError();
852 return ToTypeOrErr.takeError();
859 return ToOrErr.takeError();
862 return ToTypeOrErr.takeError();
863 return TemplateArgument(dyn_cast<ValueDecl>((*ToOrErr)->getCanonicalDecl()),
870 return ToTypeOrErr.takeError();
878 return ToTypeOrErr.takeError();
881 return ToValueOrErr.takeError();
888 if (!ToTemplateOrErr)
889 return ToTemplateOrErr.takeError();
897 if (!ToTemplateOrErr)
898 return ToTemplateOrErr.takeError();
908 return ToExpr.takeError();
914 return std::move(Err);
921 llvm_unreachable(
"Invalid template argument kind");
929 return ArgOrErr.takeError();
938 return E.takeError();
944 return TSIOrErr.takeError();
946 auto ToTemplateQualifierLocOrErr =
948 if (!ToTemplateQualifierLocOrErr)
949 return ToTemplateQualifierLocOrErr.takeError();
951 if (!ToTemplateNameLocOrErr)
952 return ToTemplateNameLocOrErr.takeError();
953 auto ToTemplateEllipsisLocOrErr =
955 if (!ToTemplateEllipsisLocOrErr)
956 return ToTemplateEllipsisLocOrErr.takeError();
959 *ToTemplateNameLocOrErr, *ToTemplateEllipsisLocOrErr);
969 size_t NumDecls = DG.
end() - DG.
begin();
971 ToDecls.reserve(NumDecls);
972 for (
Decl *FromD : DG) {
973 if (
auto ToDOrErr =
import(FromD))
974 ToDecls.push_back(*ToDOrErr);
976 return ToDOrErr.takeError();
986 if (
D.isFieldDesignator()) {
991 return ToDotLocOrErr.takeError();
994 if (!ToFieldLocOrErr)
995 return ToFieldLocOrErr.takeError();
998 ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
1001 ExpectedSLoc ToLBracketLocOrErr =
import(
D.getLBracketLoc());
1002 if (!ToLBracketLocOrErr)
1003 return ToLBracketLocOrErr.takeError();
1005 ExpectedSLoc ToRBracketLocOrErr =
import(
D.getRBracketLoc());
1006 if (!ToRBracketLocOrErr)
1007 return ToRBracketLocOrErr.takeError();
1009 if (
D.isArrayDesignator())
1011 *ToLBracketLocOrErr,
1012 *ToRBracketLocOrErr);
1014 ExpectedSLoc ToEllipsisLocOrErr =
import(
D.getEllipsisLoc());
1015 if (!ToEllipsisLocOrErr)
1016 return ToEllipsisLocOrErr.takeError();
1018 assert(
D.isArrayRangeDesignator());
1020 D.getArrayIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
1021 *ToRBracketLocOrErr);
1026 Error Err = Error::success();
1029 auto ToConceptNameLoc =
1035 return std::move(Err);
1038 if (ASTTemplateArgs)
1040 return std::move(Err);
1058 return VarOrErr.takeError();
1063 return LocationOrErr.takeError();
1068 return std::move(Err);
1075template <
typename T>
1077 if (
Found->getLinkageInternal() != From->getLinkageInternal())
1080 if (From->hasExternalFormalLinkage())
1081 return Found->hasExternalFormalLinkage();
1084 if (From->isInAnonymousNamespace())
1085 return Found->isInAnonymousNamespace();
1087 return !
Found->isInAnonymousNamespace() &&
1088 !
Found->hasExternalFormalLinkage();
1108using namespace clang;
1117 ExpectedType UnderlyingTypeOrErr =
import(
T->getValueType());
1118 if (!UnderlyingTypeOrErr)
1119 return UnderlyingTypeOrErr.takeError();
1125 switch (
T->getKind()) {
1126#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
1127 case BuiltinType::Id: \
1128 return Importer.getToContext().SingletonId;
1129#include "clang/Basic/OpenCLImageTypes.def"
1130#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
1131 case BuiltinType::Id: \
1132 return Importer.getToContext().Id##Ty;
1133#include "clang/Basic/OpenCLExtensionTypes.def"
1134#define SVE_TYPE(Name, Id, SingletonId) \
1135 case BuiltinType::Id: \
1136 return Importer.getToContext().SingletonId;
1137#include "clang/Basic/AArch64SVEACLETypes.def"
1138#define PPC_VECTOR_TYPE(Name, Id, Size) \
1139 case BuiltinType::Id: \
1140 return Importer.getToContext().Id##Ty;
1141#include "clang/Basic/PPCTypes.def"
1142#define RVV_TYPE(Name, Id, SingletonId) \
1143 case BuiltinType::Id: \
1144 return Importer.getToContext().SingletonId;
1145#include "clang/Basic/RISCVVTypes.def"
1146#define WASM_TYPE(Name, Id, SingletonId) \
1147 case BuiltinType::Id: \
1148 return Importer.getToContext().SingletonId;
1149#include "clang/Basic/WebAssemblyReferenceTypes.def"
1150#define AMDGPU_TYPE(Name, Id, SingletonId) \
1151 case BuiltinType::Id: \
1152 return Importer.getToContext().SingletonId;
1153#include "clang/Basic/AMDGPUTypes.def"
1154#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
1155 case BuiltinType::Id: \
1156 return Importer.getToContext().SingletonId;
1157#include "clang/Basic/HLSLIntangibleTypes.def"
1158#define SHARED_SINGLETON_TYPE(Expansion)
1159#define BUILTIN_TYPE(Id, SingletonId) \
1160 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1161#include "clang/AST/BuiltinTypes.def"
1169 case BuiltinType::Char_U:
1178 case BuiltinType::Char_S:
1187 case BuiltinType::WChar_S:
1188 case BuiltinType::WChar_U:
1194 llvm_unreachable(
"Invalid BuiltinType Kind!");
1198 ExpectedType ToOriginalTypeOrErr =
import(
T->getOriginalType());
1199 if (!ToOriginalTypeOrErr)
1200 return ToOriginalTypeOrErr.takeError();
1206 ExpectedType ToElementTypeOrErr =
import(
T->getElementType());
1207 if (!ToElementTypeOrErr)
1208 return ToElementTypeOrErr.takeError();
1215 if (!ToPointeeTypeOrErr)
1216 return ToPointeeTypeOrErr.takeError();
1224 if (!ToPointeeTypeOrErr)
1225 return ToPointeeTypeOrErr.takeError();
1233 ExpectedType ToPointeeTypeOrErr =
import(
T->getPointeeTypeAsWritten());
1234 if (!ToPointeeTypeOrErr)
1235 return ToPointeeTypeOrErr.takeError();
1243 ExpectedType ToPointeeTypeOrErr =
import(
T->getPointeeTypeAsWritten());
1244 if (!ToPointeeTypeOrErr)
1245 return ToPointeeTypeOrErr.takeError();
1254 if (!ToPointeeTypeOrErr)
1255 return ToPointeeTypeOrErr.takeError();
1258 if (!ClassTypeOrErr)
1259 return ClassTypeOrErr.takeError();
1267 Error Err = Error::success();
1271 return std::move(Err);
1274 ToElementType,
T->getSize(), ToSizeExpr,
T->getSizeModifier(),
1275 T->getIndexTypeCVRQualifiers());
1281 if (!ToArrayTypeOrErr)
1282 return ToArrayTypeOrErr.takeError();
1289 ExpectedType ToElementTypeOrErr =
import(
T->getElementType());
1290 if (!ToElementTypeOrErr)
1291 return ToElementTypeOrErr.takeError();
1294 T->getSizeModifier(),
1295 T->getIndexTypeCVRQualifiers());
1300 Error Err = Error::success();
1305 return std::move(Err);
1307 ToElementType, ToSizeExpr,
T->getSizeModifier(),
1308 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
1311ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
1313 Error Err = Error::success();
1318 return std::move(Err);
1323 ToElementType, ToSizeExpr,
T->getSizeModifier(),
1324 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
1327ExpectedType ASTNodeImporter::VisitDependentSizedExtVectorType(
1329 Error Err = Error::success();
1334 return std::move(Err);
1336 ToElementType, ToSizeExpr, ToAttrLoc);
1340 ExpectedType ToElementTypeOrErr =
import(
T->getElementType());
1341 if (!ToElementTypeOrErr)
1342 return ToElementTypeOrErr.takeError();
1345 T->getNumElements(),
1346 T->getVectorKind());
1350 ExpectedType ToElementTypeOrErr =
import(
T->getElementType());
1351 if (!ToElementTypeOrErr)
1352 return ToElementTypeOrErr.takeError();
1355 T->getNumElements());
1363 if (!ToReturnTypeOrErr)
1364 return ToReturnTypeOrErr.takeError();
1373 if (!ToReturnTypeOrErr)
1374 return ToReturnTypeOrErr.takeError();
1381 return TyOrErr.takeError();
1382 ArgTypes.push_back(*TyOrErr);
1390 return TyOrErr.takeError();
1391 ExceptionTypes.push_back(*TyOrErr);
1395 Error Err = Error::success();
1412 return std::move(Err);
1415 *ToReturnTypeOrErr, ArgTypes, ToEPI);
1420 Error Err = Error::success();
1422 auto ToPrevD =
importChecked(Err,
T->getDecl()->getPreviousDecl());
1424 return std::move(Err);
1427 ToD, cast_or_null<TypeDecl>(ToPrevD));
1432 if (!ToInnerTypeOrErr)
1433 return ToInnerTypeOrErr.takeError();
1443 return Pattern.takeError();
1446 return Index.takeError();
1453 return ToDeclOrErr.takeError();
1460 if (!ToUnderlyingTypeOrErr)
1461 return ToUnderlyingTypeOrErr.takeError();
1469 return ToExprOrErr.takeError();
1474 ExpectedType ToUnderlyingTypeOrErr =
import(
T->getUnmodifiedType());
1475 if (!ToUnderlyingTypeOrErr)
1476 return ToUnderlyingTypeOrErr.takeError();
1484 return FoundOrErr.takeError();
1486 if (!UnderlyingOrErr)
1487 return UnderlyingOrErr.takeError();
1496 return ToExprOrErr.takeError();
1498 ExpectedType ToUnderlyingTypeOrErr =
import(
T->getUnderlyingType());
1499 if (!ToUnderlyingTypeOrErr)
1500 return ToUnderlyingTypeOrErr.takeError();
1503 *ToExprOrErr, *ToUnderlyingTypeOrErr);
1509 if (!ToBaseTypeOrErr)
1510 return ToBaseTypeOrErr.takeError();
1512 ExpectedType ToUnderlyingTypeOrErr =
import(
T->getUnderlyingType());
1513 if (!ToUnderlyingTypeOrErr)
1514 return ToUnderlyingTypeOrErr.takeError();
1517 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr,
T->getUTTKind());
1522 ExpectedType ToDeducedTypeOrErr =
import(
T->getDeducedType());
1523 if (!ToDeducedTypeOrErr)
1524 return ToDeducedTypeOrErr.takeError();
1526 ExpectedDecl ToTypeConstraintConcept =
import(
T->getTypeConstraintConcept());
1527 if (!ToTypeConstraintConcept)
1528 return ToTypeConstraintConcept.takeError();
1533 return std::move(Err);
1536 *ToDeducedTypeOrErr,
T->getKeyword(),
false,
1537 false, cast_or_null<ConceptDecl>(*ToTypeConstraintConcept),
1541ExpectedType ASTNodeImporter::VisitDeducedTemplateSpecializationType(
1545 if (!ToTemplateNameOrErr)
1546 return ToTemplateNameOrErr.takeError();
1547 ExpectedType ToDeducedTypeOrErr =
import(
T->getDeducedType());
1548 if (!ToDeducedTypeOrErr)
1549 return ToDeducedTypeOrErr.takeError();
1555ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
1559 return ToDeclOrErr.takeError();
1563 const Type *Ty = (*ToDeclOrErr)->getTypeForDecl();
1564 assert(isa_and_nonnull<InjectedClassNameType>(Ty));
1571 return ToDeclOrErr.takeError();
1579 return ToDeclOrErr.takeError();
1585 ExpectedType ToModifiedTypeOrErr =
import(
T->getModifiedType());
1586 if (!ToModifiedTypeOrErr)
1587 return ToModifiedTypeOrErr.takeError();
1588 ExpectedType ToEquivalentTypeOrErr =
import(
T->getEquivalentType());
1589 if (!ToEquivalentTypeOrErr)
1590 return ToEquivalentTypeOrErr.takeError();
1593 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
1599 if (!ToWrappedTypeOrErr)
1600 return ToWrappedTypeOrErr.takeError();
1602 Error Err = Error::success();
1609 return ToDeclOrErr.takeError();
1610 CoupledDecls.emplace_back(*ToDeclOrErr, TI.isDeref());
1614 *ToWrappedTypeOrErr, CountExpr,
T->isCountInBytes(),
T->isOrNull(),
1615 ArrayRef(CoupledDecls.data(), CoupledDecls.size()));
1618ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
1622 return ToDeclOrErr.takeError();
1625 T->getDepth(),
T->getIndex(),
T->isParameterPack(), *ToDeclOrErr);
1628ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
1632 return ReplacedOrErr.takeError();
1634 ExpectedType ToReplacementTypeOrErr =
import(
T->getReplacementType());
1635 if (!ToReplacementTypeOrErr)
1636 return ToReplacementTypeOrErr.takeError();
1639 *ToReplacementTypeOrErr, *ReplacedOrErr,
T->getIndex(),
1643ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmPackType(
1647 return ReplacedOrErr.takeError();
1650 if (!ToArgumentPack)
1651 return ToArgumentPack.takeError();
1654 *ReplacedOrErr,
T->getIndex(),
T->getFinal(), *ToArgumentPack);
1657ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
1659 auto ToTemplateOrErr =
import(
T->getTemplateName());
1660 if (!ToTemplateOrErr)
1661 return ToTemplateOrErr.takeError();
1666 return std::move(Err);
1673 ToCanonType = *TyOrErr;
1675 return TyOrErr.takeError();
1684 auto ToQualifierOrErr =
import(
T->getQualifier());
1685 if (!ToQualifierOrErr)
1686 return ToQualifierOrErr.takeError();
1689 if (!ToNamedTypeOrErr)
1690 return ToNamedTypeOrErr.takeError();
1693 if (!ToOwnedTagDeclOrErr)
1694 return ToOwnedTagDeclOrErr.takeError();
1699 *ToOwnedTagDeclOrErr);
1705 if (!ToPatternOrErr)
1706 return ToPatternOrErr.takeError();
1709 T->getNumExpansions(),
1713ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
1715 auto ToQualifierOrErr =
import(
T->getQualifier());
1716 if (!ToQualifierOrErr)
1717 return ToQualifierOrErr.takeError();
1722 ToPack.reserve(
T->template_arguments().size());
1724 return std::move(Err);
1727 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
1732 auto ToQualifierOrErr =
import(
T->getQualifier());
1733 if (!ToQualifierOrErr)
1734 return ToQualifierOrErr.takeError();
1743 return TyOrErr.takeError();
1755 return ToDeclOrErr.takeError();
1762 if (!ToBaseTypeOrErr)
1763 return ToBaseTypeOrErr.takeError();
1766 for (
auto TypeArg :
T->getTypeArgsAsWritten()) {
1768 TypeArgs.push_back(*TyOrErr);
1770 return TyOrErr.takeError();
1774 for (
auto *
P :
T->quals()) {
1776 Protocols.push_back(*ProtocolOrErr);
1778 return ProtocolOrErr.takeError();
1784 T->isKindOfTypeAsWritten());
1790 if (!ToPointeeTypeOrErr)
1791 return ToPointeeTypeOrErr.takeError();
1798 ExpectedType ToUnderlyingTypeOrErr =
import(
T->getUnderlyingType());
1799 if (!ToUnderlyingTypeOrErr)
1800 return ToUnderlyingTypeOrErr.takeError();
1808 Error Err = Error::success();
1809 QualType ToOriginalType = importChecked(Err,
T->getOriginalType());
1810 QualType ToAdjustedType = importChecked(Err,
T->getAdjustedType());
1812 return std::move(Err);
1814 return Importer.getToContext().getAdjustedType(ToOriginalType,
1819 return Importer.getToContext().getBitIntType(
T->isUnsigned(),
1823ExpectedType clang::ASTNodeImporter::VisitBTFTagAttributedType(
1825 Error Err = Error::success();
1826 const BTFTypeTagAttr *ToBTFAttr = importChecked(Err,
T->getAttr());
1827 QualType ToWrappedType = importChecked(Err,
T->getWrappedType());
1829 return std::move(Err);
1831 return Importer.getToContext().getBTFTagAttributedType(ToBTFAttr,
1835ExpectedType clang::ASTNodeImporter::VisitConstantMatrixType(
1837 ExpectedType ToElementTypeOrErr =
import(
T->getElementType());
1838 if (!ToElementTypeOrErr)
1839 return ToElementTypeOrErr.takeError();
1841 return Importer.getToContext().getConstantMatrixType(
1842 *ToElementTypeOrErr,
T->getNumRows(),
T->getNumColumns());
1845ExpectedType clang::ASTNodeImporter::VisitDependentAddressSpaceType(
1847 Error Err = Error::success();
1849 Expr *ToAddrSpaceExpr = importChecked(Err,
T->getAddrSpaceExpr());
1852 return std::move(Err);
1854 return Importer.getToContext().getDependentAddressSpaceType(
1855 ToPointeeType, ToAddrSpaceExpr, ToAttrLoc);
1858ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType(
1860 ExpectedExpr ToNumBitsExprOrErr =
import(
T->getNumBitsExpr());
1861 if (!ToNumBitsExprOrErr)
1862 return ToNumBitsExprOrErr.takeError();
1863 return Importer.getToContext().getDependentBitIntType(
T->isUnsigned(),
1864 *ToNumBitsExprOrErr);
1867ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType(
1869 Error Err = Error::success();
1870 QualType ToElementType = importChecked(Err,
T->getElementType());
1871 Expr *ToRowExpr = importChecked(Err,
T->getRowExpr());
1872 Expr *ToColumnExpr = importChecked(Err,
T->getColumnExpr());
1875 return std::move(Err);
1877 return Importer.getToContext().getDependentSizedMatrixType(
1878 ToElementType, ToRowExpr, ToColumnExpr, ToAttrLoc);
1881ExpectedType clang::ASTNodeImporter::VisitDependentVectorType(
1883 Error Err = Error::success();
1884 QualType ToElementType = importChecked(Err,
T->getElementType());
1885 Expr *ToSizeExpr = importChecked(Err,
T->getSizeExpr());
1888 return std::move(Err);
1890 return Importer.getToContext().getDependentVectorType(
1891 ToElementType, ToSizeExpr, ToAttrLoc,
T->getVectorKind());
1894ExpectedType clang::ASTNodeImporter::VisitObjCTypeParamType(
1898 return ToDeclOrErr.takeError();
1903 if (!ToProtocolOrErr)
1904 return ToProtocolOrErr.takeError();
1905 ToProtocols.push_back(*ToProtocolOrErr);
1908 return Importer.getToContext().getObjCTypeParamType(*ToDeclOrErr,
1913 ExpectedType ToElementTypeOrErr =
import(
T->getElementType());
1914 if (!ToElementTypeOrErr)
1915 return ToElementTypeOrErr.takeError();
1918 if (
T->isReadOnly())
1938 if (isa<RecordDecl>(
D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1940 auto getLeafPointeeType = [](
const Type *
T) {
1948 getLeafPointeeType(
P->getType().getCanonicalType().getTypePtr());
1949 auto *RT = dyn_cast<RecordType>(LeafT);
1950 if (RT && RT->getDecl() ==
D) {
1963 if (Error Err = importInto(Name,
D->getDeclName()))
1975 return Error::success();
1982 if (Error Err = importInto(Name,
D->getDeclName()))
1994 return Error::success();
1999 return Error::success();
2002 if (Error Err = importInto(ToD, FromD))
2005 if (
RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
2006 if (
RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
2007 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
2008 !ToRecord->getDefinition()) {
2013 return Error::success();
2016 if (
EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
2017 if (
EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
2018 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
2023 return Error::success();
2026 return Error::success();
2041 return Error::success();
2047 return ToRangeOrErr.takeError();
2048 return Error::success();
2054 return LocOrErr.takeError();
2055 return Error::success();
2063 return ToTInfoOrErr.takeError();
2064 return Error::success();
2067 llvm_unreachable(
"Unknown name kind.");
2074 return ToDCOrErr.takeError();
2088 auto MightNeedReordering = [](
const Decl *
D) {
2089 return isa<FieldDecl>(
D) || isa<IndirectFieldDecl>(
D) || isa<FriendDecl>(
D);
2093 Error ChildErrors = Error::success();
2094 for (
auto *From : FromDC->
decls()) {
2095 if (!MightNeedReordering(From))
2104 if (!ImportedOrErr) {
2106 ImportedOrErr.takeError());
2109 FieldDecl *FieldFrom = dyn_cast_or_null<FieldDecl>(From);
2110 Decl *ImportedDecl = *ImportedOrErr;
2111 FieldDecl *FieldTo = dyn_cast_or_null<FieldDecl>(ImportedDecl);
2112 if (FieldFrom && FieldTo) {
2144 consumeError(std::move(ChildErrors));
2145 return ToDCOrErr.takeError();
2148 if (
const auto *FromRD = dyn_cast<RecordDecl>(FromDC)) {
2152 for (
auto *
D : FromRD->decls()) {
2153 if (!MightNeedReordering(
D))
2156 assert(
D &&
"DC contains a null decl");
2159 assert(ToDC == ToD->getLexicalDeclContext() && ToDC->
containsDecl(ToD));
2171 for (
auto *From : FromDC->
decls()) {
2172 if (MightNeedReordering(From))
2178 ImportedOrErr.takeError());
2198 if (!FromRecordDecl || !ToRecordDecl) {
2202 if (RecordFrom && RecordTo) {
2203 FromRecordDecl = RecordFrom->
getDecl();
2204 ToRecordDecl = RecordTo->getDecl();
2208 if (FromRecordDecl && ToRecordDecl) {
2214 return Error::success();
2221 return ToDCOrErr.takeError();
2227 if (!ToLexicalDCOrErr)
2228 return ToLexicalDCOrErr.takeError();
2229 ToLexicalDC = *ToLexicalDCOrErr;
2233 return Error::success();
2239 "Import implicit methods to or from non-definition");
2242 if (FromM->isImplicit()) {
2245 return ToMOrErr.takeError();
2248 return Error::success();
2257 return ToTypedefOrErr.takeError();
2259 return Error::success();
2264 auto DefinitionCompleter = [To]() {
2283 auto *FromCXXRD = cast<CXXRecordDecl>(From);
2285 ToCaptures.reserve(FromCXXRD->capture_size());
2286 for (
const auto &FromCapture : FromCXXRD->captures()) {
2287 if (
auto ToCaptureOrErr =
import(FromCapture))
2288 ToCaptures.push_back(*ToCaptureOrErr);
2290 return ToCaptureOrErr.takeError();
2292 cast<CXXRecordDecl>(To)->setCaptures(Importer.
getToContext(),
2299 DefinitionCompleter();
2303 return Error::success();
2318 auto DefinitionCompleterScopeExit =
2319 llvm::make_scope_exit(DefinitionCompleter);
2325 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
2326 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
2327 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
2329 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
2330 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2332 #define FIELD(Name, Width, Merge) \
2333 ToData.Name = FromData.Name;
2334 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
2337 ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions());
2340 for (
const auto &Base1 : FromCXX->bases()) {
2343 return TyOrErr.takeError();
2346 if (Base1.isPackExpansion()) {
2347 if (
ExpectedSLoc LocOrErr =
import(Base1.getEllipsisLoc()))
2348 EllipsisLoc = *LocOrErr;
2350 return LocOrErr.takeError();
2358 auto RangeOrErr =
import(Base1.getSourceRange());
2360 return RangeOrErr.takeError();
2362 auto TSIOrErr =
import(Base1.getTypeSourceInfo());
2364 return TSIOrErr.takeError();
2370 Base1.isBaseOfClass(),
2371 Base1.getAccessSpecifierAsWritten(),
2376 ToCXX->setBases(Bases.data(), Bases.size());
2384 return Error::success();
2389 return Error::success();
2393 return Error::success();
2397 return ToInitOrErr.takeError();
2408 return Error::success();
2416 return Error::success();
2427 return ToTypeOrErr.takeError();
2430 if (!ToPromotionTypeOrErr)
2431 return ToPromotionTypeOrErr.takeError();
2442 return Error::success();
2448 for (
const auto &Arg : FromArgs) {
2449 if (
auto ToOrErr =
import(Arg))
2450 ToArgs.push_back(*ToOrErr);
2452 return ToOrErr.takeError();
2455 return Error::success();
2461 return import(From);
2464template <
typename InContainerTy>
2467 for (
const auto &FromLoc : Container) {
2468 if (
auto ToLocOrErr =
import(FromLoc))
2471 return ToLocOrErr.takeError();
2473 return Error::success();
2483 bool IgnoreTemplateParmDepth) {
2494 false, Complain,
false,
2495 IgnoreTemplateParmDepth);
2515 return std::move(Err);
2520 return LocOrErr.takeError();
2523 if (GetImportedOrCreateDecl(ToD,
D, Importer.
getToContext(), DC, *LocOrErr))
2546 return std::move(Err);
2552 Name.getAsIdentifierInfo()))
2555 Error Err = Error::success();
2560 return std::move(Err);
2564 addDeclToContexts(
D, ToD);
2572 return LocOrErr.takeError();
2573 auto ColonLocOrErr =
import(
D->getColonLoc());
2575 return ColonLocOrErr.takeError();
2580 return DCOrErr.takeError();
2585 DC, *LocOrErr, *ColonLocOrErr))
2599 return DCOrErr.takeError();
2603 Error Err = Error::success();
2609 return std::move(Err);
2612 if (GetImportedOrCreateDecl(
2613 ToD,
D, Importer.
getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2614 ToRParenLoc,
D->isFailed()))
2629 return std::move(Err);
2638 if (
auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2644 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
2645 for (
auto *FoundDecl : FoundDecls) {
2649 if (
auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
2650 MergeWithNamespace = FoundNS;
2651 ConflictingDecls.clear();
2655 ConflictingDecls.push_back(FoundDecl);
2658 if (!ConflictingDecls.empty()) {
2661 ConflictingDecls.size());
2663 Name = NameOrErr.get();
2665 return NameOrErr.takeError();
2671 return BeginLocOrErr.takeError();
2673 if (!RBraceLocOrErr)
2674 return RBraceLocOrErr.takeError();
2679 if (GetImportedOrCreateDecl(ToNamespace,
D, Importer.
getToContext(), DC,
2680 D->isInline(), *BeginLocOrErr,
Loc,
2681 Name.getAsIdentifierInfo(),
2682 nullptr,
D->isNested()))
2691 if (
auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2692 TU->setAnonymousNamespace(ToNamespace);
2694 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2700 return std::move(Err);
2712 return std::move(Err);
2718 Error Err = Error::success();
2722 auto ToTargetNameLoc =
importChecked(Err,
D->getTargetNameLoc());
2725 return std::move(Err);
2730 if (GetImportedOrCreateDecl(
2731 ToD,
D, Importer.
getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2732 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
2750 return std::move(Err);
2769 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
2770 for (
auto *FoundDecl : FoundDecls) {
2773 if (
auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
2777 QualType FromUT =
D->getUnderlyingType();
2778 QualType FoundUT = FoundTypedef->getUnderlyingType();
2792 if (FromR && FoundR &&
2803 ConflictingDecls.push_back(FoundDecl);
2808 if (!ConflictingDecls.empty()) {
2810 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
2812 Name = NameOrErr.get();
2814 return NameOrErr.takeError();
2818 Error Err = Error::success();
2820 auto ToTypeSourceInfo =
importChecked(Err,
D->getTypeSourceInfo());
2823 return std::move(Err);
2830 if (GetImportedOrCreateDecl<TypeAliasDecl>(
2832 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
2834 }
else if (GetImportedOrCreateDecl<TypedefDecl>(
2836 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
2841 return std::move(Err);
2845 Importer.AddToLookupTable(ToTypedef);
2850 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(
D) :
nullptr;
2873 return std::move(Err);
2883 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
2884 for (
auto *FoundDecl : FoundDecls) {
2887 if (
auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl)) {
2890 ConflictingDecls.push_back(FoundDecl);
2894 if (!ConflictingDecls.empty()) {
2896 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
2898 Name = NameOrErr.get();
2900 return NameOrErr.takeError();
2904 Error Err = Error::success();
2905 auto ToTemplateParameters =
importChecked(Err,
D->getTemplateParameters());
2906 auto ToTemplatedDecl =
importChecked(Err,
D->getTemplatedDecl());
2908 return std::move(Err);
2912 Name, ToTemplateParameters, ToTemplatedDecl))
2915 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
2921 updateLookupTableForTemplateParameters(*ToTemplateParameters);
2932 return std::move(Err);
2939 if (
D->isGnuLocal()) {
2942 return BeginLocOrErr.takeError();
2944 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2949 Name.getAsIdentifierInfo()))
2956 return ToStmtOrErr.takeError();
2958 ToLabel->
setStmt(*ToStmtOrErr);
2971 return std::move(Err);
2978 if (!SearchName &&
D->getTypedefNameForAnonDecl()) {
2979 if (Error Err = importInto(
2980 SearchName,
D->getTypedefNameForAnonDecl()->getDeclName()))
2981 return std::move(Err);
2991 Importer.findDeclsInToCtx(DC, SearchName);
2992 for (
auto *FoundDecl : FoundDecls) {
2996 if (
auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
2997 if (
const auto *Tag = Typedef->getUnderlyingType()->getAs<
TagType>())
2998 FoundDecl = Tag->getDecl();
3001 if (
auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
3006 if (
D->isThisDeclarationADefinition() && FoundDef)
3011 ConflictingDecls.push_back(FoundDecl);
3020 if (SearchName && !ConflictingDecls.empty()) {
3022 SearchName, DC, IDNS, ConflictingDecls.data(),
3023 ConflictingDecls.size());
3025 Name = NameOrErr.get();
3027 return NameOrErr.takeError();
3031 Error Err = Error::success();
3037 return std::move(Err);
3041 if (GetImportedOrCreateDecl(
3043 Loc, Name.getAsIdentifierInfo(), PrevDecl,
D->isScoped(),
3044 D->isScopedUsingClassTag(),
D->isFixed()))
3052 addDeclToContexts(
D, D2);
3056 EnumDecl *FromInst =
D->getInstantiatedFromMemberEnum();
3058 D2->setInstantiationOfMemberEnum(*ToInstOrErr, SK);
3060 return ToInstOrErr.takeError();
3061 if (
ExpectedSLoc POIOrErr =
import(MemberInfo->getPointOfInstantiation()))
3064 return POIOrErr.takeError();
3068 if (
D->isCompleteDefinition())
3070 return std::move(Err);
3076 bool IsFriendTemplate =
false;
3077 if (
auto *DCXX = dyn_cast<CXXRecordDecl>(
D)) {
3079 DCXX->getDescribedClassTemplate() &&
3080 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
3090 return std::move(Err);
3097 if (!SearchName &&
D->getTypedefNameForAnonDecl()) {
3098 if (Error Err = importInto(
3099 SearchName,
D->getTypedefNameForAnonDecl()->getDeclName()))
3100 return std::move(Err);
3107 bool DependentFriend = IsFriendTemplate && IsDependentContext;
3114 Importer.findDeclsInToCtx(DC, SearchName);
3115 if (!FoundDecls.empty()) {
3118 if (
D->hasExternalLexicalStorage() && !
D->isCompleteDefinition())
3122 for (
auto *FoundDecl : FoundDecls) {
3127 if (
auto *Typedef = dyn_cast<TypedefNameDecl>(
Found)) {
3128 if (
const auto *Tag = Typedef->getUnderlyingType()->getAs<
TagType>())
3129 Found = Tag->getDecl();
3132 if (
auto *FoundRecord = dyn_cast<RecordDecl>(
Found)) {
3151 if (
D->isThisDeclarationADefinition() && FoundDef) {
3155 if (
const auto *DCXX = dyn_cast<CXXRecordDecl>(
D)) {
3156 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
3157 assert(FoundCXX &&
"Record type mismatch");
3163 return std::move(Err);
3169 ConflictingDecls.push_back(FoundDecl);
3173 if (!ConflictingDecls.empty() && SearchName) {
3175 SearchName, DC, IDNS, ConflictingDecls.data(),
3176 ConflictingDecls.size());
3178 Name = NameOrErr.get();
3180 return NameOrErr.takeError();
3186 return BeginLocOrErr.takeError();
3191 if (
auto *DCXX = dyn_cast<CXXRecordDecl>(
D)) {
3192 if (DCXX->isLambda()) {
3193 auto TInfoOrErr =
import(DCXX->getLambdaTypeInfo());
3195 return TInfoOrErr.takeError();
3196 if (GetImportedOrCreateSpecialDecl(
3198 DC, *TInfoOrErr,
Loc, DCXX->getLambdaDependencyKind(),
3199 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
3204 return CDeclOrErr.takeError();
3207 }
else if (DCXX->isInjectedClassName()) {
3210 const bool DelayTypeCreation =
true;
3211 if (GetImportedOrCreateDecl(
3213 *BeginLocOrErr,
Loc, Name.getAsIdentifierInfo(),
3214 cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation))
3217 D2CXX, dyn_cast<CXXRecordDecl>(DC));
3219 if (GetImportedOrCreateDecl(D2CXX,
D, Importer.
getToContext(),
3220 D->getTagKind(), DC, *BeginLocOrErr,
Loc,
3221 Name.getAsIdentifierInfo(),
3222 cast_or_null<CXXRecordDecl>(PrevDecl)))
3229 addDeclToContexts(
D, D2);
3232 DCXX->getDescribedClassTemplate()) {
3234 if (Error Err = importInto(ToDescribed, FromDescribed))
3235 return std::move(Err);
3237 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
3257 const Type *FrontTy =
3258 cast<CXXRecordDecl>(Redecls.front())->getTypeForDecl();
3261 InjSpec = InjTy->getInjectedSpecializationType();
3264 for (
auto *R : Redecls) {
3265 auto *RI = cast<CXXRecordDecl>(R);
3266 if (R != Redecls.front() ||
3267 !isa<InjectedClassNameType>(RI->getTypeForDecl()))
3268 RI->setTypeForDecl(
nullptr);
3283 DCXX->getMemberSpecializationInfo()) {
3285 MemberInfo->getTemplateSpecializationKind();
3291 return ToInstOrErr.takeError();
3294 import(MemberInfo->getPointOfInstantiation()))
3298 return POIOrErr.takeError();
3303 D->getTagKind(), DC, *BeginLocOrErr,
Loc,
3304 Name.getAsIdentifierInfo(), PrevDecl))
3307 addDeclToContexts(
D, D2);
3310 if (
auto BraceRangeOrErr =
import(
D->getBraceRange()))
3313 return BraceRangeOrErr.takeError();
3314 if (
auto QualifierLocOrErr =
import(
D->getQualifierLoc()))
3317 return QualifierLocOrErr.takeError();
3319 if (
D->isAnonymousStructOrUnion())
3322 if (
D->isCompleteDefinition())
3324 return std::move(Err);
3336 return std::move(Err);
3345 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
3346 for (
auto *FoundDecl : FoundDecls) {
3350 if (
auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
3353 ConflictingDecls.push_back(FoundDecl);
3357 if (!ConflictingDecls.empty()) {
3359 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
3361 Name = NameOrErr.get();
3363 return NameOrErr.takeError();
3369 return TypeOrErr.takeError();
3373 return InitOrErr.takeError();
3376 if (GetImportedOrCreateDecl(
3378 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr,
D->getInitVal()))
3379 return ToEnumerator;
3384 return ToEnumerator;
3387template <
typename DeclTy>
3390 unsigned int Num = FromD->getNumTemplateParameterLists();
3392 return Error::success();
3394 for (
unsigned int I = 0; I < Num; ++I)
3396 import(FromD->getTemplateParameterList(I)))
3397 ToTPLists[I] = *ToTPListOrErr;
3399 return ToTPListOrErr.takeError();
3400 ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists);
3401 return Error::success();
3409 return Error::success();
3415 return Error::success();
3421 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
3423 return InstFDOrErr.takeError();
3429 return POIOrErr.takeError();
3431 return Error::success();
3435 auto FunctionAndArgsOrErr =
3437 if (!FunctionAndArgsOrErr)
3438 return FunctionAndArgsOrErr.takeError();
3441 Importer.
getToContext(), std::get<1>(*FunctionAndArgsOrErr));
3445 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
3446 if (FromTAArgsAsWritten)
3448 *FromTAArgsAsWritten, ToTAInfo))
3451 ExpectedSLoc POIOrErr =
import(FTSInfo->getPointOfInstantiation());
3453 return POIOrErr.takeError();
3459 ToFD->setFunctionTemplateSpecialization(
3460 std::get<0>(*FunctionAndArgsOrErr), ToTAList,
nullptr,
3461 TSK, FromTAArgsAsWritten ? &ToTAInfo :
nullptr, *POIOrErr);
3462 return Error::success();
3470 Candidates.
addDecl(*ToFTDOrErr);
3472 return ToFTDOrErr.takeError();
3477 const auto *FromTAArgsAsWritten = FromInfo->TemplateArgumentsAsWritten;
3478 if (FromTAArgsAsWritten)
3485 FromTAArgsAsWritten ? &ToTAInfo :
nullptr);
3486 return Error::success();
3489 llvm_unreachable(
"All cases should be covered!");
3494 auto FunctionAndArgsOrErr =
3496 if (!FunctionAndArgsOrErr)
3497 return FunctionAndArgsOrErr.takeError();
3501 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
3502 void *InsertPos =
nullptr;
3513 return ToBodyOrErr.takeError();
3515 return Error::success();
3524 assert(DCi &&
"Declaration should have a context");
3538 ToProcess.push_back(S);
3539 while (!ToProcess.empty()) {
3540 const Stmt *CurrentS = ToProcess.pop_back_val();
3542 if (
const auto *DeclRef = dyn_cast<DeclRefExpr>(CurrentS)) {
3543 if (
const Decl *
D = DeclRef->getDecl())
3546 }
else if (
const auto *
E =
3547 dyn_cast_or_null<SubstNonTypeTemplateParmExpr>(CurrentS)) {
3548 if (
const Decl *
D =
E->getAssociatedDecl())
3581class IsTypeDeclaredInsideVisitor
3582 :
public TypeVisitor<IsTypeDeclaredInsideVisitor, std::optional<bool>> {
3584 IsTypeDeclaredInsideVisitor(
const FunctionDecl *ParentDC)
3585 : ParentDC(ParentDC) {}
3591 if (std::optional<bool> Res = Visit(
T.getTypePtr()))
3594 T.getSingleStepDesugaredType(ParentDC->getParentASTContext());
3596 if (std::optional<bool> Res = Visit(DsT.
getTypePtr()))
3599 DsT =
T.getSingleStepDesugaredType(ParentDC->getParentASTContext());
3604 std::optional<bool> VisitTagType(
const TagType *
T) {
3605 if (
auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(
T->getDecl()))
3606 for (
const auto &Arg : Spec->getTemplateArgs().asArray())
3607 if (checkTemplateArgument(Arg))
3612 std::optional<bool> VisitPointerType(
const PointerType *
T) {
3617 return CheckType(
T->getPointeeTypeAsWritten());
3620 std::optional<bool> VisitTypedefType(
const TypedefType *
T) {
3626 std::optional<bool> VisitUsingType(
const UsingType *
T) {
3627 if (
T->getFoundDecl() &&
3636 for (
const auto &Arg :
T->template_arguments())
3637 if (checkTemplateArgument(Arg))
3644 return CheckType(
T->getBaseType());
3659 return CheckType(
T->getElementType());
3664 "Variable array should not occur in deduced return type of a function");
3668 llvm_unreachable(
"Incomplete array should not occur in deduced return type "
3673 llvm_unreachable(
"Dependent array should not occur in deduced return type "
3700 if (checkTemplateArgument(PackArg))
3712 llvm_unreachable(
"Unknown TemplateArgument::ArgKind enum");
3722 assert(FromFPT &&
"Must be called on FunctionProtoType");
3724 auto IsCXX11Lambda = [&]() {
3725 if (Importer.FromContext.
getLangOpts().CPlusPlus14)
3728 if (
const auto *MD = dyn_cast<CXXMethodDecl>(
D))
3729 return cast<CXXRecordDecl>(MD->getDeclContext())->isLambda();
3734 QualType RetT = FromFPT->getReturnType();
3735 if (isa<AutoType>(RetT.
getTypePtr()) || IsCXX11Lambda()) {
3737 IsTypeDeclaredInsideVisitor Visitor(Def ? Def :
D);
3738 return Visitor.CheckType(RetT);
3755 auto RedeclIt = Redecls.begin();
3758 for (; RedeclIt != Redecls.end() && *RedeclIt !=
D; ++RedeclIt) {
3761 return ToRedeclOrErr.takeError();
3763 assert(*RedeclIt ==
D);
3771 return std::move(Err);
3783 if (
D->getTemplatedKind() ==
3786 if (!FoundFunctionOrErr)
3787 return FoundFunctionOrErr.takeError();
3788 if (
FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
3789 if (
Decl *Def = FindAndMapDefinition(
D, FoundFunction))
3791 FoundByLookup = FoundFunction;
3799 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
3800 for (
auto *FoundDecl : FoundDecls) {
3804 if (
auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
3809 if (
Decl *Def = FindAndMapDefinition(
D, FoundFunction))
3811 FoundByLookup = FoundFunction;
3822 Importer.
ToDiag(
Loc, diag::warn_odr_function_type_inconsistent)
3823 << Name <<
D->getType() << FoundFunction->getType();
3824 Importer.
ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here)
3825 << FoundFunction->getType();
3826 ConflictingDecls.push_back(FoundDecl);
3830 if (!ConflictingDecls.empty()) {
3832 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
3834 Name = NameOrErr.get();
3836 return NameOrErr.takeError();
3846 if (FoundByLookup) {
3847 if (isa<CXXMethodDecl>(FoundByLookup)) {
3849 if (!
D->doesThisDeclarationHaveABody()) {
3851 D->getDescribedFunctionTemplate()) {
3856 "Templated function mapped to non-templated?");
3872 return std::move(Err);
3883 bool UsedDifferentProtoType =
false;
3885 QualType FromReturnTy = FromFPT->getReturnType();
3893 UsedDifferentProtoType =
true;
3904 FromEPI = DefaultEPI;
3905 UsedDifferentProtoType =
true;
3908 FromReturnTy, FromFPT->getParamTypes(), FromEPI);
3913 Error Err = Error::success();
3916 auto ToInnerLocStart =
importChecked(Err,
D->getInnerLocStart());
3920 auto TrailingRequiresClause =
3923 return std::move(Err);
3927 for (
auto *
P :
D->parameters()) {
3929 Parameters.push_back(*ToPOrErr);
3931 return ToPOrErr.takeError();
3936 if (
auto *FromConstructor = dyn_cast<CXXConstructorDecl>(
D)) {
3938 importExplicitSpecifier(Err, FromConstructor->getExplicitSpecifier());
3940 return std::move(Err);
3942 if (FromConstructor->isInheritingConstructor()) {
3944 import(FromConstructor->getInheritedConstructor());
3945 if (!ImportedInheritedCtor)
3946 return ImportedInheritedCtor.takeError();
3947 ToInheritedConstructor = *ImportedInheritedCtor;
3949 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
3950 ToFunction,
D, Importer.
getToContext(), cast<CXXRecordDecl>(DC),
3951 ToInnerLocStart, NameInfo,
T, TInfo, ESpec,
D->UsesFPIntrin(),
3952 D->isInlineSpecified(),
D->
isImplicit(),
D->getConstexprKind(),
3953 ToInheritedConstructor, TrailingRequiresClause))
3957 Error Err = Error::success();
3959 Err,
const_cast<FunctionDecl *
>(FromDtor->getOperatorDelete()));
3960 auto ToThisArg =
importChecked(Err, FromDtor->getOperatorDeleteThisArg());
3962 return std::move(Err);
3964 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
3965 ToFunction,
D, Importer.
getToContext(), cast<CXXRecordDecl>(DC),
3966 ToInnerLocStart, NameInfo,
T, TInfo,
D->UsesFPIntrin(),
3967 D->isInlineSpecified(),
D->
isImplicit(),
D->getConstexprKind(),
3968 TrailingRequiresClause))
3975 dyn_cast<CXXConversionDecl>(
D)) {
3977 importExplicitSpecifier(Err, FromConversion->getExplicitSpecifier());
3979 return std::move(Err);
3980 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3981 ToFunction,
D, Importer.
getToContext(), cast<CXXRecordDecl>(DC),
3982 ToInnerLocStart, NameInfo,
T, TInfo,
D->UsesFPIntrin(),
3983 D->isInlineSpecified(), ESpec,
D->getConstexprKind(),
3986 }
else if (
auto *Method = dyn_cast<CXXMethodDecl>(
D)) {
3987 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3988 ToFunction,
D, Importer.
getToContext(), cast<CXXRecordDecl>(DC),
3989 ToInnerLocStart, NameInfo,
T, TInfo, Method->getStorageClass(),
3990 Method->UsesFPIntrin(), Method->isInlineSpecified(),
3993 }
else if (
auto *Guide = dyn_cast<CXXDeductionGuideDecl>(
D)) {
3995 importExplicitSpecifier(Err, Guide->getExplicitSpecifier());
3999 return std::move(Err);
4000 if (GetImportedOrCreateDecl<CXXDeductionGuideDecl>(
4001 ToFunction,
D, Importer.
getToContext(), DC, ToInnerLocStart, ESpec,
4002 NameInfo,
T, TInfo, ToEndLoc, Ctor))
4004 cast<CXXDeductionGuideDecl>(ToFunction)
4005 ->setDeductionCandidateKind(Guide->getDeductionCandidateKind());
4007 if (GetImportedOrCreateDecl(
4008 ToFunction,
D, Importer.
getToContext(), DC, ToInnerLocStart,
4009 NameInfo,
T, TInfo,
D->getStorageClass(),
D->UsesFPIntrin(),
4010 D->isInlineSpecified(),
D->hasWrittenPrototype(),
4011 D->getConstexprKind(), TrailingRequiresClause))
4016 if (FoundByLookup) {
4029 auto Imported =
import(Msg);
4031 return Imported.takeError();
4045 D->FriendConstraintRefersToEnclosingTemplate());
4055 for (
auto *Param : Parameters) {
4056 Param->setOwningFunction(ToFunction);
4061 ToFunction->setParams(Parameters);
4068 for (
unsigned I = 0, N = Parameters.size(); I != N; ++I)
4069 ProtoLoc.setParam(I, Parameters[I]);
4075 auto ToFTOrErr =
import(FromFT);
4077 return ToFTOrErr.takeError();
4081 if (
auto *FromConstructor = dyn_cast<CXXConstructorDecl>(
D)) {
4082 if (
unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
4086 FromConstructor->inits(), CtorInitializers))
4087 return std::move(Err);
4090 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
4091 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
4092 ToCtor->setCtorInitializers(Memory);
4093 ToCtor->setNumCtorInitializers(NumInitializers);
4099 return std::move(Err);
4101 if (
auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(
D))
4104 return std::move(Err);
4106 if (
D->doesThisDeclarationHaveABody()) {
4110 return std::move(Err);
4114 if (UsedDifferentProtoType) {
4116 ToFunction->
setType(*TyOrErr);
4118 return TyOrErr.takeError();
4122 return TSIOrErr.takeError();
4127 addDeclToContexts(
D, ToFunction);
4130 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
4133 return ToRedeclOrErr.takeError();
4167 return std::move(Err);
4172 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
4173 for (
auto *FoundDecl : FoundDecls) {
4174 if (
FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
4182 FoundField->getType())) {
4190 if (
Expr *FromInitializer =
D->getInClassInitializer()) {
4191 if (
ExpectedExpr ToInitializerOrErr =
import(FromInitializer)) {
4194 assert(FoundField->hasInClassInitializer() &&
4195 "Field should have an in-class initializer if it has an "
4196 "expression for it.");
4197 if (!FoundField->getInClassInitializer())
4198 FoundField->setInClassInitializer(*ToInitializerOrErr);
4200 return ToInitializerOrErr.takeError();
4207 Importer.
ToDiag(
Loc, diag::warn_odr_field_type_inconsistent)
4208 << Name <<
D->getType() << FoundField->getType();
4209 Importer.
ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
4210 << FoundField->getType();
4216 Error Err = Error::success();
4220 auto ToInnerLocStart =
importChecked(Err,
D->getInnerLocStart());
4222 return std::move(Err);
4223 const Type *ToCapturedVLAType =
nullptr;
4225 ToCapturedVLAType, cast_or_null<Type>(
D->getCapturedVLAType())))
4226 return std::move(Err);
4229 if (GetImportedOrCreateDecl(ToField,
D, Importer.
getToContext(), DC,
4230 ToInnerLocStart,
Loc, Name.getAsIdentifierInfo(),
4231 ToType, ToTInfo, ToBitWidth,
D->isMutable(),
4232 D->getInClassInitStyle()))
4238 if (ToCapturedVLAType)
4243 auto ToInitializer =
importChecked(Err,
D->getInClassInitializer());
4245 return std::move(Err);
4246 if (ToInitializer) {
4248 if (AlreadyImported)
4249 assert(ToInitializer == AlreadyImported &&
4250 "Duplicate import of in-class initializer.");
4265 return std::move(Err);
4270 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
4271 for (
unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4272 if (
auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
4280 FoundField->getType(),
4287 if (!Name && I < N-1)
4291 Importer.
ToDiag(
Loc, diag::warn_odr_field_type_inconsistent)
4292 << Name <<
D->getType() << FoundField->getType();
4293 Importer.
ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
4294 << FoundField->getType();
4301 auto TypeOrErr =
import(
D->getType());
4303 return TypeOrErr.takeError();
4309 for (
auto *PI :
D->chain())
4311 NamedChain[i++] = *ToD;
4313 return ToD.takeError();
4317 if (GetImportedOrCreateDecl(ToIndirectField,
D, Importer.
getToContext(), DC,
4318 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
4320 return ToIndirectField;
4325 return ToIndirectField;
4355 unsigned int FriendCount = 0;
4356 std::optional<unsigned int> FriendPosition;
4359 for (
FriendDecl *FoundFriend : RD->friends()) {
4360 if (FoundFriend == FD) {
4361 FriendPosition = FriendCount;
4368 assert(FriendPosition &&
"Friend decl not found in own parent.");
4370 return {FriendCount, *FriendPosition};
4377 return std::move(Err);
4382 const auto *RD = cast<CXXRecordDecl>(DC);
4384 for (
FriendDecl *ImportedFriend : RD->friends())
4386 ImportedEquivalentFriends.push_back(ImportedFriend);
4391 assert(ImportedEquivalentFriends.size() <= CountAndPosition.
TotalCount &&
4392 "Class with non-matching friends is imported, ODR check wrong?");
4393 if (ImportedEquivalentFriends.size() == CountAndPosition.
TotalCount)
4395 D, ImportedEquivalentFriends[CountAndPosition.
IndexOfDecl]);
4400 if (
NamedDecl *FriendD =
D->getFriendDecl()) {
4402 if (Error Err = importInto(ToFriendD, FriendD))
4403 return std::move(Err);
4411 if (
auto TSIOrErr =
import(
D->getFriendType()))
4414 return TSIOrErr.takeError();
4419 for (
unsigned I = 0; I <
D->NumTPLists; I++) {
4420 if (
auto ListOrErr =
import(FromTPLists[I]))
4421 ToTPLists[I] = *ListOrErr;
4423 return ListOrErr.takeError();
4428 return LocationOrErr.takeError();
4429 auto FriendLocOrErr =
import(
D->getFriendLoc());
4430 if (!FriendLocOrErr)
4431 return FriendLocOrErr.takeError();
4432 auto EllipsisLocOrErr =
import(
D->getEllipsisLoc());
4433 if (!EllipsisLocOrErr)
4434 return EllipsisLocOrErr.takeError();
4437 if (GetImportedOrCreateDecl(FrD,
D, Importer.
getToContext(), DC,
4438 *LocationOrErr, ToFU, *FriendLocOrErr,
4439 *EllipsisLocOrErr, ToTPLists))
4455 return std::move(Err);
4460 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
4461 for (
auto *FoundDecl : FoundDecls) {
4462 if (
ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
4464 FoundIvar->getType())) {
4469 Importer.
ToDiag(
Loc, diag::warn_odr_ivar_type_inconsistent)
4470 << Name <<
D->getType() << FoundIvar->getType();
4471 Importer.
ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
4472 << FoundIvar->getType();
4478 Error Err = Error::success();
4480 auto ToTypeSourceInfo =
importChecked(Err,
D->getTypeSourceInfo());
4482 auto ToInnerLocStart =
importChecked(Err,
D->getInnerLocStart());
4484 return std::move(Err);
4487 if (GetImportedOrCreateDecl(
4488 ToIvar,
D, Importer.
getToContext(), cast<ObjCContainerDecl>(DC),
4489 ToInnerLocStart,
Loc, Name.getAsIdentifierInfo(),
4490 ToType, ToTypeSourceInfo,
4491 D->getAccessControl(),ToBitWidth,
D->getSynthesize()))
4502 auto RedeclIt = Redecls.begin();
4505 for (; RedeclIt != Redecls.end() && *RedeclIt !=
D; ++RedeclIt) {
4508 return RedeclOrErr.takeError();
4510 assert(*RedeclIt ==
D);
4518 return std::move(Err);
4524 VarDecl *FoundByLookup =
nullptr;
4525 if (
D->isFileVarDecl()) {
4528 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
4529 for (
auto *FoundDecl : FoundDecls) {
4533 if (
auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
4537 FoundVar->getType())) {
4542 if (
D->isThisDeclarationADefinition() && FoundDef)
4549 const VarDecl *FoundDInit =
nullptr;
4550 if (
D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
4554 FoundByLookup = FoundVar;
4562 if (FoundArray && TArray) {
4563 if (isa<IncompleteArrayType>(FoundArray) &&
4564 isa<ConstantArrayType>(TArray)) {
4566 if (
auto TyOrErr =
import(
D->getType()))
4567 FoundVar->setType(*TyOrErr);
4569 return TyOrErr.takeError();
4571 FoundByLookup = FoundVar;
4573 }
else if (isa<IncompleteArrayType>(TArray) &&
4574 isa<ConstantArrayType>(FoundArray)) {
4575 FoundByLookup = FoundVar;
4580 Importer.
ToDiag(
Loc, diag::warn_odr_variable_type_inconsistent)
4581 << Name <<
D->getType() << FoundVar->getType();
4582 Importer.
ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
4583 << FoundVar->getType();
4584 ConflictingDecls.push_back(FoundDecl);
4588 if (!ConflictingDecls.empty()) {
4590 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
4592 Name = NameOrErr.get();
4594 return NameOrErr.takeError();
4598 Error Err = Error::success();
4600 auto ToTypeSourceInfo =
importChecked(Err,
D->getTypeSourceInfo());
4601 auto ToInnerLocStart =
importChecked(Err,
D->getInnerLocStart());
4604 return std::move(Err);
4607 if (
auto *FromDecomp = dyn_cast<DecompositionDecl>(
D)) {
4611 return std::move(Err);
4613 if (GetImportedOrCreateDecl(
4614 ToDecomp, FromDecomp, Importer.
getToContext(), DC, ToInnerLocStart,
4615 Loc, ToType, ToTypeSourceInfo,
D->getStorageClass(),
Bindings))
4620 if (GetImportedOrCreateDecl(ToVar,
D, Importer.
getToContext(), DC,
4621 ToInnerLocStart,
Loc,
4622 Name.getAsIdentifierInfo(), ToType,
4623 ToTypeSourceInfo,
D->getStorageClass()))
4631 if (
D->isInlineSpecified())
4636 if (FoundByLookup) {
4642 if (
D->getDescribedVarTemplate()) {
4643 auto ToVTOrErr =
import(
D->getDescribedVarTemplate());
4645 return ToVTOrErr.takeError();
4648 VarDecl *FromInst =
D->getInstantiatedFromStaticDataMember();
4652 return ToInstOrErr.takeError();
4653 if (
ExpectedSLoc POIOrErr =
import(MSI->getPointOfInstantiation()))
4656 return POIOrErr.takeError();
4660 return std::move(Err);
4662 if (
D->isConstexpr())
4665 addDeclToContexts(
D, ToVar);
4668 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
4671 return RedeclOrErr.takeError();
4682 Error Err = Error::success();
4687 return std::move(Err);
4691 if (GetImportedOrCreateDecl(ToParm,
D, Importer.
getToContext(), DC,
4692 ToLocation, ToDeclName.getAsIdentifierInfo(),
4693 ToType,
D->getParameterKind()))
4709 return ToDefArgOrErr.takeError();
4713 if (
auto ToDefArgOrErr =
import(FromParam->
getDefaultArg()))
4716 return ToDefArgOrErr.takeError();
4719 return Error::success();
4724 Error Err = Error::success();
4729 return std::move(Err);
4738 Error Err = Error::success();
4741 auto ToInnerLocStart =
importChecked(Err,
D->getInnerLocStart());
4743 auto ToTypeSourceInfo =
importChecked(Err,
D->getTypeSourceInfo());
4745 return std::move(Err);
4748 if (GetImportedOrCreateDecl(ToParm,
D, Importer.
getToContext(), DC,
4749 ToInnerLocStart, ToLocation,
4750 ToDeclName.getAsIdentifierInfo(), ToType,
4751 ToTypeSourceInfo,
D->getStorageClass(),
4759 return std::move(Err);
4761 if (
D->isObjCMethodParameter()) {
4766 D->getFunctionScopeIndex());
4779 return std::move(Err);
4783 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
4784 for (
auto *FoundDecl : FoundDecls) {
4785 if (
auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
4786 if (FoundMethod->isInstanceMethod() !=
D->isInstanceMethod())
4791 FoundMethod->getReturnType())) {
4792 Importer.
ToDiag(
Loc, diag::warn_odr_objc_method_result_type_inconsistent)
4793 <<
D->isInstanceMethod() << Name <<
D->getReturnType()
4794 << FoundMethod->getReturnType();
4795 Importer.
ToDiag(FoundMethod->getLocation(),
4796 diag::note_odr_objc_method_here)
4797 <<
D->isInstanceMethod() << Name;