blob: c4d1181528cc95a1d7569b6263953775ad561392 [file] [log] [blame]
Douglas Gregor96e578d2010-02-05 17:54:41 +00001//===--- 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 Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000027
28using namespace clang;
29
30namespace {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000031 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000032 public DeclVisitor<ASTNodeImporter, Decl *>,
33 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000034 ASTImporter &Importer;
35
36 public:
37 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
38
39 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000040 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000041 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000042
43 // Importing types
Douglas Gregore4c83e42010-02-09 22:48:33 +000044 QualType VisitType(Type *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000045 QualType VisitBuiltinType(BuiltinType *T);
46 QualType VisitComplexType(ComplexType *T);
47 QualType VisitPointerType(PointerType *T);
48 QualType VisitBlockPointerType(BlockPointerType *T);
49 QualType VisitLValueReferenceType(LValueReferenceType *T);
50 QualType VisitRValueReferenceType(RValueReferenceType *T);
51 QualType VisitMemberPointerType(MemberPointerType *T);
52 QualType VisitConstantArrayType(ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(IncompleteArrayType *T);
54 QualType VisitVariableArrayType(VariableArrayType *T);
55 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
57 QualType VisitVectorType(VectorType *T);
58 QualType VisitExtVectorType(ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(FunctionProtoType *T);
61 // FIXME: UnresolvedUsingType
62 QualType VisitTypedefType(TypedefType *T);
63 QualType VisitTypeOfExprType(TypeOfExprType *T);
64 // FIXME: DependentTypeOfExprType
65 QualType VisitTypeOfType(TypeOfType *T);
66 QualType VisitDecltypeType(DecltypeType *T);
67 // FIXME: DependentDecltypeType
68 QualType VisitRecordType(RecordType *T);
69 QualType VisitEnumType(EnumType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000070 // FIXME: TemplateTypeParmType
71 // FIXME: SubstTemplateTypeParmType
Douglas Gregore2e50d332010-12-01 01:36:18 +000072 QualType VisitTemplateSpecializationType(TemplateSpecializationType *T);
Abramo Bagnara6150c882010-05-11 21:36:43 +000073 QualType VisitElaboratedType(ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000074 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000075 // FIXME: DependentTemplateSpecializationType
Douglas Gregor96e578d2010-02-05 17:54:41 +000076 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
John McCall8b07ec22010-05-15 11:32:37 +000077 QualType VisitObjCObjectType(ObjCObjectType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000078 QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000079
80 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000081 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregorf18a2c72010-02-21 18:26:36 +000083 SourceLocation &Loc);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000084 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
85 DeclarationNameInfo& To);
Douglas Gregor968d6332010-02-21 18:24:45 +000086 void ImportDeclContext(DeclContext *FromDC);
Douglas Gregore2e50d332010-12-01 01:36:18 +000087 bool ImportDefinition(RecordDecl *From, RecordDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +000088 TemplateParameterList *ImportTemplateParameterList(
89 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +000090 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
91 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
92 unsigned NumFromArgs,
93 llvm::SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor5c73e912010-02-11 00:48:18 +000094 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor3996e242010-02-15 22:01:00 +000095 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregora082a492010-11-30 19:14:50 +000096 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +000097 Decl *VisitDecl(Decl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +000098 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor5fa74c32010-02-10 21:10:29 +000099 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000100 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000101 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000102 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000103 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000104 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
105 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
106 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
107 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000108 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000109 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000110 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000111 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000112 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000113 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000114 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000115 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000116 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000117 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000118 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor8661a722010-02-18 02:12:22 +0000119 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000120 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000121 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
122 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
123 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
124 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000125 Decl *VisitClassTemplateSpecializationDecl(
126 ClassTemplateSpecializationDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000127
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000128 // Importing statements
129 Stmt *VisitStmt(Stmt *S);
130
131 // Importing expressions
132 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000133 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000134 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000135 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000136 Expr *VisitParenExpr(ParenExpr *E);
137 Expr *VisitUnaryOperator(UnaryOperator *E);
Douglas Gregord8552cd2010-02-19 01:24:23 +0000138 Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000139 Expr *VisitBinaryOperator(BinaryOperator *E);
140 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000141 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000142 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000143 };
144}
145
146//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000147// Structural Equivalence
148//----------------------------------------------------------------------------
149
150namespace {
151 struct StructuralEquivalenceContext {
152 /// \brief AST contexts for which we are checking structural equivalence.
153 ASTContext &C1, &C2;
154
Douglas Gregor3996e242010-02-15 22:01:00 +0000155 /// \brief The set of "tentative" equivalences between two canonical
156 /// declarations, mapping from a declaration in the first context to the
157 /// declaration in the second context that we believe to be equivalent.
158 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
159
160 /// \brief Queue of declarations in the first context whose equivalence
161 /// with a declaration in the second context still needs to be verified.
162 std::deque<Decl *> DeclsToCheck;
163
Douglas Gregorb4964f72010-02-15 23:54:17 +0000164 /// \brief Declaration (from, to) pairs that are known not to be equivalent
165 /// (which we have already complained about).
166 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
167
Douglas Gregor3996e242010-02-15 22:01:00 +0000168 /// \brief Whether we're being strict about the spelling of types when
169 /// unifying two types.
170 bool StrictTypeSpelling;
171
172 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000173 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor3996e242010-02-15 22:01:00 +0000174 bool StrictTypeSpelling = false)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000175 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000176 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000177
178 /// \brief Determine whether the two declarations are structurally
179 /// equivalent.
180 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
181
182 /// \brief Determine whether the two types are structurally equivalent.
183 bool IsStructurallyEquivalent(QualType T1, QualType T2);
184
185 private:
186 /// \brief Finish checking all of the structural equivalences.
187 ///
188 /// \returns true if an error occurred, false otherwise.
189 bool Finish();
190
191 public:
192 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000193 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000194 }
195
196 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000197 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000198 }
199 };
200}
201
202static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
203 QualType T1, QualType T2);
204static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
205 Decl *D1, Decl *D2);
206
207/// \brief Determine if two APInts have the same value, after zero-extending
208/// one of them (if needed!) to ensure that the bit-widths match.
209static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
210 if (I1.getBitWidth() == I2.getBitWidth())
211 return I1 == I2;
212
213 if (I1.getBitWidth() > I2.getBitWidth())
214 return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
215
216 return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
217}
218
219/// \brief Determine if two APSInts have the same value, zero- or sign-extending
220/// as needed.
221static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
222 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
223 return I1 == I2;
224
225 // Check for a bit-width mismatch.
226 if (I1.getBitWidth() > I2.getBitWidth())
227 return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
228 else if (I2.getBitWidth() > I1.getBitWidth())
229 return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
230
231 // We have a signedness mismatch. Turn the signed value into an unsigned
232 // value.
233 if (I1.isSigned()) {
234 if (I1.isNegative())
235 return false;
236
237 return llvm::APSInt(I1, true) == I2;
238 }
239
240 if (I2.isNegative())
241 return false;
242
243 return I1 == llvm::APSInt(I2, true);
244}
245
246/// \brief Determine structural equivalence of two expressions.
247static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
248 Expr *E1, Expr *E2) {
249 if (!E1 || !E2)
250 return E1 == E2;
251
252 // FIXME: Actually perform a structural comparison!
253 return true;
254}
255
256/// \brief Determine whether two identifiers are equivalent.
257static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
258 const IdentifierInfo *Name2) {
259 if (!Name1 || !Name2)
260 return Name1 == Name2;
261
262 return Name1->getName() == Name2->getName();
263}
264
265/// \brief Determine whether two nested-name-specifiers are equivalent.
266static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
267 NestedNameSpecifier *NNS1,
268 NestedNameSpecifier *NNS2) {
269 // FIXME: Implement!
270 return true;
271}
272
273/// \brief Determine whether two template arguments are equivalent.
274static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
275 const TemplateArgument &Arg1,
276 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000277 if (Arg1.getKind() != Arg2.getKind())
278 return false;
279
280 switch (Arg1.getKind()) {
281 case TemplateArgument::Null:
282 return true;
283
284 case TemplateArgument::Type:
285 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
286
287 case TemplateArgument::Integral:
288 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
289 Arg2.getIntegralType()))
290 return false;
291
292 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
293
294 case TemplateArgument::Declaration:
295 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
296
297 case TemplateArgument::Template:
298 return IsStructurallyEquivalent(Context,
299 Arg1.getAsTemplate(),
300 Arg2.getAsTemplate());
301
302 case TemplateArgument::Expression:
303 return IsStructurallyEquivalent(Context,
304 Arg1.getAsExpr(), Arg2.getAsExpr());
305
306 case TemplateArgument::Pack:
307 if (Arg1.pack_size() != Arg2.pack_size())
308 return false;
309
310 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
311 if (!IsStructurallyEquivalent(Context,
312 Arg1.pack_begin()[I],
313 Arg2.pack_begin()[I]))
314 return false;
315
316 return true;
317 }
318
319 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000320 return true;
321}
322
323/// \brief Determine structural equivalence for the common part of array
324/// types.
325static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
326 const ArrayType *Array1,
327 const ArrayType *Array2) {
328 if (!IsStructurallyEquivalent(Context,
329 Array1->getElementType(),
330 Array2->getElementType()))
331 return false;
332 if (Array1->getSizeModifier() != Array2->getSizeModifier())
333 return false;
334 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
335 return false;
336
337 return true;
338}
339
340/// \brief Determine structural equivalence of two types.
341static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
342 QualType T1, QualType T2) {
343 if (T1.isNull() || T2.isNull())
344 return T1.isNull() && T2.isNull();
345
346 if (!Context.StrictTypeSpelling) {
347 // We aren't being strict about token-to-token equivalence of types,
348 // so map down to the canonical type.
349 T1 = Context.C1.getCanonicalType(T1);
350 T2 = Context.C2.getCanonicalType(T2);
351 }
352
353 if (T1.getQualifiers() != T2.getQualifiers())
354 return false;
355
Douglas Gregorb4964f72010-02-15 23:54:17 +0000356 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000357
Douglas Gregorb4964f72010-02-15 23:54:17 +0000358 if (T1->getTypeClass() != T2->getTypeClass()) {
359 // Compare function types with prototypes vs. without prototypes as if
360 // both did not have prototypes.
361 if (T1->getTypeClass() == Type::FunctionProto &&
362 T2->getTypeClass() == Type::FunctionNoProto)
363 TC = Type::FunctionNoProto;
364 else if (T1->getTypeClass() == Type::FunctionNoProto &&
365 T2->getTypeClass() == Type::FunctionProto)
366 TC = Type::FunctionNoProto;
367 else
368 return false;
369 }
370
371 switch (TC) {
372 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000373 // FIXME: Deal with Char_S/Char_U.
374 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
375 return false;
376 break;
377
378 case Type::Complex:
379 if (!IsStructurallyEquivalent(Context,
380 cast<ComplexType>(T1)->getElementType(),
381 cast<ComplexType>(T2)->getElementType()))
382 return false;
383 break;
384
385 case Type::Pointer:
386 if (!IsStructurallyEquivalent(Context,
387 cast<PointerType>(T1)->getPointeeType(),
388 cast<PointerType>(T2)->getPointeeType()))
389 return false;
390 break;
391
392 case Type::BlockPointer:
393 if (!IsStructurallyEquivalent(Context,
394 cast<BlockPointerType>(T1)->getPointeeType(),
395 cast<BlockPointerType>(T2)->getPointeeType()))
396 return false;
397 break;
398
399 case Type::LValueReference:
400 case Type::RValueReference: {
401 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
402 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
403 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
404 return false;
405 if (Ref1->isInnerRef() != Ref2->isInnerRef())
406 return false;
407 if (!IsStructurallyEquivalent(Context,
408 Ref1->getPointeeTypeAsWritten(),
409 Ref2->getPointeeTypeAsWritten()))
410 return false;
411 break;
412 }
413
414 case Type::MemberPointer: {
415 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
416 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
417 if (!IsStructurallyEquivalent(Context,
418 MemPtr1->getPointeeType(),
419 MemPtr2->getPointeeType()))
420 return false;
421 if (!IsStructurallyEquivalent(Context,
422 QualType(MemPtr1->getClass(), 0),
423 QualType(MemPtr2->getClass(), 0)))
424 return false;
425 break;
426 }
427
428 case Type::ConstantArray: {
429 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
430 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
431 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
432 return false;
433
434 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
435 return false;
436 break;
437 }
438
439 case Type::IncompleteArray:
440 if (!IsArrayStructurallyEquivalent(Context,
441 cast<ArrayType>(T1),
442 cast<ArrayType>(T2)))
443 return false;
444 break;
445
446 case Type::VariableArray: {
447 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
448 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
449 if (!IsStructurallyEquivalent(Context,
450 Array1->getSizeExpr(), Array2->getSizeExpr()))
451 return false;
452
453 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
454 return false;
455
456 break;
457 }
458
459 case Type::DependentSizedArray: {
460 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
461 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
462 if (!IsStructurallyEquivalent(Context,
463 Array1->getSizeExpr(), Array2->getSizeExpr()))
464 return false;
465
466 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
467 return false;
468
469 break;
470 }
471
472 case Type::DependentSizedExtVector: {
473 const DependentSizedExtVectorType *Vec1
474 = cast<DependentSizedExtVectorType>(T1);
475 const DependentSizedExtVectorType *Vec2
476 = cast<DependentSizedExtVectorType>(T2);
477 if (!IsStructurallyEquivalent(Context,
478 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
479 return false;
480 if (!IsStructurallyEquivalent(Context,
481 Vec1->getElementType(),
482 Vec2->getElementType()))
483 return false;
484 break;
485 }
486
487 case Type::Vector:
488 case Type::ExtVector: {
489 const VectorType *Vec1 = cast<VectorType>(T1);
490 const VectorType *Vec2 = cast<VectorType>(T2);
491 if (!IsStructurallyEquivalent(Context,
492 Vec1->getElementType(),
493 Vec2->getElementType()))
494 return false;
495 if (Vec1->getNumElements() != Vec2->getNumElements())
496 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000497 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000498 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000499 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000500 }
501
502 case Type::FunctionProto: {
503 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
504 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
505 if (Proto1->getNumArgs() != Proto2->getNumArgs())
506 return false;
507 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
508 if (!IsStructurallyEquivalent(Context,
509 Proto1->getArgType(I),
510 Proto2->getArgType(I)))
511 return false;
512 }
513 if (Proto1->isVariadic() != Proto2->isVariadic())
514 return false;
515 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
516 return false;
517 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
518 return false;
519 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
520 return false;
521 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
522 if (!IsStructurallyEquivalent(Context,
523 Proto1->getExceptionType(I),
524 Proto2->getExceptionType(I)))
525 return false;
526 }
527 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
528 return false;
529
530 // Fall through to check the bits common with FunctionNoProtoType.
531 }
532
533 case Type::FunctionNoProto: {
534 const FunctionType *Function1 = cast<FunctionType>(T1);
535 const FunctionType *Function2 = cast<FunctionType>(T2);
536 if (!IsStructurallyEquivalent(Context,
537 Function1->getResultType(),
538 Function2->getResultType()))
539 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000540 if (Function1->getExtInfo() != Function2->getExtInfo())
541 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000542 break;
543 }
544
545 case Type::UnresolvedUsing:
546 if (!IsStructurallyEquivalent(Context,
547 cast<UnresolvedUsingType>(T1)->getDecl(),
548 cast<UnresolvedUsingType>(T2)->getDecl()))
549 return false;
550
551 break;
552
553 case Type::Typedef:
554 if (!IsStructurallyEquivalent(Context,
555 cast<TypedefType>(T1)->getDecl(),
556 cast<TypedefType>(T2)->getDecl()))
557 return false;
558 break;
559
560 case Type::TypeOfExpr:
561 if (!IsStructurallyEquivalent(Context,
562 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
563 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
564 return false;
565 break;
566
567 case Type::TypeOf:
568 if (!IsStructurallyEquivalent(Context,
569 cast<TypeOfType>(T1)->getUnderlyingType(),
570 cast<TypeOfType>(T2)->getUnderlyingType()))
571 return false;
572 break;
573
574 case Type::Decltype:
575 if (!IsStructurallyEquivalent(Context,
576 cast<DecltypeType>(T1)->getUnderlyingExpr(),
577 cast<DecltypeType>(T2)->getUnderlyingExpr()))
578 return false;
579 break;
580
581 case Type::Record:
582 case Type::Enum:
583 if (!IsStructurallyEquivalent(Context,
584 cast<TagType>(T1)->getDecl(),
585 cast<TagType>(T2)->getDecl()))
586 return false;
587 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000588
Douglas Gregor3996e242010-02-15 22:01:00 +0000589 case Type::TemplateTypeParm: {
590 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
591 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
592 if (Parm1->getDepth() != Parm2->getDepth())
593 return false;
594 if (Parm1->getIndex() != Parm2->getIndex())
595 return false;
596 if (Parm1->isParameterPack() != Parm2->isParameterPack())
597 return false;
598
599 // Names of template type parameters are never significant.
600 break;
601 }
602
603 case Type::SubstTemplateTypeParm: {
604 const SubstTemplateTypeParmType *Subst1
605 = cast<SubstTemplateTypeParmType>(T1);
606 const SubstTemplateTypeParmType *Subst2
607 = cast<SubstTemplateTypeParmType>(T2);
608 if (!IsStructurallyEquivalent(Context,
609 QualType(Subst1->getReplacedParameter(), 0),
610 QualType(Subst2->getReplacedParameter(), 0)))
611 return false;
612 if (!IsStructurallyEquivalent(Context,
613 Subst1->getReplacementType(),
614 Subst2->getReplacementType()))
615 return false;
616 break;
617 }
618
619 case Type::TemplateSpecialization: {
620 const TemplateSpecializationType *Spec1
621 = cast<TemplateSpecializationType>(T1);
622 const TemplateSpecializationType *Spec2
623 = cast<TemplateSpecializationType>(T2);
624 if (!IsStructurallyEquivalent(Context,
625 Spec1->getTemplateName(),
626 Spec2->getTemplateName()))
627 return false;
628 if (Spec1->getNumArgs() != Spec2->getNumArgs())
629 return false;
630 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
631 if (!IsStructurallyEquivalent(Context,
632 Spec1->getArg(I), Spec2->getArg(I)))
633 return false;
634 }
635 break;
636 }
637
Abramo Bagnara6150c882010-05-11 21:36:43 +0000638 case Type::Elaborated: {
639 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
640 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
641 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
642 if (Elab1->getKeyword() != Elab2->getKeyword())
643 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000644 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000645 Elab1->getQualifier(),
646 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000647 return false;
648 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000649 Elab1->getNamedType(),
650 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000651 return false;
652 break;
653 }
654
John McCalle78aac42010-03-10 03:28:59 +0000655 case Type::InjectedClassName: {
656 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
657 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
658 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000659 Inj1->getInjectedSpecializationType(),
660 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000661 return false;
662 break;
663 }
664
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000665 case Type::DependentName: {
666 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
667 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000668 if (!IsStructurallyEquivalent(Context,
669 Typename1->getQualifier(),
670 Typename2->getQualifier()))
671 return false;
672 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
673 Typename2->getIdentifier()))
674 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000675
676 break;
677 }
678
John McCallc392f372010-06-11 00:33:02 +0000679 case Type::DependentTemplateSpecialization: {
680 const DependentTemplateSpecializationType *Spec1 =
681 cast<DependentTemplateSpecializationType>(T1);
682 const DependentTemplateSpecializationType *Spec2 =
683 cast<DependentTemplateSpecializationType>(T2);
684 if (!IsStructurallyEquivalent(Context,
685 Spec1->getQualifier(),
686 Spec2->getQualifier()))
687 return false;
688 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
689 Spec2->getIdentifier()))
690 return false;
691 if (Spec1->getNumArgs() != Spec2->getNumArgs())
692 return false;
693 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
694 if (!IsStructurallyEquivalent(Context,
695 Spec1->getArg(I), Spec2->getArg(I)))
696 return false;
697 }
698 break;
699 }
700
Douglas Gregor3996e242010-02-15 22:01:00 +0000701 case Type::ObjCInterface: {
702 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
703 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
704 if (!IsStructurallyEquivalent(Context,
705 Iface1->getDecl(), Iface2->getDecl()))
706 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000707 break;
708 }
709
710 case Type::ObjCObject: {
711 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
712 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
713 if (!IsStructurallyEquivalent(Context,
714 Obj1->getBaseType(),
715 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000716 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000717 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
718 return false;
719 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000720 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000721 Obj1->getProtocol(I),
722 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000723 return false;
724 }
725 break;
726 }
727
728 case Type::ObjCObjectPointer: {
729 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
730 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
731 if (!IsStructurallyEquivalent(Context,
732 Ptr1->getPointeeType(),
733 Ptr2->getPointeeType()))
734 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000735 break;
736 }
737
738 } // end switch
739
740 return true;
741}
742
743/// \brief Determine structural equivalence of two records.
744static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
745 RecordDecl *D1, RecordDecl *D2) {
746 if (D1->isUnion() != D2->isUnion()) {
747 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
748 << Context.C2.getTypeDeclType(D2);
749 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
750 << D1->getDeclName() << (unsigned)D1->getTagKind();
751 return false;
752 }
753
Douglas Gregore2e50d332010-12-01 01:36:18 +0000754 // If both declarations are class template specializations, we know
755 // the ODR applies, so check the template and template arguments.
756 ClassTemplateSpecializationDecl *Spec1
757 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
758 ClassTemplateSpecializationDecl *Spec2
759 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
760 if (Spec1 && Spec2) {
761 // Check that the specialized templates are the same.
762 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
763 Spec2->getSpecializedTemplate()))
764 return false;
765
766 // Check that the template arguments are the same.
767 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
768 return false;
769
770 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
771 if (!IsStructurallyEquivalent(Context,
772 Spec1->getTemplateArgs().get(I),
773 Spec2->getTemplateArgs().get(I)))
774 return false;
775 }
776 // If one is a class template specialization and the other is not, these
777 // structures are diferent.
778 else if (Spec1 || Spec2)
779 return false;
780
Douglas Gregorb4964f72010-02-15 23:54:17 +0000781 // Compare the definitions of these two records. If either or both are
782 // incomplete, we assume that they are equivalent.
783 D1 = D1->getDefinition();
784 D2 = D2->getDefinition();
785 if (!D1 || !D2)
786 return true;
787
Douglas Gregor3996e242010-02-15 22:01:00 +0000788 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
789 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
790 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
791 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregora082a492010-11-30 19:14:50 +0000792 << Context.C2.getTypeDeclType(D2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000793 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000794 << D2CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000795 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000796 << D1CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000797 return false;
798 }
799
800 // Check the base classes.
801 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
802 BaseEnd1 = D1CXX->bases_end(),
803 Base2 = D2CXX->bases_begin();
804 Base1 != BaseEnd1;
805 ++Base1, ++Base2) {
806 if (!IsStructurallyEquivalent(Context,
807 Base1->getType(), Base2->getType())) {
808 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
809 << Context.C2.getTypeDeclType(D2);
810 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
811 << Base2->getType()
812 << Base2->getSourceRange();
813 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
814 << Base1->getType()
815 << Base1->getSourceRange();
816 return false;
817 }
818
819 // Check virtual vs. non-virtual inheritance mismatch.
820 if (Base1->isVirtual() != Base2->isVirtual()) {
821 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
822 << Context.C2.getTypeDeclType(D2);
823 Context.Diag2(Base2->getSourceRange().getBegin(),
824 diag::note_odr_virtual_base)
825 << Base2->isVirtual() << Base2->getSourceRange();
826 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
827 << Base1->isVirtual()
828 << Base1->getSourceRange();
829 return false;
830 }
831 }
832 } else if (D1CXX->getNumBases() > 0) {
833 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
834 << Context.C2.getTypeDeclType(D2);
835 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
836 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
837 << Base1->getType()
838 << Base1->getSourceRange();
839 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
840 return false;
841 }
842 }
843
844 // Check the fields for consistency.
845 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
846 Field2End = D2->field_end();
847 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
848 Field1End = D1->field_end();
849 Field1 != Field1End;
850 ++Field1, ++Field2) {
851 if (Field2 == Field2End) {
852 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
853 << Context.C2.getTypeDeclType(D2);
854 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
855 << Field1->getDeclName() << Field1->getType();
856 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
857 return false;
858 }
859
860 if (!IsStructurallyEquivalent(Context,
861 Field1->getType(), Field2->getType())) {
862 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
863 << Context.C2.getTypeDeclType(D2);
864 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
865 << Field2->getDeclName() << Field2->getType();
866 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
867 << Field1->getDeclName() << Field1->getType();
868 return false;
869 }
870
871 if (Field1->isBitField() != Field2->isBitField()) {
872 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
873 << Context.C2.getTypeDeclType(D2);
874 if (Field1->isBitField()) {
875 llvm::APSInt Bits;
876 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
877 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
878 << Field1->getDeclName() << Field1->getType()
879 << Bits.toString(10, false);
880 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
881 << Field2->getDeclName();
882 } else {
883 llvm::APSInt Bits;
884 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
885 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
886 << Field2->getDeclName() << Field2->getType()
887 << Bits.toString(10, false);
888 Context.Diag1(Field1->getLocation(),
889 diag::note_odr_not_bit_field)
890 << Field1->getDeclName();
891 }
892 return false;
893 }
894
895 if (Field1->isBitField()) {
896 // Make sure that the bit-fields are the same length.
897 llvm::APSInt Bits1, Bits2;
898 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
899 return false;
900 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
901 return false;
902
903 if (!IsSameValue(Bits1, Bits2)) {
904 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
905 << Context.C2.getTypeDeclType(D2);
906 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
907 << Field2->getDeclName() << Field2->getType()
908 << Bits2.toString(10, false);
909 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
910 << Field1->getDeclName() << Field1->getType()
911 << Bits1.toString(10, false);
912 return false;
913 }
914 }
915 }
916
917 if (Field2 != Field2End) {
918 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
919 << Context.C2.getTypeDeclType(D2);
920 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
921 << Field2->getDeclName() << Field2->getType();
922 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
923 return false;
924 }
925
926 return true;
927}
928
929/// \brief Determine structural equivalence of two enums.
930static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
931 EnumDecl *D1, EnumDecl *D2) {
932 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
933 EC2End = D2->enumerator_end();
934 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
935 EC1End = D1->enumerator_end();
936 EC1 != EC1End; ++EC1, ++EC2) {
937 if (EC2 == EC2End) {
938 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
939 << Context.C2.getTypeDeclType(D2);
940 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
941 << EC1->getDeclName()
942 << EC1->getInitVal().toString(10);
943 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
944 return false;
945 }
946
947 llvm::APSInt Val1 = EC1->getInitVal();
948 llvm::APSInt Val2 = EC2->getInitVal();
949 if (!IsSameValue(Val1, Val2) ||
950 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
951 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
952 << Context.C2.getTypeDeclType(D2);
953 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
954 << EC2->getDeclName()
955 << EC2->getInitVal().toString(10);
956 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
957 << EC1->getDeclName()
958 << EC1->getInitVal().toString(10);
959 return false;
960 }
961 }
962
963 if (EC2 != EC2End) {
964 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
965 << Context.C2.getTypeDeclType(D2);
966 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
967 << EC2->getDeclName()
968 << EC2->getInitVal().toString(10);
969 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
970 return false;
971 }
972
973 return true;
974}
Douglas Gregora082a492010-11-30 19:14:50 +0000975
976static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
977 TemplateParameterList *Params1,
978 TemplateParameterList *Params2) {
979 if (Params1->size() != Params2->size()) {
980 Context.Diag2(Params2->getTemplateLoc(),
981 diag::err_odr_different_num_template_parameters)
982 << Params1->size() << Params2->size();
983 Context.Diag1(Params1->getTemplateLoc(),
984 diag::note_odr_template_parameter_list);
985 return false;
986 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000987
Douglas Gregora082a492010-11-30 19:14:50 +0000988 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
989 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
990 Context.Diag2(Params2->getParam(I)->getLocation(),
991 diag::err_odr_different_template_parameter_kind);
992 Context.Diag1(Params1->getParam(I)->getLocation(),
993 diag::note_odr_template_parameter_here);
994 return false;
995 }
996
997 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
998 Params2->getParam(I))) {
999
1000 return false;
1001 }
1002 }
1003
1004 return true;
1005}
1006
1007static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1008 TemplateTypeParmDecl *D1,
1009 TemplateTypeParmDecl *D2) {
1010 if (D1->isParameterPack() != D2->isParameterPack()) {
1011 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1012 << D2->isParameterPack();
1013 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1014 << D1->isParameterPack();
1015 return false;
1016 }
1017
1018 return true;
1019}
1020
1021static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1022 NonTypeTemplateParmDecl *D1,
1023 NonTypeTemplateParmDecl *D2) {
1024 // FIXME: Enable once we have variadic templates.
1025#if 0
1026 if (D1->isParameterPack() != D2->isParameterPack()) {
1027 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1028 << D2->isParameterPack();
1029 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1030 << D1->isParameterPack();
1031 return false;
1032 }
1033#endif
1034
1035 // Check types.
1036 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1037 Context.Diag2(D2->getLocation(),
1038 diag::err_odr_non_type_parameter_type_inconsistent)
1039 << D2->getType() << D1->getType();
1040 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1041 << D1->getType();
1042 return false;
1043 }
1044
1045 return true;
1046}
1047
1048static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1049 TemplateTemplateParmDecl *D1,
1050 TemplateTemplateParmDecl *D2) {
1051 // FIXME: Enable once we have variadic templates.
1052#if 0
1053 if (D1->isParameterPack() != D2->isParameterPack()) {
1054 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1055 << D2->isParameterPack();
1056 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1057 << D1->isParameterPack();
1058 return false;
1059 }
1060#endif
1061
1062 // Check template parameter lists.
1063 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1064 D2->getTemplateParameters());
1065}
1066
1067static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1068 ClassTemplateDecl *D1,
1069 ClassTemplateDecl *D2) {
1070 // Check template parameters.
1071 if (!IsStructurallyEquivalent(Context,
1072 D1->getTemplateParameters(),
1073 D2->getTemplateParameters()))
1074 return false;
1075
1076 // Check the templated declaration.
1077 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1078 D2->getTemplatedDecl());
1079}
1080
Douglas Gregor3996e242010-02-15 22:01:00 +00001081/// \brief Determine structural equivalence of two declarations.
1082static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1083 Decl *D1, Decl *D2) {
1084 // FIXME: Check for known structural equivalences via a callback of some sort.
1085
Douglas Gregorb4964f72010-02-15 23:54:17 +00001086 // Check whether we already know that these two declarations are not
1087 // structurally equivalent.
1088 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1089 D2->getCanonicalDecl())))
1090 return false;
1091
Douglas Gregor3996e242010-02-15 22:01:00 +00001092 // Determine whether we've already produced a tentative equivalence for D1.
1093 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1094 if (EquivToD1)
1095 return EquivToD1 == D2->getCanonicalDecl();
1096
1097 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1098 EquivToD1 = D2->getCanonicalDecl();
1099 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1100 return true;
1101}
1102
1103bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1104 Decl *D2) {
1105 if (!::IsStructurallyEquivalent(*this, D1, D2))
1106 return false;
1107
1108 return !Finish();
1109}
1110
1111bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1112 QualType T2) {
1113 if (!::IsStructurallyEquivalent(*this, T1, T2))
1114 return false;
1115
1116 return !Finish();
1117}
1118
1119bool StructuralEquivalenceContext::Finish() {
1120 while (!DeclsToCheck.empty()) {
1121 // Check the next declaration.
1122 Decl *D1 = DeclsToCheck.front();
1123 DeclsToCheck.pop_front();
1124
1125 Decl *D2 = TentativeEquivalences[D1];
1126 assert(D2 && "Unrecorded tentative equivalence?");
1127
Douglas Gregorb4964f72010-02-15 23:54:17 +00001128 bool Equivalent = true;
1129
Douglas Gregor3996e242010-02-15 22:01:00 +00001130 // FIXME: Switch on all declaration kinds. For now, we're just going to
1131 // check the obvious ones.
1132 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1133 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1134 // Check for equivalent structure names.
1135 IdentifierInfo *Name1 = Record1->getIdentifier();
1136 if (!Name1 && Record1->getTypedefForAnonDecl())
1137 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
1138 IdentifierInfo *Name2 = Record2->getIdentifier();
1139 if (!Name2 && Record2->getTypedefForAnonDecl())
1140 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001141 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1142 !::IsStructurallyEquivalent(*this, Record1, Record2))
1143 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001144 } else {
1145 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001146 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001147 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001148 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001149 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1150 // Check for equivalent enum names.
1151 IdentifierInfo *Name1 = Enum1->getIdentifier();
1152 if (!Name1 && Enum1->getTypedefForAnonDecl())
1153 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
1154 IdentifierInfo *Name2 = Enum2->getIdentifier();
1155 if (!Name2 && Enum2->getTypedefForAnonDecl())
1156 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001157 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1158 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1159 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001160 } else {
1161 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001162 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001163 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001164 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001165 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
1166 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001167 Typedef2->getIdentifier()) ||
1168 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001169 Typedef1->getUnderlyingType(),
1170 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001171 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001172 } else {
1173 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001174 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001175 }
Douglas Gregora082a492010-11-30 19:14:50 +00001176 } else if (ClassTemplateDecl *ClassTemplate1
1177 = dyn_cast<ClassTemplateDecl>(D1)) {
1178 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1179 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1180 ClassTemplate2->getIdentifier()) ||
1181 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1182 Equivalent = false;
1183 } else {
1184 // Class template/non-class-template mismatch.
1185 Equivalent = false;
1186 }
1187 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1188 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1189 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1190 Equivalent = false;
1191 } else {
1192 // Kind mismatch.
1193 Equivalent = false;
1194 }
1195 } else if (NonTypeTemplateParmDecl *NTTP1
1196 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1197 if (NonTypeTemplateParmDecl *NTTP2
1198 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1199 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1200 Equivalent = false;
1201 } else {
1202 // Kind mismatch.
1203 Equivalent = false;
1204 }
1205 } else if (TemplateTemplateParmDecl *TTP1
1206 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1207 if (TemplateTemplateParmDecl *TTP2
1208 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1209 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1210 Equivalent = false;
1211 } else {
1212 // Kind mismatch.
1213 Equivalent = false;
1214 }
1215 }
1216
Douglas Gregorb4964f72010-02-15 23:54:17 +00001217 if (!Equivalent) {
1218 // Note that these two declarations are not equivalent (and we already
1219 // know about it).
1220 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1221 D2->getCanonicalDecl()));
1222 return true;
1223 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001224 // FIXME: Check other declaration kinds!
1225 }
1226
1227 return false;
1228}
1229
1230//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001231// Import Types
1232//----------------------------------------------------------------------------
1233
Douglas Gregore4c83e42010-02-09 22:48:33 +00001234QualType ASTNodeImporter::VisitType(Type *T) {
1235 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1236 << T->getTypeClassName();
1237 return QualType();
1238}
1239
Douglas Gregor96e578d2010-02-05 17:54:41 +00001240QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
1241 switch (T->getKind()) {
1242 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1243 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1244
1245 case BuiltinType::Char_U:
1246 // The context we're importing from has an unsigned 'char'. If we're
1247 // importing into a context with a signed 'char', translate to
1248 // 'unsigned char' instead.
1249 if (Importer.getToContext().getLangOptions().CharIsSigned)
1250 return Importer.getToContext().UnsignedCharTy;
1251
1252 return Importer.getToContext().CharTy;
1253
1254 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1255
1256 case BuiltinType::Char16:
1257 // FIXME: Make sure that the "to" context supports C++!
1258 return Importer.getToContext().Char16Ty;
1259
1260 case BuiltinType::Char32:
1261 // FIXME: Make sure that the "to" context supports C++!
1262 return Importer.getToContext().Char32Ty;
1263
1264 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1265 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1266 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1267 case BuiltinType::ULongLong:
1268 return Importer.getToContext().UnsignedLongLongTy;
1269 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1270
1271 case BuiltinType::Char_S:
1272 // The context we're importing from has an unsigned 'char'. If we're
1273 // importing into a context with a signed 'char', translate to
1274 // 'unsigned char' instead.
1275 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1276 return Importer.getToContext().SignedCharTy;
1277
1278 return Importer.getToContext().CharTy;
1279
1280 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1281 case BuiltinType::WChar:
1282 // FIXME: If not in C++, shall we translate to the C equivalent of
1283 // wchar_t?
1284 return Importer.getToContext().WCharTy;
1285
1286 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1287 case BuiltinType::Int : return Importer.getToContext().IntTy;
1288 case BuiltinType::Long : return Importer.getToContext().LongTy;
1289 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1290 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1291 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1292 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1293 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1294
1295 case BuiltinType::NullPtr:
1296 // FIXME: Make sure that the "to" context supports C++0x!
1297 return Importer.getToContext().NullPtrTy;
1298
1299 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1300 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1301 case BuiltinType::UndeducedAuto:
1302 // FIXME: Make sure that the "to" context supports C++0x!
1303 return Importer.getToContext().UndeducedAutoTy;
1304
1305 case BuiltinType::ObjCId:
1306 // FIXME: Make sure that the "to" context supports Objective-C!
1307 return Importer.getToContext().ObjCBuiltinIdTy;
1308
1309 case BuiltinType::ObjCClass:
1310 return Importer.getToContext().ObjCBuiltinClassTy;
1311
1312 case BuiltinType::ObjCSel:
1313 return Importer.getToContext().ObjCBuiltinSelTy;
1314 }
1315
1316 return QualType();
1317}
1318
1319QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1320 QualType ToElementType = Importer.Import(T->getElementType());
1321 if (ToElementType.isNull())
1322 return QualType();
1323
1324 return Importer.getToContext().getComplexType(ToElementType);
1325}
1326
1327QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1328 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1329 if (ToPointeeType.isNull())
1330 return QualType();
1331
1332 return Importer.getToContext().getPointerType(ToPointeeType);
1333}
1334
1335QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1336 // FIXME: Check for blocks support in "to" context.
1337 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1338 if (ToPointeeType.isNull())
1339 return QualType();
1340
1341 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1342}
1343
1344QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1345 // FIXME: Check for C++ support in "to" context.
1346 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1347 if (ToPointeeType.isNull())
1348 return QualType();
1349
1350 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1351}
1352
1353QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1354 // FIXME: Check for C++0x support in "to" context.
1355 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1356 if (ToPointeeType.isNull())
1357 return QualType();
1358
1359 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1360}
1361
1362QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1363 // FIXME: Check for C++ support in "to" context.
1364 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1365 if (ToPointeeType.isNull())
1366 return QualType();
1367
1368 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1369 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1370 ClassType.getTypePtr());
1371}
1372
1373QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1374 QualType ToElementType = Importer.Import(T->getElementType());
1375 if (ToElementType.isNull())
1376 return QualType();
1377
1378 return Importer.getToContext().getConstantArrayType(ToElementType,
1379 T->getSize(),
1380 T->getSizeModifier(),
1381 T->getIndexTypeCVRQualifiers());
1382}
1383
1384QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1385 QualType ToElementType = Importer.Import(T->getElementType());
1386 if (ToElementType.isNull())
1387 return QualType();
1388
1389 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1390 T->getSizeModifier(),
1391 T->getIndexTypeCVRQualifiers());
1392}
1393
1394QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1395 QualType ToElementType = Importer.Import(T->getElementType());
1396 if (ToElementType.isNull())
1397 return QualType();
1398
1399 Expr *Size = Importer.Import(T->getSizeExpr());
1400 if (!Size)
1401 return QualType();
1402
1403 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1404 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1405 T->getSizeModifier(),
1406 T->getIndexTypeCVRQualifiers(),
1407 Brackets);
1408}
1409
1410QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1411 QualType ToElementType = Importer.Import(T->getElementType());
1412 if (ToElementType.isNull())
1413 return QualType();
1414
1415 return Importer.getToContext().getVectorType(ToElementType,
1416 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001417 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001418}
1419
1420QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1421 QualType ToElementType = Importer.Import(T->getElementType());
1422 if (ToElementType.isNull())
1423 return QualType();
1424
1425 return Importer.getToContext().getExtVectorType(ToElementType,
1426 T->getNumElements());
1427}
1428
1429QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1430 // FIXME: What happens if we're importing a function without a prototype
1431 // into C++? Should we make it variadic?
1432 QualType ToResultType = Importer.Import(T->getResultType());
1433 if (ToResultType.isNull())
1434 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001435
Douglas Gregor96e578d2010-02-05 17:54:41 +00001436 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001437 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001438}
1439
1440QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1441 QualType ToResultType = Importer.Import(T->getResultType());
1442 if (ToResultType.isNull())
1443 return QualType();
1444
1445 // Import argument types
1446 llvm::SmallVector<QualType, 4> ArgTypes;
1447 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1448 AEnd = T->arg_type_end();
1449 A != AEnd; ++A) {
1450 QualType ArgType = Importer.Import(*A);
1451 if (ArgType.isNull())
1452 return QualType();
1453 ArgTypes.push_back(ArgType);
1454 }
1455
1456 // Import exception types
1457 llvm::SmallVector<QualType, 4> ExceptionTypes;
1458 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1459 EEnd = T->exception_end();
1460 E != EEnd; ++E) {
1461 QualType ExceptionType = Importer.Import(*E);
1462 if (ExceptionType.isNull())
1463 return QualType();
1464 ExceptionTypes.push_back(ExceptionType);
1465 }
1466
1467 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1468 ArgTypes.size(),
1469 T->isVariadic(),
1470 T->getTypeQuals(),
1471 T->hasExceptionSpec(),
1472 T->hasAnyExceptionSpec(),
1473 ExceptionTypes.size(),
1474 ExceptionTypes.data(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001475 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001476}
1477
1478QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1479 TypedefDecl *ToDecl
1480 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1481 if (!ToDecl)
1482 return QualType();
1483
1484 return Importer.getToContext().getTypeDeclType(ToDecl);
1485}
1486
1487QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1488 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1489 if (!ToExpr)
1490 return QualType();
1491
1492 return Importer.getToContext().getTypeOfExprType(ToExpr);
1493}
1494
1495QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1496 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1497 if (ToUnderlyingType.isNull())
1498 return QualType();
1499
1500 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1501}
1502
1503QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1504 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1505 if (!ToExpr)
1506 return QualType();
1507
1508 return Importer.getToContext().getDecltypeType(ToExpr);
1509}
1510
1511QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1512 RecordDecl *ToDecl
1513 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1514 if (!ToDecl)
1515 return QualType();
1516
1517 return Importer.getToContext().getTagDeclType(ToDecl);
1518}
1519
1520QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1521 EnumDecl *ToDecl
1522 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1523 if (!ToDecl)
1524 return QualType();
1525
1526 return Importer.getToContext().getTagDeclType(ToDecl);
1527}
1528
Douglas Gregore2e50d332010-12-01 01:36:18 +00001529QualType ASTNodeImporter::VisitTemplateSpecializationType(
1530 TemplateSpecializationType *T) {
1531 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1532 if (ToTemplate.isNull())
1533 return QualType();
1534
1535 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1536 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1537 return QualType();
1538
1539 QualType ToCanonType;
1540 if (!QualType(T, 0).isCanonical()) {
1541 QualType FromCanonType
1542 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1543 ToCanonType =Importer.Import(FromCanonType);
1544 if (ToCanonType.isNull())
1545 return QualType();
1546 }
1547 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1548 ToTemplateArgs.data(),
1549 ToTemplateArgs.size(),
1550 ToCanonType);
1551}
1552
Douglas Gregor96e578d2010-02-05 17:54:41 +00001553QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001554 NestedNameSpecifier *ToQualifier = 0;
1555 // Note: the qualifier in an ElaboratedType is optional.
1556 if (T->getQualifier()) {
1557 ToQualifier = Importer.Import(T->getQualifier());
1558 if (!ToQualifier)
1559 return QualType();
1560 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001561
1562 QualType ToNamedType = Importer.Import(T->getNamedType());
1563 if (ToNamedType.isNull())
1564 return QualType();
1565
Abramo Bagnara6150c882010-05-11 21:36:43 +00001566 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1567 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001568}
1569
1570QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1571 ObjCInterfaceDecl *Class
1572 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1573 if (!Class)
1574 return QualType();
1575
John McCall8b07ec22010-05-15 11:32:37 +00001576 return Importer.getToContext().getObjCInterfaceType(Class);
1577}
1578
1579QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1580 QualType ToBaseType = Importer.Import(T->getBaseType());
1581 if (ToBaseType.isNull())
1582 return QualType();
1583
Douglas Gregor96e578d2010-02-05 17:54:41 +00001584 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001585 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001586 PEnd = T->qual_end();
1587 P != PEnd; ++P) {
1588 ObjCProtocolDecl *Protocol
1589 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1590 if (!Protocol)
1591 return QualType();
1592 Protocols.push_back(Protocol);
1593 }
1594
John McCall8b07ec22010-05-15 11:32:37 +00001595 return Importer.getToContext().getObjCObjectType(ToBaseType,
1596 Protocols.data(),
1597 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001598}
1599
1600QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1601 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1602 if (ToPointeeType.isNull())
1603 return QualType();
1604
John McCall8b07ec22010-05-15 11:32:37 +00001605 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001606}
1607
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001608//----------------------------------------------------------------------------
1609// Import Declarations
1610//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001611bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1612 DeclContext *&LexicalDC,
1613 DeclarationName &Name,
1614 SourceLocation &Loc) {
1615 // Import the context of this declaration.
1616 DC = Importer.ImportContext(D->getDeclContext());
1617 if (!DC)
1618 return true;
1619
1620 LexicalDC = DC;
1621 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1622 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1623 if (!LexicalDC)
1624 return true;
1625 }
1626
1627 // Import the name of this declaration.
1628 Name = Importer.Import(D->getDeclName());
1629 if (D->getDeclName() && !Name)
1630 return true;
1631
1632 // Import the location of this declaration.
1633 Loc = Importer.Import(D->getLocation());
1634 return false;
1635}
1636
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001637void
1638ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1639 DeclarationNameInfo& To) {
1640 // NOTE: To.Name and To.Loc are already imported.
1641 // We only have to import To.LocInfo.
1642 switch (To.getName().getNameKind()) {
1643 case DeclarationName::Identifier:
1644 case DeclarationName::ObjCZeroArgSelector:
1645 case DeclarationName::ObjCOneArgSelector:
1646 case DeclarationName::ObjCMultiArgSelector:
1647 case DeclarationName::CXXUsingDirective:
1648 return;
1649
1650 case DeclarationName::CXXOperatorName: {
1651 SourceRange Range = From.getCXXOperatorNameRange();
1652 To.setCXXOperatorNameRange(Importer.Import(Range));
1653 return;
1654 }
1655 case DeclarationName::CXXLiteralOperatorName: {
1656 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1657 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1658 return;
1659 }
1660 case DeclarationName::CXXConstructorName:
1661 case DeclarationName::CXXDestructorName:
1662 case DeclarationName::CXXConversionFunctionName: {
1663 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1664 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1665 return;
1666 }
1667 assert(0 && "Unknown name kind.");
1668 }
1669}
1670
Douglas Gregor968d6332010-02-21 18:24:45 +00001671void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1672 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1673 FromEnd = FromDC->decls_end();
1674 From != FromEnd;
1675 ++From)
1676 Importer.Import(*From);
1677}
1678
Douglas Gregore2e50d332010-12-01 01:36:18 +00001679bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1680 if (To->getDefinition())
1681 return false;
1682
1683 To->startDefinition();
1684
1685 // Add base classes.
1686 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1687 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1688
1689 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1690 for (CXXRecordDecl::base_class_iterator
1691 Base1 = FromCXX->bases_begin(),
1692 FromBaseEnd = FromCXX->bases_end();
1693 Base1 != FromBaseEnd;
1694 ++Base1) {
1695 QualType T = Importer.Import(Base1->getType());
1696 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001697 return true;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001698
1699 Bases.push_back(
1700 new (Importer.getToContext())
1701 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1702 Base1->isVirtual(),
1703 Base1->isBaseOfClass(),
1704 Base1->getAccessSpecifierAsWritten(),
1705 Importer.Import(Base1->getTypeSourceInfo())));
1706 }
1707 if (!Bases.empty())
1708 ToCXX->setBases(Bases.data(), Bases.size());
1709 }
1710
1711 ImportDeclContext(From);
1712 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001713 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001714}
1715
Douglas Gregora082a492010-11-30 19:14:50 +00001716TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1717 TemplateParameterList *Params) {
1718 llvm::SmallVector<NamedDecl *, 4> ToParams;
1719 ToParams.reserve(Params->size());
1720 for (TemplateParameterList::iterator P = Params->begin(),
1721 PEnd = Params->end();
1722 P != PEnd; ++P) {
1723 Decl *To = Importer.Import(*P);
1724 if (!To)
1725 return 0;
1726
1727 ToParams.push_back(cast<NamedDecl>(To));
1728 }
1729
1730 return TemplateParameterList::Create(Importer.getToContext(),
1731 Importer.Import(Params->getTemplateLoc()),
1732 Importer.Import(Params->getLAngleLoc()),
1733 ToParams.data(), ToParams.size(),
1734 Importer.Import(Params->getRAngleLoc()));
1735}
1736
Douglas Gregore2e50d332010-12-01 01:36:18 +00001737TemplateArgument
1738ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1739 switch (From.getKind()) {
1740 case TemplateArgument::Null:
1741 return TemplateArgument();
1742
1743 case TemplateArgument::Type: {
1744 QualType ToType = Importer.Import(From.getAsType());
1745 if (ToType.isNull())
1746 return TemplateArgument();
1747 return TemplateArgument(ToType);
1748 }
1749
1750 case TemplateArgument::Integral: {
1751 QualType ToType = Importer.Import(From.getIntegralType());
1752 if (ToType.isNull())
1753 return TemplateArgument();
1754 return TemplateArgument(*From.getAsIntegral(), ToType);
1755 }
1756
1757 case TemplateArgument::Declaration:
1758 if (Decl *To = Importer.Import(From.getAsDecl()))
1759 return TemplateArgument(To);
1760 return TemplateArgument();
1761
1762 case TemplateArgument::Template: {
1763 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1764 if (ToTemplate.isNull())
1765 return TemplateArgument();
1766
1767 return TemplateArgument(ToTemplate);
1768 }
1769
1770 case TemplateArgument::Expression:
1771 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1772 return TemplateArgument(ToExpr);
1773 return TemplateArgument();
1774
1775 case TemplateArgument::Pack: {
1776 llvm::SmallVector<TemplateArgument, 2> ToPack;
1777 ToPack.reserve(From.pack_size());
1778 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1779 return TemplateArgument();
1780
1781 TemplateArgument *ToArgs
1782 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1783 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1784 return TemplateArgument(ToArgs, ToPack.size());
1785 }
1786 }
1787
1788 llvm_unreachable("Invalid template argument kind");
1789 return TemplateArgument();
1790}
1791
1792bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1793 unsigned NumFromArgs,
1794 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1795 for (unsigned I = 0; I != NumFromArgs; ++I) {
1796 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1797 if (To.isNull() && !FromArgs[I].isNull())
1798 return true;
1799
1800 ToArgs.push_back(To);
1801 }
1802
1803 return false;
1804}
1805
Douglas Gregor5c73e912010-02-11 00:48:18 +00001806bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001807 RecordDecl *ToRecord) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001808 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001809 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001810 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001811 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001812}
1813
Douglas Gregor98c10182010-02-12 22:17:39 +00001814bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001815 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001816 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001817 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001818 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001819}
1820
Douglas Gregora082a492010-11-30 19:14:50 +00001821bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1822 ClassTemplateDecl *To) {
1823 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1824 Importer.getToContext(),
1825 Importer.getNonEquivalentDecls());
1826 return Ctx.IsStructurallyEquivalent(From, To);
1827}
1828
Douglas Gregore4c83e42010-02-09 22:48:33 +00001829Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001830 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001831 << D->getDeclKindName();
1832 return 0;
1833}
1834
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001835Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1836 // Import the major distinguishing characteristics of this namespace.
1837 DeclContext *DC, *LexicalDC;
1838 DeclarationName Name;
1839 SourceLocation Loc;
1840 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1841 return 0;
1842
1843 NamespaceDecl *MergeWithNamespace = 0;
1844 if (!Name) {
1845 // This is an anonymous namespace. Adopt an existing anonymous
1846 // namespace if we can.
1847 // FIXME: Not testable.
1848 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1849 MergeWithNamespace = TU->getAnonymousNamespace();
1850 else
1851 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1852 } else {
1853 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1854 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1855 Lookup.first != Lookup.second;
1856 ++Lookup.first) {
John McCalle87beb22010-04-23 18:46:30 +00001857 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001858 continue;
1859
1860 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1861 MergeWithNamespace = FoundNS;
1862 ConflictingDecls.clear();
1863 break;
1864 }
1865
1866 ConflictingDecls.push_back(*Lookup.first);
1867 }
1868
1869 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001870 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001871 ConflictingDecls.data(),
1872 ConflictingDecls.size());
1873 }
1874 }
1875
1876 // Create the "to" namespace, if needed.
1877 NamespaceDecl *ToNamespace = MergeWithNamespace;
1878 if (!ToNamespace) {
1879 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1880 Name.getAsIdentifierInfo());
1881 ToNamespace->setLexicalDeclContext(LexicalDC);
1882 LexicalDC->addDecl(ToNamespace);
1883
1884 // If this is an anonymous namespace, register it as the anonymous
1885 // namespace within its context.
1886 if (!Name) {
1887 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1888 TU->setAnonymousNamespace(ToNamespace);
1889 else
1890 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1891 }
1892 }
1893 Importer.Imported(D, ToNamespace);
1894
1895 ImportDeclContext(D);
1896
1897 return ToNamespace;
1898}
1899
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001900Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1901 // Import the major distinguishing characteristics of this typedef.
1902 DeclContext *DC, *LexicalDC;
1903 DeclarationName Name;
1904 SourceLocation Loc;
1905 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1906 return 0;
1907
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001908 // If this typedef is not in block scope, determine whether we've
1909 // seen a typedef with the same name (that we can merge with) or any
1910 // other entity by that name (which name lookup could conflict with).
1911 if (!DC->isFunctionOrMethod()) {
1912 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1913 unsigned IDNS = Decl::IDNS_Ordinary;
1914 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1915 Lookup.first != Lookup.second;
1916 ++Lookup.first) {
1917 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1918 continue;
1919 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001920 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1921 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001922 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001923 }
1924
1925 ConflictingDecls.push_back(*Lookup.first);
1926 }
1927
1928 if (!ConflictingDecls.empty()) {
1929 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1930 ConflictingDecls.data(),
1931 ConflictingDecls.size());
1932 if (!Name)
1933 return 0;
1934 }
1935 }
1936
Douglas Gregorb4964f72010-02-15 23:54:17 +00001937 // Import the underlying type of this typedef;
1938 QualType T = Importer.Import(D->getUnderlyingType());
1939 if (T.isNull())
1940 return 0;
1941
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001942 // Create the new typedef node.
1943 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1944 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1945 Loc, Name.getAsIdentifierInfo(),
1946 TInfo);
Douglas Gregordd483172010-02-22 17:42:47 +00001947 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001948 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001949 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001950 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00001951
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001952 return ToTypedef;
1953}
1954
Douglas Gregor98c10182010-02-12 22:17:39 +00001955Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1956 // Import the major distinguishing characteristics of this enum.
1957 DeclContext *DC, *LexicalDC;
1958 DeclarationName Name;
1959 SourceLocation Loc;
1960 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1961 return 0;
1962
1963 // Figure out what enum name we're looking for.
1964 unsigned IDNS = Decl::IDNS_Tag;
1965 DeclarationName SearchName = Name;
1966 if (!SearchName && D->getTypedefForAnonDecl()) {
1967 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1968 IDNS = Decl::IDNS_Ordinary;
1969 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1970 IDNS |= Decl::IDNS_Ordinary;
1971
1972 // We may already have an enum of the same name; try to find and match it.
1973 if (!DC->isFunctionOrMethod() && SearchName) {
1974 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1975 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1976 Lookup.first != Lookup.second;
1977 ++Lookup.first) {
1978 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1979 continue;
1980
1981 Decl *Found = *Lookup.first;
1982 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1983 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1984 Found = Tag->getDecl();
1985 }
1986
1987 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001988 if (IsStructuralMatch(D, FoundEnum))
1989 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001990 }
1991
1992 ConflictingDecls.push_back(*Lookup.first);
1993 }
1994
1995 if (!ConflictingDecls.empty()) {
1996 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1997 ConflictingDecls.data(),
1998 ConflictingDecls.size());
1999 }
2000 }
2001
2002 // Create the enum declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002003 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002004 Name.getAsIdentifierInfo(),
2005 Importer.Import(D->getTagKeywordLoc()), 0,
2006 D->isScoped(), D->isScopedUsingClassTag(),
2007 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002008 // Import the qualifier, if any.
2009 if (D->getQualifier()) {
2010 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2011 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2012 D2->setQualifierInfo(NNS, NNSRange);
2013 }
Douglas Gregordd483172010-02-22 17:42:47 +00002014 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002015 D2->setLexicalDeclContext(LexicalDC);
2016 Importer.Imported(D, D2);
2017 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002018
2019 // Import the integer type.
2020 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2021 if (ToIntegerType.isNull())
2022 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002023 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002024
2025 // Import the definition
2026 if (D->isDefinition()) {
2027 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2028 if (T.isNull())
2029 return 0;
2030
2031 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2032 if (ToPromotionType.isNull())
2033 return 0;
2034
Douglas Gregor3996e242010-02-15 22:01:00 +00002035 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00002036 ImportDeclContext(D);
John McCall9aa35be2010-05-06 08:49:23 +00002037
2038 // FIXME: we might need to merge the number of positive or negative bits
2039 // if the enumerator lists don't match.
2040 D2->completeDefinition(T, ToPromotionType,
2041 D->getNumPositiveBits(),
2042 D->getNumNegativeBits());
Douglas Gregor98c10182010-02-12 22:17:39 +00002043 }
2044
Douglas Gregor3996e242010-02-15 22:01:00 +00002045 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002046}
2047
Douglas Gregor5c73e912010-02-11 00:48:18 +00002048Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2049 // If this record has a definition in the translation unit we're coming from,
2050 // but this particular declaration is not that definition, import the
2051 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002052 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002053 if (Definition && Definition != D) {
2054 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002055 if (!ImportedDef)
2056 return 0;
2057
2058 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002059 }
2060
2061 // Import the major distinguishing characteristics of this record.
2062 DeclContext *DC, *LexicalDC;
2063 DeclarationName Name;
2064 SourceLocation Loc;
2065 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2066 return 0;
2067
2068 // Figure out what structure name we're looking for.
2069 unsigned IDNS = Decl::IDNS_Tag;
2070 DeclarationName SearchName = Name;
2071 if (!SearchName && D->getTypedefForAnonDecl()) {
2072 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2073 IDNS = Decl::IDNS_Ordinary;
2074 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2075 IDNS |= Decl::IDNS_Ordinary;
2076
2077 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002078 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002079 if (!DC->isFunctionOrMethod() && SearchName) {
2080 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2081 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2082 Lookup.first != Lookup.second;
2083 ++Lookup.first) {
2084 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2085 continue;
2086
2087 Decl *Found = *Lookup.first;
2088 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2089 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2090 Found = Tag->getDecl();
2091 }
2092
2093 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002094 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2095 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2096 // The record types structurally match, or the "from" translation
2097 // unit only had a forward declaration anyway; call it the same
2098 // function.
2099 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002100 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002101 }
2102 } else {
2103 // We have a forward declaration of this type, so adopt that forward
2104 // declaration rather than building a new one.
2105 AdoptDecl = FoundRecord;
2106 continue;
2107 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002108 }
2109
2110 ConflictingDecls.push_back(*Lookup.first);
2111 }
2112
2113 if (!ConflictingDecls.empty()) {
2114 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2115 ConflictingDecls.data(),
2116 ConflictingDecls.size());
2117 }
2118 }
2119
2120 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002121 RecordDecl *D2 = AdoptDecl;
2122 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002123 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002124 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002125 D->getTagKind(),
2126 DC, Loc,
2127 Name.getAsIdentifierInfo(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002128 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002129 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002130 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002131 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002132 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00002133 DC, Loc,
2134 Name.getAsIdentifierInfo(),
2135 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00002136 }
John McCall3e11ebe2010-03-15 10:12:16 +00002137 // Import the qualifier, if any.
2138 if (D->getQualifier()) {
2139 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2140 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2141 D2->setQualifierInfo(NNS, NNSRange);
2142 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002143 D2->setLexicalDeclContext(LexicalDC);
2144 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002145 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002146
Douglas Gregor3996e242010-02-15 22:01:00 +00002147 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002148
Douglas Gregore2e50d332010-12-01 01:36:18 +00002149 if (D->isDefinition() && ImportDefinition(D, D2))
2150 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002151
Douglas Gregor3996e242010-02-15 22:01:00 +00002152 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002153}
2154
Douglas Gregor98c10182010-02-12 22:17:39 +00002155Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2156 // Import the major distinguishing characteristics of this enumerator.
2157 DeclContext *DC, *LexicalDC;
2158 DeclarationName Name;
2159 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002160 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002161 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002162
2163 QualType T = Importer.Import(D->getType());
2164 if (T.isNull())
2165 return 0;
2166
Douglas Gregor98c10182010-02-12 22:17:39 +00002167 // Determine whether there are any other declarations with the same name and
2168 // in the same context.
2169 if (!LexicalDC->isFunctionOrMethod()) {
2170 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2171 unsigned IDNS = Decl::IDNS_Ordinary;
2172 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2173 Lookup.first != Lookup.second;
2174 ++Lookup.first) {
2175 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2176 continue;
2177
2178 ConflictingDecls.push_back(*Lookup.first);
2179 }
2180
2181 if (!ConflictingDecls.empty()) {
2182 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2183 ConflictingDecls.data(),
2184 ConflictingDecls.size());
2185 if (!Name)
2186 return 0;
2187 }
2188 }
2189
2190 Expr *Init = Importer.Import(D->getInitExpr());
2191 if (D->getInitExpr() && !Init)
2192 return 0;
2193
2194 EnumConstantDecl *ToEnumerator
2195 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2196 Name.getAsIdentifierInfo(), T,
2197 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002198 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002199 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002200 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002201 LexicalDC->addDecl(ToEnumerator);
2202 return ToEnumerator;
2203}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002204
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002205Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2206 // Import the major distinguishing characteristics of this function.
2207 DeclContext *DC, *LexicalDC;
2208 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002209 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002210 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002211 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002212
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002213 // Try to find a function in our own ("to") context with the same name, same
2214 // type, and in the same context as the function we're importing.
2215 if (!LexicalDC->isFunctionOrMethod()) {
2216 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2217 unsigned IDNS = Decl::IDNS_Ordinary;
2218 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2219 Lookup.first != Lookup.second;
2220 ++Lookup.first) {
2221 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2222 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002223
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002224 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2225 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2226 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002227 if (Importer.IsStructurallyEquivalent(D->getType(),
2228 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002229 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002230 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002231 }
2232
2233 // FIXME: Check for overloading more carefully, e.g., by boosting
2234 // Sema::IsOverload out to the AST library.
2235
2236 // Function overloading is okay in C++.
2237 if (Importer.getToContext().getLangOptions().CPlusPlus)
2238 continue;
2239
2240 // Complain about inconsistent function types.
2241 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002242 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002243 Importer.ToDiag(FoundFunction->getLocation(),
2244 diag::note_odr_value_here)
2245 << FoundFunction->getType();
2246 }
2247 }
2248
2249 ConflictingDecls.push_back(*Lookup.first);
2250 }
2251
2252 if (!ConflictingDecls.empty()) {
2253 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2254 ConflictingDecls.data(),
2255 ConflictingDecls.size());
2256 if (!Name)
2257 return 0;
2258 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002259 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002260
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002261 DeclarationNameInfo NameInfo(Name, Loc);
2262 // Import additional name location/type info.
2263 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2264
Douglas Gregorb4964f72010-02-15 23:54:17 +00002265 // Import the type.
2266 QualType T = Importer.Import(D->getType());
2267 if (T.isNull())
2268 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002269
2270 // Import the function parameters.
2271 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2272 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2273 P != PEnd; ++P) {
2274 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2275 if (!ToP)
2276 return 0;
2277
2278 Parameters.push_back(ToP);
2279 }
2280
2281 // Create the imported function.
2282 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002283 FunctionDecl *ToFunction = 0;
2284 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2285 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2286 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002287 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002288 FromConstructor->isExplicit(),
2289 D->isInlineSpecified(),
2290 D->isImplicit());
2291 } else if (isa<CXXDestructorDecl>(D)) {
2292 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2293 cast<CXXRecordDecl>(DC),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002294 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002295 D->isInlineSpecified(),
2296 D->isImplicit());
2297 } else if (CXXConversionDecl *FromConversion
2298 = dyn_cast<CXXConversionDecl>(D)) {
2299 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2300 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002301 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002302 D->isInlineSpecified(),
2303 FromConversion->isExplicit());
Douglas Gregora50ad132010-11-29 16:04:58 +00002304 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2305 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2306 cast<CXXRecordDecl>(DC),
2307 NameInfo, T, TInfo,
2308 Method->isStatic(),
2309 Method->getStorageClassAsWritten(),
2310 Method->isInlineSpecified());
Douglas Gregor00eace12010-02-21 18:29:16 +00002311 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002312 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2313 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002314 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002315 D->isInlineSpecified(),
2316 D->hasWrittenPrototype());
2317 }
John McCall3e11ebe2010-03-15 10:12:16 +00002318
2319 // Import the qualifier, if any.
2320 if (D->getQualifier()) {
2321 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2322 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2323 ToFunction->setQualifierInfo(NNS, NNSRange);
2324 }
Douglas Gregordd483172010-02-22 17:42:47 +00002325 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002326 ToFunction->setLexicalDeclContext(LexicalDC);
2327 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002328
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002329 // Set the parameters.
2330 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002331 Parameters[I]->setOwningFunction(ToFunction);
2332 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002333 }
Douglas Gregor43f54792010-02-17 02:12:47 +00002334 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002335
2336 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002337
2338 // Add this function to the lexical context.
2339 LexicalDC->addDecl(ToFunction);
2340
Douglas Gregor43f54792010-02-17 02:12:47 +00002341 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002342}
2343
Douglas Gregor00eace12010-02-21 18:29:16 +00002344Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2345 return VisitFunctionDecl(D);
2346}
2347
2348Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2349 return VisitCXXMethodDecl(D);
2350}
2351
2352Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2353 return VisitCXXMethodDecl(D);
2354}
2355
2356Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2357 return VisitCXXMethodDecl(D);
2358}
2359
Douglas Gregor5c73e912010-02-11 00:48:18 +00002360Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2361 // Import the major distinguishing characteristics of a variable.
2362 DeclContext *DC, *LexicalDC;
2363 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002364 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002365 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2366 return 0;
2367
2368 // Import the type.
2369 QualType T = Importer.Import(D->getType());
2370 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002371 return 0;
2372
2373 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2374 Expr *BitWidth = Importer.Import(D->getBitWidth());
2375 if (!BitWidth && D->getBitWidth())
2376 return 0;
2377
2378 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2379 Loc, Name.getAsIdentifierInfo(),
2380 T, TInfo, BitWidth, D->isMutable());
Douglas Gregordd483172010-02-22 17:42:47 +00002381 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002382 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002383 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002384 LexicalDC->addDecl(ToField);
2385 return ToField;
2386}
2387
Francois Pichet783dd6e2010-11-21 06:08:52 +00002388Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2389 // Import the major distinguishing characteristics of a variable.
2390 DeclContext *DC, *LexicalDC;
2391 DeclarationName Name;
2392 SourceLocation Loc;
2393 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2394 return 0;
2395
2396 // Import the type.
2397 QualType T = Importer.Import(D->getType());
2398 if (T.isNull())
2399 return 0;
2400
2401 NamedDecl **NamedChain =
2402 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2403
2404 unsigned i = 0;
2405 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2406 PE = D->chain_end(); PI != PE; ++PI) {
2407 Decl* D = Importer.Import(*PI);
2408 if (!D)
2409 return 0;
2410 NamedChain[i++] = cast<NamedDecl>(D);
2411 }
2412
2413 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2414 Importer.getToContext(), DC,
2415 Loc, Name.getAsIdentifierInfo(), T,
2416 NamedChain, D->getChainingSize());
2417 ToIndirectField->setAccess(D->getAccess());
2418 ToIndirectField->setLexicalDeclContext(LexicalDC);
2419 Importer.Imported(D, ToIndirectField);
2420 LexicalDC->addDecl(ToIndirectField);
2421 return ToIndirectField;
2422}
2423
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002424Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2425 // Import the major distinguishing characteristics of an ivar.
2426 DeclContext *DC, *LexicalDC;
2427 DeclarationName Name;
2428 SourceLocation Loc;
2429 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2430 return 0;
2431
2432 // Determine whether we've already imported this ivar
2433 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2434 Lookup.first != Lookup.second;
2435 ++Lookup.first) {
2436 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2437 if (Importer.IsStructurallyEquivalent(D->getType(),
2438 FoundIvar->getType())) {
2439 Importer.Imported(D, FoundIvar);
2440 return FoundIvar;
2441 }
2442
2443 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2444 << Name << D->getType() << FoundIvar->getType();
2445 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2446 << FoundIvar->getType();
2447 return 0;
2448 }
2449 }
2450
2451 // Import the type.
2452 QualType T = Importer.Import(D->getType());
2453 if (T.isNull())
2454 return 0;
2455
2456 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2457 Expr *BitWidth = Importer.Import(D->getBitWidth());
2458 if (!BitWidth && D->getBitWidth())
2459 return 0;
2460
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002461 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2462 cast<ObjCContainerDecl>(DC),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002463 Loc, Name.getAsIdentifierInfo(),
2464 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002465 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002466 ToIvar->setLexicalDeclContext(LexicalDC);
2467 Importer.Imported(D, ToIvar);
2468 LexicalDC->addDecl(ToIvar);
2469 return ToIvar;
2470
2471}
2472
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002473Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2474 // Import the major distinguishing characteristics of a variable.
2475 DeclContext *DC, *LexicalDC;
2476 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002477 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002478 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002479 return 0;
2480
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002481 // Try to find a variable in our own ("to") context with the same name and
2482 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002483 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002484 VarDecl *MergeWithVar = 0;
2485 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2486 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002487 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002488 Lookup.first != Lookup.second;
2489 ++Lookup.first) {
2490 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2491 continue;
2492
2493 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2494 // We have found a variable that we may need to merge with. Check it.
2495 if (isExternalLinkage(FoundVar->getLinkage()) &&
2496 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002497 if (Importer.IsStructurallyEquivalent(D->getType(),
2498 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002499 MergeWithVar = FoundVar;
2500 break;
2501 }
2502
Douglas Gregor56521c52010-02-12 17:23:39 +00002503 const ArrayType *FoundArray
2504 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2505 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002506 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002507 if (FoundArray && TArray) {
2508 if (isa<IncompleteArrayType>(FoundArray) &&
2509 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002510 // Import the type.
2511 QualType T = Importer.Import(D->getType());
2512 if (T.isNull())
2513 return 0;
2514
Douglas Gregor56521c52010-02-12 17:23:39 +00002515 FoundVar->setType(T);
2516 MergeWithVar = FoundVar;
2517 break;
2518 } else if (isa<IncompleteArrayType>(TArray) &&
2519 isa<ConstantArrayType>(FoundArray)) {
2520 MergeWithVar = FoundVar;
2521 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002522 }
2523 }
2524
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002525 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002526 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002527 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2528 << FoundVar->getType();
2529 }
2530 }
2531
2532 ConflictingDecls.push_back(*Lookup.first);
2533 }
2534
2535 if (MergeWithVar) {
2536 // An equivalent variable with external linkage has been found. Link
2537 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002538 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002539
2540 if (VarDecl *DDef = D->getDefinition()) {
2541 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2542 Importer.ToDiag(ExistingDef->getLocation(),
2543 diag::err_odr_variable_multiple_def)
2544 << Name;
2545 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2546 } else {
2547 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002548 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002549 }
2550 }
2551
2552 return MergeWithVar;
2553 }
2554
2555 if (!ConflictingDecls.empty()) {
2556 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2557 ConflictingDecls.data(),
2558 ConflictingDecls.size());
2559 if (!Name)
2560 return 0;
2561 }
2562 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002563
Douglas Gregorb4964f72010-02-15 23:54:17 +00002564 // Import the type.
2565 QualType T = Importer.Import(D->getType());
2566 if (T.isNull())
2567 return 0;
2568
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002569 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002570 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002571 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2572 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002573 D->getStorageClass(),
2574 D->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002575 // Import the qualifier, if any.
2576 if (D->getQualifier()) {
2577 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2578 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2579 ToVar->setQualifierInfo(NNS, NNSRange);
2580 }
Douglas Gregordd483172010-02-22 17:42:47 +00002581 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002582 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002583 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002584 LexicalDC->addDecl(ToVar);
2585
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002586 // Merge the initializer.
2587 // FIXME: Can we really import any initializer? Alternatively, we could force
2588 // ourselves to import every declaration of a variable and then only use
2589 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002590 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002591
2592 // FIXME: Other bits to merge?
2593
2594 return ToVar;
2595}
2596
Douglas Gregor8b228d72010-02-17 21:22:52 +00002597Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2598 // Parameters are created in the translation unit's context, then moved
2599 // into the function declaration's context afterward.
2600 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2601
2602 // Import the name of this declaration.
2603 DeclarationName Name = Importer.Import(D->getDeclName());
2604 if (D->getDeclName() && !Name)
2605 return 0;
2606
2607 // Import the location of this declaration.
2608 SourceLocation Loc = Importer.Import(D->getLocation());
2609
2610 // Import the parameter's type.
2611 QualType T = Importer.Import(D->getType());
2612 if (T.isNull())
2613 return 0;
2614
2615 // Create the imported parameter.
2616 ImplicitParamDecl *ToParm
2617 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2618 Loc, Name.getAsIdentifierInfo(),
2619 T);
2620 return Importer.Imported(D, ToParm);
2621}
2622
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002623Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2624 // Parameters are created in the translation unit's context, then moved
2625 // into the function declaration's context afterward.
2626 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2627
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002628 // Import the name of this declaration.
2629 DeclarationName Name = Importer.Import(D->getDeclName());
2630 if (D->getDeclName() && !Name)
2631 return 0;
2632
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002633 // Import the location of this declaration.
2634 SourceLocation Loc = Importer.Import(D->getLocation());
2635
2636 // Import the parameter's type.
2637 QualType T = Importer.Import(D->getType());
2638 if (T.isNull())
2639 return 0;
2640
2641 // Create the imported parameter.
2642 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2643 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2644 Loc, Name.getAsIdentifierInfo(),
2645 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002646 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002647 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002648 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002649 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002650}
2651
Douglas Gregor43f54792010-02-17 02:12:47 +00002652Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2653 // Import the major distinguishing characteristics of a method.
2654 DeclContext *DC, *LexicalDC;
2655 DeclarationName Name;
2656 SourceLocation Loc;
2657 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2658 return 0;
2659
2660 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2661 Lookup.first != Lookup.second;
2662 ++Lookup.first) {
2663 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2664 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2665 continue;
2666
2667 // Check return types.
2668 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2669 FoundMethod->getResultType())) {
2670 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2671 << D->isInstanceMethod() << Name
2672 << D->getResultType() << FoundMethod->getResultType();
2673 Importer.ToDiag(FoundMethod->getLocation(),
2674 diag::note_odr_objc_method_here)
2675 << D->isInstanceMethod() << Name;
2676 return 0;
2677 }
2678
2679 // Check the number of parameters.
2680 if (D->param_size() != FoundMethod->param_size()) {
2681 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2682 << D->isInstanceMethod() << Name
2683 << D->param_size() << FoundMethod->param_size();
2684 Importer.ToDiag(FoundMethod->getLocation(),
2685 diag::note_odr_objc_method_here)
2686 << D->isInstanceMethod() << Name;
2687 return 0;
2688 }
2689
2690 // Check parameter types.
2691 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2692 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2693 P != PEnd; ++P, ++FoundP) {
2694 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2695 (*FoundP)->getType())) {
2696 Importer.FromDiag((*P)->getLocation(),
2697 diag::err_odr_objc_method_param_type_inconsistent)
2698 << D->isInstanceMethod() << Name
2699 << (*P)->getType() << (*FoundP)->getType();
2700 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2701 << (*FoundP)->getType();
2702 return 0;
2703 }
2704 }
2705
2706 // Check variadic/non-variadic.
2707 // Check the number of parameters.
2708 if (D->isVariadic() != FoundMethod->isVariadic()) {
2709 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2710 << D->isInstanceMethod() << Name;
2711 Importer.ToDiag(FoundMethod->getLocation(),
2712 diag::note_odr_objc_method_here)
2713 << D->isInstanceMethod() << Name;
2714 return 0;
2715 }
2716
2717 // FIXME: Any other bits we need to merge?
2718 return Importer.Imported(D, FoundMethod);
2719 }
2720 }
2721
2722 // Import the result type.
2723 QualType ResultTy = Importer.Import(D->getResultType());
2724 if (ResultTy.isNull())
2725 return 0;
2726
Douglas Gregor12852d92010-03-08 14:59:44 +00002727 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2728
Douglas Gregor43f54792010-02-17 02:12:47 +00002729 ObjCMethodDecl *ToMethod
2730 = ObjCMethodDecl::Create(Importer.getToContext(),
2731 Loc,
2732 Importer.Import(D->getLocEnd()),
2733 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00002734 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00002735 D->isInstanceMethod(),
2736 D->isVariadic(),
2737 D->isSynthesized(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002738 D->isDefined(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002739 D->getImplementationControl());
2740
2741 // FIXME: When we decide to merge method definitions, we'll need to
2742 // deal with implicit parameters.
2743
2744 // Import the parameters
2745 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2746 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2747 FromPEnd = D->param_end();
2748 FromP != FromPEnd;
2749 ++FromP) {
2750 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2751 if (!ToP)
2752 return 0;
2753
2754 ToParams.push_back(ToP);
2755 }
2756
2757 // Set the parameters.
2758 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2759 ToParams[I]->setOwningFunction(ToMethod);
2760 ToMethod->addDecl(ToParams[I]);
2761 }
2762 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00002763 ToParams.data(), ToParams.size(),
2764 ToParams.size());
Douglas Gregor43f54792010-02-17 02:12:47 +00002765
2766 ToMethod->setLexicalDeclContext(LexicalDC);
2767 Importer.Imported(D, ToMethod);
2768 LexicalDC->addDecl(ToMethod);
2769 return ToMethod;
2770}
2771
Douglas Gregor84c51c32010-02-18 01:47:50 +00002772Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2773 // Import the major distinguishing characteristics of a category.
2774 DeclContext *DC, *LexicalDC;
2775 DeclarationName Name;
2776 SourceLocation Loc;
2777 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2778 return 0;
2779
2780 ObjCInterfaceDecl *ToInterface
2781 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2782 if (!ToInterface)
2783 return 0;
2784
2785 // Determine if we've already encountered this category.
2786 ObjCCategoryDecl *MergeWithCategory
2787 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2788 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2789 if (!ToCategory) {
2790 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2791 Importer.Import(D->getAtLoc()),
2792 Loc,
2793 Importer.Import(D->getCategoryNameLoc()),
2794 Name.getAsIdentifierInfo());
2795 ToCategory->setLexicalDeclContext(LexicalDC);
2796 LexicalDC->addDecl(ToCategory);
2797 Importer.Imported(D, ToCategory);
2798
2799 // Link this category into its class's category list.
2800 ToCategory->setClassInterface(ToInterface);
2801 ToCategory->insertNextClassCategory();
2802
2803 // Import protocols
2804 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2805 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2806 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2807 = D->protocol_loc_begin();
2808 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2809 FromProtoEnd = D->protocol_end();
2810 FromProto != FromProtoEnd;
2811 ++FromProto, ++FromProtoLoc) {
2812 ObjCProtocolDecl *ToProto
2813 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2814 if (!ToProto)
2815 return 0;
2816 Protocols.push_back(ToProto);
2817 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2818 }
2819
2820 // FIXME: If we're merging, make sure that the protocol list is the same.
2821 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2822 ProtocolLocs.data(), Importer.getToContext());
2823
2824 } else {
2825 Importer.Imported(D, ToCategory);
2826 }
2827
2828 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002829 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002830
2831 // If we have an implementation, import it as well.
2832 if (D->getImplementation()) {
2833 ObjCCategoryImplDecl *Impl
2834 = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2835 if (!Impl)
2836 return 0;
2837
2838 ToCategory->setImplementation(Impl);
2839 }
2840
2841 return ToCategory;
2842}
2843
Douglas Gregor98d156a2010-02-17 16:12:00 +00002844Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002845 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002846 DeclContext *DC, *LexicalDC;
2847 DeclarationName Name;
2848 SourceLocation Loc;
2849 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2850 return 0;
2851
2852 ObjCProtocolDecl *MergeWithProtocol = 0;
2853 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2854 Lookup.first != Lookup.second;
2855 ++Lookup.first) {
2856 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2857 continue;
2858
2859 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2860 break;
2861 }
2862
2863 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2864 if (!ToProto || ToProto->isForwardDecl()) {
2865 if (!ToProto) {
2866 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2867 Name.getAsIdentifierInfo());
2868 ToProto->setForwardDecl(D->isForwardDecl());
2869 ToProto->setLexicalDeclContext(LexicalDC);
2870 LexicalDC->addDecl(ToProto);
2871 }
2872 Importer.Imported(D, ToProto);
2873
2874 // Import protocols
2875 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2876 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2877 ObjCProtocolDecl::protocol_loc_iterator
2878 FromProtoLoc = D->protocol_loc_begin();
2879 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2880 FromProtoEnd = D->protocol_end();
2881 FromProto != FromProtoEnd;
2882 ++FromProto, ++FromProtoLoc) {
2883 ObjCProtocolDecl *ToProto
2884 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2885 if (!ToProto)
2886 return 0;
2887 Protocols.push_back(ToProto);
2888 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2889 }
2890
2891 // FIXME: If we're merging, make sure that the protocol list is the same.
2892 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2893 ProtocolLocs.data(), Importer.getToContext());
2894 } else {
2895 Importer.Imported(D, ToProto);
2896 }
2897
Douglas Gregor84c51c32010-02-18 01:47:50 +00002898 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00002899 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002900
2901 return ToProto;
2902}
2903
Douglas Gregor45635322010-02-16 01:20:57 +00002904Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2905 // Import the major distinguishing characteristics of an @interface.
2906 DeclContext *DC, *LexicalDC;
2907 DeclarationName Name;
2908 SourceLocation Loc;
2909 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2910 return 0;
2911
2912 ObjCInterfaceDecl *MergeWithIface = 0;
2913 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2914 Lookup.first != Lookup.second;
2915 ++Lookup.first) {
2916 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2917 continue;
2918
2919 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2920 break;
2921 }
2922
2923 ObjCInterfaceDecl *ToIface = MergeWithIface;
2924 if (!ToIface || ToIface->isForwardDecl()) {
2925 if (!ToIface) {
2926 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2927 DC, Loc,
2928 Name.getAsIdentifierInfo(),
Douglas Gregor1c283312010-08-11 12:19:30 +00002929 Importer.Import(D->getClassLoc()),
Douglas Gregor45635322010-02-16 01:20:57 +00002930 D->isForwardDecl(),
2931 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00002932 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00002933 ToIface->setLexicalDeclContext(LexicalDC);
2934 LexicalDC->addDecl(ToIface);
2935 }
2936 Importer.Imported(D, ToIface);
2937
Douglas Gregor45635322010-02-16 01:20:57 +00002938 if (D->getSuperClass()) {
2939 ObjCInterfaceDecl *Super
2940 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2941 if (!Super)
2942 return 0;
2943
2944 ToIface->setSuperClass(Super);
2945 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2946 }
2947
2948 // Import protocols
2949 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2950 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2951 ObjCInterfaceDecl::protocol_loc_iterator
2952 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002953
2954 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregor45635322010-02-16 01:20:57 +00002955 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2956 FromProtoEnd = D->protocol_end();
2957 FromProto != FromProtoEnd;
2958 ++FromProto, ++FromProtoLoc) {
2959 ObjCProtocolDecl *ToProto
2960 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2961 if (!ToProto)
2962 return 0;
2963 Protocols.push_back(ToProto);
2964 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2965 }
2966
2967 // FIXME: If we're merging, make sure that the protocol list is the same.
2968 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2969 ProtocolLocs.data(), Importer.getToContext());
2970
Douglas Gregor45635322010-02-16 01:20:57 +00002971 // Import @end range
2972 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2973 } else {
2974 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002975
2976 // Check for consistency of superclasses.
2977 DeclarationName FromSuperName, ToSuperName;
2978 if (D->getSuperClass())
2979 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2980 if (ToIface->getSuperClass())
2981 ToSuperName = ToIface->getSuperClass()->getDeclName();
2982 if (FromSuperName != ToSuperName) {
2983 Importer.ToDiag(ToIface->getLocation(),
2984 diag::err_odr_objc_superclass_inconsistent)
2985 << ToIface->getDeclName();
2986 if (ToIface->getSuperClass())
2987 Importer.ToDiag(ToIface->getSuperClassLoc(),
2988 diag::note_odr_objc_superclass)
2989 << ToIface->getSuperClass()->getDeclName();
2990 else
2991 Importer.ToDiag(ToIface->getLocation(),
2992 diag::note_odr_objc_missing_superclass);
2993 if (D->getSuperClass())
2994 Importer.FromDiag(D->getSuperClassLoc(),
2995 diag::note_odr_objc_superclass)
2996 << D->getSuperClass()->getDeclName();
2997 else
2998 Importer.FromDiag(D->getLocation(),
2999 diag::note_odr_objc_missing_superclass);
3000 return 0;
3001 }
Douglas Gregor45635322010-02-16 01:20:57 +00003002 }
3003
Douglas Gregor84c51c32010-02-18 01:47:50 +00003004 // Import categories. When the categories themselves are imported, they'll
3005 // hook themselves into this interface.
3006 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3007 FromCat = FromCat->getNextClassCategory())
3008 Importer.Import(FromCat);
3009
Douglas Gregor45635322010-02-16 01:20:57 +00003010 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00003011 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00003012
3013 // If we have an @implementation, import it as well.
3014 if (D->getImplementation()) {
3015 ObjCImplementationDecl *Impl
3016 = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
3017 if (!Impl)
3018 return 0;
3019
3020 ToIface->setImplementation(Impl);
3021 }
3022
Douglas Gregor98d156a2010-02-17 16:12:00 +00003023 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003024}
3025
Douglas Gregora11c4582010-02-17 18:02:10 +00003026Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3027 // Import the major distinguishing characteristics of an @property.
3028 DeclContext *DC, *LexicalDC;
3029 DeclarationName Name;
3030 SourceLocation Loc;
3031 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3032 return 0;
3033
3034 // Check whether we have already imported this property.
3035 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3036 Lookup.first != Lookup.second;
3037 ++Lookup.first) {
3038 if (ObjCPropertyDecl *FoundProp
3039 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3040 // Check property types.
3041 if (!Importer.IsStructurallyEquivalent(D->getType(),
3042 FoundProp->getType())) {
3043 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3044 << Name << D->getType() << FoundProp->getType();
3045 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3046 << FoundProp->getType();
3047 return 0;
3048 }
3049
3050 // FIXME: Check property attributes, getters, setters, etc.?
3051
3052 // Consider these properties to be equivalent.
3053 Importer.Imported(D, FoundProp);
3054 return FoundProp;
3055 }
3056 }
3057
3058 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003059 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3060 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003061 return 0;
3062
3063 // Create the new property.
3064 ObjCPropertyDecl *ToProperty
3065 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3066 Name.getAsIdentifierInfo(),
3067 Importer.Import(D->getAtLoc()),
3068 T,
3069 D->getPropertyImplementation());
3070 Importer.Imported(D, ToProperty);
3071 ToProperty->setLexicalDeclContext(LexicalDC);
3072 LexicalDC->addDecl(ToProperty);
3073
3074 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003075 ToProperty->setPropertyAttributesAsWritten(
3076 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003077 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3078 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3079 ToProperty->setGetterMethodDecl(
3080 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3081 ToProperty->setSetterMethodDecl(
3082 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3083 ToProperty->setPropertyIvarDecl(
3084 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3085 return ToProperty;
3086}
3087
Douglas Gregor8661a722010-02-18 02:12:22 +00003088Decl *
3089ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3090 // Import the context of this declaration.
3091 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3092 if (!DC)
3093 return 0;
3094
3095 DeclContext *LexicalDC = DC;
3096 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3097 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3098 if (!LexicalDC)
3099 return 0;
3100 }
3101
3102 // Import the location of this declaration.
3103 SourceLocation Loc = Importer.Import(D->getLocation());
3104
3105 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3106 llvm::SmallVector<SourceLocation, 4> Locations;
3107 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3108 = D->protocol_loc_begin();
3109 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3110 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3111 FromProto != FromProtoEnd;
3112 ++FromProto, ++FromProtoLoc) {
3113 ObjCProtocolDecl *ToProto
3114 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3115 if (!ToProto)
3116 continue;
3117
3118 Protocols.push_back(ToProto);
3119 Locations.push_back(Importer.Import(*FromProtoLoc));
3120 }
3121
3122 ObjCForwardProtocolDecl *ToForward
3123 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3124 Protocols.data(), Protocols.size(),
3125 Locations.data());
3126 ToForward->setLexicalDeclContext(LexicalDC);
3127 LexicalDC->addDecl(ToForward);
3128 Importer.Imported(D, ToForward);
3129 return ToForward;
3130}
3131
Douglas Gregor06537af2010-02-18 02:04:09 +00003132Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3133 // Import the context of this declaration.
3134 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3135 if (!DC)
3136 return 0;
3137
3138 DeclContext *LexicalDC = DC;
3139 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3140 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3141 if (!LexicalDC)
3142 return 0;
3143 }
3144
3145 // Import the location of this declaration.
3146 SourceLocation Loc = Importer.Import(D->getLocation());
3147
3148 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3149 llvm::SmallVector<SourceLocation, 4> Locations;
3150 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3151 From != FromEnd; ++From) {
3152 ObjCInterfaceDecl *ToIface
3153 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3154 if (!ToIface)
3155 continue;
3156
3157 Interfaces.push_back(ToIface);
3158 Locations.push_back(Importer.Import(From->getLocation()));
3159 }
3160
3161 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3162 Loc,
3163 Interfaces.data(),
3164 Locations.data(),
3165 Interfaces.size());
3166 ToClass->setLexicalDeclContext(LexicalDC);
3167 LexicalDC->addDecl(ToClass);
3168 Importer.Imported(D, ToClass);
3169 return ToClass;
3170}
3171
Douglas Gregora082a492010-11-30 19:14:50 +00003172Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3173 // For template arguments, we adopt the translation unit as our declaration
3174 // context. This context will be fixed when the actual template declaration
3175 // is created.
3176
3177 // FIXME: Import default argument.
3178 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3179 Importer.getToContext().getTranslationUnitDecl(),
3180 Importer.Import(D->getLocation()),
3181 D->getDepth(),
3182 D->getIndex(),
3183 Importer.Import(D->getIdentifier()),
3184 D->wasDeclaredWithTypename(),
3185 D->isParameterPack());
3186}
3187
3188Decl *
3189ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3190 // Import the name of this declaration.
3191 DeclarationName Name = Importer.Import(D->getDeclName());
3192 if (D->getDeclName() && !Name)
3193 return 0;
3194
3195 // Import the location of this declaration.
3196 SourceLocation Loc = Importer.Import(D->getLocation());
3197
3198 // Import the type of this declaration.
3199 QualType T = Importer.Import(D->getType());
3200 if (T.isNull())
3201 return 0;
3202
3203 // Import type-source information.
3204 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3205 if (D->getTypeSourceInfo() && !TInfo)
3206 return 0;
3207
3208 // FIXME: Import default argument.
3209
3210 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3211 Importer.getToContext().getTranslationUnitDecl(),
3212 Loc, D->getDepth(), D->getPosition(),
3213 Name.getAsIdentifierInfo(),
3214 T, TInfo);
3215}
3216
3217Decl *
3218ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3219 // Import the name of this declaration.
3220 DeclarationName Name = Importer.Import(D->getDeclName());
3221 if (D->getDeclName() && !Name)
3222 return 0;
3223
3224 // Import the location of this declaration.
3225 SourceLocation Loc = Importer.Import(D->getLocation());
3226
3227 // Import template parameters.
3228 TemplateParameterList *TemplateParams
3229 = ImportTemplateParameterList(D->getTemplateParameters());
3230 if (!TemplateParams)
3231 return 0;
3232
3233 // FIXME: Import default argument.
3234
3235 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3236 Importer.getToContext().getTranslationUnitDecl(),
3237 Loc, D->getDepth(), D->getPosition(),
3238 Name.getAsIdentifierInfo(),
3239 TemplateParams);
3240}
3241
3242Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3243 // If this record has a definition in the translation unit we're coming from,
3244 // but this particular declaration is not that definition, import the
3245 // definition and map to that.
3246 CXXRecordDecl *Definition
3247 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3248 if (Definition && Definition != D->getTemplatedDecl()) {
3249 Decl *ImportedDef
3250 = Importer.Import(Definition->getDescribedClassTemplate());
3251 if (!ImportedDef)
3252 return 0;
3253
3254 return Importer.Imported(D, ImportedDef);
3255 }
3256
3257 // Import the major distinguishing characteristics of this class template.
3258 DeclContext *DC, *LexicalDC;
3259 DeclarationName Name;
3260 SourceLocation Loc;
3261 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3262 return 0;
3263
3264 // We may already have a template of the same name; try to find and match it.
3265 if (!DC->isFunctionOrMethod()) {
3266 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3267 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3268 Lookup.first != Lookup.second;
3269 ++Lookup.first) {
3270 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3271 continue;
3272
3273 Decl *Found = *Lookup.first;
3274 if (ClassTemplateDecl *FoundTemplate
3275 = dyn_cast<ClassTemplateDecl>(Found)) {
3276 if (IsStructuralMatch(D, FoundTemplate)) {
3277 // The class templates structurally match; call it the same template.
3278 // FIXME: We may be filling in a forward declaration here. Handle
3279 // this case!
3280 Importer.Imported(D->getTemplatedDecl(),
3281 FoundTemplate->getTemplatedDecl());
3282 return Importer.Imported(D, FoundTemplate);
3283 }
3284 }
3285
3286 ConflictingDecls.push_back(*Lookup.first);
3287 }
3288
3289 if (!ConflictingDecls.empty()) {
3290 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3291 ConflictingDecls.data(),
3292 ConflictingDecls.size());
3293 }
3294
3295 if (!Name)
3296 return 0;
3297 }
3298
3299 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3300
3301 // Create the declaration that is being templated.
3302 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3303 DTemplated->getTagKind(),
3304 DC,
3305 Importer.Import(DTemplated->getLocation()),
3306 Name.getAsIdentifierInfo(),
3307 Importer.Import(DTemplated->getTagKeywordLoc()));
3308 D2Templated->setAccess(DTemplated->getAccess());
3309
3310
3311 // Import the qualifier, if any.
3312 if (DTemplated->getQualifier()) {
3313 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3314 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3315 D2Templated->setQualifierInfo(NNS, NNSRange);
3316 }
3317 D2Templated->setLexicalDeclContext(LexicalDC);
3318
3319 // Create the class template declaration itself.
3320 TemplateParameterList *TemplateParams
3321 = ImportTemplateParameterList(D->getTemplateParameters());
3322 if (!TemplateParams)
3323 return 0;
3324
3325 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3326 Loc, Name, TemplateParams,
3327 D2Templated,
3328 /*PrevDecl=*/0);
3329 D2Templated->setDescribedClassTemplate(D2);
3330
3331 D2->setAccess(D->getAccess());
3332 D2->setLexicalDeclContext(LexicalDC);
3333 LexicalDC->addDecl(D2);
3334
3335 // Note the relationship between the class templates.
3336 Importer.Imported(D, D2);
3337 Importer.Imported(DTemplated, D2Templated);
3338
3339 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3340 // FIXME: Import definition!
3341 }
3342
3343 return D2;
3344}
3345
Douglas Gregore2e50d332010-12-01 01:36:18 +00003346Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3347 ClassTemplateSpecializationDecl *D) {
3348 // If this record has a definition in the translation unit we're coming from,
3349 // but this particular declaration is not that definition, import the
3350 // definition and map to that.
3351 TagDecl *Definition = D->getDefinition();
3352 if (Definition && Definition != D) {
3353 Decl *ImportedDef = Importer.Import(Definition);
3354 if (!ImportedDef)
3355 return 0;
3356
3357 return Importer.Imported(D, ImportedDef);
3358 }
3359
3360 ClassTemplateDecl *ClassTemplate
3361 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3362 D->getSpecializedTemplate()));
3363 if (!ClassTemplate)
3364 return 0;
3365
3366 // Import the context of this declaration.
3367 DeclContext *DC = ClassTemplate->getDeclContext();
3368 if (!DC)
3369 return 0;
3370
3371 DeclContext *LexicalDC = DC;
3372 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3373 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3374 if (!LexicalDC)
3375 return 0;
3376 }
3377
3378 // Import the location of this declaration.
3379 SourceLocation Loc = Importer.Import(D->getLocation());
3380
3381 // Import template arguments.
3382 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3383 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3384 D->getTemplateArgs().size(),
3385 TemplateArgs))
3386 return 0;
3387
3388 // Try to find an existing specialization with these template arguments.
3389 void *InsertPos = 0;
3390 ClassTemplateSpecializationDecl *D2
3391 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3392 TemplateArgs.size(), InsertPos);
3393 if (D2) {
3394 // We already have a class template specialization with these template
3395 // arguments.
3396
3397 // FIXME: Check for specialization vs. instantiation errors.
3398
3399 if (RecordDecl *FoundDef = D2->getDefinition()) {
3400 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3401 // The record types structurally match, or the "from" translation
3402 // unit only had a forward declaration anyway; call it the same
3403 // function.
3404 return Importer.Imported(D, FoundDef);
3405 }
3406 }
3407 } else {
3408 // Create a new specialization.
3409 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3410 D->getTagKind(), DC,
3411 Loc, ClassTemplate,
3412 TemplateArgs.data(),
3413 TemplateArgs.size(),
3414 /*PrevDecl=*/0);
3415 D2->setSpecializationKind(D->getSpecializationKind());
3416
3417 // Add this specialization to the class template.
3418 ClassTemplate->AddSpecialization(D2, InsertPos);
3419
3420 // Import the qualifier, if any.
3421 if (D->getQualifier()) {
3422 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3423 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3424 D2->setQualifierInfo(NNS, NNSRange);
3425 }
3426
3427
3428 // Add the specialization to this context.
3429 D2->setLexicalDeclContext(LexicalDC);
3430 LexicalDC->addDecl(D2);
3431 }
3432 Importer.Imported(D, D2);
3433
3434 if (D->isDefinition() && ImportDefinition(D, D2))
3435 return 0;
3436
3437 return D2;
3438}
3439
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003440//----------------------------------------------------------------------------
3441// Import Statements
3442//----------------------------------------------------------------------------
3443
3444Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3445 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3446 << S->getStmtClassName();
3447 return 0;
3448}
3449
3450//----------------------------------------------------------------------------
3451// Import Expressions
3452//----------------------------------------------------------------------------
3453Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3454 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3455 << E->getStmtClassName();
3456 return 0;
3457}
3458
Douglas Gregor52f820e2010-02-19 01:17:02 +00003459Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3460 NestedNameSpecifier *Qualifier = 0;
3461 if (E->getQualifier()) {
3462 Qualifier = Importer.Import(E->getQualifier());
3463 if (!E->getQualifier())
3464 return 0;
3465 }
3466
3467 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3468 if (!ToD)
3469 return 0;
3470
3471 QualType T = Importer.Import(E->getType());
3472 if (T.isNull())
3473 return 0;
3474
3475 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3476 Importer.Import(E->getQualifierRange()),
3477 ToD,
3478 Importer.Import(E->getLocation()),
John McCall7decc9e2010-11-18 06:31:45 +00003479 T, E->getValueKind(),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003480 /*FIXME:TemplateArgs=*/0);
3481}
3482
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003483Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3484 QualType T = Importer.Import(E->getType());
3485 if (T.isNull())
3486 return 0;
3487
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003488 return IntegerLiteral::Create(Importer.getToContext(),
3489 E->getValue(), T,
3490 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003491}
3492
Douglas Gregor623421d2010-02-18 02:21:22 +00003493Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3494 QualType T = Importer.Import(E->getType());
3495 if (T.isNull())
3496 return 0;
3497
3498 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3499 E->isWide(), T,
3500 Importer.Import(E->getLocation()));
3501}
3502
Douglas Gregorc74247e2010-02-19 01:07:06 +00003503Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3504 Expr *SubExpr = Importer.Import(E->getSubExpr());
3505 if (!SubExpr)
3506 return 0;
3507
3508 return new (Importer.getToContext())
3509 ParenExpr(Importer.Import(E->getLParen()),
3510 Importer.Import(E->getRParen()),
3511 SubExpr);
3512}
3513
3514Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3515 QualType T = Importer.Import(E->getType());
3516 if (T.isNull())
3517 return 0;
3518
3519 Expr *SubExpr = Importer.Import(E->getSubExpr());
3520 if (!SubExpr)
3521 return 0;
3522
3523 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003524 T, E->getValueKind(),
3525 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003526 Importer.Import(E->getOperatorLoc()));
3527}
3528
Douglas Gregord8552cd2010-02-19 01:24:23 +00003529Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3530 QualType ResultType = Importer.Import(E->getType());
3531
3532 if (E->isArgumentType()) {
3533 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3534 if (!TInfo)
3535 return 0;
3536
3537 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3538 TInfo, ResultType,
3539 Importer.Import(E->getOperatorLoc()),
3540 Importer.Import(E->getRParenLoc()));
3541 }
3542
3543 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3544 if (!SubExpr)
3545 return 0;
3546
3547 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3548 SubExpr, ResultType,
3549 Importer.Import(E->getOperatorLoc()),
3550 Importer.Import(E->getRParenLoc()));
3551}
3552
Douglas Gregorc74247e2010-02-19 01:07:06 +00003553Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3554 QualType T = Importer.Import(E->getType());
3555 if (T.isNull())
3556 return 0;
3557
3558 Expr *LHS = Importer.Import(E->getLHS());
3559 if (!LHS)
3560 return 0;
3561
3562 Expr *RHS = Importer.Import(E->getRHS());
3563 if (!RHS)
3564 return 0;
3565
3566 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003567 T, E->getValueKind(),
3568 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003569 Importer.Import(E->getOperatorLoc()));
3570}
3571
3572Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3573 QualType T = Importer.Import(E->getType());
3574 if (T.isNull())
3575 return 0;
3576
3577 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3578 if (CompLHSType.isNull())
3579 return 0;
3580
3581 QualType CompResultType = Importer.Import(E->getComputationResultType());
3582 if (CompResultType.isNull())
3583 return 0;
3584
3585 Expr *LHS = Importer.Import(E->getLHS());
3586 if (!LHS)
3587 return 0;
3588
3589 Expr *RHS = Importer.Import(E->getRHS());
3590 if (!RHS)
3591 return 0;
3592
3593 return new (Importer.getToContext())
3594 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003595 T, E->getValueKind(),
3596 E->getObjectKind(),
3597 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00003598 Importer.Import(E->getOperatorLoc()));
3599}
3600
John McCallcf142162010-08-07 06:22:56 +00003601bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3602 if (E->path_empty()) return false;
3603
3604 // TODO: import cast paths
3605 return true;
3606}
3607
Douglas Gregor98c10182010-02-12 22:17:39 +00003608Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3609 QualType T = Importer.Import(E->getType());
3610 if (T.isNull())
3611 return 0;
3612
3613 Expr *SubExpr = Importer.Import(E->getSubExpr());
3614 if (!SubExpr)
3615 return 0;
John McCallcf142162010-08-07 06:22:56 +00003616
3617 CXXCastPath BasePath;
3618 if (ImportCastPath(E, BasePath))
3619 return 0;
3620
3621 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00003622 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00003623}
3624
Douglas Gregor5481d322010-02-19 01:32:14 +00003625Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3626 QualType T = Importer.Import(E->getType());
3627 if (T.isNull())
3628 return 0;
3629
3630 Expr *SubExpr = Importer.Import(E->getSubExpr());
3631 if (!SubExpr)
3632 return 0;
3633
3634 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3635 if (!TInfo && E->getTypeInfoAsWritten())
3636 return 0;
3637
John McCallcf142162010-08-07 06:22:56 +00003638 CXXCastPath BasePath;
3639 if (ImportCastPath(E, BasePath))
3640 return 0;
3641
John McCall7decc9e2010-11-18 06:31:45 +00003642 return CStyleCastExpr::Create(Importer.getToContext(), T,
3643 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00003644 SubExpr, &BasePath, TInfo,
3645 Importer.Import(E->getLParenLoc()),
3646 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00003647}
3648
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003649ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Chris Lattner5159f612010-11-23 08:35:12 +00003650 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor96e578d2010-02-05 17:54:41 +00003651 : ToContext(ToContext), FromContext(FromContext),
Chris Lattner5159f612010-11-23 08:35:12 +00003652 ToFileManager(ToFileManager), FromFileManager(FromFileManager) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00003653 ImportedDecls[FromContext.getTranslationUnitDecl()]
3654 = ToContext.getTranslationUnitDecl();
3655}
3656
3657ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00003658
3659QualType ASTImporter::Import(QualType FromT) {
3660 if (FromT.isNull())
3661 return QualType();
3662
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003663 // Check whether we've already imported this type.
3664 llvm::DenseMap<Type *, Type *>::iterator Pos
3665 = ImportedTypes.find(FromT.getTypePtr());
3666 if (Pos != ImportedTypes.end())
3667 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003668
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003669 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00003670 ASTNodeImporter Importer(*this);
3671 QualType ToT = Importer.Visit(FromT.getTypePtr());
3672 if (ToT.isNull())
3673 return ToT;
3674
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003675 // Record the imported type.
3676 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3677
Douglas Gregor96e578d2010-02-05 17:54:41 +00003678 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
3679}
3680
Douglas Gregor62d311f2010-02-09 19:21:46 +00003681TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003682 if (!FromTSI)
3683 return FromTSI;
3684
3685 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00003686 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003687 QualType T = Import(FromTSI->getType());
3688 if (T.isNull())
3689 return 0;
3690
3691 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnara1108e7b2010-05-20 10:00:11 +00003692 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003693}
3694
3695Decl *ASTImporter::Import(Decl *FromD) {
3696 if (!FromD)
3697 return 0;
3698
3699 // Check whether we've already imported this declaration.
3700 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3701 if (Pos != ImportedDecls.end())
3702 return Pos->second;
3703
3704 // Import the type
3705 ASTNodeImporter Importer(*this);
3706 Decl *ToD = Importer.Visit(FromD);
3707 if (!ToD)
3708 return 0;
3709
3710 // Record the imported declaration.
3711 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003712
3713 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3714 // Keep track of anonymous tags that have an associated typedef.
3715 if (FromTag->getTypedefForAnonDecl())
3716 AnonTagsWithPendingTypedefs.push_back(FromTag);
3717 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3718 // When we've finished transforming a typedef, see whether it was the
3719 // typedef for an anonymous tag.
3720 for (llvm::SmallVector<TagDecl *, 4>::iterator
3721 FromTag = AnonTagsWithPendingTypedefs.begin(),
3722 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3723 FromTag != FromTagEnd; ++FromTag) {
3724 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3725 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3726 // We found the typedef for an anonymous tag; link them.
3727 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3728 AnonTagsWithPendingTypedefs.erase(FromTag);
3729 break;
3730 }
3731 }
3732 }
3733 }
3734
Douglas Gregor62d311f2010-02-09 19:21:46 +00003735 return ToD;
3736}
3737
3738DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3739 if (!FromDC)
3740 return FromDC;
3741
3742 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3743}
3744
3745Expr *ASTImporter::Import(Expr *FromE) {
3746 if (!FromE)
3747 return 0;
3748
3749 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3750}
3751
3752Stmt *ASTImporter::Import(Stmt *FromS) {
3753 if (!FromS)
3754 return 0;
3755
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003756 // Check whether we've already imported this declaration.
3757 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3758 if (Pos != ImportedStmts.end())
3759 return Pos->second;
3760
3761 // Import the type
3762 ASTNodeImporter Importer(*this);
3763 Stmt *ToS = Importer.Visit(FromS);
3764 if (!ToS)
3765 return 0;
3766
3767 // Record the imported declaration.
3768 ImportedStmts[FromS] = ToS;
3769 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003770}
3771
3772NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3773 if (!FromNNS)
3774 return 0;
3775
3776 // FIXME: Implement!
3777 return 0;
3778}
3779
Douglas Gregore2e50d332010-12-01 01:36:18 +00003780TemplateName ASTImporter::Import(TemplateName From) {
3781 switch (From.getKind()) {
3782 case TemplateName::Template:
3783 if (TemplateDecl *ToTemplate
3784 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3785 return TemplateName(ToTemplate);
3786
3787 return TemplateName();
3788
3789 case TemplateName::OverloadedTemplate: {
3790 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
3791 UnresolvedSet<2> ToTemplates;
3792 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
3793 E = FromStorage->end();
3794 I != E; ++I) {
3795 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
3796 ToTemplates.addDecl(To);
3797 else
3798 return TemplateName();
3799 }
3800 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
3801 ToTemplates.end());
3802 }
3803
3804 case TemplateName::QualifiedTemplate: {
3805 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
3806 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
3807 if (!Qualifier)
3808 return TemplateName();
3809
3810 if (TemplateDecl *ToTemplate
3811 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3812 return ToContext.getQualifiedTemplateName(Qualifier,
3813 QTN->hasTemplateKeyword(),
3814 ToTemplate);
3815
3816 return TemplateName();
3817 }
3818
3819 case TemplateName::DependentTemplate: {
3820 DependentTemplateName *DTN = From.getAsDependentTemplateName();
3821 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
3822 if (!Qualifier)
3823 return TemplateName();
3824
3825 if (DTN->isIdentifier()) {
3826 return ToContext.getDependentTemplateName(Qualifier,
3827 Import(DTN->getIdentifier()));
3828 }
3829
3830 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
3831 }
3832 }
3833
3834 llvm_unreachable("Invalid template name kind");
3835 return TemplateName();
3836}
3837
Douglas Gregor62d311f2010-02-09 19:21:46 +00003838SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
3839 if (FromLoc.isInvalid())
3840 return SourceLocation();
3841
Douglas Gregor811663e2010-02-10 00:15:17 +00003842 SourceManager &FromSM = FromContext.getSourceManager();
3843
3844 // For now, map everything down to its spelling location, so that we
3845 // don't have to import macro instantiations.
3846 // FIXME: Import macro instantiations!
3847 FromLoc = FromSM.getSpellingLoc(FromLoc);
3848 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3849 SourceManager &ToSM = ToContext.getSourceManager();
3850 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3851 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003852}
3853
3854SourceRange ASTImporter::Import(SourceRange FromRange) {
3855 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
3856}
3857
Douglas Gregor811663e2010-02-10 00:15:17 +00003858FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00003859 llvm::DenseMap<FileID, FileID>::iterator Pos
3860 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00003861 if (Pos != ImportedFileIDs.end())
3862 return Pos->second;
3863
3864 SourceManager &FromSM = FromContext.getSourceManager();
3865 SourceManager &ToSM = ToContext.getSourceManager();
3866 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3867 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3868
3869 // Include location of this file.
3870 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3871
3872 // Map the FileID for to the "to" source manager.
3873 FileID ToID;
3874 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3875 if (Cache->Entry) {
3876 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3877 // disk again
3878 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3879 // than mmap the files several times.
Chris Lattner5159f612010-11-23 08:35:12 +00003880 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00003881 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3882 FromSLoc.getFile().getFileCharacteristic());
3883 } else {
3884 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003885 const llvm::MemoryBuffer *
3886 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00003887 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00003888 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00003889 FromBuf->getBufferIdentifier());
3890 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
3891 }
3892
3893
Sebastian Redl99219f12010-09-30 01:03:06 +00003894 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00003895 return ToID;
3896}
3897
Douglas Gregor96e578d2010-02-05 17:54:41 +00003898DeclarationName ASTImporter::Import(DeclarationName FromName) {
3899 if (!FromName)
3900 return DeclarationName();
3901
3902 switch (FromName.getNameKind()) {
3903 case DeclarationName::Identifier:
3904 return Import(FromName.getAsIdentifierInfo());
3905
3906 case DeclarationName::ObjCZeroArgSelector:
3907 case DeclarationName::ObjCOneArgSelector:
3908 case DeclarationName::ObjCMultiArgSelector:
3909 return Import(FromName.getObjCSelector());
3910
3911 case DeclarationName::CXXConstructorName: {
3912 QualType T = Import(FromName.getCXXNameType());
3913 if (T.isNull())
3914 return DeclarationName();
3915
3916 return ToContext.DeclarationNames.getCXXConstructorName(
3917 ToContext.getCanonicalType(T));
3918 }
3919
3920 case DeclarationName::CXXDestructorName: {
3921 QualType T = Import(FromName.getCXXNameType());
3922 if (T.isNull())
3923 return DeclarationName();
3924
3925 return ToContext.DeclarationNames.getCXXDestructorName(
3926 ToContext.getCanonicalType(T));
3927 }
3928
3929 case DeclarationName::CXXConversionFunctionName: {
3930 QualType T = Import(FromName.getCXXNameType());
3931 if (T.isNull())
3932 return DeclarationName();
3933
3934 return ToContext.DeclarationNames.getCXXConversionFunctionName(
3935 ToContext.getCanonicalType(T));
3936 }
3937
3938 case DeclarationName::CXXOperatorName:
3939 return ToContext.DeclarationNames.getCXXOperatorName(
3940 FromName.getCXXOverloadedOperator());
3941
3942 case DeclarationName::CXXLiteralOperatorName:
3943 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
3944 Import(FromName.getCXXLiteralIdentifier()));
3945
3946 case DeclarationName::CXXUsingDirective:
3947 // FIXME: STATICS!
3948 return DeclarationName::getUsingDirectiveName();
3949 }
3950
3951 // Silence bogus GCC warning
3952 return DeclarationName();
3953}
3954
Douglas Gregore2e50d332010-12-01 01:36:18 +00003955IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00003956 if (!FromId)
3957 return 0;
3958
3959 return &ToContext.Idents.get(FromId->getName());
3960}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003961
Douglas Gregor43f54792010-02-17 02:12:47 +00003962Selector ASTImporter::Import(Selector FromSel) {
3963 if (FromSel.isNull())
3964 return Selector();
3965
3966 llvm::SmallVector<IdentifierInfo *, 4> Idents;
3967 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
3968 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
3969 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
3970 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
3971}
3972
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003973DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
3974 DeclContext *DC,
3975 unsigned IDNS,
3976 NamedDecl **Decls,
3977 unsigned NumDecls) {
3978 return Name;
3979}
3980
3981DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003982 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003983}
3984
3985DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003986 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003987}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003988
3989Decl *ASTImporter::Imported(Decl *From, Decl *To) {
3990 ImportedDecls[From] = To;
3991 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00003992}
Douglas Gregorb4964f72010-02-15 23:54:17 +00003993
3994bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
3995 llvm::DenseMap<Type *, Type *>::iterator Pos
3996 = ImportedTypes.find(From.getTypePtr());
3997 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
3998 return true;
3999
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004000 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004001 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004002}