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