Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1 | //===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the ASTImporter class which imports AST nodes from one |
| 11 | // context into another context. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "clang/AST/ASTImporter.h" |
| 15 | |
| 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeVisitor.h" |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 23 | #include "clang/Basic/FileManager.h" |
| 24 | #include "clang/Basic/SourceManager.h" |
| 25 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 26 | #include <deque> |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | |
| 30 | namespace { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 31 | class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>, |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 32 | public DeclVisitor<ASTNodeImporter, Decl *>, |
| 33 | public StmtVisitor<ASTNodeImporter, Stmt *> { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 34 | ASTImporter &Importer; |
| 35 | |
| 36 | public: |
| 37 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { } |
| 38 | |
| 39 | using TypeVisitor<ASTNodeImporter, QualType>::Visit; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 40 | using DeclVisitor<ASTNodeImporter, Decl *>::Visit; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 41 | using StmtVisitor<ASTNodeImporter, Stmt *>::Visit; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 42 | |
| 43 | // Importing types |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 44 | QualType VisitType(Type *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 45 | QualType VisitBuiltinType(BuiltinType *T); |
| 46 | QualType VisitComplexType(ComplexType *T); |
| 47 | QualType VisitPointerType(PointerType *T); |
| 48 | QualType VisitBlockPointerType(BlockPointerType *T); |
| 49 | QualType VisitLValueReferenceType(LValueReferenceType *T); |
| 50 | QualType VisitRValueReferenceType(RValueReferenceType *T); |
| 51 | QualType VisitMemberPointerType(MemberPointerType *T); |
| 52 | QualType VisitConstantArrayType(ConstantArrayType *T); |
| 53 | QualType VisitIncompleteArrayType(IncompleteArrayType *T); |
| 54 | QualType VisitVariableArrayType(VariableArrayType *T); |
| 55 | // FIXME: DependentSizedArrayType |
| 56 | // FIXME: DependentSizedExtVectorType |
| 57 | QualType VisitVectorType(VectorType *T); |
| 58 | QualType VisitExtVectorType(ExtVectorType *T); |
| 59 | QualType VisitFunctionNoProtoType(FunctionNoProtoType *T); |
| 60 | QualType VisitFunctionProtoType(FunctionProtoType *T); |
| 61 | // FIXME: UnresolvedUsingType |
| 62 | QualType VisitTypedefType(TypedefType *T); |
| 63 | QualType VisitTypeOfExprType(TypeOfExprType *T); |
| 64 | // FIXME: DependentTypeOfExprType |
| 65 | QualType VisitTypeOfType(TypeOfType *T); |
| 66 | QualType VisitDecltypeType(DecltypeType *T); |
| 67 | // FIXME: DependentDecltypeType |
| 68 | QualType VisitRecordType(RecordType *T); |
| 69 | QualType VisitEnumType(EnumType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 70 | // FIXME: TemplateTypeParmType |
| 71 | // FIXME: SubstTemplateTypeParmType |
| 72 | // FIXME: TemplateSpecializationType |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 73 | QualType VisitElaboratedType(ElaboratedType *T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 74 | // FIXME: DependentNameType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 75 | // FIXME: DependentTemplateSpecializationType |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 76 | QualType VisitObjCInterfaceType(ObjCInterfaceType *T); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 77 | QualType VisitObjCObjectType(ObjCObjectType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 78 | QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 79 | |
| 80 | // Importing declarations |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 81 | bool ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 82 | DeclContext *&LexicalDC, DeclarationName &Name, |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 83 | SourceLocation &Loc); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 84 | void ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 85 | DeclarationNameInfo& To); |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 86 | void ImportDeclContext(DeclContext *FromDC); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 87 | bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 88 | bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 89 | Decl *VisitDecl(Decl *D); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 90 | Decl *VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 91 | Decl *VisitTypedefDecl(TypedefDecl *D); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 92 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 93 | Decl *VisitRecordDecl(RecordDecl *D); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 94 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 95 | Decl *VisitFunctionDecl(FunctionDecl *D); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 96 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D); |
| 97 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
| 98 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
| 99 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 100 | Decl *VisitFieldDecl(FieldDecl *D); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 101 | Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 102 | Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 103 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 104 | Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 105 | Decl *VisitParmVarDecl(ParmVarDecl *D); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 106 | Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 107 | Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 108 | Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 109 | Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 110 | Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
Douglas Gregor | 8661a72 | 2010-02-18 02:12:22 +0000 | [diff] [blame] | 111 | Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
Douglas Gregor | 06537af | 2010-02-18 02:04:09 +0000 | [diff] [blame] | 112 | Decl *VisitObjCClassDecl(ObjCClassDecl *D); |
| 113 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 114 | // Importing statements |
| 115 | Stmt *VisitStmt(Stmt *S); |
| 116 | |
| 117 | // Importing expressions |
| 118 | Expr *VisitExpr(Expr *E); |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 119 | Expr *VisitDeclRefExpr(DeclRefExpr *E); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 120 | Expr *VisitIntegerLiteral(IntegerLiteral *E); |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 121 | Expr *VisitCharacterLiteral(CharacterLiteral *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 122 | Expr *VisitParenExpr(ParenExpr *E); |
| 123 | Expr *VisitUnaryOperator(UnaryOperator *E); |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 124 | Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 125 | Expr *VisitBinaryOperator(BinaryOperator *E); |
| 126 | Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 127 | Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 128 | Expr *VisitCStyleCastExpr(CStyleCastExpr *E); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 129 | }; |
| 130 | } |
| 131 | |
| 132 | //---------------------------------------------------------------------------- |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 133 | // Structural Equivalence |
| 134 | //---------------------------------------------------------------------------- |
| 135 | |
| 136 | namespace { |
| 137 | struct StructuralEquivalenceContext { |
| 138 | /// \brief AST contexts for which we are checking structural equivalence. |
| 139 | ASTContext &C1, &C2; |
| 140 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 141 | /// \brief The set of "tentative" equivalences between two canonical |
| 142 | /// declarations, mapping from a declaration in the first context to the |
| 143 | /// declaration in the second context that we believe to be equivalent. |
| 144 | llvm::DenseMap<Decl *, Decl *> TentativeEquivalences; |
| 145 | |
| 146 | /// \brief Queue of declarations in the first context whose equivalence |
| 147 | /// with a declaration in the second context still needs to be verified. |
| 148 | std::deque<Decl *> DeclsToCheck; |
| 149 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 150 | /// \brief Declaration (from, to) pairs that are known not to be equivalent |
| 151 | /// (which we have already complained about). |
| 152 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls; |
| 153 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 154 | /// \brief Whether we're being strict about the spelling of types when |
| 155 | /// unifying two types. |
| 156 | bool StrictTypeSpelling; |
| 157 | |
| 158 | StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2, |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 159 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls, |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 160 | bool StrictTypeSpelling = false) |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 161 | : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 162 | StrictTypeSpelling(StrictTypeSpelling) { } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 163 | |
| 164 | /// \brief Determine whether the two declarations are structurally |
| 165 | /// equivalent. |
| 166 | bool IsStructurallyEquivalent(Decl *D1, Decl *D2); |
| 167 | |
| 168 | /// \brief Determine whether the two types are structurally equivalent. |
| 169 | bool IsStructurallyEquivalent(QualType T1, QualType T2); |
| 170 | |
| 171 | private: |
| 172 | /// \brief Finish checking all of the structural equivalences. |
| 173 | /// |
| 174 | /// \returns true if an error occurred, false otherwise. |
| 175 | bool Finish(); |
| 176 | |
| 177 | public: |
| 178 | DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 179 | return C1.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 183 | return C2.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 184 | } |
| 185 | }; |
| 186 | } |
| 187 | |
| 188 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 189 | QualType T1, QualType T2); |
| 190 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 191 | Decl *D1, Decl *D2); |
| 192 | |
| 193 | /// \brief Determine if two APInts have the same value, after zero-extending |
| 194 | /// one of them (if needed!) to ensure that the bit-widths match. |
| 195 | static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) { |
| 196 | if (I1.getBitWidth() == I2.getBitWidth()) |
| 197 | return I1 == I2; |
| 198 | |
| 199 | if (I1.getBitWidth() > I2.getBitWidth()) |
| 200 | return I1 == llvm::APInt(I2).zext(I1.getBitWidth()); |
| 201 | |
| 202 | return llvm::APInt(I1).zext(I2.getBitWidth()) == I2; |
| 203 | } |
| 204 | |
| 205 | /// \brief Determine if two APSInts have the same value, zero- or sign-extending |
| 206 | /// as needed. |
| 207 | static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) { |
| 208 | if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned()) |
| 209 | return I1 == I2; |
| 210 | |
| 211 | // Check for a bit-width mismatch. |
| 212 | if (I1.getBitWidth() > I2.getBitWidth()) |
| 213 | return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth())); |
| 214 | else if (I2.getBitWidth() > I1.getBitWidth()) |
| 215 | return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2); |
| 216 | |
| 217 | // We have a signedness mismatch. Turn the signed value into an unsigned |
| 218 | // value. |
| 219 | if (I1.isSigned()) { |
| 220 | if (I1.isNegative()) |
| 221 | return false; |
| 222 | |
| 223 | return llvm::APSInt(I1, true) == I2; |
| 224 | } |
| 225 | |
| 226 | if (I2.isNegative()) |
| 227 | return false; |
| 228 | |
| 229 | return I1 == llvm::APSInt(I2, true); |
| 230 | } |
| 231 | |
| 232 | /// \brief Determine structural equivalence of two expressions. |
| 233 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 234 | Expr *E1, Expr *E2) { |
| 235 | if (!E1 || !E2) |
| 236 | return E1 == E2; |
| 237 | |
| 238 | // FIXME: Actually perform a structural comparison! |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | /// \brief Determine whether two identifiers are equivalent. |
| 243 | static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, |
| 244 | const IdentifierInfo *Name2) { |
| 245 | if (!Name1 || !Name2) |
| 246 | return Name1 == Name2; |
| 247 | |
| 248 | return Name1->getName() == Name2->getName(); |
| 249 | } |
| 250 | |
| 251 | /// \brief Determine whether two nested-name-specifiers are equivalent. |
| 252 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 253 | NestedNameSpecifier *NNS1, |
| 254 | NestedNameSpecifier *NNS2) { |
| 255 | // FIXME: Implement! |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | /// \brief Determine whether two template arguments are equivalent. |
| 260 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 261 | const TemplateArgument &Arg1, |
| 262 | const TemplateArgument &Arg2) { |
| 263 | // FIXME: Implement! |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | /// \brief Determine structural equivalence for the common part of array |
| 268 | /// types. |
| 269 | static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 270 | const ArrayType *Array1, |
| 271 | const ArrayType *Array2) { |
| 272 | if (!IsStructurallyEquivalent(Context, |
| 273 | Array1->getElementType(), |
| 274 | Array2->getElementType())) |
| 275 | return false; |
| 276 | if (Array1->getSizeModifier() != Array2->getSizeModifier()) |
| 277 | return false; |
| 278 | if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers()) |
| 279 | return false; |
| 280 | |
| 281 | return true; |
| 282 | } |
| 283 | |
| 284 | /// \brief Determine structural equivalence of two types. |
| 285 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 286 | QualType T1, QualType T2) { |
| 287 | if (T1.isNull() || T2.isNull()) |
| 288 | return T1.isNull() && T2.isNull(); |
| 289 | |
| 290 | if (!Context.StrictTypeSpelling) { |
| 291 | // We aren't being strict about token-to-token equivalence of types, |
| 292 | // so map down to the canonical type. |
| 293 | T1 = Context.C1.getCanonicalType(T1); |
| 294 | T2 = Context.C2.getCanonicalType(T2); |
| 295 | } |
| 296 | |
| 297 | if (T1.getQualifiers() != T2.getQualifiers()) |
| 298 | return false; |
| 299 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 300 | Type::TypeClass TC = T1->getTypeClass(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 301 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 302 | if (T1->getTypeClass() != T2->getTypeClass()) { |
| 303 | // Compare function types with prototypes vs. without prototypes as if |
| 304 | // both did not have prototypes. |
| 305 | if (T1->getTypeClass() == Type::FunctionProto && |
| 306 | T2->getTypeClass() == Type::FunctionNoProto) |
| 307 | TC = Type::FunctionNoProto; |
| 308 | else if (T1->getTypeClass() == Type::FunctionNoProto && |
| 309 | T2->getTypeClass() == Type::FunctionProto) |
| 310 | TC = Type::FunctionNoProto; |
| 311 | else |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | switch (TC) { |
| 316 | case Type::Builtin: |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 317 | // FIXME: Deal with Char_S/Char_U. |
| 318 | if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind()) |
| 319 | return false; |
| 320 | break; |
| 321 | |
| 322 | case Type::Complex: |
| 323 | if (!IsStructurallyEquivalent(Context, |
| 324 | cast<ComplexType>(T1)->getElementType(), |
| 325 | cast<ComplexType>(T2)->getElementType())) |
| 326 | return false; |
| 327 | break; |
| 328 | |
| 329 | case Type::Pointer: |
| 330 | if (!IsStructurallyEquivalent(Context, |
| 331 | cast<PointerType>(T1)->getPointeeType(), |
| 332 | cast<PointerType>(T2)->getPointeeType())) |
| 333 | return false; |
| 334 | break; |
| 335 | |
| 336 | case Type::BlockPointer: |
| 337 | if (!IsStructurallyEquivalent(Context, |
| 338 | cast<BlockPointerType>(T1)->getPointeeType(), |
| 339 | cast<BlockPointerType>(T2)->getPointeeType())) |
| 340 | return false; |
| 341 | break; |
| 342 | |
| 343 | case Type::LValueReference: |
| 344 | case Type::RValueReference: { |
| 345 | const ReferenceType *Ref1 = cast<ReferenceType>(T1); |
| 346 | const ReferenceType *Ref2 = cast<ReferenceType>(T2); |
| 347 | if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue()) |
| 348 | return false; |
| 349 | if (Ref1->isInnerRef() != Ref2->isInnerRef()) |
| 350 | return false; |
| 351 | if (!IsStructurallyEquivalent(Context, |
| 352 | Ref1->getPointeeTypeAsWritten(), |
| 353 | Ref2->getPointeeTypeAsWritten())) |
| 354 | return false; |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | case Type::MemberPointer: { |
| 359 | const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1); |
| 360 | const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2); |
| 361 | if (!IsStructurallyEquivalent(Context, |
| 362 | MemPtr1->getPointeeType(), |
| 363 | MemPtr2->getPointeeType())) |
| 364 | return false; |
| 365 | if (!IsStructurallyEquivalent(Context, |
| 366 | QualType(MemPtr1->getClass(), 0), |
| 367 | QualType(MemPtr2->getClass(), 0))) |
| 368 | return false; |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | case Type::ConstantArray: { |
| 373 | const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1); |
| 374 | const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2); |
| 375 | if (!IsSameValue(Array1->getSize(), Array2->getSize())) |
| 376 | return false; |
| 377 | |
| 378 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 379 | return false; |
| 380 | break; |
| 381 | } |
| 382 | |
| 383 | case Type::IncompleteArray: |
| 384 | if (!IsArrayStructurallyEquivalent(Context, |
| 385 | cast<ArrayType>(T1), |
| 386 | cast<ArrayType>(T2))) |
| 387 | return false; |
| 388 | break; |
| 389 | |
| 390 | case Type::VariableArray: { |
| 391 | const VariableArrayType *Array1 = cast<VariableArrayType>(T1); |
| 392 | const VariableArrayType *Array2 = cast<VariableArrayType>(T2); |
| 393 | if (!IsStructurallyEquivalent(Context, |
| 394 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 395 | return false; |
| 396 | |
| 397 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 398 | return false; |
| 399 | |
| 400 | break; |
| 401 | } |
| 402 | |
| 403 | case Type::DependentSizedArray: { |
| 404 | const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1); |
| 405 | const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2); |
| 406 | if (!IsStructurallyEquivalent(Context, |
| 407 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 408 | return false; |
| 409 | |
| 410 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 411 | return false; |
| 412 | |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | case Type::DependentSizedExtVector: { |
| 417 | const DependentSizedExtVectorType *Vec1 |
| 418 | = cast<DependentSizedExtVectorType>(T1); |
| 419 | const DependentSizedExtVectorType *Vec2 |
| 420 | = cast<DependentSizedExtVectorType>(T2); |
| 421 | if (!IsStructurallyEquivalent(Context, |
| 422 | Vec1->getSizeExpr(), Vec2->getSizeExpr())) |
| 423 | return false; |
| 424 | if (!IsStructurallyEquivalent(Context, |
| 425 | Vec1->getElementType(), |
| 426 | Vec2->getElementType())) |
| 427 | return false; |
| 428 | break; |
| 429 | } |
| 430 | |
| 431 | case Type::Vector: |
| 432 | case Type::ExtVector: { |
| 433 | const VectorType *Vec1 = cast<VectorType>(T1); |
| 434 | const VectorType *Vec2 = cast<VectorType>(T2); |
| 435 | if (!IsStructurallyEquivalent(Context, |
| 436 | Vec1->getElementType(), |
| 437 | Vec2->getElementType())) |
| 438 | return false; |
| 439 | if (Vec1->getNumElements() != Vec2->getNumElements()) |
| 440 | return false; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 441 | if (Vec1->getVectorKind() != Vec2->getVectorKind()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 442 | return false; |
Douglas Gregor | 01cc437 | 2010-02-19 01:36:36 +0000 | [diff] [blame] | 443 | break; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | case Type::FunctionProto: { |
| 447 | const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1); |
| 448 | const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2); |
| 449 | if (Proto1->getNumArgs() != Proto2->getNumArgs()) |
| 450 | return false; |
| 451 | for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) { |
| 452 | if (!IsStructurallyEquivalent(Context, |
| 453 | Proto1->getArgType(I), |
| 454 | Proto2->getArgType(I))) |
| 455 | return false; |
| 456 | } |
| 457 | if (Proto1->isVariadic() != Proto2->isVariadic()) |
| 458 | return false; |
| 459 | if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec()) |
| 460 | return false; |
| 461 | if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec()) |
| 462 | return false; |
| 463 | if (Proto1->getNumExceptions() != Proto2->getNumExceptions()) |
| 464 | return false; |
| 465 | for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) { |
| 466 | if (!IsStructurallyEquivalent(Context, |
| 467 | Proto1->getExceptionType(I), |
| 468 | Proto2->getExceptionType(I))) |
| 469 | return false; |
| 470 | } |
| 471 | if (Proto1->getTypeQuals() != Proto2->getTypeQuals()) |
| 472 | return false; |
| 473 | |
| 474 | // Fall through to check the bits common with FunctionNoProtoType. |
| 475 | } |
| 476 | |
| 477 | case Type::FunctionNoProto: { |
| 478 | const FunctionType *Function1 = cast<FunctionType>(T1); |
| 479 | const FunctionType *Function2 = cast<FunctionType>(T2); |
| 480 | if (!IsStructurallyEquivalent(Context, |
| 481 | Function1->getResultType(), |
| 482 | Function2->getResultType())) |
| 483 | return false; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 484 | if (Function1->getExtInfo() != Function2->getExtInfo()) |
| 485 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | |
| 489 | case Type::UnresolvedUsing: |
| 490 | if (!IsStructurallyEquivalent(Context, |
| 491 | cast<UnresolvedUsingType>(T1)->getDecl(), |
| 492 | cast<UnresolvedUsingType>(T2)->getDecl())) |
| 493 | return false; |
| 494 | |
| 495 | break; |
| 496 | |
| 497 | case Type::Typedef: |
| 498 | if (!IsStructurallyEquivalent(Context, |
| 499 | cast<TypedefType>(T1)->getDecl(), |
| 500 | cast<TypedefType>(T2)->getDecl())) |
| 501 | return false; |
| 502 | break; |
| 503 | |
| 504 | case Type::TypeOfExpr: |
| 505 | if (!IsStructurallyEquivalent(Context, |
| 506 | cast<TypeOfExprType>(T1)->getUnderlyingExpr(), |
| 507 | cast<TypeOfExprType>(T2)->getUnderlyingExpr())) |
| 508 | return false; |
| 509 | break; |
| 510 | |
| 511 | case Type::TypeOf: |
| 512 | if (!IsStructurallyEquivalent(Context, |
| 513 | cast<TypeOfType>(T1)->getUnderlyingType(), |
| 514 | cast<TypeOfType>(T2)->getUnderlyingType())) |
| 515 | return false; |
| 516 | break; |
| 517 | |
| 518 | case Type::Decltype: |
| 519 | if (!IsStructurallyEquivalent(Context, |
| 520 | cast<DecltypeType>(T1)->getUnderlyingExpr(), |
| 521 | cast<DecltypeType>(T2)->getUnderlyingExpr())) |
| 522 | return false; |
| 523 | break; |
| 524 | |
| 525 | case Type::Record: |
| 526 | case Type::Enum: |
| 527 | if (!IsStructurallyEquivalent(Context, |
| 528 | cast<TagType>(T1)->getDecl(), |
| 529 | cast<TagType>(T2)->getDecl())) |
| 530 | return false; |
| 531 | break; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 532 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 533 | case Type::TemplateTypeParm: { |
| 534 | const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1); |
| 535 | const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2); |
| 536 | if (Parm1->getDepth() != Parm2->getDepth()) |
| 537 | return false; |
| 538 | if (Parm1->getIndex() != Parm2->getIndex()) |
| 539 | return false; |
| 540 | if (Parm1->isParameterPack() != Parm2->isParameterPack()) |
| 541 | return false; |
| 542 | |
| 543 | // Names of template type parameters are never significant. |
| 544 | break; |
| 545 | } |
| 546 | |
| 547 | case Type::SubstTemplateTypeParm: { |
| 548 | const SubstTemplateTypeParmType *Subst1 |
| 549 | = cast<SubstTemplateTypeParmType>(T1); |
| 550 | const SubstTemplateTypeParmType *Subst2 |
| 551 | = cast<SubstTemplateTypeParmType>(T2); |
| 552 | if (!IsStructurallyEquivalent(Context, |
| 553 | QualType(Subst1->getReplacedParameter(), 0), |
| 554 | QualType(Subst2->getReplacedParameter(), 0))) |
| 555 | return false; |
| 556 | if (!IsStructurallyEquivalent(Context, |
| 557 | Subst1->getReplacementType(), |
| 558 | Subst2->getReplacementType())) |
| 559 | return false; |
| 560 | break; |
| 561 | } |
| 562 | |
| 563 | case Type::TemplateSpecialization: { |
| 564 | const TemplateSpecializationType *Spec1 |
| 565 | = cast<TemplateSpecializationType>(T1); |
| 566 | const TemplateSpecializationType *Spec2 |
| 567 | = cast<TemplateSpecializationType>(T2); |
| 568 | if (!IsStructurallyEquivalent(Context, |
| 569 | Spec1->getTemplateName(), |
| 570 | Spec2->getTemplateName())) |
| 571 | return false; |
| 572 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 573 | return false; |
| 574 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 575 | if (!IsStructurallyEquivalent(Context, |
| 576 | Spec1->getArg(I), Spec2->getArg(I))) |
| 577 | return false; |
| 578 | } |
| 579 | break; |
| 580 | } |
| 581 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 582 | case Type::Elaborated: { |
| 583 | const ElaboratedType *Elab1 = cast<ElaboratedType>(T1); |
| 584 | const ElaboratedType *Elab2 = cast<ElaboratedType>(T2); |
| 585 | // CHECKME: what if a keyword is ETK_None or ETK_typename ? |
| 586 | if (Elab1->getKeyword() != Elab2->getKeyword()) |
| 587 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 588 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 589 | Elab1->getQualifier(), |
| 590 | Elab2->getQualifier())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 591 | return false; |
| 592 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 593 | Elab1->getNamedType(), |
| 594 | Elab2->getNamedType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 595 | return false; |
| 596 | break; |
| 597 | } |
| 598 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 599 | case Type::InjectedClassName: { |
| 600 | const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1); |
| 601 | const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2); |
| 602 | if (!IsStructurallyEquivalent(Context, |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 603 | Inj1->getInjectedSpecializationType(), |
| 604 | Inj2->getInjectedSpecializationType())) |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 605 | return false; |
| 606 | break; |
| 607 | } |
| 608 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 609 | case Type::DependentName: { |
| 610 | const DependentNameType *Typename1 = cast<DependentNameType>(T1); |
| 611 | const DependentNameType *Typename2 = cast<DependentNameType>(T2); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 612 | if (!IsStructurallyEquivalent(Context, |
| 613 | Typename1->getQualifier(), |
| 614 | Typename2->getQualifier())) |
| 615 | return false; |
| 616 | if (!IsStructurallyEquivalent(Typename1->getIdentifier(), |
| 617 | Typename2->getIdentifier())) |
| 618 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 619 | |
| 620 | break; |
| 621 | } |
| 622 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 623 | case Type::DependentTemplateSpecialization: { |
| 624 | const DependentTemplateSpecializationType *Spec1 = |
| 625 | cast<DependentTemplateSpecializationType>(T1); |
| 626 | const DependentTemplateSpecializationType *Spec2 = |
| 627 | cast<DependentTemplateSpecializationType>(T2); |
| 628 | if (!IsStructurallyEquivalent(Context, |
| 629 | Spec1->getQualifier(), |
| 630 | Spec2->getQualifier())) |
| 631 | return false; |
| 632 | if (!IsStructurallyEquivalent(Spec1->getIdentifier(), |
| 633 | Spec2->getIdentifier())) |
| 634 | return false; |
| 635 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 636 | return false; |
| 637 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 638 | if (!IsStructurallyEquivalent(Context, |
| 639 | Spec1->getArg(I), Spec2->getArg(I))) |
| 640 | return false; |
| 641 | } |
| 642 | break; |
| 643 | } |
| 644 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 645 | case Type::ObjCInterface: { |
| 646 | const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1); |
| 647 | const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2); |
| 648 | if (!IsStructurallyEquivalent(Context, |
| 649 | Iface1->getDecl(), Iface2->getDecl())) |
| 650 | return false; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 651 | break; |
| 652 | } |
| 653 | |
| 654 | case Type::ObjCObject: { |
| 655 | const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1); |
| 656 | const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2); |
| 657 | if (!IsStructurallyEquivalent(Context, |
| 658 | Obj1->getBaseType(), |
| 659 | Obj2->getBaseType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 660 | return false; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 661 | if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) |
| 662 | return false; |
| 663 | for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 664 | if (!IsStructurallyEquivalent(Context, |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 665 | Obj1->getProtocol(I), |
| 666 | Obj2->getProtocol(I))) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 667 | return false; |
| 668 | } |
| 669 | break; |
| 670 | } |
| 671 | |
| 672 | case Type::ObjCObjectPointer: { |
| 673 | const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1); |
| 674 | const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2); |
| 675 | if (!IsStructurallyEquivalent(Context, |
| 676 | Ptr1->getPointeeType(), |
| 677 | Ptr2->getPointeeType())) |
| 678 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 679 | break; |
| 680 | } |
| 681 | |
| 682 | } // end switch |
| 683 | |
| 684 | return true; |
| 685 | } |
| 686 | |
| 687 | /// \brief Determine structural equivalence of two records. |
| 688 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 689 | RecordDecl *D1, RecordDecl *D2) { |
| 690 | if (D1->isUnion() != D2->isUnion()) { |
| 691 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 692 | << Context.C2.getTypeDeclType(D2); |
| 693 | Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here) |
| 694 | << D1->getDeclName() << (unsigned)D1->getTagKind(); |
| 695 | return false; |
| 696 | } |
| 697 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 698 | // Compare the definitions of these two records. If either or both are |
| 699 | // incomplete, we assume that they are equivalent. |
| 700 | D1 = D1->getDefinition(); |
| 701 | D2 = D2->getDefinition(); |
| 702 | if (!D1 || !D2) |
| 703 | return true; |
| 704 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 705 | if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) { |
| 706 | if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) { |
| 707 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) { |
| 708 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 709 | << Context.C2.getTypeDeclType(D2); |
| 710 | Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases) |
| 711 | << D2CXX->getNumBases(); |
| 712 | Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases) |
| 713 | << D1CXX->getNumBases(); |
| 714 | return false; |
| 715 | } |
| 716 | |
| 717 | // Check the base classes. |
| 718 | for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(), |
| 719 | BaseEnd1 = D1CXX->bases_end(), |
| 720 | Base2 = D2CXX->bases_begin(); |
| 721 | Base1 != BaseEnd1; |
| 722 | ++Base1, ++Base2) { |
| 723 | if (!IsStructurallyEquivalent(Context, |
| 724 | Base1->getType(), Base2->getType())) { |
| 725 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 726 | << Context.C2.getTypeDeclType(D2); |
| 727 | Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base) |
| 728 | << Base2->getType() |
| 729 | << Base2->getSourceRange(); |
| 730 | Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base) |
| 731 | << Base1->getType() |
| 732 | << Base1->getSourceRange(); |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | // Check virtual vs. non-virtual inheritance mismatch. |
| 737 | if (Base1->isVirtual() != Base2->isVirtual()) { |
| 738 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 739 | << Context.C2.getTypeDeclType(D2); |
| 740 | Context.Diag2(Base2->getSourceRange().getBegin(), |
| 741 | diag::note_odr_virtual_base) |
| 742 | << Base2->isVirtual() << Base2->getSourceRange(); |
| 743 | Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base) |
| 744 | << Base1->isVirtual() |
| 745 | << Base1->getSourceRange(); |
| 746 | return false; |
| 747 | } |
| 748 | } |
| 749 | } else if (D1CXX->getNumBases() > 0) { |
| 750 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 751 | << Context.C2.getTypeDeclType(D2); |
| 752 | const CXXBaseSpecifier *Base1 = D1CXX->bases_begin(); |
| 753 | Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base) |
| 754 | << Base1->getType() |
| 755 | << Base1->getSourceRange(); |
| 756 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_base); |
| 757 | return false; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | // Check the fields for consistency. |
| 762 | CXXRecordDecl::field_iterator Field2 = D2->field_begin(), |
| 763 | Field2End = D2->field_end(); |
| 764 | for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(), |
| 765 | Field1End = D1->field_end(); |
| 766 | Field1 != Field1End; |
| 767 | ++Field1, ++Field2) { |
| 768 | if (Field2 == Field2End) { |
| 769 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 770 | << Context.C2.getTypeDeclType(D2); |
| 771 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 772 | << Field1->getDeclName() << Field1->getType(); |
| 773 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_field); |
| 774 | return false; |
| 775 | } |
| 776 | |
| 777 | if (!IsStructurallyEquivalent(Context, |
| 778 | Field1->getType(), Field2->getType())) { |
| 779 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 780 | << Context.C2.getTypeDeclType(D2); |
| 781 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 782 | << Field2->getDeclName() << Field2->getType(); |
| 783 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 784 | << Field1->getDeclName() << Field1->getType(); |
| 785 | return false; |
| 786 | } |
| 787 | |
| 788 | if (Field1->isBitField() != Field2->isBitField()) { |
| 789 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 790 | << Context.C2.getTypeDeclType(D2); |
| 791 | if (Field1->isBitField()) { |
| 792 | llvm::APSInt Bits; |
| 793 | Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1); |
| 794 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 795 | << Field1->getDeclName() << Field1->getType() |
| 796 | << Bits.toString(10, false); |
| 797 | Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field) |
| 798 | << Field2->getDeclName(); |
| 799 | } else { |
| 800 | llvm::APSInt Bits; |
| 801 | Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2); |
| 802 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 803 | << Field2->getDeclName() << Field2->getType() |
| 804 | << Bits.toString(10, false); |
| 805 | Context.Diag1(Field1->getLocation(), |
| 806 | diag::note_odr_not_bit_field) |
| 807 | << Field1->getDeclName(); |
| 808 | } |
| 809 | return false; |
| 810 | } |
| 811 | |
| 812 | if (Field1->isBitField()) { |
| 813 | // Make sure that the bit-fields are the same length. |
| 814 | llvm::APSInt Bits1, Bits2; |
| 815 | if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1)) |
| 816 | return false; |
| 817 | if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2)) |
| 818 | return false; |
| 819 | |
| 820 | if (!IsSameValue(Bits1, Bits2)) { |
| 821 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 822 | << Context.C2.getTypeDeclType(D2); |
| 823 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 824 | << Field2->getDeclName() << Field2->getType() |
| 825 | << Bits2.toString(10, false); |
| 826 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 827 | << Field1->getDeclName() << Field1->getType() |
| 828 | << Bits1.toString(10, false); |
| 829 | return false; |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | if (Field2 != Field2End) { |
| 835 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 836 | << Context.C2.getTypeDeclType(D2); |
| 837 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 838 | << Field2->getDeclName() << Field2->getType(); |
| 839 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_field); |
| 840 | return false; |
| 841 | } |
| 842 | |
| 843 | return true; |
| 844 | } |
| 845 | |
| 846 | /// \brief Determine structural equivalence of two enums. |
| 847 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 848 | EnumDecl *D1, EnumDecl *D2) { |
| 849 | EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(), |
| 850 | EC2End = D2->enumerator_end(); |
| 851 | for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(), |
| 852 | EC1End = D1->enumerator_end(); |
| 853 | EC1 != EC1End; ++EC1, ++EC2) { |
| 854 | if (EC2 == EC2End) { |
| 855 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 856 | << Context.C2.getTypeDeclType(D2); |
| 857 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 858 | << EC1->getDeclName() |
| 859 | << EC1->getInitVal().toString(10); |
| 860 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); |
| 861 | return false; |
| 862 | } |
| 863 | |
| 864 | llvm::APSInt Val1 = EC1->getInitVal(); |
| 865 | llvm::APSInt Val2 = EC2->getInitVal(); |
| 866 | if (!IsSameValue(Val1, Val2) || |
| 867 | !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) { |
| 868 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 869 | << Context.C2.getTypeDeclType(D2); |
| 870 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 871 | << EC2->getDeclName() |
| 872 | << EC2->getInitVal().toString(10); |
| 873 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 874 | << EC1->getDeclName() |
| 875 | << EC1->getInitVal().toString(10); |
| 876 | return false; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | if (EC2 != EC2End) { |
| 881 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 882 | << Context.C2.getTypeDeclType(D2); |
| 883 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 884 | << EC2->getDeclName() |
| 885 | << EC2->getInitVal().toString(10); |
| 886 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); |
| 887 | return false; |
| 888 | } |
| 889 | |
| 890 | return true; |
| 891 | } |
| 892 | |
| 893 | /// \brief Determine structural equivalence of two declarations. |
| 894 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 895 | Decl *D1, Decl *D2) { |
| 896 | // FIXME: Check for known structural equivalences via a callback of some sort. |
| 897 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 898 | // Check whether we already know that these two declarations are not |
| 899 | // structurally equivalent. |
| 900 | if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(), |
| 901 | D2->getCanonicalDecl()))) |
| 902 | return false; |
| 903 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 904 | // Determine whether we've already produced a tentative equivalence for D1. |
| 905 | Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()]; |
| 906 | if (EquivToD1) |
| 907 | return EquivToD1 == D2->getCanonicalDecl(); |
| 908 | |
| 909 | // Produce a tentative equivalence D1 <-> D2, which will be checked later. |
| 910 | EquivToD1 = D2->getCanonicalDecl(); |
| 911 | Context.DeclsToCheck.push_back(D1->getCanonicalDecl()); |
| 912 | return true; |
| 913 | } |
| 914 | |
| 915 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1, |
| 916 | Decl *D2) { |
| 917 | if (!::IsStructurallyEquivalent(*this, D1, D2)) |
| 918 | return false; |
| 919 | |
| 920 | return !Finish(); |
| 921 | } |
| 922 | |
| 923 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1, |
| 924 | QualType T2) { |
| 925 | if (!::IsStructurallyEquivalent(*this, T1, T2)) |
| 926 | return false; |
| 927 | |
| 928 | return !Finish(); |
| 929 | } |
| 930 | |
| 931 | bool StructuralEquivalenceContext::Finish() { |
| 932 | while (!DeclsToCheck.empty()) { |
| 933 | // Check the next declaration. |
| 934 | Decl *D1 = DeclsToCheck.front(); |
| 935 | DeclsToCheck.pop_front(); |
| 936 | |
| 937 | Decl *D2 = TentativeEquivalences[D1]; |
| 938 | assert(D2 && "Unrecorded tentative equivalence?"); |
| 939 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 940 | bool Equivalent = true; |
| 941 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 942 | // FIXME: Switch on all declaration kinds. For now, we're just going to |
| 943 | // check the obvious ones. |
| 944 | if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) { |
| 945 | if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) { |
| 946 | // Check for equivalent structure names. |
| 947 | IdentifierInfo *Name1 = Record1->getIdentifier(); |
| 948 | if (!Name1 && Record1->getTypedefForAnonDecl()) |
| 949 | Name1 = Record1->getTypedefForAnonDecl()->getIdentifier(); |
| 950 | IdentifierInfo *Name2 = Record2->getIdentifier(); |
| 951 | if (!Name2 && Record2->getTypedefForAnonDecl()) |
| 952 | Name2 = Record2->getTypedefForAnonDecl()->getIdentifier(); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 953 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 954 | !::IsStructurallyEquivalent(*this, Record1, Record2)) |
| 955 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 956 | } else { |
| 957 | // Record/non-record mismatch. |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 958 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 959 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 960 | } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 961 | if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) { |
| 962 | // Check for equivalent enum names. |
| 963 | IdentifierInfo *Name1 = Enum1->getIdentifier(); |
| 964 | if (!Name1 && Enum1->getTypedefForAnonDecl()) |
| 965 | Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier(); |
| 966 | IdentifierInfo *Name2 = Enum2->getIdentifier(); |
| 967 | if (!Name2 && Enum2->getTypedefForAnonDecl()) |
| 968 | Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier(); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 969 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 970 | !::IsStructurallyEquivalent(*this, Enum1, Enum2)) |
| 971 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 972 | } else { |
| 973 | // Enum/non-enum mismatch |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 974 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 975 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 976 | } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 977 | if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) { |
| 978 | if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 979 | Typedef2->getIdentifier()) || |
| 980 | !::IsStructurallyEquivalent(*this, |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 981 | Typedef1->getUnderlyingType(), |
| 982 | Typedef2->getUnderlyingType())) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 983 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 984 | } else { |
| 985 | // Typedef/non-typedef mismatch. |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 986 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 987 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 988 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 989 | |
| 990 | if (!Equivalent) { |
| 991 | // Note that these two declarations are not equivalent (and we already |
| 992 | // know about it). |
| 993 | NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(), |
| 994 | D2->getCanonicalDecl())); |
| 995 | return true; |
| 996 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 997 | // FIXME: Check other declaration kinds! |
| 998 | } |
| 999 | |
| 1000 | return false; |
| 1001 | } |
| 1002 | |
| 1003 | //---------------------------------------------------------------------------- |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1004 | // Import Types |
| 1005 | //---------------------------------------------------------------------------- |
| 1006 | |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 1007 | QualType ASTNodeImporter::VisitType(Type *T) { |
| 1008 | Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) |
| 1009 | << T->getTypeClassName(); |
| 1010 | return QualType(); |
| 1011 | } |
| 1012 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1013 | QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) { |
| 1014 | switch (T->getKind()) { |
| 1015 | case BuiltinType::Void: return Importer.getToContext().VoidTy; |
| 1016 | case BuiltinType::Bool: return Importer.getToContext().BoolTy; |
| 1017 | |
| 1018 | case BuiltinType::Char_U: |
| 1019 | // The context we're importing from has an unsigned 'char'. If we're |
| 1020 | // importing into a context with a signed 'char', translate to |
| 1021 | // 'unsigned char' instead. |
| 1022 | if (Importer.getToContext().getLangOptions().CharIsSigned) |
| 1023 | return Importer.getToContext().UnsignedCharTy; |
| 1024 | |
| 1025 | return Importer.getToContext().CharTy; |
| 1026 | |
| 1027 | case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy; |
| 1028 | |
| 1029 | case BuiltinType::Char16: |
| 1030 | // FIXME: Make sure that the "to" context supports C++! |
| 1031 | return Importer.getToContext().Char16Ty; |
| 1032 | |
| 1033 | case BuiltinType::Char32: |
| 1034 | // FIXME: Make sure that the "to" context supports C++! |
| 1035 | return Importer.getToContext().Char32Ty; |
| 1036 | |
| 1037 | case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy; |
| 1038 | case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy; |
| 1039 | case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy; |
| 1040 | case BuiltinType::ULongLong: |
| 1041 | return Importer.getToContext().UnsignedLongLongTy; |
| 1042 | case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty; |
| 1043 | |
| 1044 | case BuiltinType::Char_S: |
| 1045 | // The context we're importing from has an unsigned 'char'. If we're |
| 1046 | // importing into a context with a signed 'char', translate to |
| 1047 | // 'unsigned char' instead. |
| 1048 | if (!Importer.getToContext().getLangOptions().CharIsSigned) |
| 1049 | return Importer.getToContext().SignedCharTy; |
| 1050 | |
| 1051 | return Importer.getToContext().CharTy; |
| 1052 | |
| 1053 | case BuiltinType::SChar: return Importer.getToContext().SignedCharTy; |
| 1054 | case BuiltinType::WChar: |
| 1055 | // FIXME: If not in C++, shall we translate to the C equivalent of |
| 1056 | // wchar_t? |
| 1057 | return Importer.getToContext().WCharTy; |
| 1058 | |
| 1059 | case BuiltinType::Short : return Importer.getToContext().ShortTy; |
| 1060 | case BuiltinType::Int : return Importer.getToContext().IntTy; |
| 1061 | case BuiltinType::Long : return Importer.getToContext().LongTy; |
| 1062 | case BuiltinType::LongLong : return Importer.getToContext().LongLongTy; |
| 1063 | case BuiltinType::Int128 : return Importer.getToContext().Int128Ty; |
| 1064 | case BuiltinType::Float: return Importer.getToContext().FloatTy; |
| 1065 | case BuiltinType::Double: return Importer.getToContext().DoubleTy; |
| 1066 | case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy; |
| 1067 | |
| 1068 | case BuiltinType::NullPtr: |
| 1069 | // FIXME: Make sure that the "to" context supports C++0x! |
| 1070 | return Importer.getToContext().NullPtrTy; |
| 1071 | |
| 1072 | case BuiltinType::Overload: return Importer.getToContext().OverloadTy; |
| 1073 | case BuiltinType::Dependent: return Importer.getToContext().DependentTy; |
| 1074 | case BuiltinType::UndeducedAuto: |
| 1075 | // FIXME: Make sure that the "to" context supports C++0x! |
| 1076 | return Importer.getToContext().UndeducedAutoTy; |
| 1077 | |
| 1078 | case BuiltinType::ObjCId: |
| 1079 | // FIXME: Make sure that the "to" context supports Objective-C! |
| 1080 | return Importer.getToContext().ObjCBuiltinIdTy; |
| 1081 | |
| 1082 | case BuiltinType::ObjCClass: |
| 1083 | return Importer.getToContext().ObjCBuiltinClassTy; |
| 1084 | |
| 1085 | case BuiltinType::ObjCSel: |
| 1086 | return Importer.getToContext().ObjCBuiltinSelTy; |
| 1087 | } |
| 1088 | |
| 1089 | return QualType(); |
| 1090 | } |
| 1091 | |
| 1092 | QualType ASTNodeImporter::VisitComplexType(ComplexType *T) { |
| 1093 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1094 | if (ToElementType.isNull()) |
| 1095 | return QualType(); |
| 1096 | |
| 1097 | return Importer.getToContext().getComplexType(ToElementType); |
| 1098 | } |
| 1099 | |
| 1100 | QualType ASTNodeImporter::VisitPointerType(PointerType *T) { |
| 1101 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1102 | if (ToPointeeType.isNull()) |
| 1103 | return QualType(); |
| 1104 | |
| 1105 | return Importer.getToContext().getPointerType(ToPointeeType); |
| 1106 | } |
| 1107 | |
| 1108 | QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) { |
| 1109 | // FIXME: Check for blocks support in "to" context. |
| 1110 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1111 | if (ToPointeeType.isNull()) |
| 1112 | return QualType(); |
| 1113 | |
| 1114 | return Importer.getToContext().getBlockPointerType(ToPointeeType); |
| 1115 | } |
| 1116 | |
| 1117 | QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) { |
| 1118 | // FIXME: Check for C++ support in "to" context. |
| 1119 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1120 | if (ToPointeeType.isNull()) |
| 1121 | return QualType(); |
| 1122 | |
| 1123 | return Importer.getToContext().getLValueReferenceType(ToPointeeType); |
| 1124 | } |
| 1125 | |
| 1126 | QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) { |
| 1127 | // FIXME: Check for C++0x support in "to" context. |
| 1128 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1129 | if (ToPointeeType.isNull()) |
| 1130 | return QualType(); |
| 1131 | |
| 1132 | return Importer.getToContext().getRValueReferenceType(ToPointeeType); |
| 1133 | } |
| 1134 | |
| 1135 | QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) { |
| 1136 | // FIXME: Check for C++ support in "to" context. |
| 1137 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1138 | if (ToPointeeType.isNull()) |
| 1139 | return QualType(); |
| 1140 | |
| 1141 | QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); |
| 1142 | return Importer.getToContext().getMemberPointerType(ToPointeeType, |
| 1143 | ClassType.getTypePtr()); |
| 1144 | } |
| 1145 | |
| 1146 | QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) { |
| 1147 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1148 | if (ToElementType.isNull()) |
| 1149 | return QualType(); |
| 1150 | |
| 1151 | return Importer.getToContext().getConstantArrayType(ToElementType, |
| 1152 | T->getSize(), |
| 1153 | T->getSizeModifier(), |
| 1154 | T->getIndexTypeCVRQualifiers()); |
| 1155 | } |
| 1156 | |
| 1157 | QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) { |
| 1158 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1159 | if (ToElementType.isNull()) |
| 1160 | return QualType(); |
| 1161 | |
| 1162 | return Importer.getToContext().getIncompleteArrayType(ToElementType, |
| 1163 | T->getSizeModifier(), |
| 1164 | T->getIndexTypeCVRQualifiers()); |
| 1165 | } |
| 1166 | |
| 1167 | QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) { |
| 1168 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1169 | if (ToElementType.isNull()) |
| 1170 | return QualType(); |
| 1171 | |
| 1172 | Expr *Size = Importer.Import(T->getSizeExpr()); |
| 1173 | if (!Size) |
| 1174 | return QualType(); |
| 1175 | |
| 1176 | SourceRange Brackets = Importer.Import(T->getBracketsRange()); |
| 1177 | return Importer.getToContext().getVariableArrayType(ToElementType, Size, |
| 1178 | T->getSizeModifier(), |
| 1179 | T->getIndexTypeCVRQualifiers(), |
| 1180 | Brackets); |
| 1181 | } |
| 1182 | |
| 1183 | QualType ASTNodeImporter::VisitVectorType(VectorType *T) { |
| 1184 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1185 | if (ToElementType.isNull()) |
| 1186 | return QualType(); |
| 1187 | |
| 1188 | return Importer.getToContext().getVectorType(ToElementType, |
| 1189 | T->getNumElements(), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1190 | T->getVectorKind()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) { |
| 1194 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1195 | if (ToElementType.isNull()) |
| 1196 | return QualType(); |
| 1197 | |
| 1198 | return Importer.getToContext().getExtVectorType(ToElementType, |
| 1199 | T->getNumElements()); |
| 1200 | } |
| 1201 | |
| 1202 | QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) { |
| 1203 | // FIXME: What happens if we're importing a function without a prototype |
| 1204 | // into C++? Should we make it variadic? |
| 1205 | QualType ToResultType = Importer.Import(T->getResultType()); |
| 1206 | if (ToResultType.isNull()) |
| 1207 | return QualType(); |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1208 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1209 | return Importer.getToContext().getFunctionNoProtoType(ToResultType, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1210 | T->getExtInfo()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) { |
| 1214 | QualType ToResultType = Importer.Import(T->getResultType()); |
| 1215 | if (ToResultType.isNull()) |
| 1216 | return QualType(); |
| 1217 | |
| 1218 | // Import argument types |
| 1219 | llvm::SmallVector<QualType, 4> ArgTypes; |
| 1220 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
| 1221 | AEnd = T->arg_type_end(); |
| 1222 | A != AEnd; ++A) { |
| 1223 | QualType ArgType = Importer.Import(*A); |
| 1224 | if (ArgType.isNull()) |
| 1225 | return QualType(); |
| 1226 | ArgTypes.push_back(ArgType); |
| 1227 | } |
| 1228 | |
| 1229 | // Import exception types |
| 1230 | llvm::SmallVector<QualType, 4> ExceptionTypes; |
| 1231 | for (FunctionProtoType::exception_iterator E = T->exception_begin(), |
| 1232 | EEnd = T->exception_end(); |
| 1233 | E != EEnd; ++E) { |
| 1234 | QualType ExceptionType = Importer.Import(*E); |
| 1235 | if (ExceptionType.isNull()) |
| 1236 | return QualType(); |
| 1237 | ExceptionTypes.push_back(ExceptionType); |
| 1238 | } |
| 1239 | |
| 1240 | return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(), |
| 1241 | ArgTypes.size(), |
| 1242 | T->isVariadic(), |
| 1243 | T->getTypeQuals(), |
| 1244 | T->hasExceptionSpec(), |
| 1245 | T->hasAnyExceptionSpec(), |
| 1246 | ExceptionTypes.size(), |
| 1247 | ExceptionTypes.data(), |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1248 | T->getExtInfo()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) { |
| 1252 | TypedefDecl *ToDecl |
| 1253 | = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl())); |
| 1254 | if (!ToDecl) |
| 1255 | return QualType(); |
| 1256 | |
| 1257 | return Importer.getToContext().getTypeDeclType(ToDecl); |
| 1258 | } |
| 1259 | |
| 1260 | QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) { |
| 1261 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1262 | if (!ToExpr) |
| 1263 | return QualType(); |
| 1264 | |
| 1265 | return Importer.getToContext().getTypeOfExprType(ToExpr); |
| 1266 | } |
| 1267 | |
| 1268 | QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) { |
| 1269 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1270 | if (ToUnderlyingType.isNull()) |
| 1271 | return QualType(); |
| 1272 | |
| 1273 | return Importer.getToContext().getTypeOfType(ToUnderlyingType); |
| 1274 | } |
| 1275 | |
| 1276 | QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) { |
| 1277 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1278 | if (!ToExpr) |
| 1279 | return QualType(); |
| 1280 | |
| 1281 | return Importer.getToContext().getDecltypeType(ToExpr); |
| 1282 | } |
| 1283 | |
| 1284 | QualType ASTNodeImporter::VisitRecordType(RecordType *T) { |
| 1285 | RecordDecl *ToDecl |
| 1286 | = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); |
| 1287 | if (!ToDecl) |
| 1288 | return QualType(); |
| 1289 | |
| 1290 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 1291 | } |
| 1292 | |
| 1293 | QualType ASTNodeImporter::VisitEnumType(EnumType *T) { |
| 1294 | EnumDecl *ToDecl |
| 1295 | = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); |
| 1296 | if (!ToDecl) |
| 1297 | return QualType(); |
| 1298 | |
| 1299 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 1300 | } |
| 1301 | |
| 1302 | QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1303 | NestedNameSpecifier *ToQualifier = 0; |
| 1304 | // Note: the qualifier in an ElaboratedType is optional. |
| 1305 | if (T->getQualifier()) { |
| 1306 | ToQualifier = Importer.Import(T->getQualifier()); |
| 1307 | if (!ToQualifier) |
| 1308 | return QualType(); |
| 1309 | } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1310 | |
| 1311 | QualType ToNamedType = Importer.Import(T->getNamedType()); |
| 1312 | if (ToNamedType.isNull()) |
| 1313 | return QualType(); |
| 1314 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1315 | return Importer.getToContext().getElaboratedType(T->getKeyword(), |
| 1316 | ToQualifier, ToNamedType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) { |
| 1320 | ObjCInterfaceDecl *Class |
| 1321 | = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); |
| 1322 | if (!Class) |
| 1323 | return QualType(); |
| 1324 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1325 | return Importer.getToContext().getObjCInterfaceType(Class); |
| 1326 | } |
| 1327 | |
| 1328 | QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) { |
| 1329 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 1330 | if (ToBaseType.isNull()) |
| 1331 | return QualType(); |
| 1332 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1333 | llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1334 | for (ObjCObjectType::qual_iterator P = T->qual_begin(), |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1335 | PEnd = T->qual_end(); |
| 1336 | P != PEnd; ++P) { |
| 1337 | ObjCProtocolDecl *Protocol |
| 1338 | = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P)); |
| 1339 | if (!Protocol) |
| 1340 | return QualType(); |
| 1341 | Protocols.push_back(Protocol); |
| 1342 | } |
| 1343 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1344 | return Importer.getToContext().getObjCObjectType(ToBaseType, |
| 1345 | Protocols.data(), |
| 1346 | Protocols.size()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) { |
| 1350 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1351 | if (ToPointeeType.isNull()) |
| 1352 | return QualType(); |
| 1353 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1354 | return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 1357 | //---------------------------------------------------------------------------- |
| 1358 | // Import Declarations |
| 1359 | //---------------------------------------------------------------------------- |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1360 | bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 1361 | DeclContext *&LexicalDC, |
| 1362 | DeclarationName &Name, |
| 1363 | SourceLocation &Loc) { |
| 1364 | // Import the context of this declaration. |
| 1365 | DC = Importer.ImportContext(D->getDeclContext()); |
| 1366 | if (!DC) |
| 1367 | return true; |
| 1368 | |
| 1369 | LexicalDC = DC; |
| 1370 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 1371 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 1372 | if (!LexicalDC) |
| 1373 | return true; |
| 1374 | } |
| 1375 | |
| 1376 | // Import the name of this declaration. |
| 1377 | Name = Importer.Import(D->getDeclName()); |
| 1378 | if (D->getDeclName() && !Name) |
| 1379 | return true; |
| 1380 | |
| 1381 | // Import the location of this declaration. |
| 1382 | Loc = Importer.Import(D->getLocation()); |
| 1383 | return false; |
| 1384 | } |
| 1385 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1386 | void |
| 1387 | ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 1388 | DeclarationNameInfo& To) { |
| 1389 | // NOTE: To.Name and To.Loc are already imported. |
| 1390 | // We only have to import To.LocInfo. |
| 1391 | switch (To.getName().getNameKind()) { |
| 1392 | case DeclarationName::Identifier: |
| 1393 | case DeclarationName::ObjCZeroArgSelector: |
| 1394 | case DeclarationName::ObjCOneArgSelector: |
| 1395 | case DeclarationName::ObjCMultiArgSelector: |
| 1396 | case DeclarationName::CXXUsingDirective: |
| 1397 | return; |
| 1398 | |
| 1399 | case DeclarationName::CXXOperatorName: { |
| 1400 | SourceRange Range = From.getCXXOperatorNameRange(); |
| 1401 | To.setCXXOperatorNameRange(Importer.Import(Range)); |
| 1402 | return; |
| 1403 | } |
| 1404 | case DeclarationName::CXXLiteralOperatorName: { |
| 1405 | SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); |
| 1406 | To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); |
| 1407 | return; |
| 1408 | } |
| 1409 | case DeclarationName::CXXConstructorName: |
| 1410 | case DeclarationName::CXXDestructorName: |
| 1411 | case DeclarationName::CXXConversionFunctionName: { |
| 1412 | TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); |
| 1413 | To.setNamedTypeInfo(Importer.Import(FromTInfo)); |
| 1414 | return; |
| 1415 | } |
| 1416 | assert(0 && "Unknown name kind."); |
| 1417 | } |
| 1418 | } |
| 1419 | |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 1420 | void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) { |
| 1421 | for (DeclContext::decl_iterator From = FromDC->decls_begin(), |
| 1422 | FromEnd = FromDC->decls_end(); |
| 1423 | From != FromEnd; |
| 1424 | ++From) |
| 1425 | Importer.Import(*From); |
| 1426 | } |
| 1427 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1428 | bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1429 | RecordDecl *ToRecord) { |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 1430 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1431 | Importer.getToContext(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1432 | Importer.getNonEquivalentDecls()); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 1433 | return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1436 | bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 1437 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1438 | Importer.getToContext(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1439 | Importer.getNonEquivalentDecls()); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 1440 | return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 1443 | Decl *ASTNodeImporter::VisitDecl(Decl *D) { |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 1444 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 1445 | << D->getDeclKindName(); |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 1449 | Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1450 | // Import the major distinguishing characteristics of this namespace. |
| 1451 | DeclContext *DC, *LexicalDC; |
| 1452 | DeclarationName Name; |
| 1453 | SourceLocation Loc; |
| 1454 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 1455 | return 0; |
| 1456 | |
| 1457 | NamespaceDecl *MergeWithNamespace = 0; |
| 1458 | if (!Name) { |
| 1459 | // This is an anonymous namespace. Adopt an existing anonymous |
| 1460 | // namespace if we can. |
| 1461 | // FIXME: Not testable. |
| 1462 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 1463 | MergeWithNamespace = TU->getAnonymousNamespace(); |
| 1464 | else |
| 1465 | MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); |
| 1466 | } else { |
| 1467 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 1468 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 1469 | Lookup.first != Lookup.second; |
| 1470 | ++Lookup.first) { |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1471 | if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace)) |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 1472 | continue; |
| 1473 | |
| 1474 | if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) { |
| 1475 | MergeWithNamespace = FoundNS; |
| 1476 | ConflictingDecls.clear(); |
| 1477 | break; |
| 1478 | } |
| 1479 | |
| 1480 | ConflictingDecls.push_back(*Lookup.first); |
| 1481 | } |
| 1482 | |
| 1483 | if (!ConflictingDecls.empty()) { |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1484 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 1485 | ConflictingDecls.data(), |
| 1486 | ConflictingDecls.size()); |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | // Create the "to" namespace, if needed. |
| 1491 | NamespaceDecl *ToNamespace = MergeWithNamespace; |
| 1492 | if (!ToNamespace) { |
| 1493 | ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc, |
| 1494 | Name.getAsIdentifierInfo()); |
| 1495 | ToNamespace->setLexicalDeclContext(LexicalDC); |
| 1496 | LexicalDC->addDecl(ToNamespace); |
| 1497 | |
| 1498 | // If this is an anonymous namespace, register it as the anonymous |
| 1499 | // namespace within its context. |
| 1500 | if (!Name) { |
| 1501 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 1502 | TU->setAnonymousNamespace(ToNamespace); |
| 1503 | else |
| 1504 | cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); |
| 1505 | } |
| 1506 | } |
| 1507 | Importer.Imported(D, ToNamespace); |
| 1508 | |
| 1509 | ImportDeclContext(D); |
| 1510 | |
| 1511 | return ToNamespace; |
| 1512 | } |
| 1513 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 1514 | Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { |
| 1515 | // Import the major distinguishing characteristics of this typedef. |
| 1516 | DeclContext *DC, *LexicalDC; |
| 1517 | DeclarationName Name; |
| 1518 | SourceLocation Loc; |
| 1519 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 1520 | return 0; |
| 1521 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 1522 | // If this typedef is not in block scope, determine whether we've |
| 1523 | // seen a typedef with the same name (that we can merge with) or any |
| 1524 | // other entity by that name (which name lookup could conflict with). |
| 1525 | if (!DC->isFunctionOrMethod()) { |
| 1526 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 1527 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 1528 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 1529 | Lookup.first != Lookup.second; |
| 1530 | ++Lookup.first) { |
| 1531 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 1532 | continue; |
| 1533 | if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1534 | if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), |
| 1535 | FoundTypedef->getUnderlyingType())) |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 1536 | return Importer.Imported(D, FoundTypedef); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | ConflictingDecls.push_back(*Lookup.first); |
| 1540 | } |
| 1541 | |
| 1542 | if (!ConflictingDecls.empty()) { |
| 1543 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 1544 | ConflictingDecls.data(), |
| 1545 | ConflictingDecls.size()); |
| 1546 | if (!Name) |
| 1547 | return 0; |
| 1548 | } |
| 1549 | } |
| 1550 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1551 | // Import the underlying type of this typedef; |
| 1552 | QualType T = Importer.Import(D->getUnderlyingType()); |
| 1553 | if (T.isNull()) |
| 1554 | return 0; |
| 1555 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 1556 | // Create the new typedef node. |
| 1557 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 1558 | TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC, |
| 1559 | Loc, Name.getAsIdentifierInfo(), |
| 1560 | TInfo); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 1561 | ToTypedef->setAccess(D->getAccess()); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 1562 | ToTypedef->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 1563 | Importer.Imported(D, ToTypedef); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 1564 | LexicalDC->addDecl(ToTypedef); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1565 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 1566 | return ToTypedef; |
| 1567 | } |
| 1568 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1569 | Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { |
| 1570 | // Import the major distinguishing characteristics of this enum. |
| 1571 | DeclContext *DC, *LexicalDC; |
| 1572 | DeclarationName Name; |
| 1573 | SourceLocation Loc; |
| 1574 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 1575 | return 0; |
| 1576 | |
| 1577 | // Figure out what enum name we're looking for. |
| 1578 | unsigned IDNS = Decl::IDNS_Tag; |
| 1579 | DeclarationName SearchName = Name; |
| 1580 | if (!SearchName && D->getTypedefForAnonDecl()) { |
| 1581 | SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName()); |
| 1582 | IDNS = Decl::IDNS_Ordinary; |
| 1583 | } else if (Importer.getToContext().getLangOptions().CPlusPlus) |
| 1584 | IDNS |= Decl::IDNS_Ordinary; |
| 1585 | |
| 1586 | // We may already have an enum of the same name; try to find and match it. |
| 1587 | if (!DC->isFunctionOrMethod() && SearchName) { |
| 1588 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 1589 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 1590 | Lookup.first != Lookup.second; |
| 1591 | ++Lookup.first) { |
| 1592 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 1593 | continue; |
| 1594 | |
| 1595 | Decl *Found = *Lookup.first; |
| 1596 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) { |
| 1597 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 1598 | Found = Tag->getDecl(); |
| 1599 | } |
| 1600 | |
| 1601 | if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) { |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 1602 | if (IsStructuralMatch(D, FoundEnum)) |
| 1603 | return Importer.Imported(D, FoundEnum); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | ConflictingDecls.push_back(*Lookup.first); |
| 1607 | } |
| 1608 | |
| 1609 | if (!ConflictingDecls.empty()) { |
| 1610 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 1611 | ConflictingDecls.data(), |
| 1612 | ConflictingDecls.size()); |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | // Create the enum declaration. |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1617 | EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc, |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1618 | Name.getAsIdentifierInfo(), |
| 1619 | Importer.Import(D->getTagKeywordLoc()), |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1620 | 0, D->isScoped(), D->isFixed()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1621 | // Import the qualifier, if any. |
| 1622 | if (D->getQualifier()) { |
| 1623 | NestedNameSpecifier *NNS = Importer.Import(D->getQualifier()); |
| 1624 | SourceRange NNSRange = Importer.Import(D->getQualifierRange()); |
| 1625 | D2->setQualifierInfo(NNS, NNSRange); |
| 1626 | } |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 1627 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1628 | D2->setLexicalDeclContext(LexicalDC); |
| 1629 | Importer.Imported(D, D2); |
| 1630 | LexicalDC->addDecl(D2); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1631 | |
| 1632 | // Import the integer type. |
| 1633 | QualType ToIntegerType = Importer.Import(D->getIntegerType()); |
| 1634 | if (ToIntegerType.isNull()) |
| 1635 | return 0; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1636 | D2->setIntegerType(ToIntegerType); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1637 | |
| 1638 | // Import the definition |
| 1639 | if (D->isDefinition()) { |
| 1640 | QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D)); |
| 1641 | if (T.isNull()) |
| 1642 | return 0; |
| 1643 | |
| 1644 | QualType ToPromotionType = Importer.Import(D->getPromotionType()); |
| 1645 | if (ToPromotionType.isNull()) |
| 1646 | return 0; |
| 1647 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1648 | D2->startDefinition(); |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 1649 | ImportDeclContext(D); |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 1650 | |
| 1651 | // FIXME: we might need to merge the number of positive or negative bits |
| 1652 | // if the enumerator lists don't match. |
| 1653 | D2->completeDefinition(T, ToPromotionType, |
| 1654 | D->getNumPositiveBits(), |
| 1655 | D->getNumNegativeBits()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1658 | return D2; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1661 | Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { |
| 1662 | // If this record has a definition in the translation unit we're coming from, |
| 1663 | // but this particular declaration is not that definition, import the |
| 1664 | // definition and map to that. |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1665 | TagDecl *Definition = D->getDefinition(); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1666 | if (Definition && Definition != D) { |
| 1667 | Decl *ImportedDef = Importer.Import(Definition); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 1668 | if (!ImportedDef) |
| 1669 | return 0; |
| 1670 | |
| 1671 | return Importer.Imported(D, ImportedDef); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | // Import the major distinguishing characteristics of this record. |
| 1675 | DeclContext *DC, *LexicalDC; |
| 1676 | DeclarationName Name; |
| 1677 | SourceLocation Loc; |
| 1678 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 1679 | return 0; |
| 1680 | |
| 1681 | // Figure out what structure name we're looking for. |
| 1682 | unsigned IDNS = Decl::IDNS_Tag; |
| 1683 | DeclarationName SearchName = Name; |
| 1684 | if (!SearchName && D->getTypedefForAnonDecl()) { |
| 1685 | SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName()); |
| 1686 | IDNS = Decl::IDNS_Ordinary; |
| 1687 | } else if (Importer.getToContext().getLangOptions().CPlusPlus) |
| 1688 | IDNS |= Decl::IDNS_Ordinary; |
| 1689 | |
| 1690 | // We may already have a record of the same name; try to find and match it. |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 1691 | RecordDecl *AdoptDecl = 0; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1692 | if (!DC->isFunctionOrMethod() && SearchName) { |
| 1693 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 1694 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 1695 | Lookup.first != Lookup.second; |
| 1696 | ++Lookup.first) { |
| 1697 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 1698 | continue; |
| 1699 | |
| 1700 | Decl *Found = *Lookup.first; |
| 1701 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) { |
| 1702 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 1703 | Found = Tag->getDecl(); |
| 1704 | } |
| 1705 | |
| 1706 | if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) { |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 1707 | if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { |
| 1708 | if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) { |
| 1709 | // The record types structurally match, or the "from" translation |
| 1710 | // unit only had a forward declaration anyway; call it the same |
| 1711 | // function. |
| 1712 | // FIXME: For C++, we should also merge methods here. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 1713 | return Importer.Imported(D, FoundDef); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 1714 | } |
| 1715 | } else { |
| 1716 | // We have a forward declaration of this type, so adopt that forward |
| 1717 | // declaration rather than building a new one. |
| 1718 | AdoptDecl = FoundRecord; |
| 1719 | continue; |
| 1720 | } |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | ConflictingDecls.push_back(*Lookup.first); |
| 1724 | } |
| 1725 | |
| 1726 | if (!ConflictingDecls.empty()) { |
| 1727 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 1728 | ConflictingDecls.data(), |
| 1729 | ConflictingDecls.size()); |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | // Create the record declaration. |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1734 | RecordDecl *D2 = AdoptDecl; |
| 1735 | if (!D2) { |
John McCall | 1c70e99 | 2010-06-03 19:28:45 +0000 | [diff] [blame] | 1736 | if (isa<CXXRecordDecl>(D)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1737 | CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(), |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 1738 | D->getTagKind(), |
| 1739 | DC, Loc, |
| 1740 | Name.getAsIdentifierInfo(), |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1741 | Importer.Import(D->getTagKeywordLoc())); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1742 | D2 = D2CXX; |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 1743 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 1744 | } else { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1745 | D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 1746 | DC, Loc, |
| 1747 | Name.getAsIdentifierInfo(), |
| 1748 | Importer.Import(D->getTagKeywordLoc())); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1749 | } |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1750 | // Import the qualifier, if any. |
| 1751 | if (D->getQualifier()) { |
| 1752 | NestedNameSpecifier *NNS = Importer.Import(D->getQualifier()); |
| 1753 | SourceRange NNSRange = Importer.Import(D->getQualifierRange()); |
| 1754 | D2->setQualifierInfo(NNS, NNSRange); |
| 1755 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1756 | D2->setLexicalDeclContext(LexicalDC); |
| 1757 | LexicalDC->addDecl(D2); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1758 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 1759 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1760 | Importer.Imported(D, D2); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 1761 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1762 | if (D->isDefinition()) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1763 | D2->startDefinition(); |
John McCall | 1c70e99 | 2010-06-03 19:28:45 +0000 | [diff] [blame] | 1764 | |
| 1765 | // Add base classes. |
| 1766 | if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) { |
| 1767 | CXXRecordDecl *D1CXX = cast<CXXRecordDecl>(D); |
| 1768 | |
| 1769 | llvm::SmallVector<CXXBaseSpecifier *, 4> Bases; |
| 1770 | for (CXXRecordDecl::base_class_iterator |
| 1771 | Base1 = D1CXX->bases_begin(), |
| 1772 | FromBaseEnd = D1CXX->bases_end(); |
| 1773 | Base1 != FromBaseEnd; |
| 1774 | ++Base1) { |
| 1775 | QualType T = Importer.Import(Base1->getType()); |
| 1776 | if (T.isNull()) |
| 1777 | return 0; |
| 1778 | |
| 1779 | Bases.push_back( |
| 1780 | new (Importer.getToContext()) |
| 1781 | CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()), |
| 1782 | Base1->isVirtual(), |
| 1783 | Base1->isBaseOfClass(), |
| 1784 | Base1->getAccessSpecifierAsWritten(), |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1785 | Importer.Import(Base1->getTypeSourceInfo()))); |
John McCall | 1c70e99 | 2010-06-03 19:28:45 +0000 | [diff] [blame] | 1786 | } |
| 1787 | if (!Bases.empty()) |
| 1788 | D2CXX->setBases(Bases.data(), Bases.size()); |
| 1789 | } |
| 1790 | |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 1791 | ImportDeclContext(D); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1792 | D2->completeDefinition(); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1795 | return D2; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1798 | Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 1799 | // Import the major distinguishing characteristics of this enumerator. |
| 1800 | DeclContext *DC, *LexicalDC; |
| 1801 | DeclarationName Name; |
| 1802 | SourceLocation Loc; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1803 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1804 | return 0; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1805 | |
| 1806 | QualType T = Importer.Import(D->getType()); |
| 1807 | if (T.isNull()) |
| 1808 | return 0; |
| 1809 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1810 | // Determine whether there are any other declarations with the same name and |
| 1811 | // in the same context. |
| 1812 | if (!LexicalDC->isFunctionOrMethod()) { |
| 1813 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 1814 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 1815 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 1816 | Lookup.first != Lookup.second; |
| 1817 | ++Lookup.first) { |
| 1818 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 1819 | continue; |
| 1820 | |
| 1821 | ConflictingDecls.push_back(*Lookup.first); |
| 1822 | } |
| 1823 | |
| 1824 | if (!ConflictingDecls.empty()) { |
| 1825 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 1826 | ConflictingDecls.data(), |
| 1827 | ConflictingDecls.size()); |
| 1828 | if (!Name) |
| 1829 | return 0; |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | Expr *Init = Importer.Import(D->getInitExpr()); |
| 1834 | if (D->getInitExpr() && !Init) |
| 1835 | return 0; |
| 1836 | |
| 1837 | EnumConstantDecl *ToEnumerator |
| 1838 | = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc, |
| 1839 | Name.getAsIdentifierInfo(), T, |
| 1840 | Init, D->getInitVal()); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 1841 | ToEnumerator->setAccess(D->getAccess()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1842 | ToEnumerator->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 1843 | Importer.Imported(D, ToEnumerator); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1844 | LexicalDC->addDecl(ToEnumerator); |
| 1845 | return ToEnumerator; |
| 1846 | } |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1847 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1848 | Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { |
| 1849 | // Import the major distinguishing characteristics of this function. |
| 1850 | DeclContext *DC, *LexicalDC; |
| 1851 | DeclarationName Name; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1852 | SourceLocation Loc; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1853 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 1854 | return 0; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1855 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1856 | // Try to find a function in our own ("to") context with the same name, same |
| 1857 | // type, and in the same context as the function we're importing. |
| 1858 | if (!LexicalDC->isFunctionOrMethod()) { |
| 1859 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 1860 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 1861 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 1862 | Lookup.first != Lookup.second; |
| 1863 | ++Lookup.first) { |
| 1864 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 1865 | continue; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 1866 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1867 | if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) { |
| 1868 | if (isExternalLinkage(FoundFunction->getLinkage()) && |
| 1869 | isExternalLinkage(D->getLinkage())) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1870 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 1871 | FoundFunction->getType())) { |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1872 | // FIXME: Actually try to merge the body and other attributes. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 1873 | return Importer.Imported(D, FoundFunction); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | // FIXME: Check for overloading more carefully, e.g., by boosting |
| 1877 | // Sema::IsOverload out to the AST library. |
| 1878 | |
| 1879 | // Function overloading is okay in C++. |
| 1880 | if (Importer.getToContext().getLangOptions().CPlusPlus) |
| 1881 | continue; |
| 1882 | |
| 1883 | // Complain about inconsistent function types. |
| 1884 | Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1885 | << Name << D->getType() << FoundFunction->getType(); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1886 | Importer.ToDiag(FoundFunction->getLocation(), |
| 1887 | diag::note_odr_value_here) |
| 1888 | << FoundFunction->getType(); |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | ConflictingDecls.push_back(*Lookup.first); |
| 1893 | } |
| 1894 | |
| 1895 | if (!ConflictingDecls.empty()) { |
| 1896 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 1897 | ConflictingDecls.data(), |
| 1898 | ConflictingDecls.size()); |
| 1899 | if (!Name) |
| 1900 | return 0; |
| 1901 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 1902 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1903 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1904 | DeclarationNameInfo NameInfo(Name, Loc); |
| 1905 | // Import additional name location/type info. |
| 1906 | ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); |
| 1907 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1908 | // Import the type. |
| 1909 | QualType T = Importer.Import(D->getType()); |
| 1910 | if (T.isNull()) |
| 1911 | return 0; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1912 | |
| 1913 | // Import the function parameters. |
| 1914 | llvm::SmallVector<ParmVarDecl *, 8> Parameters; |
| 1915 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 1916 | P != PEnd; ++P) { |
| 1917 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P)); |
| 1918 | if (!ToP) |
| 1919 | return 0; |
| 1920 | |
| 1921 | Parameters.push_back(ToP); |
| 1922 | } |
| 1923 | |
| 1924 | // Create the imported function. |
| 1925 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 1926 | FunctionDecl *ToFunction = 0; |
| 1927 | if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 1928 | ToFunction = CXXConstructorDecl::Create(Importer.getToContext(), |
| 1929 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1930 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 1931 | FromConstructor->isExplicit(), |
| 1932 | D->isInlineSpecified(), |
| 1933 | D->isImplicit()); |
| 1934 | } else if (isa<CXXDestructorDecl>(D)) { |
| 1935 | ToFunction = CXXDestructorDecl::Create(Importer.getToContext(), |
| 1936 | cast<CXXRecordDecl>(DC), |
Craig Silverstein | af8808d | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 1937 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 1938 | D->isInlineSpecified(), |
| 1939 | D->isImplicit()); |
| 1940 | } else if (CXXConversionDecl *FromConversion |
| 1941 | = dyn_cast<CXXConversionDecl>(D)) { |
| 1942 | ToFunction = CXXConversionDecl::Create(Importer.getToContext(), |
| 1943 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1944 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 1945 | D->isInlineSpecified(), |
| 1946 | FromConversion->isExplicit()); |
| 1947 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1948 | ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, |
| 1949 | NameInfo, T, TInfo, D->getStorageClass(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1950 | D->getStorageClassAsWritten(), |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 1951 | D->isInlineSpecified(), |
| 1952 | D->hasWrittenPrototype()); |
| 1953 | } |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1954 | |
| 1955 | // Import the qualifier, if any. |
| 1956 | if (D->getQualifier()) { |
| 1957 | NestedNameSpecifier *NNS = Importer.Import(D->getQualifier()); |
| 1958 | SourceRange NNSRange = Importer.Import(D->getQualifierRange()); |
| 1959 | ToFunction->setQualifierInfo(NNS, NNSRange); |
| 1960 | } |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 1961 | ToFunction->setAccess(D->getAccess()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 1962 | ToFunction->setLexicalDeclContext(LexicalDC); |
| 1963 | Importer.Imported(D, ToFunction); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1965 | // Set the parameters. |
| 1966 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 1967 | Parameters[I]->setOwningFunction(ToFunction); |
| 1968 | ToFunction->addDecl(Parameters[I]); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1969 | } |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 1970 | ToFunction->setParams(Parameters.data(), Parameters.size()); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1971 | |
| 1972 | // FIXME: Other bits to merge? |
Douglas Gregor | 0eaa2bf | 2010-10-01 23:55:07 +0000 | [diff] [blame] | 1973 | |
| 1974 | // Add this function to the lexical context. |
| 1975 | LexicalDC->addDecl(ToFunction); |
| 1976 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 1977 | return ToFunction; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 1980 | Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 1981 | return VisitFunctionDecl(D); |
| 1982 | } |
| 1983 | |
| 1984 | Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| 1985 | return VisitCXXMethodDecl(D); |
| 1986 | } |
| 1987 | |
| 1988 | Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
| 1989 | return VisitCXXMethodDecl(D); |
| 1990 | } |
| 1991 | |
| 1992 | Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| 1993 | return VisitCXXMethodDecl(D); |
| 1994 | } |
| 1995 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1996 | Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { |
| 1997 | // Import the major distinguishing characteristics of a variable. |
| 1998 | DeclContext *DC, *LexicalDC; |
| 1999 | DeclarationName Name; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2000 | SourceLocation Loc; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2001 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2002 | return 0; |
| 2003 | |
| 2004 | // Import the type. |
| 2005 | QualType T = Importer.Import(D->getType()); |
| 2006 | if (T.isNull()) |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2007 | return 0; |
| 2008 | |
| 2009 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 2010 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 2011 | if (!BitWidth && D->getBitWidth()) |
| 2012 | return 0; |
| 2013 | |
| 2014 | FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, |
| 2015 | Loc, Name.getAsIdentifierInfo(), |
| 2016 | T, TInfo, BitWidth, D->isMutable()); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2017 | ToField->setAccess(D->getAccess()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2018 | ToField->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2019 | Importer.Imported(D, ToField); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2020 | LexicalDC->addDecl(ToField); |
| 2021 | return ToField; |
| 2022 | } |
| 2023 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2024 | Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 2025 | // Import the major distinguishing characteristics of a variable. |
| 2026 | DeclContext *DC, *LexicalDC; |
| 2027 | DeclarationName Name; |
| 2028 | SourceLocation Loc; |
| 2029 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2030 | return 0; |
| 2031 | |
| 2032 | // Import the type. |
| 2033 | QualType T = Importer.Import(D->getType()); |
| 2034 | if (T.isNull()) |
| 2035 | return 0; |
| 2036 | |
| 2037 | NamedDecl **NamedChain = |
| 2038 | new (Importer.getToContext())NamedDecl*[D->getChainingSize()]; |
| 2039 | |
| 2040 | unsigned i = 0; |
| 2041 | for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(), |
| 2042 | PE = D->chain_end(); PI != PE; ++PI) { |
| 2043 | Decl* D = Importer.Import(*PI); |
| 2044 | if (!D) |
| 2045 | return 0; |
| 2046 | NamedChain[i++] = cast<NamedDecl>(D); |
| 2047 | } |
| 2048 | |
| 2049 | IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create( |
| 2050 | Importer.getToContext(), DC, |
| 2051 | Loc, Name.getAsIdentifierInfo(), T, |
| 2052 | NamedChain, D->getChainingSize()); |
| 2053 | ToIndirectField->setAccess(D->getAccess()); |
| 2054 | ToIndirectField->setLexicalDeclContext(LexicalDC); |
| 2055 | Importer.Imported(D, ToIndirectField); |
| 2056 | LexicalDC->addDecl(ToIndirectField); |
| 2057 | return ToIndirectField; |
| 2058 | } |
| 2059 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2060 | Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 2061 | // Import the major distinguishing characteristics of an ivar. |
| 2062 | DeclContext *DC, *LexicalDC; |
| 2063 | DeclarationName Name; |
| 2064 | SourceLocation Loc; |
| 2065 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2066 | return 0; |
| 2067 | |
| 2068 | // Determine whether we've already imported this ivar |
| 2069 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2070 | Lookup.first != Lookup.second; |
| 2071 | ++Lookup.first) { |
| 2072 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) { |
| 2073 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2074 | FoundIvar->getType())) { |
| 2075 | Importer.Imported(D, FoundIvar); |
| 2076 | return FoundIvar; |
| 2077 | } |
| 2078 | |
| 2079 | Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) |
| 2080 | << Name << D->getType() << FoundIvar->getType(); |
| 2081 | Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) |
| 2082 | << FoundIvar->getType(); |
| 2083 | return 0; |
| 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | // Import the type. |
| 2088 | QualType T = Importer.Import(D->getType()); |
| 2089 | if (T.isNull()) |
| 2090 | return 0; |
| 2091 | |
| 2092 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 2093 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 2094 | if (!BitWidth && D->getBitWidth()) |
| 2095 | return 0; |
| 2096 | |
Daniel Dunbar | fe3ead7 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 2097 | ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), |
| 2098 | cast<ObjCContainerDecl>(DC), |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2099 | Loc, Name.getAsIdentifierInfo(), |
| 2100 | T, TInfo, D->getAccessControl(), |
Fariborz Jahanian | aea8e1e | 2010-07-17 18:35:47 +0000 | [diff] [blame] | 2101 | BitWidth, D->getSynthesize()); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2102 | ToIvar->setLexicalDeclContext(LexicalDC); |
| 2103 | Importer.Imported(D, ToIvar); |
| 2104 | LexicalDC->addDecl(ToIvar); |
| 2105 | return ToIvar; |
| 2106 | |
| 2107 | } |
| 2108 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2109 | Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { |
| 2110 | // Import the major distinguishing characteristics of a variable. |
| 2111 | DeclContext *DC, *LexicalDC; |
| 2112 | DeclarationName Name; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2113 | SourceLocation Loc; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2114 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2115 | return 0; |
| 2116 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2117 | // Try to find a variable in our own ("to") context with the same name and |
| 2118 | // in the same context as the variable we're importing. |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2119 | if (D->isFileVarDecl()) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2120 | VarDecl *MergeWithVar = 0; |
| 2121 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 2122 | unsigned IDNS = Decl::IDNS_Ordinary; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2123 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2124 | Lookup.first != Lookup.second; |
| 2125 | ++Lookup.first) { |
| 2126 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 2127 | continue; |
| 2128 | |
| 2129 | if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) { |
| 2130 | // We have found a variable that we may need to merge with. Check it. |
| 2131 | if (isExternalLinkage(FoundVar->getLinkage()) && |
| 2132 | isExternalLinkage(D->getLinkage())) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2133 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2134 | FoundVar->getType())) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2135 | MergeWithVar = FoundVar; |
| 2136 | break; |
| 2137 | } |
| 2138 | |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 2139 | const ArrayType *FoundArray |
| 2140 | = Importer.getToContext().getAsArrayType(FoundVar->getType()); |
| 2141 | const ArrayType *TArray |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2142 | = Importer.getToContext().getAsArrayType(D->getType()); |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 2143 | if (FoundArray && TArray) { |
| 2144 | if (isa<IncompleteArrayType>(FoundArray) && |
| 2145 | isa<ConstantArrayType>(TArray)) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2146 | // Import the type. |
| 2147 | QualType T = Importer.Import(D->getType()); |
| 2148 | if (T.isNull()) |
| 2149 | return 0; |
| 2150 | |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 2151 | FoundVar->setType(T); |
| 2152 | MergeWithVar = FoundVar; |
| 2153 | break; |
| 2154 | } else if (isa<IncompleteArrayType>(TArray) && |
| 2155 | isa<ConstantArrayType>(FoundArray)) { |
| 2156 | MergeWithVar = FoundVar; |
| 2157 | break; |
Douglas Gregor | 2fbe558 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2161 | Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2162 | << Name << D->getType() << FoundVar->getType(); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2163 | Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) |
| 2164 | << FoundVar->getType(); |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | ConflictingDecls.push_back(*Lookup.first); |
| 2169 | } |
| 2170 | |
| 2171 | if (MergeWithVar) { |
| 2172 | // An equivalent variable with external linkage has been found. Link |
| 2173 | // the two declarations, then merge them. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2174 | Importer.Imported(D, MergeWithVar); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2175 | |
| 2176 | if (VarDecl *DDef = D->getDefinition()) { |
| 2177 | if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { |
| 2178 | Importer.ToDiag(ExistingDef->getLocation(), |
| 2179 | diag::err_odr_variable_multiple_def) |
| 2180 | << Name; |
| 2181 | Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); |
| 2182 | } else { |
| 2183 | Expr *Init = Importer.Import(DDef->getInit()); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2184 | MergeWithVar->setInit(Init); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2185 | } |
| 2186 | } |
| 2187 | |
| 2188 | return MergeWithVar; |
| 2189 | } |
| 2190 | |
| 2191 | if (!ConflictingDecls.empty()) { |
| 2192 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2193 | ConflictingDecls.data(), |
| 2194 | ConflictingDecls.size()); |
| 2195 | if (!Name) |
| 2196 | return 0; |
| 2197 | } |
| 2198 | } |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 2199 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2200 | // Import the type. |
| 2201 | QualType T = Importer.Import(D->getType()); |
| 2202 | if (T.isNull()) |
| 2203 | return 0; |
| 2204 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2205 | // Create the imported variable. |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 2206 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2207 | VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc, |
| 2208 | Name.getAsIdentifierInfo(), T, TInfo, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2209 | D->getStorageClass(), |
| 2210 | D->getStorageClassAsWritten()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2211 | // Import the qualifier, if any. |
| 2212 | if (D->getQualifier()) { |
| 2213 | NestedNameSpecifier *NNS = Importer.Import(D->getQualifier()); |
| 2214 | SourceRange NNSRange = Importer.Import(D->getQualifierRange()); |
| 2215 | ToVar->setQualifierInfo(NNS, NNSRange); |
| 2216 | } |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2217 | ToVar->setAccess(D->getAccess()); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2218 | ToVar->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2219 | Importer.Imported(D, ToVar); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2220 | LexicalDC->addDecl(ToVar); |
| 2221 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2222 | // Merge the initializer. |
| 2223 | // FIXME: Can we really import any initializer? Alternatively, we could force |
| 2224 | // ourselves to import every declaration of a variable and then only use |
| 2225 | // getInit() here. |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2226 | ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer()))); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2227 | |
| 2228 | // FIXME: Other bits to merge? |
| 2229 | |
| 2230 | return ToVar; |
| 2231 | } |
| 2232 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 2233 | Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 2234 | // Parameters are created in the translation unit's context, then moved |
| 2235 | // into the function declaration's context afterward. |
| 2236 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 2237 | |
| 2238 | // Import the name of this declaration. |
| 2239 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 2240 | if (D->getDeclName() && !Name) |
| 2241 | return 0; |
| 2242 | |
| 2243 | // Import the location of this declaration. |
| 2244 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 2245 | |
| 2246 | // Import the parameter's type. |
| 2247 | QualType T = Importer.Import(D->getType()); |
| 2248 | if (T.isNull()) |
| 2249 | return 0; |
| 2250 | |
| 2251 | // Create the imported parameter. |
| 2252 | ImplicitParamDecl *ToParm |
| 2253 | = ImplicitParamDecl::Create(Importer.getToContext(), DC, |
| 2254 | Loc, Name.getAsIdentifierInfo(), |
| 2255 | T); |
| 2256 | return Importer.Imported(D, ToParm); |
| 2257 | } |
| 2258 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2259 | Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { |
| 2260 | // Parameters are created in the translation unit's context, then moved |
| 2261 | // into the function declaration's context afterward. |
| 2262 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 2263 | |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 2264 | // Import the name of this declaration. |
| 2265 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 2266 | if (D->getDeclName() && !Name) |
| 2267 | return 0; |
| 2268 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2269 | // Import the location of this declaration. |
| 2270 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 2271 | |
| 2272 | // Import the parameter's type. |
| 2273 | QualType T = Importer.Import(D->getType()); |
| 2274 | if (T.isNull()) |
| 2275 | return 0; |
| 2276 | |
| 2277 | // Create the imported parameter. |
| 2278 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 2279 | ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, |
| 2280 | Loc, Name.getAsIdentifierInfo(), |
| 2281 | T, TInfo, D->getStorageClass(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2282 | D->getStorageClassAsWritten(), |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2283 | /*FIXME: Default argument*/ 0); |
John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 2284 | ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2285 | return Importer.Imported(D, ToParm); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2288 | Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 2289 | // Import the major distinguishing characteristics of a method. |
| 2290 | DeclContext *DC, *LexicalDC; |
| 2291 | DeclarationName Name; |
| 2292 | SourceLocation Loc; |
| 2293 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2294 | return 0; |
| 2295 | |
| 2296 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2297 | Lookup.first != Lookup.second; |
| 2298 | ++Lookup.first) { |
| 2299 | if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) { |
| 2300 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) |
| 2301 | continue; |
| 2302 | |
| 2303 | // Check return types. |
| 2304 | if (!Importer.IsStructurallyEquivalent(D->getResultType(), |
| 2305 | FoundMethod->getResultType())) { |
| 2306 | Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) |
| 2307 | << D->isInstanceMethod() << Name |
| 2308 | << D->getResultType() << FoundMethod->getResultType(); |
| 2309 | Importer.ToDiag(FoundMethod->getLocation(), |
| 2310 | diag::note_odr_objc_method_here) |
| 2311 | << D->isInstanceMethod() << Name; |
| 2312 | return 0; |
| 2313 | } |
| 2314 | |
| 2315 | // Check the number of parameters. |
| 2316 | if (D->param_size() != FoundMethod->param_size()) { |
| 2317 | Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) |
| 2318 | << D->isInstanceMethod() << Name |
| 2319 | << D->param_size() << FoundMethod->param_size(); |
| 2320 | Importer.ToDiag(FoundMethod->getLocation(), |
| 2321 | diag::note_odr_objc_method_here) |
| 2322 | << D->isInstanceMethod() << Name; |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
| 2326 | // Check parameter types. |
| 2327 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
| 2328 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); |
| 2329 | P != PEnd; ++P, ++FoundP) { |
| 2330 | if (!Importer.IsStructurallyEquivalent((*P)->getType(), |
| 2331 | (*FoundP)->getType())) { |
| 2332 | Importer.FromDiag((*P)->getLocation(), |
| 2333 | diag::err_odr_objc_method_param_type_inconsistent) |
| 2334 | << D->isInstanceMethod() << Name |
| 2335 | << (*P)->getType() << (*FoundP)->getType(); |
| 2336 | Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) |
| 2337 | << (*FoundP)->getType(); |
| 2338 | return 0; |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | // Check variadic/non-variadic. |
| 2343 | // Check the number of parameters. |
| 2344 | if (D->isVariadic() != FoundMethod->isVariadic()) { |
| 2345 | Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) |
| 2346 | << D->isInstanceMethod() << Name; |
| 2347 | Importer.ToDiag(FoundMethod->getLocation(), |
| 2348 | diag::note_odr_objc_method_here) |
| 2349 | << D->isInstanceMethod() << Name; |
| 2350 | return 0; |
| 2351 | } |
| 2352 | |
| 2353 | // FIXME: Any other bits we need to merge? |
| 2354 | return Importer.Imported(D, FoundMethod); |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | // Import the result type. |
| 2359 | QualType ResultTy = Importer.Import(D->getResultType()); |
| 2360 | if (ResultTy.isNull()) |
| 2361 | return 0; |
| 2362 | |
Douglas Gregor | 12852d9 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 2363 | TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo()); |
| 2364 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2365 | ObjCMethodDecl *ToMethod |
| 2366 | = ObjCMethodDecl::Create(Importer.getToContext(), |
| 2367 | Loc, |
| 2368 | Importer.Import(D->getLocEnd()), |
| 2369 | Name.getObjCSelector(), |
Douglas Gregor | 12852d9 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 2370 | ResultTy, ResultTInfo, DC, |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2371 | D->isInstanceMethod(), |
| 2372 | D->isVariadic(), |
| 2373 | D->isSynthesized(), |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 2374 | D->isDefined(), |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2375 | D->getImplementationControl()); |
| 2376 | |
| 2377 | // FIXME: When we decide to merge method definitions, we'll need to |
| 2378 | // deal with implicit parameters. |
| 2379 | |
| 2380 | // Import the parameters |
| 2381 | llvm::SmallVector<ParmVarDecl *, 5> ToParams; |
| 2382 | for (ObjCMethodDecl::param_iterator FromP = D->param_begin(), |
| 2383 | FromPEnd = D->param_end(); |
| 2384 | FromP != FromPEnd; |
| 2385 | ++FromP) { |
| 2386 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP)); |
| 2387 | if (!ToP) |
| 2388 | return 0; |
| 2389 | |
| 2390 | ToParams.push_back(ToP); |
| 2391 | } |
| 2392 | |
| 2393 | // Set the parameters. |
| 2394 | for (unsigned I = 0, N = ToParams.size(); I != N; ++I) { |
| 2395 | ToParams[I]->setOwningFunction(ToMethod); |
| 2396 | ToMethod->addDecl(ToParams[I]); |
| 2397 | } |
| 2398 | ToMethod->setMethodParams(Importer.getToContext(), |
Fariborz Jahanian | cdabb31 | 2010-04-09 15:40:42 +0000 | [diff] [blame] | 2399 | ToParams.data(), ToParams.size(), |
| 2400 | ToParams.size()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2401 | |
| 2402 | ToMethod->setLexicalDeclContext(LexicalDC); |
| 2403 | Importer.Imported(D, ToMethod); |
| 2404 | LexicalDC->addDecl(ToMethod); |
| 2405 | return ToMethod; |
| 2406 | } |
| 2407 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 2408 | Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 2409 | // Import the major distinguishing characteristics of a category. |
| 2410 | DeclContext *DC, *LexicalDC; |
| 2411 | DeclarationName Name; |
| 2412 | SourceLocation Loc; |
| 2413 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2414 | return 0; |
| 2415 | |
| 2416 | ObjCInterfaceDecl *ToInterface |
| 2417 | = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); |
| 2418 | if (!ToInterface) |
| 2419 | return 0; |
| 2420 | |
| 2421 | // Determine if we've already encountered this category. |
| 2422 | ObjCCategoryDecl *MergeWithCategory |
| 2423 | = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); |
| 2424 | ObjCCategoryDecl *ToCategory = MergeWithCategory; |
| 2425 | if (!ToCategory) { |
| 2426 | ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, |
| 2427 | Importer.Import(D->getAtLoc()), |
| 2428 | Loc, |
| 2429 | Importer.Import(D->getCategoryNameLoc()), |
| 2430 | Name.getAsIdentifierInfo()); |
| 2431 | ToCategory->setLexicalDeclContext(LexicalDC); |
| 2432 | LexicalDC->addDecl(ToCategory); |
| 2433 | Importer.Imported(D, ToCategory); |
| 2434 | |
| 2435 | // Link this category into its class's category list. |
| 2436 | ToCategory->setClassInterface(ToInterface); |
| 2437 | ToCategory->insertNextClassCategory(); |
| 2438 | |
| 2439 | // Import protocols |
| 2440 | llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 2441 | llvm::SmallVector<SourceLocation, 4> ProtocolLocs; |
| 2442 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc |
| 2443 | = D->protocol_loc_begin(); |
| 2444 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 2445 | FromProtoEnd = D->protocol_end(); |
| 2446 | FromProto != FromProtoEnd; |
| 2447 | ++FromProto, ++FromProtoLoc) { |
| 2448 | ObjCProtocolDecl *ToProto |
| 2449 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 2450 | if (!ToProto) |
| 2451 | return 0; |
| 2452 | Protocols.push_back(ToProto); |
| 2453 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 2454 | } |
| 2455 | |
| 2456 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 2457 | ToCategory->setProtocolList(Protocols.data(), Protocols.size(), |
| 2458 | ProtocolLocs.data(), Importer.getToContext()); |
| 2459 | |
| 2460 | } else { |
| 2461 | Importer.Imported(D, ToCategory); |
| 2462 | } |
| 2463 | |
| 2464 | // Import all of the members of this category. |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 2465 | ImportDeclContext(D); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 2466 | |
| 2467 | // If we have an implementation, import it as well. |
| 2468 | if (D->getImplementation()) { |
| 2469 | ObjCCategoryImplDecl *Impl |
| 2470 | = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation())); |
| 2471 | if (!Impl) |
| 2472 | return 0; |
| 2473 | |
| 2474 | ToCategory->setImplementation(Impl); |
| 2475 | } |
| 2476 | |
| 2477 | return ToCategory; |
| 2478 | } |
| 2479 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 2480 | Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 2481 | // Import the major distinguishing characteristics of a protocol. |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 2482 | DeclContext *DC, *LexicalDC; |
| 2483 | DeclarationName Name; |
| 2484 | SourceLocation Loc; |
| 2485 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2486 | return 0; |
| 2487 | |
| 2488 | ObjCProtocolDecl *MergeWithProtocol = 0; |
| 2489 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2490 | Lookup.first != Lookup.second; |
| 2491 | ++Lookup.first) { |
| 2492 | if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) |
| 2493 | continue; |
| 2494 | |
| 2495 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first))) |
| 2496 | break; |
| 2497 | } |
| 2498 | |
| 2499 | ObjCProtocolDecl *ToProto = MergeWithProtocol; |
| 2500 | if (!ToProto || ToProto->isForwardDecl()) { |
| 2501 | if (!ToProto) { |
| 2502 | ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc, |
| 2503 | Name.getAsIdentifierInfo()); |
| 2504 | ToProto->setForwardDecl(D->isForwardDecl()); |
| 2505 | ToProto->setLexicalDeclContext(LexicalDC); |
| 2506 | LexicalDC->addDecl(ToProto); |
| 2507 | } |
| 2508 | Importer.Imported(D, ToProto); |
| 2509 | |
| 2510 | // Import protocols |
| 2511 | llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 2512 | llvm::SmallVector<SourceLocation, 4> ProtocolLocs; |
| 2513 | ObjCProtocolDecl::protocol_loc_iterator |
| 2514 | FromProtoLoc = D->protocol_loc_begin(); |
| 2515 | for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 2516 | FromProtoEnd = D->protocol_end(); |
| 2517 | FromProto != FromProtoEnd; |
| 2518 | ++FromProto, ++FromProtoLoc) { |
| 2519 | ObjCProtocolDecl *ToProto |
| 2520 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 2521 | if (!ToProto) |
| 2522 | return 0; |
| 2523 | Protocols.push_back(ToProto); |
| 2524 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 2525 | } |
| 2526 | |
| 2527 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 2528 | ToProto->setProtocolList(Protocols.data(), Protocols.size(), |
| 2529 | ProtocolLocs.data(), Importer.getToContext()); |
| 2530 | } else { |
| 2531 | Importer.Imported(D, ToProto); |
| 2532 | } |
| 2533 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 2534 | // Import all of the members of this protocol. |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 2535 | ImportDeclContext(D); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 2536 | |
| 2537 | return ToProto; |
| 2538 | } |
| 2539 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 2540 | Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 2541 | // Import the major distinguishing characteristics of an @interface. |
| 2542 | DeclContext *DC, *LexicalDC; |
| 2543 | DeclarationName Name; |
| 2544 | SourceLocation Loc; |
| 2545 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2546 | return 0; |
| 2547 | |
| 2548 | ObjCInterfaceDecl *MergeWithIface = 0; |
| 2549 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2550 | Lookup.first != Lookup.second; |
| 2551 | ++Lookup.first) { |
| 2552 | if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 2553 | continue; |
| 2554 | |
| 2555 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first))) |
| 2556 | break; |
| 2557 | } |
| 2558 | |
| 2559 | ObjCInterfaceDecl *ToIface = MergeWithIface; |
| 2560 | if (!ToIface || ToIface->isForwardDecl()) { |
| 2561 | if (!ToIface) { |
| 2562 | ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), |
| 2563 | DC, Loc, |
| 2564 | Name.getAsIdentifierInfo(), |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2565 | Importer.Import(D->getClassLoc()), |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 2566 | D->isForwardDecl(), |
| 2567 | D->isImplicitInterfaceDecl()); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 2568 | ToIface->setForwardDecl(D->isForwardDecl()); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 2569 | ToIface->setLexicalDeclContext(LexicalDC); |
| 2570 | LexicalDC->addDecl(ToIface); |
| 2571 | } |
| 2572 | Importer.Imported(D, ToIface); |
| 2573 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 2574 | if (D->getSuperClass()) { |
| 2575 | ObjCInterfaceDecl *Super |
| 2576 | = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass())); |
| 2577 | if (!Super) |
| 2578 | return 0; |
| 2579 | |
| 2580 | ToIface->setSuperClass(Super); |
| 2581 | ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc())); |
| 2582 | } |
| 2583 | |
| 2584 | // Import protocols |
| 2585 | llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 2586 | llvm::SmallVector<SourceLocation, 4> ProtocolLocs; |
| 2587 | ObjCInterfaceDecl::protocol_loc_iterator |
| 2588 | FromProtoLoc = D->protocol_loc_begin(); |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2589 | |
| 2590 | // FIXME: Should we be usng all_referenced_protocol_begin() here? |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 2591 | for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 2592 | FromProtoEnd = D->protocol_end(); |
| 2593 | FromProto != FromProtoEnd; |
| 2594 | ++FromProto, ++FromProtoLoc) { |
| 2595 | ObjCProtocolDecl *ToProto |
| 2596 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 2597 | if (!ToProto) |
| 2598 | return 0; |
| 2599 | Protocols.push_back(ToProto); |
| 2600 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 2601 | } |
| 2602 | |
| 2603 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 2604 | ToIface->setProtocolList(Protocols.data(), Protocols.size(), |
| 2605 | ProtocolLocs.data(), Importer.getToContext()); |
| 2606 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 2607 | // Import @end range |
| 2608 | ToIface->setAtEndRange(Importer.Import(D->getAtEndRange())); |
| 2609 | } else { |
| 2610 | Importer.Imported(D, ToIface); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2611 | |
| 2612 | // Check for consistency of superclasses. |
| 2613 | DeclarationName FromSuperName, ToSuperName; |
| 2614 | if (D->getSuperClass()) |
| 2615 | FromSuperName = Importer.Import(D->getSuperClass()->getDeclName()); |
| 2616 | if (ToIface->getSuperClass()) |
| 2617 | ToSuperName = ToIface->getSuperClass()->getDeclName(); |
| 2618 | if (FromSuperName != ToSuperName) { |
| 2619 | Importer.ToDiag(ToIface->getLocation(), |
| 2620 | diag::err_odr_objc_superclass_inconsistent) |
| 2621 | << ToIface->getDeclName(); |
| 2622 | if (ToIface->getSuperClass()) |
| 2623 | Importer.ToDiag(ToIface->getSuperClassLoc(), |
| 2624 | diag::note_odr_objc_superclass) |
| 2625 | << ToIface->getSuperClass()->getDeclName(); |
| 2626 | else |
| 2627 | Importer.ToDiag(ToIface->getLocation(), |
| 2628 | diag::note_odr_objc_missing_superclass); |
| 2629 | if (D->getSuperClass()) |
| 2630 | Importer.FromDiag(D->getSuperClassLoc(), |
| 2631 | diag::note_odr_objc_superclass) |
| 2632 | << D->getSuperClass()->getDeclName(); |
| 2633 | else |
| 2634 | Importer.FromDiag(D->getLocation(), |
| 2635 | diag::note_odr_objc_missing_superclass); |
| 2636 | return 0; |
| 2637 | } |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 2640 | // Import categories. When the categories themselves are imported, they'll |
| 2641 | // hook themselves into this interface. |
| 2642 | for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat; |
| 2643 | FromCat = FromCat->getNextClassCategory()) |
| 2644 | Importer.Import(FromCat); |
| 2645 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 2646 | // Import all of the members of this class. |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 2647 | ImportDeclContext(D); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 2648 | |
| 2649 | // If we have an @implementation, import it as well. |
| 2650 | if (D->getImplementation()) { |
| 2651 | ObjCImplementationDecl *Impl |
| 2652 | = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation())); |
| 2653 | if (!Impl) |
| 2654 | return 0; |
| 2655 | |
| 2656 | ToIface->setImplementation(Impl); |
| 2657 | } |
| 2658 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 2659 | return ToIface; |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 2662 | Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 2663 | // Import the major distinguishing characteristics of an @property. |
| 2664 | DeclContext *DC, *LexicalDC; |
| 2665 | DeclarationName Name; |
| 2666 | SourceLocation Loc; |
| 2667 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2668 | return 0; |
| 2669 | |
| 2670 | // Check whether we have already imported this property. |
| 2671 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2672 | Lookup.first != Lookup.second; |
| 2673 | ++Lookup.first) { |
| 2674 | if (ObjCPropertyDecl *FoundProp |
| 2675 | = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) { |
| 2676 | // Check property types. |
| 2677 | if (!Importer.IsStructurallyEquivalent(D->getType(), |
| 2678 | FoundProp->getType())) { |
| 2679 | Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) |
| 2680 | << Name << D->getType() << FoundProp->getType(); |
| 2681 | Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) |
| 2682 | << FoundProp->getType(); |
| 2683 | return 0; |
| 2684 | } |
| 2685 | |
| 2686 | // FIXME: Check property attributes, getters, setters, etc.? |
| 2687 | |
| 2688 | // Consider these properties to be equivalent. |
| 2689 | Importer.Imported(D, FoundProp); |
| 2690 | return FoundProp; |
| 2691 | } |
| 2692 | } |
| 2693 | |
| 2694 | // Import the type. |
John McCall | 339bb66 | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 2695 | TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo()); |
| 2696 | if (!T) |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 2697 | return 0; |
| 2698 | |
| 2699 | // Create the new property. |
| 2700 | ObjCPropertyDecl *ToProperty |
| 2701 | = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, |
| 2702 | Name.getAsIdentifierInfo(), |
| 2703 | Importer.Import(D->getAtLoc()), |
| 2704 | T, |
| 2705 | D->getPropertyImplementation()); |
| 2706 | Importer.Imported(D, ToProperty); |
| 2707 | ToProperty->setLexicalDeclContext(LexicalDC); |
| 2708 | LexicalDC->addDecl(ToProperty); |
| 2709 | |
| 2710 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 2711 | ToProperty->setPropertyAttributesAsWritten( |
| 2712 | D->getPropertyAttributesAsWritten()); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 2713 | ToProperty->setGetterName(Importer.Import(D->getGetterName())); |
| 2714 | ToProperty->setSetterName(Importer.Import(D->getSetterName())); |
| 2715 | ToProperty->setGetterMethodDecl( |
| 2716 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); |
| 2717 | ToProperty->setSetterMethodDecl( |
| 2718 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); |
| 2719 | ToProperty->setPropertyIvarDecl( |
| 2720 | cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); |
| 2721 | return ToProperty; |
| 2722 | } |
| 2723 | |
Douglas Gregor | 8661a72 | 2010-02-18 02:12:22 +0000 | [diff] [blame] | 2724 | Decl * |
| 2725 | ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 2726 | // Import the context of this declaration. |
| 2727 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 2728 | if (!DC) |
| 2729 | return 0; |
| 2730 | |
| 2731 | DeclContext *LexicalDC = DC; |
| 2732 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 2733 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 2734 | if (!LexicalDC) |
| 2735 | return 0; |
| 2736 | } |
| 2737 | |
| 2738 | // Import the location of this declaration. |
| 2739 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 2740 | |
| 2741 | llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 2742 | llvm::SmallVector<SourceLocation, 4> Locations; |
| 2743 | ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc |
| 2744 | = D->protocol_loc_begin(); |
| 2745 | for (ObjCForwardProtocolDecl::protocol_iterator FromProto |
| 2746 | = D->protocol_begin(), FromProtoEnd = D->protocol_end(); |
| 2747 | FromProto != FromProtoEnd; |
| 2748 | ++FromProto, ++FromProtoLoc) { |
| 2749 | ObjCProtocolDecl *ToProto |
| 2750 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 2751 | if (!ToProto) |
| 2752 | continue; |
| 2753 | |
| 2754 | Protocols.push_back(ToProto); |
| 2755 | Locations.push_back(Importer.Import(*FromProtoLoc)); |
| 2756 | } |
| 2757 | |
| 2758 | ObjCForwardProtocolDecl *ToForward |
| 2759 | = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc, |
| 2760 | Protocols.data(), Protocols.size(), |
| 2761 | Locations.data()); |
| 2762 | ToForward->setLexicalDeclContext(LexicalDC); |
| 2763 | LexicalDC->addDecl(ToForward); |
| 2764 | Importer.Imported(D, ToForward); |
| 2765 | return ToForward; |
| 2766 | } |
| 2767 | |
Douglas Gregor | 06537af | 2010-02-18 02:04:09 +0000 | [diff] [blame] | 2768 | Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 2769 | // Import the context of this declaration. |
| 2770 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 2771 | if (!DC) |
| 2772 | return 0; |
| 2773 | |
| 2774 | DeclContext *LexicalDC = DC; |
| 2775 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 2776 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 2777 | if (!LexicalDC) |
| 2778 | return 0; |
| 2779 | } |
| 2780 | |
| 2781 | // Import the location of this declaration. |
| 2782 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 2783 | |
| 2784 | llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces; |
| 2785 | llvm::SmallVector<SourceLocation, 4> Locations; |
| 2786 | for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end(); |
| 2787 | From != FromEnd; ++From) { |
| 2788 | ObjCInterfaceDecl *ToIface |
| 2789 | = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface())); |
| 2790 | if (!ToIface) |
| 2791 | continue; |
| 2792 | |
| 2793 | Interfaces.push_back(ToIface); |
| 2794 | Locations.push_back(Importer.Import(From->getLocation())); |
| 2795 | } |
| 2796 | |
| 2797 | ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC, |
| 2798 | Loc, |
| 2799 | Interfaces.data(), |
| 2800 | Locations.data(), |
| 2801 | Interfaces.size()); |
| 2802 | ToClass->setLexicalDeclContext(LexicalDC); |
| 2803 | LexicalDC->addDecl(ToClass); |
| 2804 | Importer.Imported(D, ToClass); |
| 2805 | return ToClass; |
| 2806 | } |
| 2807 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 2808 | //---------------------------------------------------------------------------- |
| 2809 | // Import Statements |
| 2810 | //---------------------------------------------------------------------------- |
| 2811 | |
| 2812 | Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { |
| 2813 | Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) |
| 2814 | << S->getStmtClassName(); |
| 2815 | return 0; |
| 2816 | } |
| 2817 | |
| 2818 | //---------------------------------------------------------------------------- |
| 2819 | // Import Expressions |
| 2820 | //---------------------------------------------------------------------------- |
| 2821 | Expr *ASTNodeImporter::VisitExpr(Expr *E) { |
| 2822 | Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) |
| 2823 | << E->getStmtClassName(); |
| 2824 | return 0; |
| 2825 | } |
| 2826 | |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 2827 | Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { |
| 2828 | NestedNameSpecifier *Qualifier = 0; |
| 2829 | if (E->getQualifier()) { |
| 2830 | Qualifier = Importer.Import(E->getQualifier()); |
| 2831 | if (!E->getQualifier()) |
| 2832 | return 0; |
| 2833 | } |
| 2834 | |
| 2835 | ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); |
| 2836 | if (!ToD) |
| 2837 | return 0; |
| 2838 | |
| 2839 | QualType T = Importer.Import(E->getType()); |
| 2840 | if (T.isNull()) |
| 2841 | return 0; |
| 2842 | |
| 2843 | return DeclRefExpr::Create(Importer.getToContext(), Qualifier, |
| 2844 | Importer.Import(E->getQualifierRange()), |
| 2845 | ToD, |
| 2846 | Importer.Import(E->getLocation()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2847 | T, E->getValueKind(), |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 2848 | /*FIXME:TemplateArgs=*/0); |
| 2849 | } |
| 2850 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 2851 | Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { |
| 2852 | QualType T = Importer.Import(E->getType()); |
| 2853 | if (T.isNull()) |
| 2854 | return 0; |
| 2855 | |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2856 | return IntegerLiteral::Create(Importer.getToContext(), |
| 2857 | E->getValue(), T, |
| 2858 | Importer.Import(E->getLocation())); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 2861 | Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { |
| 2862 | QualType T = Importer.Import(E->getType()); |
| 2863 | if (T.isNull()) |
| 2864 | return 0; |
| 2865 | |
| 2866 | return new (Importer.getToContext()) CharacterLiteral(E->getValue(), |
| 2867 | E->isWide(), T, |
| 2868 | Importer.Import(E->getLocation())); |
| 2869 | } |
| 2870 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 2871 | Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { |
| 2872 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 2873 | if (!SubExpr) |
| 2874 | return 0; |
| 2875 | |
| 2876 | return new (Importer.getToContext()) |
| 2877 | ParenExpr(Importer.Import(E->getLParen()), |
| 2878 | Importer.Import(E->getRParen()), |
| 2879 | SubExpr); |
| 2880 | } |
| 2881 | |
| 2882 | Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { |
| 2883 | QualType T = Importer.Import(E->getType()); |
| 2884 | if (T.isNull()) |
| 2885 | return 0; |
| 2886 | |
| 2887 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 2888 | if (!SubExpr) |
| 2889 | return 0; |
| 2890 | |
| 2891 | return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2892 | T, E->getValueKind(), |
| 2893 | E->getObjectKind(), |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 2894 | Importer.Import(E->getOperatorLoc())); |
| 2895 | } |
| 2896 | |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 2897 | Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { |
| 2898 | QualType ResultType = Importer.Import(E->getType()); |
| 2899 | |
| 2900 | if (E->isArgumentType()) { |
| 2901 | TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); |
| 2902 | if (!TInfo) |
| 2903 | return 0; |
| 2904 | |
| 2905 | return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(), |
| 2906 | TInfo, ResultType, |
| 2907 | Importer.Import(E->getOperatorLoc()), |
| 2908 | Importer.Import(E->getRParenLoc())); |
| 2909 | } |
| 2910 | |
| 2911 | Expr *SubExpr = Importer.Import(E->getArgumentExpr()); |
| 2912 | if (!SubExpr) |
| 2913 | return 0; |
| 2914 | |
| 2915 | return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(), |
| 2916 | SubExpr, ResultType, |
| 2917 | Importer.Import(E->getOperatorLoc()), |
| 2918 | Importer.Import(E->getRParenLoc())); |
| 2919 | } |
| 2920 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 2921 | Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { |
| 2922 | QualType T = Importer.Import(E->getType()); |
| 2923 | if (T.isNull()) |
| 2924 | return 0; |
| 2925 | |
| 2926 | Expr *LHS = Importer.Import(E->getLHS()); |
| 2927 | if (!LHS) |
| 2928 | return 0; |
| 2929 | |
| 2930 | Expr *RHS = Importer.Import(E->getRHS()); |
| 2931 | if (!RHS) |
| 2932 | return 0; |
| 2933 | |
| 2934 | return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2935 | T, E->getValueKind(), |
| 2936 | E->getObjectKind(), |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 2937 | Importer.Import(E->getOperatorLoc())); |
| 2938 | } |
| 2939 | |
| 2940 | Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { |
| 2941 | QualType T = Importer.Import(E->getType()); |
| 2942 | if (T.isNull()) |
| 2943 | return 0; |
| 2944 | |
| 2945 | QualType CompLHSType = Importer.Import(E->getComputationLHSType()); |
| 2946 | if (CompLHSType.isNull()) |
| 2947 | return 0; |
| 2948 | |
| 2949 | QualType CompResultType = Importer.Import(E->getComputationResultType()); |
| 2950 | if (CompResultType.isNull()) |
| 2951 | return 0; |
| 2952 | |
| 2953 | Expr *LHS = Importer.Import(E->getLHS()); |
| 2954 | if (!LHS) |
| 2955 | return 0; |
| 2956 | |
| 2957 | Expr *RHS = Importer.Import(E->getRHS()); |
| 2958 | if (!RHS) |
| 2959 | return 0; |
| 2960 | |
| 2961 | return new (Importer.getToContext()) |
| 2962 | CompoundAssignOperator(LHS, RHS, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2963 | T, E->getValueKind(), |
| 2964 | E->getObjectKind(), |
| 2965 | CompLHSType, CompResultType, |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 2966 | Importer.Import(E->getOperatorLoc())); |
| 2967 | } |
| 2968 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2969 | bool ImportCastPath(CastExpr *E, CXXCastPath &Path) { |
| 2970 | if (E->path_empty()) return false; |
| 2971 | |
| 2972 | // TODO: import cast paths |
| 2973 | return true; |
| 2974 | } |
| 2975 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2976 | Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 2977 | QualType T = Importer.Import(E->getType()); |
| 2978 | if (T.isNull()) |
| 2979 | return 0; |
| 2980 | |
| 2981 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 2982 | if (!SubExpr) |
| 2983 | return 0; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2984 | |
| 2985 | CXXCastPath BasePath; |
| 2986 | if (ImportCastPath(E, BasePath)) |
| 2987 | return 0; |
| 2988 | |
| 2989 | return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2990 | SubExpr, &BasePath, E->getValueKind()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 2993 | Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) { |
| 2994 | QualType T = Importer.Import(E->getType()); |
| 2995 | if (T.isNull()) |
| 2996 | return 0; |
| 2997 | |
| 2998 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 2999 | if (!SubExpr) |
| 3000 | return 0; |
| 3001 | |
| 3002 | TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); |
| 3003 | if (!TInfo && E->getTypeInfoAsWritten()) |
| 3004 | return 0; |
| 3005 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3006 | CXXCastPath BasePath; |
| 3007 | if (ImportCastPath(E, BasePath)) |
| 3008 | return 0; |
| 3009 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3010 | return CStyleCastExpr::Create(Importer.getToContext(), T, |
| 3011 | E->getValueKind(), E->getCastKind(), |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3012 | SubExpr, &BasePath, TInfo, |
| 3013 | Importer.Import(E->getLParenLoc()), |
| 3014 | Importer.Import(E->getRParenLoc())); |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 3017 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 3018 | const FileSystemOptions &ToFileSystemOpts, |
| 3019 | ASTContext &FromContext, FileManager &FromFileManager, |
| 3020 | const FileSystemOptions &FromFileSystemOpts) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 3021 | : ToContext(ToContext), FromContext(FromContext), |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 3022 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 3023 | ToFileSystemOpts(ToFileSystemOpts), FromFileSystemOpts(FromFileSystemOpts) { |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3024 | ImportedDecls[FromContext.getTranslationUnitDecl()] |
| 3025 | = ToContext.getTranslationUnitDecl(); |
| 3026 | } |
| 3027 | |
| 3028 | ASTImporter::~ASTImporter() { } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 3029 | |
| 3030 | QualType ASTImporter::Import(QualType FromT) { |
| 3031 | if (FromT.isNull()) |
| 3032 | return QualType(); |
| 3033 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 3034 | // Check whether we've already imported this type. |
| 3035 | llvm::DenseMap<Type *, Type *>::iterator Pos |
| 3036 | = ImportedTypes.find(FromT.getTypePtr()); |
| 3037 | if (Pos != ImportedTypes.end()) |
| 3038 | return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 3039 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 3040 | // Import the type |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 3041 | ASTNodeImporter Importer(*this); |
| 3042 | QualType ToT = Importer.Visit(FromT.getTypePtr()); |
| 3043 | if (ToT.isNull()) |
| 3044 | return ToT; |
| 3045 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 3046 | // Record the imported type. |
| 3047 | ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr(); |
| 3048 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 3049 | return ToContext.getQualifiedType(ToT, FromT.getQualifiers()); |
| 3050 | } |
| 3051 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3052 | TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3053 | if (!FromTSI) |
| 3054 | return FromTSI; |
| 3055 | |
| 3056 | // FIXME: For now we just create a "trivial" type source info based |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 3057 | // on the type and a single location. Implement a real version of this. |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3058 | QualType T = Import(FromTSI->getType()); |
| 3059 | if (T.isNull()) |
| 3060 | return 0; |
| 3061 | |
| 3062 | return ToContext.getTrivialTypeSourceInfo(T, |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 3063 | FromTSI->getTypeLoc().getSourceRange().getBegin()); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | Decl *ASTImporter::Import(Decl *FromD) { |
| 3067 | if (!FromD) |
| 3068 | return 0; |
| 3069 | |
| 3070 | // Check whether we've already imported this declaration. |
| 3071 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); |
| 3072 | if (Pos != ImportedDecls.end()) |
| 3073 | return Pos->second; |
| 3074 | |
| 3075 | // Import the type |
| 3076 | ASTNodeImporter Importer(*this); |
| 3077 | Decl *ToD = Importer.Visit(FromD); |
| 3078 | if (!ToD) |
| 3079 | return 0; |
| 3080 | |
| 3081 | // Record the imported declaration. |
| 3082 | ImportedDecls[FromD] = ToD; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3083 | |
| 3084 | if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) { |
| 3085 | // Keep track of anonymous tags that have an associated typedef. |
| 3086 | if (FromTag->getTypedefForAnonDecl()) |
| 3087 | AnonTagsWithPendingTypedefs.push_back(FromTag); |
| 3088 | } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) { |
| 3089 | // When we've finished transforming a typedef, see whether it was the |
| 3090 | // typedef for an anonymous tag. |
| 3091 | for (llvm::SmallVector<TagDecl *, 4>::iterator |
| 3092 | FromTag = AnonTagsWithPendingTypedefs.begin(), |
| 3093 | FromTagEnd = AnonTagsWithPendingTypedefs.end(); |
| 3094 | FromTag != FromTagEnd; ++FromTag) { |
| 3095 | if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) { |
| 3096 | if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) { |
| 3097 | // We found the typedef for an anonymous tag; link them. |
| 3098 | ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD)); |
| 3099 | AnonTagsWithPendingTypedefs.erase(FromTag); |
| 3100 | break; |
| 3101 | } |
| 3102 | } |
| 3103 | } |
| 3104 | } |
| 3105 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3106 | return ToD; |
| 3107 | } |
| 3108 | |
| 3109 | DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { |
| 3110 | if (!FromDC) |
| 3111 | return FromDC; |
| 3112 | |
| 3113 | return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); |
| 3114 | } |
| 3115 | |
| 3116 | Expr *ASTImporter::Import(Expr *FromE) { |
| 3117 | if (!FromE) |
| 3118 | return 0; |
| 3119 | |
| 3120 | return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); |
| 3121 | } |
| 3122 | |
| 3123 | Stmt *ASTImporter::Import(Stmt *FromS) { |
| 3124 | if (!FromS) |
| 3125 | return 0; |
| 3126 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 3127 | // Check whether we've already imported this declaration. |
| 3128 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); |
| 3129 | if (Pos != ImportedStmts.end()) |
| 3130 | return Pos->second; |
| 3131 | |
| 3132 | // Import the type |
| 3133 | ASTNodeImporter Importer(*this); |
| 3134 | Stmt *ToS = Importer.Visit(FromS); |
| 3135 | if (!ToS) |
| 3136 | return 0; |
| 3137 | |
| 3138 | // Record the imported declaration. |
| 3139 | ImportedStmts[FromS] = ToS; |
| 3140 | return ToS; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { |
| 3144 | if (!FromNNS) |
| 3145 | return 0; |
| 3146 | |
| 3147 | // FIXME: Implement! |
| 3148 | return 0; |
| 3149 | } |
| 3150 | |
| 3151 | SourceLocation ASTImporter::Import(SourceLocation FromLoc) { |
| 3152 | if (FromLoc.isInvalid()) |
| 3153 | return SourceLocation(); |
| 3154 | |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 3155 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 3156 | |
| 3157 | // For now, map everything down to its spelling location, so that we |
| 3158 | // don't have to import macro instantiations. |
| 3159 | // FIXME: Import macro instantiations! |
| 3160 | FromLoc = FromSM.getSpellingLoc(FromLoc); |
| 3161 | std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); |
| 3162 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 3163 | return ToSM.getLocForStartOfFile(Import(Decomposed.first)) |
| 3164 | .getFileLocWithOffset(Decomposed.second); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | SourceRange ASTImporter::Import(SourceRange FromRange) { |
| 3168 | return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); |
| 3169 | } |
| 3170 | |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 3171 | FileID ASTImporter::Import(FileID FromID) { |
Sebastian Redl | 99219f1 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 3172 | llvm::DenseMap<FileID, FileID>::iterator Pos |
| 3173 | = ImportedFileIDs.find(FromID); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 3174 | if (Pos != ImportedFileIDs.end()) |
| 3175 | return Pos->second; |
| 3176 | |
| 3177 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 3178 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 3179 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); |
| 3180 | assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet"); |
| 3181 | |
| 3182 | // Include location of this file. |
| 3183 | SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); |
| 3184 | |
| 3185 | // Map the FileID for to the "to" source manager. |
| 3186 | FileID ToID; |
| 3187 | const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); |
| 3188 | if (Cache->Entry) { |
| 3189 | // FIXME: We probably want to use getVirtualFile(), so we don't hit the |
| 3190 | // disk again |
| 3191 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather |
| 3192 | // than mmap the files several times. |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 3193 | const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName(), |
| 3194 | ToFileSystemOpts); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 3195 | ToID = ToSM.createFileID(Entry, ToIncludeLoc, |
| 3196 | FromSLoc.getFile().getFileCharacteristic()); |
| 3197 | } else { |
| 3198 | // FIXME: We want to re-use the existing MemoryBuffer! |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 3199 | const llvm::MemoryBuffer * |
| 3200 | FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 3201 | llvm::MemoryBuffer *ToBuf |
Chris Lattner | 58c7934 | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 3202 | = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 3203 | FromBuf->getBufferIdentifier()); |
| 3204 | ToID = ToSM.createFileIDForMemBuffer(ToBuf); |
| 3205 | } |
| 3206 | |
| 3207 | |
Sebastian Redl | 99219f1 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 3208 | ImportedFileIDs[FromID] = ToID; |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 3209 | return ToID; |
| 3210 | } |
| 3211 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 3212 | DeclarationName ASTImporter::Import(DeclarationName FromName) { |
| 3213 | if (!FromName) |
| 3214 | return DeclarationName(); |
| 3215 | |
| 3216 | switch (FromName.getNameKind()) { |
| 3217 | case DeclarationName::Identifier: |
| 3218 | return Import(FromName.getAsIdentifierInfo()); |
| 3219 | |
| 3220 | case DeclarationName::ObjCZeroArgSelector: |
| 3221 | case DeclarationName::ObjCOneArgSelector: |
| 3222 | case DeclarationName::ObjCMultiArgSelector: |
| 3223 | return Import(FromName.getObjCSelector()); |
| 3224 | |
| 3225 | case DeclarationName::CXXConstructorName: { |
| 3226 | QualType T = Import(FromName.getCXXNameType()); |
| 3227 | if (T.isNull()) |
| 3228 | return DeclarationName(); |
| 3229 | |
| 3230 | return ToContext.DeclarationNames.getCXXConstructorName( |
| 3231 | ToContext.getCanonicalType(T)); |
| 3232 | } |
| 3233 | |
| 3234 | case DeclarationName::CXXDestructorName: { |
| 3235 | QualType T = Import(FromName.getCXXNameType()); |
| 3236 | if (T.isNull()) |
| 3237 | return DeclarationName(); |
| 3238 | |
| 3239 | return ToContext.DeclarationNames.getCXXDestructorName( |
| 3240 | ToContext.getCanonicalType(T)); |
| 3241 | } |
| 3242 | |
| 3243 | case DeclarationName::CXXConversionFunctionName: { |
| 3244 | QualType T = Import(FromName.getCXXNameType()); |
| 3245 | if (T.isNull()) |
| 3246 | return DeclarationName(); |
| 3247 | |
| 3248 | return ToContext.DeclarationNames.getCXXConversionFunctionName( |
| 3249 | ToContext.getCanonicalType(T)); |
| 3250 | } |
| 3251 | |
| 3252 | case DeclarationName::CXXOperatorName: |
| 3253 | return ToContext.DeclarationNames.getCXXOperatorName( |
| 3254 | FromName.getCXXOverloadedOperator()); |
| 3255 | |
| 3256 | case DeclarationName::CXXLiteralOperatorName: |
| 3257 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( |
| 3258 | Import(FromName.getCXXLiteralIdentifier())); |
| 3259 | |
| 3260 | case DeclarationName::CXXUsingDirective: |
| 3261 | // FIXME: STATICS! |
| 3262 | return DeclarationName::getUsingDirectiveName(); |
| 3263 | } |
| 3264 | |
| 3265 | // Silence bogus GCC warning |
| 3266 | return DeclarationName(); |
| 3267 | } |
| 3268 | |
| 3269 | IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) { |
| 3270 | if (!FromId) |
| 3271 | return 0; |
| 3272 | |
| 3273 | return &ToContext.Idents.get(FromId->getName()); |
| 3274 | } |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3275 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3276 | Selector ASTImporter::Import(Selector FromSel) { |
| 3277 | if (FromSel.isNull()) |
| 3278 | return Selector(); |
| 3279 | |
| 3280 | llvm::SmallVector<IdentifierInfo *, 4> Idents; |
| 3281 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); |
| 3282 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) |
| 3283 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); |
| 3284 | return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); |
| 3285 | } |
| 3286 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3287 | DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, |
| 3288 | DeclContext *DC, |
| 3289 | unsigned IDNS, |
| 3290 | NamedDecl **Decls, |
| 3291 | unsigned NumDecls) { |
| 3292 | return Name; |
| 3293 | } |
| 3294 | |
| 3295 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 3296 | return ToContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 3300 | return FromContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3301 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3302 | |
| 3303 | Decl *ASTImporter::Imported(Decl *From, Decl *To) { |
| 3304 | ImportedDecls[From] = To; |
| 3305 | return To; |
Daniel Dunbar | 9ced542 | 2010-02-13 20:24:39 +0000 | [diff] [blame] | 3306 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3307 | |
| 3308 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) { |
| 3309 | llvm::DenseMap<Type *, Type *>::iterator Pos |
| 3310 | = ImportedTypes.find(From.getTypePtr()); |
| 3311 | if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) |
| 3312 | return true; |
| 3313 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 3314 | StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 3315 | return Ctx.IsStructurallyEquivalent(From, To); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3316 | } |