blob: 5993e9ac34ef584a6642c983147977be5fd22423 [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 Gregorfa7a0e52010-02-10 17:47:19 +000022#include "clang/AST/TypeLoc.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000023#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000024#include "clang/Basic/FileManager.h"
25#include "clang/Basic/SourceManager.h"
26#include "llvm/Support/MemoryBuffer.h"
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);
70 QualType VisitElaboratedType(ElaboratedType *T);
71 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
73 // FIXME: TemplateSpecializationType
74 QualType VisitQualifiedNameType(QualifiedNameType *T);
75 // FIXME: TypenameType
76 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
77 QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000078
79 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000080 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
81 DeclContext *&LexicalDC, DeclarationName &Name,
82 SourceLocation &Loc);
83 bool ImportDeclParts(DeclaratorDecl *D,
84 DeclContext *&DC, DeclContext *&LexicalDC,
85 DeclarationName &Name, SourceLocation &Loc,
86 QualType &T);
Douglas Gregor5c73e912010-02-11 00:48:18 +000087 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregore4c83e42010-02-09 22:48:33 +000088 Decl *VisitDecl(Decl *D);
Douglas Gregor5fa74c32010-02-10 21:10:29 +000089 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +000090 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +000091 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +000092 Decl *VisitFieldDecl(FieldDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000093 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +000094 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor7eeb5972010-02-11 19:21:55 +000095
96 // Importing statements
97 Stmt *VisitStmt(Stmt *S);
98
99 // Importing expressions
100 Expr *VisitExpr(Expr *E);
101 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000102 };
103}
104
105//----------------------------------------------------------------------------
106// Import Types
107//----------------------------------------------------------------------------
108
Douglas Gregore4c83e42010-02-09 22:48:33 +0000109QualType ASTNodeImporter::VisitType(Type *T) {
110 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
111 << T->getTypeClassName();
112 return QualType();
113}
114
Douglas Gregor96e578d2010-02-05 17:54:41 +0000115QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
116 switch (T->getKind()) {
117 case BuiltinType::Void: return Importer.getToContext().VoidTy;
118 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
119
120 case BuiltinType::Char_U:
121 // The context we're importing from has an unsigned 'char'. If we're
122 // importing into a context with a signed 'char', translate to
123 // 'unsigned char' instead.
124 if (Importer.getToContext().getLangOptions().CharIsSigned)
125 return Importer.getToContext().UnsignedCharTy;
126
127 return Importer.getToContext().CharTy;
128
129 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
130
131 case BuiltinType::Char16:
132 // FIXME: Make sure that the "to" context supports C++!
133 return Importer.getToContext().Char16Ty;
134
135 case BuiltinType::Char32:
136 // FIXME: Make sure that the "to" context supports C++!
137 return Importer.getToContext().Char32Ty;
138
139 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
140 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
141 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
142 case BuiltinType::ULongLong:
143 return Importer.getToContext().UnsignedLongLongTy;
144 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
145
146 case BuiltinType::Char_S:
147 // The context we're importing from has an unsigned 'char'. If we're
148 // importing into a context with a signed 'char', translate to
149 // 'unsigned char' instead.
150 if (!Importer.getToContext().getLangOptions().CharIsSigned)
151 return Importer.getToContext().SignedCharTy;
152
153 return Importer.getToContext().CharTy;
154
155 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
156 case BuiltinType::WChar:
157 // FIXME: If not in C++, shall we translate to the C equivalent of
158 // wchar_t?
159 return Importer.getToContext().WCharTy;
160
161 case BuiltinType::Short : return Importer.getToContext().ShortTy;
162 case BuiltinType::Int : return Importer.getToContext().IntTy;
163 case BuiltinType::Long : return Importer.getToContext().LongTy;
164 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
165 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
166 case BuiltinType::Float: return Importer.getToContext().FloatTy;
167 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
168 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
169
170 case BuiltinType::NullPtr:
171 // FIXME: Make sure that the "to" context supports C++0x!
172 return Importer.getToContext().NullPtrTy;
173
174 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
175 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
176 case BuiltinType::UndeducedAuto:
177 // FIXME: Make sure that the "to" context supports C++0x!
178 return Importer.getToContext().UndeducedAutoTy;
179
180 case BuiltinType::ObjCId:
181 // FIXME: Make sure that the "to" context supports Objective-C!
182 return Importer.getToContext().ObjCBuiltinIdTy;
183
184 case BuiltinType::ObjCClass:
185 return Importer.getToContext().ObjCBuiltinClassTy;
186
187 case BuiltinType::ObjCSel:
188 return Importer.getToContext().ObjCBuiltinSelTy;
189 }
190
191 return QualType();
192}
193
194QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
195 QualType ToElementType = Importer.Import(T->getElementType());
196 if (ToElementType.isNull())
197 return QualType();
198
199 return Importer.getToContext().getComplexType(ToElementType);
200}
201
202QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
203 QualType ToPointeeType = Importer.Import(T->getPointeeType());
204 if (ToPointeeType.isNull())
205 return QualType();
206
207 return Importer.getToContext().getPointerType(ToPointeeType);
208}
209
210QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
211 // FIXME: Check for blocks support in "to" context.
212 QualType ToPointeeType = Importer.Import(T->getPointeeType());
213 if (ToPointeeType.isNull())
214 return QualType();
215
216 return Importer.getToContext().getBlockPointerType(ToPointeeType);
217}
218
219QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
220 // FIXME: Check for C++ support in "to" context.
221 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
222 if (ToPointeeType.isNull())
223 return QualType();
224
225 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
226}
227
228QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
229 // FIXME: Check for C++0x support in "to" context.
230 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
231 if (ToPointeeType.isNull())
232 return QualType();
233
234 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
235}
236
237QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
238 // FIXME: Check for C++ support in "to" context.
239 QualType ToPointeeType = Importer.Import(T->getPointeeType());
240 if (ToPointeeType.isNull())
241 return QualType();
242
243 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
244 return Importer.getToContext().getMemberPointerType(ToPointeeType,
245 ClassType.getTypePtr());
246}
247
248QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
249 QualType ToElementType = Importer.Import(T->getElementType());
250 if (ToElementType.isNull())
251 return QualType();
252
253 return Importer.getToContext().getConstantArrayType(ToElementType,
254 T->getSize(),
255 T->getSizeModifier(),
256 T->getIndexTypeCVRQualifiers());
257}
258
259QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
260 QualType ToElementType = Importer.Import(T->getElementType());
261 if (ToElementType.isNull())
262 return QualType();
263
264 return Importer.getToContext().getIncompleteArrayType(ToElementType,
265 T->getSizeModifier(),
266 T->getIndexTypeCVRQualifiers());
267}
268
269QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
270 QualType ToElementType = Importer.Import(T->getElementType());
271 if (ToElementType.isNull())
272 return QualType();
273
274 Expr *Size = Importer.Import(T->getSizeExpr());
275 if (!Size)
276 return QualType();
277
278 SourceRange Brackets = Importer.Import(T->getBracketsRange());
279 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
280 T->getSizeModifier(),
281 T->getIndexTypeCVRQualifiers(),
282 Brackets);
283}
284
285QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
286 QualType ToElementType = Importer.Import(T->getElementType());
287 if (ToElementType.isNull())
288 return QualType();
289
290 return Importer.getToContext().getVectorType(ToElementType,
291 T->getNumElements(),
292 T->isAltiVec(),
293 T->isPixel());
294}
295
296QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
297 QualType ToElementType = Importer.Import(T->getElementType());
298 if (ToElementType.isNull())
299 return QualType();
300
301 return Importer.getToContext().getExtVectorType(ToElementType,
302 T->getNumElements());
303}
304
305QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
306 // FIXME: What happens if we're importing a function without a prototype
307 // into C++? Should we make it variadic?
308 QualType ToResultType = Importer.Import(T->getResultType());
309 if (ToResultType.isNull())
310 return QualType();
311
312 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
313 T->getNoReturnAttr(),
314 T->getCallConv());
315}
316
317QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
318 QualType ToResultType = Importer.Import(T->getResultType());
319 if (ToResultType.isNull())
320 return QualType();
321
322 // Import argument types
323 llvm::SmallVector<QualType, 4> ArgTypes;
324 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
325 AEnd = T->arg_type_end();
326 A != AEnd; ++A) {
327 QualType ArgType = Importer.Import(*A);
328 if (ArgType.isNull())
329 return QualType();
330 ArgTypes.push_back(ArgType);
331 }
332
333 // Import exception types
334 llvm::SmallVector<QualType, 4> ExceptionTypes;
335 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
336 EEnd = T->exception_end();
337 E != EEnd; ++E) {
338 QualType ExceptionType = Importer.Import(*E);
339 if (ExceptionType.isNull())
340 return QualType();
341 ExceptionTypes.push_back(ExceptionType);
342 }
343
344 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
345 ArgTypes.size(),
346 T->isVariadic(),
347 T->getTypeQuals(),
348 T->hasExceptionSpec(),
349 T->hasAnyExceptionSpec(),
350 ExceptionTypes.size(),
351 ExceptionTypes.data(),
352 T->getNoReturnAttr(),
353 T->getCallConv());
354}
355
356QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
357 TypedefDecl *ToDecl
358 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
359 if (!ToDecl)
360 return QualType();
361
362 return Importer.getToContext().getTypeDeclType(ToDecl);
363}
364
365QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
366 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
367 if (!ToExpr)
368 return QualType();
369
370 return Importer.getToContext().getTypeOfExprType(ToExpr);
371}
372
373QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
374 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
375 if (ToUnderlyingType.isNull())
376 return QualType();
377
378 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
379}
380
381QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
382 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
383 if (!ToExpr)
384 return QualType();
385
386 return Importer.getToContext().getDecltypeType(ToExpr);
387}
388
389QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
390 RecordDecl *ToDecl
391 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
392 if (!ToDecl)
393 return QualType();
394
395 return Importer.getToContext().getTagDeclType(ToDecl);
396}
397
398QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
399 EnumDecl *ToDecl
400 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
401 if (!ToDecl)
402 return QualType();
403
404 return Importer.getToContext().getTagDeclType(ToDecl);
405}
406
407QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
408 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
409 if (ToUnderlyingType.isNull())
410 return QualType();
411
412 return Importer.getToContext().getElaboratedType(ToUnderlyingType,
413 T->getTagKind());
414}
415
416QualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) {
417 NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier());
418 if (!ToQualifier)
419 return QualType();
420
421 QualType ToNamedType = Importer.Import(T->getNamedType());
422 if (ToNamedType.isNull())
423 return QualType();
424
425 return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType);
426}
427
428QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
429 ObjCInterfaceDecl *Class
430 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
431 if (!Class)
432 return QualType();
433
434 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
435 for (ObjCInterfaceType::qual_iterator P = T->qual_begin(),
436 PEnd = T->qual_end();
437 P != PEnd; ++P) {
438 ObjCProtocolDecl *Protocol
439 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
440 if (!Protocol)
441 return QualType();
442 Protocols.push_back(Protocol);
443 }
444
445 return Importer.getToContext().getObjCInterfaceType(Class,
446 Protocols.data(),
447 Protocols.size());
448}
449
450QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
451 QualType ToPointeeType = Importer.Import(T->getPointeeType());
452 if (ToPointeeType.isNull())
453 return QualType();
454
455 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
456 for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(),
457 PEnd = T->qual_end();
458 P != PEnd; ++P) {
459 ObjCProtocolDecl *Protocol
460 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
461 if (!Protocol)
462 return QualType();
463 Protocols.push_back(Protocol);
464 }
465
466 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType,
467 Protocols.data(),
468 Protocols.size());
469}
470
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000471//----------------------------------------------------------------------------
472// Import Declarations
473//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000474bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
475 DeclContext *&LexicalDC,
476 DeclarationName &Name,
477 SourceLocation &Loc) {
478 // Import the context of this declaration.
479 DC = Importer.ImportContext(D->getDeclContext());
480 if (!DC)
481 return true;
482
483 LexicalDC = DC;
484 if (D->getDeclContext() != D->getLexicalDeclContext()) {
485 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
486 if (!LexicalDC)
487 return true;
488 }
489
490 // Import the name of this declaration.
491 Name = Importer.Import(D->getDeclName());
492 if (D->getDeclName() && !Name)
493 return true;
494
495 // Import the location of this declaration.
496 Loc = Importer.Import(D->getLocation());
497 return false;
498}
499
500bool ASTNodeImporter::ImportDeclParts(DeclaratorDecl *D,
501 DeclContext *&DC,
502 DeclContext *&LexicalDC,
503 DeclarationName &Name,
504 SourceLocation &Loc,
505 QualType &T) {
506 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
507 return true;
508
509 // Import the type of this declaration.
510 T = Importer.Import(D->getType());
511 if (T.isNull())
512 return true;
513
514 return false;
515}
516
Douglas Gregor5c73e912010-02-11 00:48:18 +0000517bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
518 RecordDecl *ToRecord) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000519 if (FromRecord->isUnion() != ToRecord->isUnion()) {
520 Importer.ToDiag(ToRecord->getLocation(),
521 diag::warn_odr_class_type_inconsistent)
522 << Importer.getToContext().getTypeDeclType(ToRecord);
523 Importer.FromDiag(FromRecord->getLocation(), diag::note_odr_tag_kind_here)
524 << FromRecord->getDeclName() << (unsigned)FromRecord->getTagKind();
Douglas Gregor5c73e912010-02-11 00:48:18 +0000525 return false;
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000526 }
527
Douglas Gregor5c73e912010-02-11 00:48:18 +0000528 if (CXXRecordDecl *FromCXX = dyn_cast<CXXRecordDecl>(FromRecord)) {
529 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(ToRecord)) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000530 if (FromCXX->getNumBases() != ToCXX->getNumBases()) {
531 Importer.ToDiag(ToRecord->getLocation(),
532 diag::warn_odr_class_type_inconsistent)
533 << Importer.getToContext().getTypeDeclType(ToRecord);
534 Importer.ToDiag(ToRecord->getLocation(), diag::note_odr_number_of_bases)
535 << ToCXX->getNumBases();
536 Importer.FromDiag(FromRecord->getLocation(),
537 diag::note_odr_number_of_bases)
538 << FromCXX->getNumBases();
Douglas Gregor5c73e912010-02-11 00:48:18 +0000539 return false;
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000540 }
541
Douglas Gregor5c73e912010-02-11 00:48:18 +0000542 // Check the base classes.
543 for (CXXRecordDecl::base_class_iterator FromBase = FromCXX->bases_begin(),
544 FromBaseEnd = FromCXX->bases_end(),
545 ToBase = ToCXX->bases_begin();
546 FromBase != FromBaseEnd;
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000547 ++FromBase, ++ToBase) {
Douglas Gregor5c73e912010-02-11 00:48:18 +0000548 // Check the type we're inheriting from.
549 QualType FromBaseT = Importer.Import(FromBase->getType());
550 if (FromBaseT.isNull())
551 return false;
552
553 if (!Importer.getToContext().typesAreCompatible(FromBaseT,
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000554 ToBase->getType())) {
555 Importer.ToDiag(ToRecord->getLocation(),
556 diag::warn_odr_class_type_inconsistent)
557 << Importer.getToContext().getTypeDeclType(ToRecord);
558 Importer.ToDiag(ToBase->getSourceRange().getBegin(),
559 diag::note_odr_base)
560 << ToBase->getType()
561 << ToBase->getSourceRange();
562 Importer.FromDiag(FromBase->getSourceRange().getBegin(),
563 diag::note_odr_base)
564 << FromBase->getType()
565 << FromBase->getSourceRange();
Douglas Gregor5c73e912010-02-11 00:48:18 +0000566 return false;
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000567 }
568
569 // Check virtual vs. non-virtual inheritance mismatch.
570 if (FromBase->isVirtual() != ToBase->isVirtual()) {
571 Importer.ToDiag(ToRecord->getLocation(),
572 diag::warn_odr_class_type_inconsistent)
573 << Importer.getToContext().getTypeDeclType(ToRecord);
574 Importer.ToDiag(ToBase->getSourceRange().getBegin(),
575 diag::note_odr_virtual_base)
576 << ToBase->isVirtual() << ToBase->getSourceRange();
577 Importer.FromDiag(FromBase->getSourceRange().getBegin(),
578 diag::note_odr_base)
579 << FromBase->isVirtual()
580 << FromBase->getSourceRange();
581 return false;
582 }
Douglas Gregor5c73e912010-02-11 00:48:18 +0000583 }
584 } else if (FromCXX->getNumBases() > 0) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000585 Importer.ToDiag(ToRecord->getLocation(),
586 diag::warn_odr_class_type_inconsistent)
587 << Importer.getToContext().getTypeDeclType(ToRecord);
588 const CXXBaseSpecifier *FromBase = FromCXX->bases_begin();
589 Importer.FromDiag(FromBase->getSourceRange().getBegin(),
590 diag::note_odr_base)
591 << FromBase->getType()
592 << FromBase->getSourceRange();
593 Importer.ToDiag(ToRecord->getLocation(), diag::note_odr_missing_base);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000594 return false;
595 }
596 }
597
598 // Check the fields for consistency.
599 CXXRecordDecl::field_iterator ToField = ToRecord->field_begin(),
600 ToFieldEnd = ToRecord->field_end();
601 for (CXXRecordDecl::field_iterator FromField = FromRecord->field_begin(),
602 FromFieldEnd = FromRecord->field_end();
603 FromField != FromFieldEnd;
604 ++FromField, ++ToField) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000605 if (ToField == ToFieldEnd) {
606 Importer.ToDiag(ToRecord->getLocation(),
607 diag::warn_odr_class_type_inconsistent)
608 << Importer.getToContext().getTypeDeclType(ToRecord);
609 Importer.FromDiag(FromField->getLocation(), diag::note_odr_field)
610 << FromField->getDeclName() << FromField->getType();
611 Importer.ToDiag(ToRecord->getLocation(), diag::note_odr_missing_field);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000612 return false;
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000613 }
614
Douglas Gregor5c73e912010-02-11 00:48:18 +0000615 QualType FromT = Importer.Import(FromField->getType());
616 if (FromT.isNull())
617 return false;
618
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000619 if (!Importer.getToContext().typesAreCompatible(FromT, ToField->getType())){
620 Importer.ToDiag(ToRecord->getLocation(),
621 diag::warn_odr_class_type_inconsistent)
622 << Importer.getToContext().getTypeDeclType(ToRecord);
623 Importer.ToDiag(ToField->getLocation(), diag::note_odr_field)
624 << ToField->getDeclName() << ToField->getType();
625 Importer.FromDiag(FromField->getLocation(), diag::note_odr_field)
626 << FromField->getDeclName() << FromField->getType();
Douglas Gregor5c73e912010-02-11 00:48:18 +0000627 return false;
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000628 }
Douglas Gregor5c73e912010-02-11 00:48:18 +0000629
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000630 if (FromField->isBitField() != ToField->isBitField()) {
631 Importer.ToDiag(ToRecord->getLocation(),
632 diag::warn_odr_class_type_inconsistent)
633 << Importer.getToContext().getTypeDeclType(ToRecord);
634 if (FromField->isBitField()) {
635 llvm::APSInt Bits;
636 FromField->getBitWidth()->isIntegerConstantExpr(Bits,
637 Importer.getFromContext());
638 Importer.FromDiag(FromField->getLocation(), diag::note_odr_bit_field)
639 << FromField->getDeclName() << FromField->getType()
640 << Bits.toString(10, false);
641 Importer.ToDiag(ToField->getLocation(), diag::note_odr_not_bit_field)
642 << ToField->getDeclName();
643 } else {
644 llvm::APSInt Bits;
645 ToField->getBitWidth()->isIntegerConstantExpr(Bits,
646 Importer.getToContext());
647 Importer.ToDiag(ToField->getLocation(), diag::note_odr_bit_field)
648 << ToField->getDeclName() << ToField->getType()
649 << Bits.toString(10, false);
650 Importer.FromDiag(FromField->getLocation(),
651 diag::note_odr_not_bit_field)
652 << FromField->getDeclName();
653 }
Douglas Gregor5c73e912010-02-11 00:48:18 +0000654 return false;
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000655 }
656
Douglas Gregor5c73e912010-02-11 00:48:18 +0000657 if (FromField->isBitField()) {
658 // Make sure that the bit-fields are the same length.
659 llvm::APSInt FromBits, ToBits;
660 if (!FromField->getBitWidth()->isIntegerConstantExpr(FromBits,
661 Importer.getFromContext()))
662 return false;
663 if (!ToField->getBitWidth()->isIntegerConstantExpr(ToBits,
664 Importer.getToContext()))
665 return false;
666
667 if (FromBits.getBitWidth() > ToBits.getBitWidth())
668 ToBits.extend(FromBits.getBitWidth());
669 else if (ToBits.getBitWidth() > FromBits.getBitWidth())
670 FromBits.extend(ToBits.getBitWidth());
671
672 FromBits.setIsUnsigned(true);
673 ToBits.setIsUnsigned(true);
674
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000675 if (FromBits != ToBits) {
676 Importer.ToDiag(ToRecord->getLocation(),
677 diag::warn_odr_class_type_inconsistent)
678 << Importer.getToContext().getTypeDeclType(ToRecord);
679 Importer.ToDiag(ToField->getLocation(), diag::note_odr_bit_field)
680 << ToField->getDeclName() << ToField->getType()
681 << ToBits.toString(10, false);
682 Importer.FromDiag(FromField->getLocation(), diag::note_odr_bit_field)
683 << FromField->getDeclName() << FromField->getType()
684 << FromBits.toString(10, false);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000685 return false;
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000686 }
Douglas Gregor5c73e912010-02-11 00:48:18 +0000687 }
688 }
689
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000690 if (ToField != ToFieldEnd) {
691 Importer.ToDiag(ToRecord->getLocation(),
692 diag::warn_odr_class_type_inconsistent)
693 << Importer.getToContext().getTypeDeclType(ToRecord);
694 Importer.ToDiag(ToField->getLocation(), diag::note_odr_field)
695 << ToField->getDeclName() << ToField->getType();
696 Importer.FromDiag(FromRecord->getLocation(), diag::note_odr_missing_field);
697 return false;
698 }
699
700 return true;
Douglas Gregor5c73e912010-02-11 00:48:18 +0000701}
702
Douglas Gregore4c83e42010-02-09 22:48:33 +0000703Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +0000704 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +0000705 << D->getDeclKindName();
706 return 0;
707}
708
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000709Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
710 // Import the major distinguishing characteristics of this typedef.
711 DeclContext *DC, *LexicalDC;
712 DeclarationName Name;
713 SourceLocation Loc;
714 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
715 return 0;
716
717 // Import the underlying type of this typedef;
718 QualType T = Importer.Import(D->getUnderlyingType());
719 if (T.isNull())
720 return 0;
721
722 // If this typedef is not in block scope, determine whether we've
723 // seen a typedef with the same name (that we can merge with) or any
724 // other entity by that name (which name lookup could conflict with).
725 if (!DC->isFunctionOrMethod()) {
726 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
727 unsigned IDNS = Decl::IDNS_Ordinary;
728 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
729 Lookup.first != Lookup.second;
730 ++Lookup.first) {
731 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
732 continue;
733 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
734 if (Importer.getToContext().typesAreCompatible(T,
735 FoundTypedef->getUnderlyingType())) {
736 Importer.getImportedDecls()[D] = FoundTypedef;
737 return FoundTypedef;
738 }
739 }
740
741 ConflictingDecls.push_back(*Lookup.first);
742 }
743
744 if (!ConflictingDecls.empty()) {
745 Name = Importer.HandleNameConflict(Name, DC, IDNS,
746 ConflictingDecls.data(),
747 ConflictingDecls.size());
748 if (!Name)
749 return 0;
750 }
751 }
752
753 // Create the new typedef node.
754 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
755 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
756 Loc, Name.getAsIdentifierInfo(),
757 TInfo);
758 ToTypedef->setLexicalDeclContext(LexicalDC);
759 Importer.getImportedDecls()[D] = ToTypedef;
760 LexicalDC->addDecl(ToTypedef);
761 return ToTypedef;
762}
763
Douglas Gregor5c73e912010-02-11 00:48:18 +0000764Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
765 // If this record has a definition in the translation unit we're coming from,
766 // but this particular declaration is not that definition, import the
767 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000768 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +0000769 if (Definition && Definition != D) {
770 Decl *ImportedDef = Importer.Import(Definition);
771 Importer.getImportedDecls()[D] = ImportedDef;
772 return ImportedDef;
773 }
774
775 // Import the major distinguishing characteristics of this record.
776 DeclContext *DC, *LexicalDC;
777 DeclarationName Name;
778 SourceLocation Loc;
779 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
780 return 0;
781
782 // Figure out what structure name we're looking for.
783 unsigned IDNS = Decl::IDNS_Tag;
784 DeclarationName SearchName = Name;
785 if (!SearchName && D->getTypedefForAnonDecl()) {
786 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
787 IDNS = Decl::IDNS_Ordinary;
788 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
789 IDNS |= Decl::IDNS_Ordinary;
790
791 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +0000792 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +0000793 if (!DC->isFunctionOrMethod() && SearchName) {
794 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
795 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
796 Lookup.first != Lookup.second;
797 ++Lookup.first) {
798 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
799 continue;
800
801 Decl *Found = *Lookup.first;
802 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
803 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
804 Found = Tag->getDecl();
805 }
806
807 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +0000808 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
809 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
810 // The record types structurally match, or the "from" translation
811 // unit only had a forward declaration anyway; call it the same
812 // function.
813 // FIXME: For C++, we should also merge methods here.
814 Importer.getImportedDecls()[D] = FoundDef;
815 return FoundDef;
816 }
817 } else {
818 // We have a forward declaration of this type, so adopt that forward
819 // declaration rather than building a new one.
820 AdoptDecl = FoundRecord;
821 continue;
822 }
Douglas Gregor5c73e912010-02-11 00:48:18 +0000823 }
824
825 ConflictingDecls.push_back(*Lookup.first);
826 }
827
828 if (!ConflictingDecls.empty()) {
829 Name = Importer.HandleNameConflict(Name, DC, IDNS,
830 ConflictingDecls.data(),
831 ConflictingDecls.size());
832 }
833 }
834
835 // Create the record declaration.
Douglas Gregor25791052010-02-12 00:09:27 +0000836 RecordDecl *ToRecord = AdoptDecl;
837 if (!ToRecord) {
838 if (CXXRecordDecl *FromCXX = dyn_cast<CXXRecordDecl>(D)) {
839 CXXRecordDecl *ToCXX = CXXRecordDecl::Create(Importer.getToContext(),
840 D->getTagKind(),
841 DC, Loc,
842 Name.getAsIdentifierInfo(),
Douglas Gregor5c73e912010-02-11 00:48:18 +0000843 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor25791052010-02-12 00:09:27 +0000844 ToRecord = ToCXX;
845
846 if (D->isDefinition()) {
847 // Add base classes.
848 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
849 for (CXXRecordDecl::base_class_iterator
850 FromBase = FromCXX->bases_begin(),
851 FromBaseEnd = FromCXX->bases_end();
852 FromBase != FromBaseEnd;
853 ++FromBase) {
854 QualType T = Importer.Import(FromBase->getType());
855 if (T.isNull())
856 return 0;
857
858 Bases.push_back(
859 new (Importer.getToContext())
Douglas Gregor5c73e912010-02-11 00:48:18 +0000860 CXXBaseSpecifier(Importer.Import(FromBase->getSourceRange()),
861 FromBase->isVirtual(),
862 FromBase->isBaseOfClass(),
863 FromBase->getAccessSpecifierAsWritten(),
864 T));
Douglas Gregor25791052010-02-12 00:09:27 +0000865 }
866 if (!Bases.empty())
867 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregor5c73e912010-02-11 00:48:18 +0000868 }
Douglas Gregor25791052010-02-12 00:09:27 +0000869 } else {
870 ToRecord = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
871 DC, Loc,
872 Name.getAsIdentifierInfo(),
873 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor5c73e912010-02-11 00:48:18 +0000874 }
Douglas Gregor25791052010-02-12 00:09:27 +0000875 ToRecord->setLexicalDeclContext(LexicalDC);
876 LexicalDC->addDecl(ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000877 }
Douglas Gregor5c73e912010-02-11 00:48:18 +0000878 Importer.getImportedDecls()[D] = ToRecord;
Douglas Gregor25791052010-02-12 00:09:27 +0000879
Douglas Gregor5c73e912010-02-11 00:48:18 +0000880 if (D->isDefinition()) {
881 ToRecord->startDefinition();
882 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
883 FromMemEnd = D->decls_end();
884 FromMem != FromMemEnd;
885 ++FromMem)
886 Importer.Import(*FromMem);
887
Douglas Gregord5058122010-02-11 01:19:42 +0000888 ToRecord->completeDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +0000889 }
890
891 return ToRecord;
892}
893
894
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000895Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
896 // Import the major distinguishing characteristics of this function.
897 DeclContext *DC, *LexicalDC;
898 DeclarationName Name;
899 QualType T;
900 SourceLocation Loc;
901 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc, T))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000902 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000903
904 // Try to find a function in our own ("to") context with the same name, same
905 // type, and in the same context as the function we're importing.
906 if (!LexicalDC->isFunctionOrMethod()) {
907 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
908 unsigned IDNS = Decl::IDNS_Ordinary;
909 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
910 Lookup.first != Lookup.second;
911 ++Lookup.first) {
912 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
913 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000914
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000915 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
916 if (isExternalLinkage(FoundFunction->getLinkage()) &&
917 isExternalLinkage(D->getLinkage())) {
918 if (Importer.getToContext().typesAreCompatible(T,
919 FoundFunction->getType())) {
920 // FIXME: Actually try to merge the body and other attributes.
921 Importer.getImportedDecls()[D] = FoundFunction;
922 return FoundFunction;
923 }
924
925 // FIXME: Check for overloading more carefully, e.g., by boosting
926 // Sema::IsOverload out to the AST library.
927
928 // Function overloading is okay in C++.
929 if (Importer.getToContext().getLangOptions().CPlusPlus)
930 continue;
931
932 // Complain about inconsistent function types.
933 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
934 << Name << T << FoundFunction->getType();
935 Importer.ToDiag(FoundFunction->getLocation(),
936 diag::note_odr_value_here)
937 << FoundFunction->getType();
938 }
939 }
940
941 ConflictingDecls.push_back(*Lookup.first);
942 }
943
944 if (!ConflictingDecls.empty()) {
945 Name = Importer.HandleNameConflict(Name, DC, IDNS,
946 ConflictingDecls.data(),
947 ConflictingDecls.size());
948 if (!Name)
949 return 0;
950 }
Douglas Gregor62d311f2010-02-09 19:21:46 +0000951 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000952
953 // Import the function parameters.
954 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
955 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
956 P != PEnd; ++P) {
957 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
958 if (!ToP)
959 return 0;
960
961 Parameters.push_back(ToP);
962 }
963
964 // Create the imported function.
965 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
966 FunctionDecl *ToFunction
967 = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
968 Name, T, TInfo, D->getStorageClass(),
969 D->isInlineSpecified(),
970 D->hasWrittenPrototype());
971 ToFunction->setLexicalDeclContext(LexicalDC);
972 Importer.getImportedDecls()[D] = ToFunction;
973 LexicalDC->addDecl(ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +0000974
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000975 // Set the parameters.
976 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
977 Parameters[I]->setOwningFunction(ToFunction);
978 ToFunction->addDecl(Parameters[I]);
979 }
Douglas Gregord5058122010-02-11 01:19:42 +0000980 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000981
982 // FIXME: Other bits to merge?
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000983
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000984 return ToFunction;
985}
986
Douglas Gregor5c73e912010-02-11 00:48:18 +0000987Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
988 // Import the major distinguishing characteristics of a variable.
989 DeclContext *DC, *LexicalDC;
990 DeclarationName Name;
991 QualType T;
992 SourceLocation Loc;
993 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc, T))
994 return 0;
995
996 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
997 Expr *BitWidth = Importer.Import(D->getBitWidth());
998 if (!BitWidth && D->getBitWidth())
999 return 0;
1000
1001 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
1002 Loc, Name.getAsIdentifierInfo(),
1003 T, TInfo, BitWidth, D->isMutable());
1004 ToField->setLexicalDeclContext(LexicalDC);
1005 Importer.getImportedDecls()[D] = ToField;
1006 LexicalDC->addDecl(ToField);
1007 return ToField;
1008}
1009
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001010Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
1011 // Import the major distinguishing characteristics of a variable.
1012 DeclContext *DC, *LexicalDC;
1013 DeclarationName Name;
1014 QualType T;
1015 SourceLocation Loc;
1016 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc, T))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001017 return 0;
1018
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001019 // Try to find a variable in our own ("to") context with the same name and
1020 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00001021 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001022 VarDecl *MergeWithVar = 0;
1023 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1024 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00001025 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001026 Lookup.first != Lookup.second;
1027 ++Lookup.first) {
1028 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1029 continue;
1030
1031 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
1032 // We have found a variable that we may need to merge with. Check it.
1033 if (isExternalLinkage(FoundVar->getLinkage()) &&
1034 isExternalLinkage(D->getLinkage())) {
1035 if (Importer.getToContext().typesAreCompatible(T,
1036 FoundVar->getType())) {
1037 MergeWithVar = FoundVar;
1038 break;
1039 }
1040
Douglas Gregor2fbe5582010-02-10 17:16:49 +00001041 if (const IncompleteArrayType *FoundArray
1042 = Importer.getToContext().getAsIncompleteArrayType(
1043 FoundVar->getType())) {
1044 if (const ConstantArrayType *TArray
1045 = Importer.getToContext().getAsConstantArrayType(T)) {
1046 if (Importer.getToContext().typesAreCompatible(
1047 TArray->getElementType(),
1048 FoundArray->getElementType())) {
1049 FoundVar->setType(T);
1050 MergeWithVar = FoundVar;
1051 break;
1052 }
1053 }
1054 } else if (const IncompleteArrayType *TArray
1055 = Importer.getToContext().getAsIncompleteArrayType(T)) {
1056 if (const ConstantArrayType *FoundArray
1057 = Importer.getToContext().getAsConstantArrayType(
1058 FoundVar->getType())) {
1059 if (Importer.getToContext().typesAreCompatible(
1060 TArray->getElementType(),
1061 FoundArray->getElementType())) {
1062 MergeWithVar = FoundVar;
1063 break;
1064 }
1065 }
1066 }
1067
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001068 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
1069 << Name << T << FoundVar->getType();
1070 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
1071 << FoundVar->getType();
1072 }
1073 }
1074
1075 ConflictingDecls.push_back(*Lookup.first);
1076 }
1077
1078 if (MergeWithVar) {
1079 // An equivalent variable with external linkage has been found. Link
1080 // the two declarations, then merge them.
1081 Importer.getImportedDecls()[D] = MergeWithVar;
1082
1083 if (VarDecl *DDef = D->getDefinition()) {
1084 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
1085 Importer.ToDiag(ExistingDef->getLocation(),
1086 diag::err_odr_variable_multiple_def)
1087 << Name;
1088 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
1089 } else {
1090 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00001091 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001092 }
1093 }
1094
1095 return MergeWithVar;
1096 }
1097
1098 if (!ConflictingDecls.empty()) {
1099 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1100 ConflictingDecls.data(),
1101 ConflictingDecls.size());
1102 if (!Name)
1103 return 0;
1104 }
1105 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00001106
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001107 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00001108 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001109 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
1110 Name.getAsIdentifierInfo(), T, TInfo,
1111 D->getStorageClass());
Douglas Gregor62d311f2010-02-09 19:21:46 +00001112 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001113 Importer.getImportedDecls()[D] = ToVar;
Douglas Gregor62d311f2010-02-09 19:21:46 +00001114 LexicalDC->addDecl(ToVar);
1115
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001116 // Merge the initializer.
1117 // FIXME: Can we really import any initializer? Alternatively, we could force
1118 // ourselves to import every declaration of a variable and then only use
1119 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00001120 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001121
1122 // FIXME: Other bits to merge?
1123
1124 return ToVar;
1125}
1126
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001127Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
1128 // Parameters are created in the translation unit's context, then moved
1129 // into the function declaration's context afterward.
1130 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
1131
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00001132 // Import the name of this declaration.
1133 DeclarationName Name = Importer.Import(D->getDeclName());
1134 if (D->getDeclName() && !Name)
1135 return 0;
1136
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001137 // Import the location of this declaration.
1138 SourceLocation Loc = Importer.Import(D->getLocation());
1139
1140 // Import the parameter's type.
1141 QualType T = Importer.Import(D->getType());
1142 if (T.isNull())
1143 return 0;
1144
1145 // Create the imported parameter.
1146 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1147 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
1148 Loc, Name.getAsIdentifierInfo(),
1149 T, TInfo, D->getStorageClass(),
1150 /*FIXME: Default argument*/ 0);
1151 Importer.getImportedDecls()[D] = ToParm;
1152 return ToParm;
1153}
1154
Douglas Gregor7eeb5972010-02-11 19:21:55 +00001155//----------------------------------------------------------------------------
1156// Import Statements
1157//----------------------------------------------------------------------------
1158
1159Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
1160 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
1161 << S->getStmtClassName();
1162 return 0;
1163}
1164
1165//----------------------------------------------------------------------------
1166// Import Expressions
1167//----------------------------------------------------------------------------
1168Expr *ASTNodeImporter::VisitExpr(Expr *E) {
1169 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
1170 << E->getStmtClassName();
1171 return 0;
1172}
1173
1174Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
1175 QualType T = Importer.Import(E->getType());
1176 if (T.isNull())
1177 return 0;
1178
1179 return new (Importer.getToContext())
1180 IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
1181}
1182
1183ASTImporter::ASTImporter(Diagnostic &Diags,
1184 ASTContext &ToContext, FileManager &ToFileManager,
1185 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001186 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor811663e2010-02-10 00:15:17 +00001187 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Douglas Gregor7eeb5972010-02-11 19:21:55 +00001188 Diags(Diags) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00001189 ImportedDecls[FromContext.getTranslationUnitDecl()]
1190 = ToContext.getTranslationUnitDecl();
1191}
1192
1193ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001194
1195QualType ASTImporter::Import(QualType FromT) {
1196 if (FromT.isNull())
1197 return QualType();
1198
Douglas Gregorf65bbb32010-02-08 15:18:58 +00001199 // Check whether we've already imported this type.
1200 llvm::DenseMap<Type *, Type *>::iterator Pos
1201 = ImportedTypes.find(FromT.getTypePtr());
1202 if (Pos != ImportedTypes.end())
1203 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001204
Douglas Gregorf65bbb32010-02-08 15:18:58 +00001205 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00001206 ASTNodeImporter Importer(*this);
1207 QualType ToT = Importer.Visit(FromT.getTypePtr());
1208 if (ToT.isNull())
1209 return ToT;
1210
Douglas Gregorf65bbb32010-02-08 15:18:58 +00001211 // Record the imported type.
1212 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
1213
Douglas Gregor96e578d2010-02-05 17:54:41 +00001214 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
1215}
1216
Douglas Gregor62d311f2010-02-09 19:21:46 +00001217TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00001218 if (!FromTSI)
1219 return FromTSI;
1220
1221 // FIXME: For now we just create a "trivial" type source info based
1222 // on the type and a seingle location. Implement a real version of
1223 // this.
1224 QualType T = Import(FromTSI->getType());
1225 if (T.isNull())
1226 return 0;
1227
1228 return ToContext.getTrivialTypeSourceInfo(T,
1229 FromTSI->getTypeLoc().getFullSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00001230}
1231
1232Decl *ASTImporter::Import(Decl *FromD) {
1233 if (!FromD)
1234 return 0;
1235
1236 // Check whether we've already imported this declaration.
1237 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
1238 if (Pos != ImportedDecls.end())
1239 return Pos->second;
1240
1241 // Import the type
1242 ASTNodeImporter Importer(*this);
1243 Decl *ToD = Importer.Visit(FromD);
1244 if (!ToD)
1245 return 0;
1246
1247 // Record the imported declaration.
1248 ImportedDecls[FromD] = ToD;
1249 return ToD;
1250}
1251
1252DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
1253 if (!FromDC)
1254 return FromDC;
1255
1256 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
1257}
1258
1259Expr *ASTImporter::Import(Expr *FromE) {
1260 if (!FromE)
1261 return 0;
1262
1263 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
1264}
1265
1266Stmt *ASTImporter::Import(Stmt *FromS) {
1267 if (!FromS)
1268 return 0;
1269
Douglas Gregor7eeb5972010-02-11 19:21:55 +00001270 // Check whether we've already imported this declaration.
1271 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
1272 if (Pos != ImportedStmts.end())
1273 return Pos->second;
1274
1275 // Import the type
1276 ASTNodeImporter Importer(*this);
1277 Stmt *ToS = Importer.Visit(FromS);
1278 if (!ToS)
1279 return 0;
1280
1281 // Record the imported declaration.
1282 ImportedStmts[FromS] = ToS;
1283 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00001284}
1285
1286NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
1287 if (!FromNNS)
1288 return 0;
1289
1290 // FIXME: Implement!
1291 return 0;
1292}
1293
1294SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
1295 if (FromLoc.isInvalid())
1296 return SourceLocation();
1297
Douglas Gregor811663e2010-02-10 00:15:17 +00001298 SourceManager &FromSM = FromContext.getSourceManager();
1299
1300 // For now, map everything down to its spelling location, so that we
1301 // don't have to import macro instantiations.
1302 // FIXME: Import macro instantiations!
1303 FromLoc = FromSM.getSpellingLoc(FromLoc);
1304 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
1305 SourceManager &ToSM = ToContext.getSourceManager();
1306 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
1307 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00001308}
1309
1310SourceRange ASTImporter::Import(SourceRange FromRange) {
1311 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
1312}
1313
Douglas Gregor811663e2010-02-10 00:15:17 +00001314FileID ASTImporter::Import(FileID FromID) {
1315 llvm::DenseMap<unsigned, FileID>::iterator Pos
1316 = ImportedFileIDs.find(FromID.getHashValue());
1317 if (Pos != ImportedFileIDs.end())
1318 return Pos->second;
1319
1320 SourceManager &FromSM = FromContext.getSourceManager();
1321 SourceManager &ToSM = ToContext.getSourceManager();
1322 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
1323 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
1324
1325 // Include location of this file.
1326 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
1327
1328 // Map the FileID for to the "to" source manager.
1329 FileID ToID;
1330 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
1331 if (Cache->Entry) {
1332 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
1333 // disk again
1334 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
1335 // than mmap the files several times.
1336 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
1337 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
1338 FromSLoc.getFile().getFileCharacteristic());
1339 } else {
1340 // FIXME: We want to re-use the existing MemoryBuffer!
1341 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer();
1342 llvm::MemoryBuffer *ToBuf
1343 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(),
1344 FromBuf->getBufferEnd(),
1345 FromBuf->getBufferIdentifier());
1346 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
1347 }
1348
1349
1350 ImportedFileIDs[FromID.getHashValue()] = ToID;
1351 return ToID;
1352}
1353
Douglas Gregor96e578d2010-02-05 17:54:41 +00001354DeclarationName ASTImporter::Import(DeclarationName FromName) {
1355 if (!FromName)
1356 return DeclarationName();
1357
1358 switch (FromName.getNameKind()) {
1359 case DeclarationName::Identifier:
1360 return Import(FromName.getAsIdentifierInfo());
1361
1362 case DeclarationName::ObjCZeroArgSelector:
1363 case DeclarationName::ObjCOneArgSelector:
1364 case DeclarationName::ObjCMultiArgSelector:
1365 return Import(FromName.getObjCSelector());
1366
1367 case DeclarationName::CXXConstructorName: {
1368 QualType T = Import(FromName.getCXXNameType());
1369 if (T.isNull())
1370 return DeclarationName();
1371
1372 return ToContext.DeclarationNames.getCXXConstructorName(
1373 ToContext.getCanonicalType(T));
1374 }
1375
1376 case DeclarationName::CXXDestructorName: {
1377 QualType T = Import(FromName.getCXXNameType());
1378 if (T.isNull())
1379 return DeclarationName();
1380
1381 return ToContext.DeclarationNames.getCXXDestructorName(
1382 ToContext.getCanonicalType(T));
1383 }
1384
1385 case DeclarationName::CXXConversionFunctionName: {
1386 QualType T = Import(FromName.getCXXNameType());
1387 if (T.isNull())
1388 return DeclarationName();
1389
1390 return ToContext.DeclarationNames.getCXXConversionFunctionName(
1391 ToContext.getCanonicalType(T));
1392 }
1393
1394 case DeclarationName::CXXOperatorName:
1395 return ToContext.DeclarationNames.getCXXOperatorName(
1396 FromName.getCXXOverloadedOperator());
1397
1398 case DeclarationName::CXXLiteralOperatorName:
1399 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
1400 Import(FromName.getCXXLiteralIdentifier()));
1401
1402 case DeclarationName::CXXUsingDirective:
1403 // FIXME: STATICS!
1404 return DeclarationName::getUsingDirectiveName();
1405 }
1406
1407 // Silence bogus GCC warning
1408 return DeclarationName();
1409}
1410
1411IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
1412 if (!FromId)
1413 return 0;
1414
1415 return &ToContext.Idents.get(FromId->getName());
1416}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001417
1418DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
1419 DeclContext *DC,
1420 unsigned IDNS,
1421 NamedDecl **Decls,
1422 unsigned NumDecls) {
1423 return Name;
1424}
1425
1426DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +00001427 return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
1428 DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001429}
1430
1431DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +00001432 return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
1433 DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001434}