blob: 0628fea476d9e16548c57db4c1e8417a4afff2a7 [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())
1697 return false;
1698
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();
1713 return true;
1714}
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,
Douglas Gregor98c10182010-02-12 22:17:39 +00002004 Name.getAsIdentifierInfo(),
2005 Importer.Import(D->getTagKeywordLoc()),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002006 0, D->isScoped(), D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002007 // Import the qualifier, if any.
2008 if (D->getQualifier()) {
2009 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2010 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2011 D2->setQualifierInfo(NNS, NNSRange);
2012 }
Douglas Gregordd483172010-02-22 17:42:47 +00002013 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002014 D2->setLexicalDeclContext(LexicalDC);
2015 Importer.Imported(D, D2);
2016 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002017
2018 // Import the integer type.
2019 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2020 if (ToIntegerType.isNull())
2021 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002022 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002023
2024 // Import the definition
2025 if (D->isDefinition()) {
2026 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2027 if (T.isNull())
2028 return 0;
2029
2030 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2031 if (ToPromotionType.isNull())
2032 return 0;
2033
Douglas Gregor3996e242010-02-15 22:01:00 +00002034 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00002035 ImportDeclContext(D);
John McCall9aa35be2010-05-06 08:49:23 +00002036
2037 // FIXME: we might need to merge the number of positive or negative bits
2038 // if the enumerator lists don't match.
2039 D2->completeDefinition(T, ToPromotionType,
2040 D->getNumPositiveBits(),
2041 D->getNumNegativeBits());
Douglas Gregor98c10182010-02-12 22:17:39 +00002042 }
2043
Douglas Gregor3996e242010-02-15 22:01:00 +00002044 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002045}
2046
Douglas Gregor5c73e912010-02-11 00:48:18 +00002047Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2048 // If this record has a definition in the translation unit we're coming from,
2049 // but this particular declaration is not that definition, import the
2050 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002051 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002052 if (Definition && Definition != D) {
2053 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002054 if (!ImportedDef)
2055 return 0;
2056
2057 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002058 }
2059
2060 // Import the major distinguishing characteristics of this record.
2061 DeclContext *DC, *LexicalDC;
2062 DeclarationName Name;
2063 SourceLocation Loc;
2064 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2065 return 0;
2066
2067 // Figure out what structure name we're looking for.
2068 unsigned IDNS = Decl::IDNS_Tag;
2069 DeclarationName SearchName = Name;
2070 if (!SearchName && D->getTypedefForAnonDecl()) {
2071 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2072 IDNS = Decl::IDNS_Ordinary;
2073 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2074 IDNS |= Decl::IDNS_Ordinary;
2075
2076 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002077 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002078 if (!DC->isFunctionOrMethod() && SearchName) {
2079 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2080 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2081 Lookup.first != Lookup.second;
2082 ++Lookup.first) {
2083 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2084 continue;
2085
2086 Decl *Found = *Lookup.first;
2087 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2088 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2089 Found = Tag->getDecl();
2090 }
2091
2092 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002093 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2094 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2095 // The record types structurally match, or the "from" translation
2096 // unit only had a forward declaration anyway; call it the same
2097 // function.
2098 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002099 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002100 }
2101 } else {
2102 // We have a forward declaration of this type, so adopt that forward
2103 // declaration rather than building a new one.
2104 AdoptDecl = FoundRecord;
2105 continue;
2106 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002107 }
2108
2109 ConflictingDecls.push_back(*Lookup.first);
2110 }
2111
2112 if (!ConflictingDecls.empty()) {
2113 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2114 ConflictingDecls.data(),
2115 ConflictingDecls.size());
2116 }
2117 }
2118
2119 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002120 RecordDecl *D2 = AdoptDecl;
2121 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002122 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002123 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002124 D->getTagKind(),
2125 DC, Loc,
2126 Name.getAsIdentifierInfo(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002127 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002128 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002129 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002130 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002131 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00002132 DC, Loc,
2133 Name.getAsIdentifierInfo(),
2134 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00002135 }
John McCall3e11ebe2010-03-15 10:12:16 +00002136 // Import the qualifier, if any.
2137 if (D->getQualifier()) {
2138 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2139 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2140 D2->setQualifierInfo(NNS, NNSRange);
2141 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002142 D2->setLexicalDeclContext(LexicalDC);
2143 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002144 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002145
Douglas Gregor3996e242010-02-15 22:01:00 +00002146 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002147
Douglas Gregore2e50d332010-12-01 01:36:18 +00002148 if (D->isDefinition() && ImportDefinition(D, D2))
2149 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002150
Douglas Gregor3996e242010-02-15 22:01:00 +00002151 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002152}
2153
Douglas Gregor98c10182010-02-12 22:17:39 +00002154Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2155 // Import the major distinguishing characteristics of this enumerator.
2156 DeclContext *DC, *LexicalDC;
2157 DeclarationName Name;
2158 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002159 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002160 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002161
2162 QualType T = Importer.Import(D->getType());
2163 if (T.isNull())
2164 return 0;
2165
Douglas Gregor98c10182010-02-12 22:17:39 +00002166 // Determine whether there are any other declarations with the same name and
2167 // in the same context.
2168 if (!LexicalDC->isFunctionOrMethod()) {
2169 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2170 unsigned IDNS = Decl::IDNS_Ordinary;
2171 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2172 Lookup.first != Lookup.second;
2173 ++Lookup.first) {
2174 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2175 continue;
2176
2177 ConflictingDecls.push_back(*Lookup.first);
2178 }
2179
2180 if (!ConflictingDecls.empty()) {
2181 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2182 ConflictingDecls.data(),
2183 ConflictingDecls.size());
2184 if (!Name)
2185 return 0;
2186 }
2187 }
2188
2189 Expr *Init = Importer.Import(D->getInitExpr());
2190 if (D->getInitExpr() && !Init)
2191 return 0;
2192
2193 EnumConstantDecl *ToEnumerator
2194 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2195 Name.getAsIdentifierInfo(), T,
2196 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002197 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002198 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002199 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002200 LexicalDC->addDecl(ToEnumerator);
2201 return ToEnumerator;
2202}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002203
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002204Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2205 // Import the major distinguishing characteristics of this function.
2206 DeclContext *DC, *LexicalDC;
2207 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002208 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002209 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002210 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002211
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002212 // Try to find a function in our own ("to") context with the same name, same
2213 // type, and in the same context as the function we're importing.
2214 if (!LexicalDC->isFunctionOrMethod()) {
2215 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2216 unsigned IDNS = Decl::IDNS_Ordinary;
2217 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2218 Lookup.first != Lookup.second;
2219 ++Lookup.first) {
2220 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2221 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002222
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002223 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2224 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2225 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002226 if (Importer.IsStructurallyEquivalent(D->getType(),
2227 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002228 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002229 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002230 }
2231
2232 // FIXME: Check for overloading more carefully, e.g., by boosting
2233 // Sema::IsOverload out to the AST library.
2234
2235 // Function overloading is okay in C++.
2236 if (Importer.getToContext().getLangOptions().CPlusPlus)
2237 continue;
2238
2239 // Complain about inconsistent function types.
2240 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002241 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002242 Importer.ToDiag(FoundFunction->getLocation(),
2243 diag::note_odr_value_here)
2244 << FoundFunction->getType();
2245 }
2246 }
2247
2248 ConflictingDecls.push_back(*Lookup.first);
2249 }
2250
2251 if (!ConflictingDecls.empty()) {
2252 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2253 ConflictingDecls.data(),
2254 ConflictingDecls.size());
2255 if (!Name)
2256 return 0;
2257 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002258 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002259
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002260 DeclarationNameInfo NameInfo(Name, Loc);
2261 // Import additional name location/type info.
2262 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2263
Douglas Gregorb4964f72010-02-15 23:54:17 +00002264 // Import the type.
2265 QualType T = Importer.Import(D->getType());
2266 if (T.isNull())
2267 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002268
2269 // Import the function parameters.
2270 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2271 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2272 P != PEnd; ++P) {
2273 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2274 if (!ToP)
2275 return 0;
2276
2277 Parameters.push_back(ToP);
2278 }
2279
2280 // Create the imported function.
2281 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002282 FunctionDecl *ToFunction = 0;
2283 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2284 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2285 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002286 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002287 FromConstructor->isExplicit(),
2288 D->isInlineSpecified(),
2289 D->isImplicit());
2290 } else if (isa<CXXDestructorDecl>(D)) {
2291 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2292 cast<CXXRecordDecl>(DC),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002293 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002294 D->isInlineSpecified(),
2295 D->isImplicit());
2296 } else if (CXXConversionDecl *FromConversion
2297 = dyn_cast<CXXConversionDecl>(D)) {
2298 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2299 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002300 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002301 D->isInlineSpecified(),
2302 FromConversion->isExplicit());
Douglas Gregora50ad132010-11-29 16:04:58 +00002303 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2304 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2305 cast<CXXRecordDecl>(DC),
2306 NameInfo, T, TInfo,
2307 Method->isStatic(),
2308 Method->getStorageClassAsWritten(),
2309 Method->isInlineSpecified());
Douglas Gregor00eace12010-02-21 18:29:16 +00002310 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002311 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2312 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002313 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002314 D->isInlineSpecified(),
2315 D->hasWrittenPrototype());
2316 }
John McCall3e11ebe2010-03-15 10:12:16 +00002317
2318 // Import the qualifier, if any.
2319 if (D->getQualifier()) {
2320 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2321 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2322 ToFunction->setQualifierInfo(NNS, NNSRange);
2323 }
Douglas Gregordd483172010-02-22 17:42:47 +00002324 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002325 ToFunction->setLexicalDeclContext(LexicalDC);
2326 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002327
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002328 // Set the parameters.
2329 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002330 Parameters[I]->setOwningFunction(ToFunction);
2331 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002332 }
Douglas Gregor43f54792010-02-17 02:12:47 +00002333 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002334
2335 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002336
2337 // Add this function to the lexical context.
2338 LexicalDC->addDecl(ToFunction);
2339
Douglas Gregor43f54792010-02-17 02:12:47 +00002340 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002341}
2342
Douglas Gregor00eace12010-02-21 18:29:16 +00002343Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2344 return VisitFunctionDecl(D);
2345}
2346
2347Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2348 return VisitCXXMethodDecl(D);
2349}
2350
2351Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2352 return VisitCXXMethodDecl(D);
2353}
2354
2355Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2356 return VisitCXXMethodDecl(D);
2357}
2358
Douglas Gregor5c73e912010-02-11 00:48:18 +00002359Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2360 // Import the major distinguishing characteristics of a variable.
2361 DeclContext *DC, *LexicalDC;
2362 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002363 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002364 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2365 return 0;
2366
2367 // Import the type.
2368 QualType T = Importer.Import(D->getType());
2369 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002370 return 0;
2371
2372 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2373 Expr *BitWidth = Importer.Import(D->getBitWidth());
2374 if (!BitWidth && D->getBitWidth())
2375 return 0;
2376
2377 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2378 Loc, Name.getAsIdentifierInfo(),
2379 T, TInfo, BitWidth, D->isMutable());
Douglas Gregordd483172010-02-22 17:42:47 +00002380 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002381 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002382 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002383 LexicalDC->addDecl(ToField);
2384 return ToField;
2385}
2386
Francois Pichet783dd6e2010-11-21 06:08:52 +00002387Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2388 // Import the major distinguishing characteristics of a variable.
2389 DeclContext *DC, *LexicalDC;
2390 DeclarationName Name;
2391 SourceLocation Loc;
2392 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2393 return 0;
2394
2395 // Import the type.
2396 QualType T = Importer.Import(D->getType());
2397 if (T.isNull())
2398 return 0;
2399
2400 NamedDecl **NamedChain =
2401 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2402
2403 unsigned i = 0;
2404 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2405 PE = D->chain_end(); PI != PE; ++PI) {
2406 Decl* D = Importer.Import(*PI);
2407 if (!D)
2408 return 0;
2409 NamedChain[i++] = cast<NamedDecl>(D);
2410 }
2411
2412 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2413 Importer.getToContext(), DC,
2414 Loc, Name.getAsIdentifierInfo(), T,
2415 NamedChain, D->getChainingSize());
2416 ToIndirectField->setAccess(D->getAccess());
2417 ToIndirectField->setLexicalDeclContext(LexicalDC);
2418 Importer.Imported(D, ToIndirectField);
2419 LexicalDC->addDecl(ToIndirectField);
2420 return ToIndirectField;
2421}
2422
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002423Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2424 // Import the major distinguishing characteristics of an ivar.
2425 DeclContext *DC, *LexicalDC;
2426 DeclarationName Name;
2427 SourceLocation Loc;
2428 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2429 return 0;
2430
2431 // Determine whether we've already imported this ivar
2432 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2433 Lookup.first != Lookup.second;
2434 ++Lookup.first) {
2435 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2436 if (Importer.IsStructurallyEquivalent(D->getType(),
2437 FoundIvar->getType())) {
2438 Importer.Imported(D, FoundIvar);
2439 return FoundIvar;
2440 }
2441
2442 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2443 << Name << D->getType() << FoundIvar->getType();
2444 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2445 << FoundIvar->getType();
2446 return 0;
2447 }
2448 }
2449
2450 // Import the type.
2451 QualType T = Importer.Import(D->getType());
2452 if (T.isNull())
2453 return 0;
2454
2455 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2456 Expr *BitWidth = Importer.Import(D->getBitWidth());
2457 if (!BitWidth && D->getBitWidth())
2458 return 0;
2459
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002460 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2461 cast<ObjCContainerDecl>(DC),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002462 Loc, Name.getAsIdentifierInfo(),
2463 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002464 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002465 ToIvar->setLexicalDeclContext(LexicalDC);
2466 Importer.Imported(D, ToIvar);
2467 LexicalDC->addDecl(ToIvar);
2468 return ToIvar;
2469
2470}
2471
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002472Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2473 // Import the major distinguishing characteristics of a variable.
2474 DeclContext *DC, *LexicalDC;
2475 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002476 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002477 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002478 return 0;
2479
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002480 // Try to find a variable in our own ("to") context with the same name and
2481 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002482 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002483 VarDecl *MergeWithVar = 0;
2484 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2485 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002486 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002487 Lookup.first != Lookup.second;
2488 ++Lookup.first) {
2489 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2490 continue;
2491
2492 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2493 // We have found a variable that we may need to merge with. Check it.
2494 if (isExternalLinkage(FoundVar->getLinkage()) &&
2495 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002496 if (Importer.IsStructurallyEquivalent(D->getType(),
2497 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002498 MergeWithVar = FoundVar;
2499 break;
2500 }
2501
Douglas Gregor56521c52010-02-12 17:23:39 +00002502 const ArrayType *FoundArray
2503 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2504 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002505 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002506 if (FoundArray && TArray) {
2507 if (isa<IncompleteArrayType>(FoundArray) &&
2508 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002509 // Import the type.
2510 QualType T = Importer.Import(D->getType());
2511 if (T.isNull())
2512 return 0;
2513
Douglas Gregor56521c52010-02-12 17:23:39 +00002514 FoundVar->setType(T);
2515 MergeWithVar = FoundVar;
2516 break;
2517 } else if (isa<IncompleteArrayType>(TArray) &&
2518 isa<ConstantArrayType>(FoundArray)) {
2519 MergeWithVar = FoundVar;
2520 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002521 }
2522 }
2523
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002524 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002525 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002526 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2527 << FoundVar->getType();
2528 }
2529 }
2530
2531 ConflictingDecls.push_back(*Lookup.first);
2532 }
2533
2534 if (MergeWithVar) {
2535 // An equivalent variable with external linkage has been found. Link
2536 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002537 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002538
2539 if (VarDecl *DDef = D->getDefinition()) {
2540 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2541 Importer.ToDiag(ExistingDef->getLocation(),
2542 diag::err_odr_variable_multiple_def)
2543 << Name;
2544 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2545 } else {
2546 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002547 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002548 }
2549 }
2550
2551 return MergeWithVar;
2552 }
2553
2554 if (!ConflictingDecls.empty()) {
2555 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2556 ConflictingDecls.data(),
2557 ConflictingDecls.size());
2558 if (!Name)
2559 return 0;
2560 }
2561 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002562
Douglas Gregorb4964f72010-02-15 23:54:17 +00002563 // Import the type.
2564 QualType T = Importer.Import(D->getType());
2565 if (T.isNull())
2566 return 0;
2567
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002568 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002569 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002570 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2571 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002572 D->getStorageClass(),
2573 D->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002574 // Import the qualifier, if any.
2575 if (D->getQualifier()) {
2576 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2577 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2578 ToVar->setQualifierInfo(NNS, NNSRange);
2579 }
Douglas Gregordd483172010-02-22 17:42:47 +00002580 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002581 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002582 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002583 LexicalDC->addDecl(ToVar);
2584
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002585 // Merge the initializer.
2586 // FIXME: Can we really import any initializer? Alternatively, we could force
2587 // ourselves to import every declaration of a variable and then only use
2588 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002589 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002590
2591 // FIXME: Other bits to merge?
2592
2593 return ToVar;
2594}
2595
Douglas Gregor8b228d72010-02-17 21:22:52 +00002596Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2597 // Parameters are created in the translation unit's context, then moved
2598 // into the function declaration's context afterward.
2599 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2600
2601 // Import the name of this declaration.
2602 DeclarationName Name = Importer.Import(D->getDeclName());
2603 if (D->getDeclName() && !Name)
2604 return 0;
2605
2606 // Import the location of this declaration.
2607 SourceLocation Loc = Importer.Import(D->getLocation());
2608
2609 // Import the parameter's type.
2610 QualType T = Importer.Import(D->getType());
2611 if (T.isNull())
2612 return 0;
2613
2614 // Create the imported parameter.
2615 ImplicitParamDecl *ToParm
2616 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2617 Loc, Name.getAsIdentifierInfo(),
2618 T);
2619 return Importer.Imported(D, ToParm);
2620}
2621
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002622Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2623 // Parameters are created in the translation unit's context, then moved
2624 // into the function declaration's context afterward.
2625 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2626
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002627 // Import the name of this declaration.
2628 DeclarationName Name = Importer.Import(D->getDeclName());
2629 if (D->getDeclName() && !Name)
2630 return 0;
2631
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002632 // Import the location of this declaration.
2633 SourceLocation Loc = Importer.Import(D->getLocation());
2634
2635 // Import the parameter's type.
2636 QualType T = Importer.Import(D->getType());
2637 if (T.isNull())
2638 return 0;
2639
2640 // Create the imported parameter.
2641 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2642 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2643 Loc, Name.getAsIdentifierInfo(),
2644 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002645 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002646 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002647 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002648 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002649}
2650
Douglas Gregor43f54792010-02-17 02:12:47 +00002651Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2652 // Import the major distinguishing characteristics of a method.
2653 DeclContext *DC, *LexicalDC;
2654 DeclarationName Name;
2655 SourceLocation Loc;
2656 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2657 return 0;
2658
2659 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2660 Lookup.first != Lookup.second;
2661 ++Lookup.first) {
2662 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2663 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2664 continue;
2665
2666 // Check return types.
2667 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2668 FoundMethod->getResultType())) {
2669 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2670 << D->isInstanceMethod() << Name
2671 << D->getResultType() << FoundMethod->getResultType();
2672 Importer.ToDiag(FoundMethod->getLocation(),
2673 diag::note_odr_objc_method_here)
2674 << D->isInstanceMethod() << Name;
2675 return 0;
2676 }
2677
2678 // Check the number of parameters.
2679 if (D->param_size() != FoundMethod->param_size()) {
2680 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2681 << D->isInstanceMethod() << Name
2682 << D->param_size() << FoundMethod->param_size();
2683 Importer.ToDiag(FoundMethod->getLocation(),
2684 diag::note_odr_objc_method_here)
2685 << D->isInstanceMethod() << Name;
2686 return 0;
2687 }
2688
2689 // Check parameter types.
2690 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2691 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2692 P != PEnd; ++P, ++FoundP) {
2693 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2694 (*FoundP)->getType())) {
2695 Importer.FromDiag((*P)->getLocation(),
2696 diag::err_odr_objc_method_param_type_inconsistent)
2697 << D->isInstanceMethod() << Name
2698 << (*P)->getType() << (*FoundP)->getType();
2699 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2700 << (*FoundP)->getType();
2701 return 0;
2702 }
2703 }
2704
2705 // Check variadic/non-variadic.
2706 // Check the number of parameters.
2707 if (D->isVariadic() != FoundMethod->isVariadic()) {
2708 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2709 << D->isInstanceMethod() << Name;
2710 Importer.ToDiag(FoundMethod->getLocation(),
2711 diag::note_odr_objc_method_here)
2712 << D->isInstanceMethod() << Name;
2713 return 0;
2714 }
2715
2716 // FIXME: Any other bits we need to merge?
2717 return Importer.Imported(D, FoundMethod);
2718 }
2719 }
2720
2721 // Import the result type.
2722 QualType ResultTy = Importer.Import(D->getResultType());
2723 if (ResultTy.isNull())
2724 return 0;
2725
Douglas Gregor12852d92010-03-08 14:59:44 +00002726 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2727
Douglas Gregor43f54792010-02-17 02:12:47 +00002728 ObjCMethodDecl *ToMethod
2729 = ObjCMethodDecl::Create(Importer.getToContext(),
2730 Loc,
2731 Importer.Import(D->getLocEnd()),
2732 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00002733 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00002734 D->isInstanceMethod(),
2735 D->isVariadic(),
2736 D->isSynthesized(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002737 D->isDefined(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002738 D->getImplementationControl());
2739
2740 // FIXME: When we decide to merge method definitions, we'll need to
2741 // deal with implicit parameters.
2742
2743 // Import the parameters
2744 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2745 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2746 FromPEnd = D->param_end();
2747 FromP != FromPEnd;
2748 ++FromP) {
2749 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2750 if (!ToP)
2751 return 0;
2752
2753 ToParams.push_back(ToP);
2754 }
2755
2756 // Set the parameters.
2757 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2758 ToParams[I]->setOwningFunction(ToMethod);
2759 ToMethod->addDecl(ToParams[I]);
2760 }
2761 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00002762 ToParams.data(), ToParams.size(),
2763 ToParams.size());
Douglas Gregor43f54792010-02-17 02:12:47 +00002764
2765 ToMethod->setLexicalDeclContext(LexicalDC);
2766 Importer.Imported(D, ToMethod);
2767 LexicalDC->addDecl(ToMethod);
2768 return ToMethod;
2769}
2770
Douglas Gregor84c51c32010-02-18 01:47:50 +00002771Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2772 // Import the major distinguishing characteristics of a category.
2773 DeclContext *DC, *LexicalDC;
2774 DeclarationName Name;
2775 SourceLocation Loc;
2776 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2777 return 0;
2778
2779 ObjCInterfaceDecl *ToInterface
2780 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2781 if (!ToInterface)
2782 return 0;
2783
2784 // Determine if we've already encountered this category.
2785 ObjCCategoryDecl *MergeWithCategory
2786 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2787 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2788 if (!ToCategory) {
2789 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2790 Importer.Import(D->getAtLoc()),
2791 Loc,
2792 Importer.Import(D->getCategoryNameLoc()),
2793 Name.getAsIdentifierInfo());
2794 ToCategory->setLexicalDeclContext(LexicalDC);
2795 LexicalDC->addDecl(ToCategory);
2796 Importer.Imported(D, ToCategory);
2797
2798 // Link this category into its class's category list.
2799 ToCategory->setClassInterface(ToInterface);
2800 ToCategory->insertNextClassCategory();
2801
2802 // Import protocols
2803 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2804 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2805 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2806 = D->protocol_loc_begin();
2807 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2808 FromProtoEnd = D->protocol_end();
2809 FromProto != FromProtoEnd;
2810 ++FromProto, ++FromProtoLoc) {
2811 ObjCProtocolDecl *ToProto
2812 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2813 if (!ToProto)
2814 return 0;
2815 Protocols.push_back(ToProto);
2816 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2817 }
2818
2819 // FIXME: If we're merging, make sure that the protocol list is the same.
2820 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2821 ProtocolLocs.data(), Importer.getToContext());
2822
2823 } else {
2824 Importer.Imported(D, ToCategory);
2825 }
2826
2827 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002828 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002829
2830 // If we have an implementation, import it as well.
2831 if (D->getImplementation()) {
2832 ObjCCategoryImplDecl *Impl
2833 = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2834 if (!Impl)
2835 return 0;
2836
2837 ToCategory->setImplementation(Impl);
2838 }
2839
2840 return ToCategory;
2841}
2842
Douglas Gregor98d156a2010-02-17 16:12:00 +00002843Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002844 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002845 DeclContext *DC, *LexicalDC;
2846 DeclarationName Name;
2847 SourceLocation Loc;
2848 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2849 return 0;
2850
2851 ObjCProtocolDecl *MergeWithProtocol = 0;
2852 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2853 Lookup.first != Lookup.second;
2854 ++Lookup.first) {
2855 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2856 continue;
2857
2858 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2859 break;
2860 }
2861
2862 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2863 if (!ToProto || ToProto->isForwardDecl()) {
2864 if (!ToProto) {
2865 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2866 Name.getAsIdentifierInfo());
2867 ToProto->setForwardDecl(D->isForwardDecl());
2868 ToProto->setLexicalDeclContext(LexicalDC);
2869 LexicalDC->addDecl(ToProto);
2870 }
2871 Importer.Imported(D, ToProto);
2872
2873 // Import protocols
2874 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2875 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2876 ObjCProtocolDecl::protocol_loc_iterator
2877 FromProtoLoc = D->protocol_loc_begin();
2878 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2879 FromProtoEnd = D->protocol_end();
2880 FromProto != FromProtoEnd;
2881 ++FromProto, ++FromProtoLoc) {
2882 ObjCProtocolDecl *ToProto
2883 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2884 if (!ToProto)
2885 return 0;
2886 Protocols.push_back(ToProto);
2887 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2888 }
2889
2890 // FIXME: If we're merging, make sure that the protocol list is the same.
2891 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2892 ProtocolLocs.data(), Importer.getToContext());
2893 } else {
2894 Importer.Imported(D, ToProto);
2895 }
2896
Douglas Gregor84c51c32010-02-18 01:47:50 +00002897 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00002898 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002899
2900 return ToProto;
2901}
2902
Douglas Gregor45635322010-02-16 01:20:57 +00002903Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2904 // Import the major distinguishing characteristics of an @interface.
2905 DeclContext *DC, *LexicalDC;
2906 DeclarationName Name;
2907 SourceLocation Loc;
2908 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2909 return 0;
2910
2911 ObjCInterfaceDecl *MergeWithIface = 0;
2912 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2913 Lookup.first != Lookup.second;
2914 ++Lookup.first) {
2915 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2916 continue;
2917
2918 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2919 break;
2920 }
2921
2922 ObjCInterfaceDecl *ToIface = MergeWithIface;
2923 if (!ToIface || ToIface->isForwardDecl()) {
2924 if (!ToIface) {
2925 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2926 DC, Loc,
2927 Name.getAsIdentifierInfo(),
Douglas Gregor1c283312010-08-11 12:19:30 +00002928 Importer.Import(D->getClassLoc()),
Douglas Gregor45635322010-02-16 01:20:57 +00002929 D->isForwardDecl(),
2930 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00002931 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00002932 ToIface->setLexicalDeclContext(LexicalDC);
2933 LexicalDC->addDecl(ToIface);
2934 }
2935 Importer.Imported(D, ToIface);
2936
Douglas Gregor45635322010-02-16 01:20:57 +00002937 if (D->getSuperClass()) {
2938 ObjCInterfaceDecl *Super
2939 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2940 if (!Super)
2941 return 0;
2942
2943 ToIface->setSuperClass(Super);
2944 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2945 }
2946
2947 // Import protocols
2948 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2949 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2950 ObjCInterfaceDecl::protocol_loc_iterator
2951 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002952
2953 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregor45635322010-02-16 01:20:57 +00002954 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2955 FromProtoEnd = D->protocol_end();
2956 FromProto != FromProtoEnd;
2957 ++FromProto, ++FromProtoLoc) {
2958 ObjCProtocolDecl *ToProto
2959 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2960 if (!ToProto)
2961 return 0;
2962 Protocols.push_back(ToProto);
2963 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2964 }
2965
2966 // FIXME: If we're merging, make sure that the protocol list is the same.
2967 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2968 ProtocolLocs.data(), Importer.getToContext());
2969
Douglas Gregor45635322010-02-16 01:20:57 +00002970 // Import @end range
2971 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2972 } else {
2973 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002974
2975 // Check for consistency of superclasses.
2976 DeclarationName FromSuperName, ToSuperName;
2977 if (D->getSuperClass())
2978 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2979 if (ToIface->getSuperClass())
2980 ToSuperName = ToIface->getSuperClass()->getDeclName();
2981 if (FromSuperName != ToSuperName) {
2982 Importer.ToDiag(ToIface->getLocation(),
2983 diag::err_odr_objc_superclass_inconsistent)
2984 << ToIface->getDeclName();
2985 if (ToIface->getSuperClass())
2986 Importer.ToDiag(ToIface->getSuperClassLoc(),
2987 diag::note_odr_objc_superclass)
2988 << ToIface->getSuperClass()->getDeclName();
2989 else
2990 Importer.ToDiag(ToIface->getLocation(),
2991 diag::note_odr_objc_missing_superclass);
2992 if (D->getSuperClass())
2993 Importer.FromDiag(D->getSuperClassLoc(),
2994 diag::note_odr_objc_superclass)
2995 << D->getSuperClass()->getDeclName();
2996 else
2997 Importer.FromDiag(D->getLocation(),
2998 diag::note_odr_objc_missing_superclass);
2999 return 0;
3000 }
Douglas Gregor45635322010-02-16 01:20:57 +00003001 }
3002
Douglas Gregor84c51c32010-02-18 01:47:50 +00003003 // Import categories. When the categories themselves are imported, they'll
3004 // hook themselves into this interface.
3005 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3006 FromCat = FromCat->getNextClassCategory())
3007 Importer.Import(FromCat);
3008
Douglas Gregor45635322010-02-16 01:20:57 +00003009 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00003010 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00003011
3012 // If we have an @implementation, import it as well.
3013 if (D->getImplementation()) {
3014 ObjCImplementationDecl *Impl
3015 = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
3016 if (!Impl)
3017 return 0;
3018
3019 ToIface->setImplementation(Impl);
3020 }
3021
Douglas Gregor98d156a2010-02-17 16:12:00 +00003022 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003023}
3024
Douglas Gregora11c4582010-02-17 18:02:10 +00003025Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3026 // Import the major distinguishing characteristics of an @property.
3027 DeclContext *DC, *LexicalDC;
3028 DeclarationName Name;
3029 SourceLocation Loc;
3030 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3031 return 0;
3032
3033 // Check whether we have already imported this property.
3034 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3035 Lookup.first != Lookup.second;
3036 ++Lookup.first) {
3037 if (ObjCPropertyDecl *FoundProp
3038 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3039 // Check property types.
3040 if (!Importer.IsStructurallyEquivalent(D->getType(),
3041 FoundProp->getType())) {
3042 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3043 << Name << D->getType() << FoundProp->getType();
3044 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3045 << FoundProp->getType();
3046 return 0;
3047 }
3048
3049 // FIXME: Check property attributes, getters, setters, etc.?
3050
3051 // Consider these properties to be equivalent.
3052 Importer.Imported(D, FoundProp);
3053 return FoundProp;
3054 }
3055 }
3056
3057 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003058 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3059 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003060 return 0;
3061
3062 // Create the new property.
3063 ObjCPropertyDecl *ToProperty
3064 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3065 Name.getAsIdentifierInfo(),
3066 Importer.Import(D->getAtLoc()),
3067 T,
3068 D->getPropertyImplementation());
3069 Importer.Imported(D, ToProperty);
3070 ToProperty->setLexicalDeclContext(LexicalDC);
3071 LexicalDC->addDecl(ToProperty);
3072
3073 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003074 ToProperty->setPropertyAttributesAsWritten(
3075 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003076 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3077 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3078 ToProperty->setGetterMethodDecl(
3079 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3080 ToProperty->setSetterMethodDecl(
3081 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3082 ToProperty->setPropertyIvarDecl(
3083 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3084 return ToProperty;
3085}
3086
Douglas Gregor8661a722010-02-18 02:12:22 +00003087Decl *
3088ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3089 // Import the context of this declaration.
3090 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3091 if (!DC)
3092 return 0;
3093
3094 DeclContext *LexicalDC = DC;
3095 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3096 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3097 if (!LexicalDC)
3098 return 0;
3099 }
3100
3101 // Import the location of this declaration.
3102 SourceLocation Loc = Importer.Import(D->getLocation());
3103
3104 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3105 llvm::SmallVector<SourceLocation, 4> Locations;
3106 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3107 = D->protocol_loc_begin();
3108 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3109 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3110 FromProto != FromProtoEnd;
3111 ++FromProto, ++FromProtoLoc) {
3112 ObjCProtocolDecl *ToProto
3113 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3114 if (!ToProto)
3115 continue;
3116
3117 Protocols.push_back(ToProto);
3118 Locations.push_back(Importer.Import(*FromProtoLoc));
3119 }
3120
3121 ObjCForwardProtocolDecl *ToForward
3122 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3123 Protocols.data(), Protocols.size(),
3124 Locations.data());
3125 ToForward->setLexicalDeclContext(LexicalDC);
3126 LexicalDC->addDecl(ToForward);
3127 Importer.Imported(D, ToForward);
3128 return ToForward;
3129}
3130
Douglas Gregor06537af2010-02-18 02:04:09 +00003131Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3132 // Import the context of this declaration.
3133 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3134 if (!DC)
3135 return 0;
3136
3137 DeclContext *LexicalDC = DC;
3138 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3139 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3140 if (!LexicalDC)
3141 return 0;
3142 }
3143
3144 // Import the location of this declaration.
3145 SourceLocation Loc = Importer.Import(D->getLocation());
3146
3147 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3148 llvm::SmallVector<SourceLocation, 4> Locations;
3149 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3150 From != FromEnd; ++From) {
3151 ObjCInterfaceDecl *ToIface
3152 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3153 if (!ToIface)
3154 continue;
3155
3156 Interfaces.push_back(ToIface);
3157 Locations.push_back(Importer.Import(From->getLocation()));
3158 }
3159
3160 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3161 Loc,
3162 Interfaces.data(),
3163 Locations.data(),
3164 Interfaces.size());
3165 ToClass->setLexicalDeclContext(LexicalDC);
3166 LexicalDC->addDecl(ToClass);
3167 Importer.Imported(D, ToClass);
3168 return ToClass;
3169}
3170
Douglas Gregora082a492010-11-30 19:14:50 +00003171Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3172 // For template arguments, we adopt the translation unit as our declaration
3173 // context. This context will be fixed when the actual template declaration
3174 // is created.
3175
3176 // FIXME: Import default argument.
3177 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3178 Importer.getToContext().getTranslationUnitDecl(),
3179 Importer.Import(D->getLocation()),
3180 D->getDepth(),
3181 D->getIndex(),
3182 Importer.Import(D->getIdentifier()),
3183 D->wasDeclaredWithTypename(),
3184 D->isParameterPack());
3185}
3186
3187Decl *
3188ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3189 // Import the name of this declaration.
3190 DeclarationName Name = Importer.Import(D->getDeclName());
3191 if (D->getDeclName() && !Name)
3192 return 0;
3193
3194 // Import the location of this declaration.
3195 SourceLocation Loc = Importer.Import(D->getLocation());
3196
3197 // Import the type of this declaration.
3198 QualType T = Importer.Import(D->getType());
3199 if (T.isNull())
3200 return 0;
3201
3202 // Import type-source information.
3203 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3204 if (D->getTypeSourceInfo() && !TInfo)
3205 return 0;
3206
3207 // FIXME: Import default argument.
3208
3209 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3210 Importer.getToContext().getTranslationUnitDecl(),
3211 Loc, D->getDepth(), D->getPosition(),
3212 Name.getAsIdentifierInfo(),
3213 T, TInfo);
3214}
3215
3216Decl *
3217ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3218 // Import the name of this declaration.
3219 DeclarationName Name = Importer.Import(D->getDeclName());
3220 if (D->getDeclName() && !Name)
3221 return 0;
3222
3223 // Import the location of this declaration.
3224 SourceLocation Loc = Importer.Import(D->getLocation());
3225
3226 // Import template parameters.
3227 TemplateParameterList *TemplateParams
3228 = ImportTemplateParameterList(D->getTemplateParameters());
3229 if (!TemplateParams)
3230 return 0;
3231
3232 // FIXME: Import default argument.
3233
3234 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3235 Importer.getToContext().getTranslationUnitDecl(),
3236 Loc, D->getDepth(), D->getPosition(),
3237 Name.getAsIdentifierInfo(),
3238 TemplateParams);
3239}
3240
3241Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3242 // If this record has a definition in the translation unit we're coming from,
3243 // but this particular declaration is not that definition, import the
3244 // definition and map to that.
3245 CXXRecordDecl *Definition
3246 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3247 if (Definition && Definition != D->getTemplatedDecl()) {
3248 Decl *ImportedDef
3249 = Importer.Import(Definition->getDescribedClassTemplate());
3250 if (!ImportedDef)
3251 return 0;
3252
3253 return Importer.Imported(D, ImportedDef);
3254 }
3255
3256 // Import the major distinguishing characteristics of this class template.
3257 DeclContext *DC, *LexicalDC;
3258 DeclarationName Name;
3259 SourceLocation Loc;
3260 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3261 return 0;
3262
3263 // We may already have a template of the same name; try to find and match it.
3264 if (!DC->isFunctionOrMethod()) {
3265 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3266 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3267 Lookup.first != Lookup.second;
3268 ++Lookup.first) {
3269 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3270 continue;
3271
3272 Decl *Found = *Lookup.first;
3273 if (ClassTemplateDecl *FoundTemplate
3274 = dyn_cast<ClassTemplateDecl>(Found)) {
3275 if (IsStructuralMatch(D, FoundTemplate)) {
3276 // The class templates structurally match; call it the same template.
3277 // FIXME: We may be filling in a forward declaration here. Handle
3278 // this case!
3279 Importer.Imported(D->getTemplatedDecl(),
3280 FoundTemplate->getTemplatedDecl());
3281 return Importer.Imported(D, FoundTemplate);
3282 }
3283 }
3284
3285 ConflictingDecls.push_back(*Lookup.first);
3286 }
3287
3288 if (!ConflictingDecls.empty()) {
3289 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3290 ConflictingDecls.data(),
3291 ConflictingDecls.size());
3292 }
3293
3294 if (!Name)
3295 return 0;
3296 }
3297
3298 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3299
3300 // Create the declaration that is being templated.
3301 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3302 DTemplated->getTagKind(),
3303 DC,
3304 Importer.Import(DTemplated->getLocation()),
3305 Name.getAsIdentifierInfo(),
3306 Importer.Import(DTemplated->getTagKeywordLoc()));
3307 D2Templated->setAccess(DTemplated->getAccess());
3308
3309
3310 // Import the qualifier, if any.
3311 if (DTemplated->getQualifier()) {
3312 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3313 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3314 D2Templated->setQualifierInfo(NNS, NNSRange);
3315 }
3316 D2Templated->setLexicalDeclContext(LexicalDC);
3317
3318 // Create the class template declaration itself.
3319 TemplateParameterList *TemplateParams
3320 = ImportTemplateParameterList(D->getTemplateParameters());
3321 if (!TemplateParams)
3322 return 0;
3323
3324 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3325 Loc, Name, TemplateParams,
3326 D2Templated,
3327 /*PrevDecl=*/0);
3328 D2Templated->setDescribedClassTemplate(D2);
3329
3330 D2->setAccess(D->getAccess());
3331 D2->setLexicalDeclContext(LexicalDC);
3332 LexicalDC->addDecl(D2);
3333
3334 // Note the relationship between the class templates.
3335 Importer.Imported(D, D2);
3336 Importer.Imported(DTemplated, D2Templated);
3337
3338 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3339 // FIXME: Import definition!
3340 }
3341
3342 return D2;
3343}
3344
Douglas Gregore2e50d332010-12-01 01:36:18 +00003345Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3346 ClassTemplateSpecializationDecl *D) {
3347 // If this record has a definition in the translation unit we're coming from,
3348 // but this particular declaration is not that definition, import the
3349 // definition and map to that.
3350 TagDecl *Definition = D->getDefinition();
3351 if (Definition && Definition != D) {
3352 Decl *ImportedDef = Importer.Import(Definition);
3353 if (!ImportedDef)
3354 return 0;
3355
3356 return Importer.Imported(D, ImportedDef);
3357 }
3358
3359 ClassTemplateDecl *ClassTemplate
3360 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3361 D->getSpecializedTemplate()));
3362 if (!ClassTemplate)
3363 return 0;
3364
3365 // Import the context of this declaration.
3366 DeclContext *DC = ClassTemplate->getDeclContext();
3367 if (!DC)
3368 return 0;
3369
3370 DeclContext *LexicalDC = DC;
3371 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3372 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3373 if (!LexicalDC)
3374 return 0;
3375 }
3376
3377 // Import the location of this declaration.
3378 SourceLocation Loc = Importer.Import(D->getLocation());
3379
3380 // Import template arguments.
3381 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3382 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3383 D->getTemplateArgs().size(),
3384 TemplateArgs))
3385 return 0;
3386
3387 // Try to find an existing specialization with these template arguments.
3388 void *InsertPos = 0;
3389 ClassTemplateSpecializationDecl *D2
3390 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3391 TemplateArgs.size(), InsertPos);
3392 if (D2) {
3393 // We already have a class template specialization with these template
3394 // arguments.
3395
3396 // FIXME: Check for specialization vs. instantiation errors.
3397
3398 if (RecordDecl *FoundDef = D2->getDefinition()) {
3399 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3400 // The record types structurally match, or the "from" translation
3401 // unit only had a forward declaration anyway; call it the same
3402 // function.
3403 return Importer.Imported(D, FoundDef);
3404 }
3405 }
3406 } else {
3407 // Create a new specialization.
3408 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3409 D->getTagKind(), DC,
3410 Loc, ClassTemplate,
3411 TemplateArgs.data(),
3412 TemplateArgs.size(),
3413 /*PrevDecl=*/0);
3414 D2->setSpecializationKind(D->getSpecializationKind());
3415
3416 // Add this specialization to the class template.
3417 ClassTemplate->AddSpecialization(D2, InsertPos);
3418
3419 // Import the qualifier, if any.
3420 if (D->getQualifier()) {
3421 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3422 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3423 D2->setQualifierInfo(NNS, NNSRange);
3424 }
3425
3426
3427 // Add the specialization to this context.
3428 D2->setLexicalDeclContext(LexicalDC);
3429 LexicalDC->addDecl(D2);
3430 }
3431 Importer.Imported(D, D2);
3432
3433 if (D->isDefinition() && ImportDefinition(D, D2))
3434 return 0;
3435
3436 return D2;
3437}
3438
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003439//----------------------------------------------------------------------------
3440// Import Statements
3441//----------------------------------------------------------------------------
3442
3443Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3444 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3445 << S->getStmtClassName();
3446 return 0;
3447}
3448
3449//----------------------------------------------------------------------------
3450// Import Expressions
3451//----------------------------------------------------------------------------
3452Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3453 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3454 << E->getStmtClassName();
3455 return 0;
3456}
3457
Douglas Gregor52f820e2010-02-19 01:17:02 +00003458Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3459 NestedNameSpecifier *Qualifier = 0;
3460 if (E->getQualifier()) {
3461 Qualifier = Importer.Import(E->getQualifier());
3462 if (!E->getQualifier())
3463 return 0;
3464 }
3465
3466 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3467 if (!ToD)
3468 return 0;
3469
3470 QualType T = Importer.Import(E->getType());
3471 if (T.isNull())
3472 return 0;
3473
3474 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3475 Importer.Import(E->getQualifierRange()),
3476 ToD,
3477 Importer.Import(E->getLocation()),
John McCall7decc9e2010-11-18 06:31:45 +00003478 T, E->getValueKind(),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003479 /*FIXME:TemplateArgs=*/0);
3480}
3481
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003482Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3483 QualType T = Importer.Import(E->getType());
3484 if (T.isNull())
3485 return 0;
3486
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003487 return IntegerLiteral::Create(Importer.getToContext(),
3488 E->getValue(), T,
3489 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003490}
3491
Douglas Gregor623421d2010-02-18 02:21:22 +00003492Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3493 QualType T = Importer.Import(E->getType());
3494 if (T.isNull())
3495 return 0;
3496
3497 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3498 E->isWide(), T,
3499 Importer.Import(E->getLocation()));
3500}
3501
Douglas Gregorc74247e2010-02-19 01:07:06 +00003502Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3503 Expr *SubExpr = Importer.Import(E->getSubExpr());
3504 if (!SubExpr)
3505 return 0;
3506
3507 return new (Importer.getToContext())
3508 ParenExpr(Importer.Import(E->getLParen()),
3509 Importer.Import(E->getRParen()),
3510 SubExpr);
3511}
3512
3513Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3514 QualType T = Importer.Import(E->getType());
3515 if (T.isNull())
3516 return 0;
3517
3518 Expr *SubExpr = Importer.Import(E->getSubExpr());
3519 if (!SubExpr)
3520 return 0;
3521
3522 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003523 T, E->getValueKind(),
3524 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003525 Importer.Import(E->getOperatorLoc()));
3526}
3527
Douglas Gregord8552cd2010-02-19 01:24:23 +00003528Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3529 QualType ResultType = Importer.Import(E->getType());
3530
3531 if (E->isArgumentType()) {
3532 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3533 if (!TInfo)
3534 return 0;
3535
3536 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3537 TInfo, ResultType,
3538 Importer.Import(E->getOperatorLoc()),
3539 Importer.Import(E->getRParenLoc()));
3540 }
3541
3542 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3543 if (!SubExpr)
3544 return 0;
3545
3546 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3547 SubExpr, ResultType,
3548 Importer.Import(E->getOperatorLoc()),
3549 Importer.Import(E->getRParenLoc()));
3550}
3551
Douglas Gregorc74247e2010-02-19 01:07:06 +00003552Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3553 QualType T = Importer.Import(E->getType());
3554 if (T.isNull())
3555 return 0;
3556
3557 Expr *LHS = Importer.Import(E->getLHS());
3558 if (!LHS)
3559 return 0;
3560
3561 Expr *RHS = Importer.Import(E->getRHS());
3562 if (!RHS)
3563 return 0;
3564
3565 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003566 T, E->getValueKind(),
3567 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003568 Importer.Import(E->getOperatorLoc()));
3569}
3570
3571Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3572 QualType T = Importer.Import(E->getType());
3573 if (T.isNull())
3574 return 0;
3575
3576 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3577 if (CompLHSType.isNull())
3578 return 0;
3579
3580 QualType CompResultType = Importer.Import(E->getComputationResultType());
3581 if (CompResultType.isNull())
3582 return 0;
3583
3584 Expr *LHS = Importer.Import(E->getLHS());
3585 if (!LHS)
3586 return 0;
3587
3588 Expr *RHS = Importer.Import(E->getRHS());
3589 if (!RHS)
3590 return 0;
3591
3592 return new (Importer.getToContext())
3593 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003594 T, E->getValueKind(),
3595 E->getObjectKind(),
3596 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00003597 Importer.Import(E->getOperatorLoc()));
3598}
3599
John McCallcf142162010-08-07 06:22:56 +00003600bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3601 if (E->path_empty()) return false;
3602
3603 // TODO: import cast paths
3604 return true;
3605}
3606
Douglas Gregor98c10182010-02-12 22:17:39 +00003607Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3608 QualType T = Importer.Import(E->getType());
3609 if (T.isNull())
3610 return 0;
3611
3612 Expr *SubExpr = Importer.Import(E->getSubExpr());
3613 if (!SubExpr)
3614 return 0;
John McCallcf142162010-08-07 06:22:56 +00003615
3616 CXXCastPath BasePath;
3617 if (ImportCastPath(E, BasePath))
3618 return 0;
3619
3620 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00003621 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00003622}
3623
Douglas Gregor5481d322010-02-19 01:32:14 +00003624Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3625 QualType T = Importer.Import(E->getType());
3626 if (T.isNull())
3627 return 0;
3628
3629 Expr *SubExpr = Importer.Import(E->getSubExpr());
3630 if (!SubExpr)
3631 return 0;
3632
3633 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3634 if (!TInfo && E->getTypeInfoAsWritten())
3635 return 0;
3636
John McCallcf142162010-08-07 06:22:56 +00003637 CXXCastPath BasePath;
3638 if (ImportCastPath(E, BasePath))
3639 return 0;
3640
John McCall7decc9e2010-11-18 06:31:45 +00003641 return CStyleCastExpr::Create(Importer.getToContext(), T,
3642 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00003643 SubExpr, &BasePath, TInfo,
3644 Importer.Import(E->getLParenLoc()),
3645 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00003646}
3647
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003648ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Chris Lattner5159f612010-11-23 08:35:12 +00003649 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor96e578d2010-02-05 17:54:41 +00003650 : ToContext(ToContext), FromContext(FromContext),
Chris Lattner5159f612010-11-23 08:35:12 +00003651 ToFileManager(ToFileManager), FromFileManager(FromFileManager) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00003652 ImportedDecls[FromContext.getTranslationUnitDecl()]
3653 = ToContext.getTranslationUnitDecl();
3654}
3655
3656ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00003657
3658QualType ASTImporter::Import(QualType FromT) {
3659 if (FromT.isNull())
3660 return QualType();
3661
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003662 // Check whether we've already imported this type.
3663 llvm::DenseMap<Type *, Type *>::iterator Pos
3664 = ImportedTypes.find(FromT.getTypePtr());
3665 if (Pos != ImportedTypes.end())
3666 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003667
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003668 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00003669 ASTNodeImporter Importer(*this);
3670 QualType ToT = Importer.Visit(FromT.getTypePtr());
3671 if (ToT.isNull())
3672 return ToT;
3673
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003674 // Record the imported type.
3675 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3676
Douglas Gregor96e578d2010-02-05 17:54:41 +00003677 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
3678}
3679
Douglas Gregor62d311f2010-02-09 19:21:46 +00003680TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003681 if (!FromTSI)
3682 return FromTSI;
3683
3684 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00003685 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003686 QualType T = Import(FromTSI->getType());
3687 if (T.isNull())
3688 return 0;
3689
3690 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnara1108e7b2010-05-20 10:00:11 +00003691 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003692}
3693
3694Decl *ASTImporter::Import(Decl *FromD) {
3695 if (!FromD)
3696 return 0;
3697
3698 // Check whether we've already imported this declaration.
3699 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3700 if (Pos != ImportedDecls.end())
3701 return Pos->second;
3702
3703 // Import the type
3704 ASTNodeImporter Importer(*this);
3705 Decl *ToD = Importer.Visit(FromD);
3706 if (!ToD)
3707 return 0;
3708
3709 // Record the imported declaration.
3710 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003711
3712 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3713 // Keep track of anonymous tags that have an associated typedef.
3714 if (FromTag->getTypedefForAnonDecl())
3715 AnonTagsWithPendingTypedefs.push_back(FromTag);
3716 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3717 // When we've finished transforming a typedef, see whether it was the
3718 // typedef for an anonymous tag.
3719 for (llvm::SmallVector<TagDecl *, 4>::iterator
3720 FromTag = AnonTagsWithPendingTypedefs.begin(),
3721 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3722 FromTag != FromTagEnd; ++FromTag) {
3723 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3724 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3725 // We found the typedef for an anonymous tag; link them.
3726 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3727 AnonTagsWithPendingTypedefs.erase(FromTag);
3728 break;
3729 }
3730 }
3731 }
3732 }
3733
Douglas Gregor62d311f2010-02-09 19:21:46 +00003734 return ToD;
3735}
3736
3737DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3738 if (!FromDC)
3739 return FromDC;
3740
3741 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3742}
3743
3744Expr *ASTImporter::Import(Expr *FromE) {
3745 if (!FromE)
3746 return 0;
3747
3748 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3749}
3750
3751Stmt *ASTImporter::Import(Stmt *FromS) {
3752 if (!FromS)
3753 return 0;
3754
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003755 // Check whether we've already imported this declaration.
3756 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3757 if (Pos != ImportedStmts.end())
3758 return Pos->second;
3759
3760 // Import the type
3761 ASTNodeImporter Importer(*this);
3762 Stmt *ToS = Importer.Visit(FromS);
3763 if (!ToS)
3764 return 0;
3765
3766 // Record the imported declaration.
3767 ImportedStmts[FromS] = ToS;
3768 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003769}
3770
3771NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3772 if (!FromNNS)
3773 return 0;
3774
3775 // FIXME: Implement!
3776 return 0;
3777}
3778
Douglas Gregore2e50d332010-12-01 01:36:18 +00003779TemplateName ASTImporter::Import(TemplateName From) {
3780 switch (From.getKind()) {
3781 case TemplateName::Template:
3782 if (TemplateDecl *ToTemplate
3783 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3784 return TemplateName(ToTemplate);
3785
3786 return TemplateName();
3787
3788 case TemplateName::OverloadedTemplate: {
3789 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
3790 UnresolvedSet<2> ToTemplates;
3791 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
3792 E = FromStorage->end();
3793 I != E; ++I) {
3794 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
3795 ToTemplates.addDecl(To);
3796 else
3797 return TemplateName();
3798 }
3799 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
3800 ToTemplates.end());
3801 }
3802
3803 case TemplateName::QualifiedTemplate: {
3804 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
3805 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
3806 if (!Qualifier)
3807 return TemplateName();
3808
3809 if (TemplateDecl *ToTemplate
3810 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3811 return ToContext.getQualifiedTemplateName(Qualifier,
3812 QTN->hasTemplateKeyword(),
3813 ToTemplate);
3814
3815 return TemplateName();
3816 }
3817
3818 case TemplateName::DependentTemplate: {
3819 DependentTemplateName *DTN = From.getAsDependentTemplateName();
3820 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
3821 if (!Qualifier)
3822 return TemplateName();
3823
3824 if (DTN->isIdentifier()) {
3825 return ToContext.getDependentTemplateName(Qualifier,
3826 Import(DTN->getIdentifier()));
3827 }
3828
3829 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
3830 }
3831 }
3832
3833 llvm_unreachable("Invalid template name kind");
3834 return TemplateName();
3835}
3836
Douglas Gregor62d311f2010-02-09 19:21:46 +00003837SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
3838 if (FromLoc.isInvalid())
3839 return SourceLocation();
3840
Douglas Gregor811663e2010-02-10 00:15:17 +00003841 SourceManager &FromSM = FromContext.getSourceManager();
3842
3843 // For now, map everything down to its spelling location, so that we
3844 // don't have to import macro instantiations.
3845 // FIXME: Import macro instantiations!
3846 FromLoc = FromSM.getSpellingLoc(FromLoc);
3847 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3848 SourceManager &ToSM = ToContext.getSourceManager();
3849 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3850 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003851}
3852
3853SourceRange ASTImporter::Import(SourceRange FromRange) {
3854 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
3855}
3856
Douglas Gregor811663e2010-02-10 00:15:17 +00003857FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00003858 llvm::DenseMap<FileID, FileID>::iterator Pos
3859 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00003860 if (Pos != ImportedFileIDs.end())
3861 return Pos->second;
3862
3863 SourceManager &FromSM = FromContext.getSourceManager();
3864 SourceManager &ToSM = ToContext.getSourceManager();
3865 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3866 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3867
3868 // Include location of this file.
3869 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3870
3871 // Map the FileID for to the "to" source manager.
3872 FileID ToID;
3873 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3874 if (Cache->Entry) {
3875 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3876 // disk again
3877 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3878 // than mmap the files several times.
Chris Lattner5159f612010-11-23 08:35:12 +00003879 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00003880 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3881 FromSLoc.getFile().getFileCharacteristic());
3882 } else {
3883 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003884 const llvm::MemoryBuffer *
3885 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00003886 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00003887 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00003888 FromBuf->getBufferIdentifier());
3889 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
3890 }
3891
3892
Sebastian Redl99219f12010-09-30 01:03:06 +00003893 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00003894 return ToID;
3895}
3896
Douglas Gregor96e578d2010-02-05 17:54:41 +00003897DeclarationName ASTImporter::Import(DeclarationName FromName) {
3898 if (!FromName)
3899 return DeclarationName();
3900
3901 switch (FromName.getNameKind()) {
3902 case DeclarationName::Identifier:
3903 return Import(FromName.getAsIdentifierInfo());
3904
3905 case DeclarationName::ObjCZeroArgSelector:
3906 case DeclarationName::ObjCOneArgSelector:
3907 case DeclarationName::ObjCMultiArgSelector:
3908 return Import(FromName.getObjCSelector());
3909
3910 case DeclarationName::CXXConstructorName: {
3911 QualType T = Import(FromName.getCXXNameType());
3912 if (T.isNull())
3913 return DeclarationName();
3914
3915 return ToContext.DeclarationNames.getCXXConstructorName(
3916 ToContext.getCanonicalType(T));
3917 }
3918
3919 case DeclarationName::CXXDestructorName: {
3920 QualType T = Import(FromName.getCXXNameType());
3921 if (T.isNull())
3922 return DeclarationName();
3923
3924 return ToContext.DeclarationNames.getCXXDestructorName(
3925 ToContext.getCanonicalType(T));
3926 }
3927
3928 case DeclarationName::CXXConversionFunctionName: {
3929 QualType T = Import(FromName.getCXXNameType());
3930 if (T.isNull())
3931 return DeclarationName();
3932
3933 return ToContext.DeclarationNames.getCXXConversionFunctionName(
3934 ToContext.getCanonicalType(T));
3935 }
3936
3937 case DeclarationName::CXXOperatorName:
3938 return ToContext.DeclarationNames.getCXXOperatorName(
3939 FromName.getCXXOverloadedOperator());
3940
3941 case DeclarationName::CXXLiteralOperatorName:
3942 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
3943 Import(FromName.getCXXLiteralIdentifier()));
3944
3945 case DeclarationName::CXXUsingDirective:
3946 // FIXME: STATICS!
3947 return DeclarationName::getUsingDirectiveName();
3948 }
3949
3950 // Silence bogus GCC warning
3951 return DeclarationName();
3952}
3953
Douglas Gregore2e50d332010-12-01 01:36:18 +00003954IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00003955 if (!FromId)
3956 return 0;
3957
3958 return &ToContext.Idents.get(FromId->getName());
3959}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003960
Douglas Gregor43f54792010-02-17 02:12:47 +00003961Selector ASTImporter::Import(Selector FromSel) {
3962 if (FromSel.isNull())
3963 return Selector();
3964
3965 llvm::SmallVector<IdentifierInfo *, 4> Idents;
3966 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
3967 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
3968 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
3969 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
3970}
3971
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003972DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
3973 DeclContext *DC,
3974 unsigned IDNS,
3975 NamedDecl **Decls,
3976 unsigned NumDecls) {
3977 return Name;
3978}
3979
3980DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003981 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003982}
3983
3984DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003985 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003986}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003987
3988Decl *ASTImporter::Imported(Decl *From, Decl *To) {
3989 ImportedDecls[From] = To;
3990 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00003991}
Douglas Gregorb4964f72010-02-15 23:54:17 +00003992
3993bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
3994 llvm::DenseMap<Type *, Type *>::iterator Pos
3995 = ImportedTypes.find(From.getTypePtr());
3996 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
3997 return true;
3998
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003999 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004000 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004001}