blob: ea5b68f162699de457ff6aa0966461e4ec71bede [file] [log] [blame]
Douglas Gregor1b2949d2010-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 Gregor88523732010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor96a01b42010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor089459a2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor4800d952010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor82fc4bf2010-02-10 17:47:19 +000022#include "clang/AST/TypeLoc.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000023#include "clang/AST/TypeVisitor.h"
Douglas Gregor88523732010-02-10 00:15:17 +000024#include "clang/Basic/FileManager.h"
25#include "clang/Basic/SourceManager.h"
26#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000027
28using namespace clang;
29
30namespace {
Douglas Gregor089459a2010-02-08 21:09:39 +000031 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor4800d952010-02-11 19:21:55 +000032 public DeclVisitor<ASTNodeImporter, Decl *>,
33 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor1b2949d2010-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 Gregor9bed8792010-02-09 19:21:46 +000040 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor4800d952010-02-11 19:21:55 +000041 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor1b2949d2010-02-05 17:54:41 +000042
43 // Importing types
Douglas Gregor89cc9d62010-02-09 22:48:33 +000044 QualType VisitType(Type *T);
Douglas Gregor1b2949d2010-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 Gregor089459a2010-02-08 21:09:39 +000078
79 // Importing declarations
Douglas Gregora404ea62010-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 Gregor96a01b42010-02-11 00:48:18 +000087 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor89cc9d62010-02-09 22:48:33 +000088 Decl *VisitDecl(Decl *D);
Douglas Gregor9e5d9962010-02-10 21:10:29 +000089 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +000090 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +000091 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +000092 Decl *VisitFieldDecl(FieldDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +000093 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +000094 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor4800d952010-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 Gregor1b2949d2010-02-05 17:54:41 +0000102 };
103}
104
105//----------------------------------------------------------------------------
106// Import Types
107//----------------------------------------------------------------------------
108
Douglas Gregor89cc9d62010-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 Gregor1b2949d2010-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 Gregor089459a2010-02-08 21:09:39 +0000471//----------------------------------------------------------------------------
472// Import Declarations
473//----------------------------------------------------------------------------
Douglas Gregora404ea62010-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 Gregor96a01b42010-02-11 00:48:18 +0000517bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
518 RecordDecl *ToRecord) {
Douglas Gregor4800d952010-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 Gregor96a01b42010-02-11 00:48:18 +0000525 return false;
Douglas Gregor4800d952010-02-11 19:21:55 +0000526 }
527
Douglas Gregor96a01b42010-02-11 00:48:18 +0000528 if (CXXRecordDecl *FromCXX = dyn_cast<CXXRecordDecl>(FromRecord)) {
529 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(ToRecord)) {
Douglas Gregor4800d952010-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 Gregor96a01b42010-02-11 00:48:18 +0000539 return false;
Douglas Gregor4800d952010-02-11 19:21:55 +0000540 }
541
Douglas Gregor96a01b42010-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 Gregor4800d952010-02-11 19:21:55 +0000547 ++FromBase, ++ToBase) {
Douglas Gregor96a01b42010-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 Gregor4800d952010-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 Gregor96a01b42010-02-11 00:48:18 +0000566 return false;
Douglas Gregor4800d952010-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 Gregor96a01b42010-02-11 00:48:18 +0000583 }
584 } else if (FromCXX->getNumBases() > 0) {
Douglas Gregor4800d952010-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 Gregor96a01b42010-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 Gregor4800d952010-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 Gregor96a01b42010-02-11 00:48:18 +0000612 return false;
Douglas Gregor4800d952010-02-11 19:21:55 +0000613 }
614
Douglas Gregor96a01b42010-02-11 00:48:18 +0000615 QualType FromT = Importer.Import(FromField->getType());
616 if (FromT.isNull())
617 return false;
618
Douglas Gregor4800d952010-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 Gregor96a01b42010-02-11 00:48:18 +0000627 return false;
Douglas Gregor4800d952010-02-11 19:21:55 +0000628 }
Douglas Gregor96a01b42010-02-11 00:48:18 +0000629
Douglas Gregor4800d952010-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 Gregor96a01b42010-02-11 00:48:18 +0000654 return false;
Douglas Gregor4800d952010-02-11 19:21:55 +0000655 }
656
Douglas Gregor96a01b42010-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 Gregor4800d952010-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 Gregor96a01b42010-02-11 00:48:18 +0000685 return false;
Douglas Gregor4800d952010-02-11 19:21:55 +0000686 }
Douglas Gregor96a01b42010-02-11 00:48:18 +0000687 }
688 }
689
Douglas Gregor4800d952010-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 Gregor96a01b42010-02-11 00:48:18 +0000701}
702
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000703Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +0000704 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000705 << D->getDeclKindName();
706 return 0;
707}
708
Douglas Gregor9e5d9962010-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 Gregor96a01b42010-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 Gregor952b0172010-02-11 01:04:33 +0000768 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-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 Gregore72b5dc2010-02-12 00:09:27 +0000792 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-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 Gregore72b5dc2010-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 Gregor96a01b42010-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 Gregore72b5dc2010-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 Gregor96a01b42010-02-11 00:48:18 +0000843 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregore72b5dc2010-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 Gregor96a01b42010-02-11 00:48:18 +0000860 CXXBaseSpecifier(Importer.Import(FromBase->getSourceRange()),
861 FromBase->isVirtual(),
862 FromBase->isBaseOfClass(),
863 FromBase->getAccessSpecifierAsWritten(),
864 T));
Douglas Gregore72b5dc2010-02-12 00:09:27 +0000865 }
866 if (!Bases.empty())
867 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregor96a01b42010-02-11 00:48:18 +0000868 }
Douglas Gregore72b5dc2010-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 Gregor96a01b42010-02-11 00:48:18 +0000874 }
Douglas Gregore72b5dc2010-02-12 00:09:27 +0000875 ToRecord->setLexicalDeclContext(LexicalDC);
876 LexicalDC->addDecl(ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000877 }
Douglas Gregor96a01b42010-02-11 00:48:18 +0000878 Importer.getImportedDecls()[D] = ToRecord;
Douglas Gregore72b5dc2010-02-12 00:09:27 +0000879
Douglas Gregor96a01b42010-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 Gregor838db382010-02-11 01:19:42 +0000888 ToRecord->completeDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +0000889 }
890
891 return ToRecord;
892}
893
894
Douglas Gregora404ea62010-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 Gregor089459a2010-02-08 21:09:39 +0000902 return 0;
Douglas Gregora404ea62010-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 Gregor089459a2010-02-08 21:09:39 +0000914
Douglas Gregora404ea62010-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 Gregor9bed8792010-02-09 19:21:46 +0000951 }
Douglas Gregora404ea62010-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 Gregor9bed8792010-02-09 19:21:46 +0000974
Douglas Gregora404ea62010-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 Gregor838db382010-02-11 01:19:42 +0000980 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +0000981
982 // FIXME: Other bits to merge?
Douglas Gregor089459a2010-02-08 21:09:39 +0000983
Douglas Gregora404ea62010-02-10 19:54:31 +0000984 return ToFunction;
985}
986
Douglas Gregor96a01b42010-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 Gregora404ea62010-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 Gregor089459a2010-02-08 21:09:39 +00001017 return 0;
1018
Douglas Gregor089459a2010-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 Gregor9bed8792010-02-09 19:21:46 +00001021 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00001022 VarDecl *MergeWithVar = 0;
1023 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1024 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00001025 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-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 Gregord0145422010-02-12 17:23:39 +00001041 const ArrayType *FoundArray
1042 = Importer.getToContext().getAsArrayType(FoundVar->getType());
1043 const ArrayType *TArray
1044 = Importer.getToContext().getAsArrayType(T);
1045 if (FoundArray && TArray) {
1046 if (isa<IncompleteArrayType>(FoundArray) &&
1047 isa<ConstantArrayType>(TArray)) {
1048 FoundVar->setType(T);
1049 MergeWithVar = FoundVar;
1050 break;
1051 } else if (isa<IncompleteArrayType>(TArray) &&
1052 isa<ConstantArrayType>(FoundArray)) {
1053 MergeWithVar = FoundVar;
1054 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00001055 }
1056 }
1057
Douglas Gregor089459a2010-02-08 21:09:39 +00001058 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
1059 << Name << T << FoundVar->getType();
1060 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
1061 << FoundVar->getType();
1062 }
1063 }
1064
1065 ConflictingDecls.push_back(*Lookup.first);
1066 }
1067
1068 if (MergeWithVar) {
1069 // An equivalent variable with external linkage has been found. Link
1070 // the two declarations, then merge them.
1071 Importer.getImportedDecls()[D] = MergeWithVar;
1072
1073 if (VarDecl *DDef = D->getDefinition()) {
1074 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
1075 Importer.ToDiag(ExistingDef->getLocation(),
1076 diag::err_odr_variable_multiple_def)
1077 << Name;
1078 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
1079 } else {
1080 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00001081 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00001082 }
1083 }
1084
1085 return MergeWithVar;
1086 }
1087
1088 if (!ConflictingDecls.empty()) {
1089 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1090 ConflictingDecls.data(),
1091 ConflictingDecls.size());
1092 if (!Name)
1093 return 0;
1094 }
1095 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00001096
Douglas Gregor089459a2010-02-08 21:09:39 +00001097 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00001098 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor089459a2010-02-08 21:09:39 +00001099 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
1100 Name.getAsIdentifierInfo(), T, TInfo,
1101 D->getStorageClass());
Douglas Gregor9bed8792010-02-09 19:21:46 +00001102 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor089459a2010-02-08 21:09:39 +00001103 Importer.getImportedDecls()[D] = ToVar;
Douglas Gregor9bed8792010-02-09 19:21:46 +00001104 LexicalDC->addDecl(ToVar);
1105
Douglas Gregor089459a2010-02-08 21:09:39 +00001106 // Merge the initializer.
1107 // FIXME: Can we really import any initializer? Alternatively, we could force
1108 // ourselves to import every declaration of a variable and then only use
1109 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00001110 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00001111
1112 // FIXME: Other bits to merge?
1113
1114 return ToVar;
1115}
1116
Douglas Gregora404ea62010-02-10 19:54:31 +00001117Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
1118 // Parameters are created in the translation unit's context, then moved
1119 // into the function declaration's context afterward.
1120 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
1121
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00001122 // Import the name of this declaration.
1123 DeclarationName Name = Importer.Import(D->getDeclName());
1124 if (D->getDeclName() && !Name)
1125 return 0;
1126
Douglas Gregora404ea62010-02-10 19:54:31 +00001127 // Import the location of this declaration.
1128 SourceLocation Loc = Importer.Import(D->getLocation());
1129
1130 // Import the parameter's type.
1131 QualType T = Importer.Import(D->getType());
1132 if (T.isNull())
1133 return 0;
1134
1135 // Create the imported parameter.
1136 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1137 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
1138 Loc, Name.getAsIdentifierInfo(),
1139 T, TInfo, D->getStorageClass(),
1140 /*FIXME: Default argument*/ 0);
1141 Importer.getImportedDecls()[D] = ToParm;
1142 return ToParm;
1143}
1144
Douglas Gregor4800d952010-02-11 19:21:55 +00001145//----------------------------------------------------------------------------
1146// Import Statements
1147//----------------------------------------------------------------------------
1148
1149Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
1150 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
1151 << S->getStmtClassName();
1152 return 0;
1153}
1154
1155//----------------------------------------------------------------------------
1156// Import Expressions
1157//----------------------------------------------------------------------------
1158Expr *ASTNodeImporter::VisitExpr(Expr *E) {
1159 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
1160 << E->getStmtClassName();
1161 return 0;
1162}
1163
1164Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
1165 QualType T = Importer.Import(E->getType());
1166 if (T.isNull())
1167 return 0;
1168
1169 return new (Importer.getToContext())
1170 IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
1171}
1172
1173ASTImporter::ASTImporter(Diagnostic &Diags,
1174 ASTContext &ToContext, FileManager &ToFileManager,
1175 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001176 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor88523732010-02-10 00:15:17 +00001177 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Douglas Gregor4800d952010-02-11 19:21:55 +00001178 Diags(Diags) {
Douglas Gregor9bed8792010-02-09 19:21:46 +00001179 ImportedDecls[FromContext.getTranslationUnitDecl()]
1180 = ToContext.getTranslationUnitDecl();
1181}
1182
1183ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001184
1185QualType ASTImporter::Import(QualType FromT) {
1186 if (FromT.isNull())
1187 return QualType();
1188
Douglas Gregor169fba52010-02-08 15:18:58 +00001189 // Check whether we've already imported this type.
1190 llvm::DenseMap<Type *, Type *>::iterator Pos
1191 = ImportedTypes.find(FromT.getTypePtr());
1192 if (Pos != ImportedTypes.end())
1193 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001194
Douglas Gregor169fba52010-02-08 15:18:58 +00001195 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001196 ASTNodeImporter Importer(*this);
1197 QualType ToT = Importer.Visit(FromT.getTypePtr());
1198 if (ToT.isNull())
1199 return ToT;
1200
Douglas Gregor169fba52010-02-08 15:18:58 +00001201 // Record the imported type.
1202 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
1203
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001204 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
1205}
1206
Douglas Gregor9bed8792010-02-09 19:21:46 +00001207TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00001208 if (!FromTSI)
1209 return FromTSI;
1210
1211 // FIXME: For now we just create a "trivial" type source info based
1212 // on the type and a seingle location. Implement a real version of
1213 // this.
1214 QualType T = Import(FromTSI->getType());
1215 if (T.isNull())
1216 return 0;
1217
1218 return ToContext.getTrivialTypeSourceInfo(T,
1219 FromTSI->getTypeLoc().getFullSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00001220}
1221
1222Decl *ASTImporter::Import(Decl *FromD) {
1223 if (!FromD)
1224 return 0;
1225
1226 // Check whether we've already imported this declaration.
1227 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
1228 if (Pos != ImportedDecls.end())
1229 return Pos->second;
1230
1231 // Import the type
1232 ASTNodeImporter Importer(*this);
1233 Decl *ToD = Importer.Visit(FromD);
1234 if (!ToD)
1235 return 0;
1236
1237 // Record the imported declaration.
1238 ImportedDecls[FromD] = ToD;
1239 return ToD;
1240}
1241
1242DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
1243 if (!FromDC)
1244 return FromDC;
1245
1246 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
1247}
1248
1249Expr *ASTImporter::Import(Expr *FromE) {
1250 if (!FromE)
1251 return 0;
1252
1253 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
1254}
1255
1256Stmt *ASTImporter::Import(Stmt *FromS) {
1257 if (!FromS)
1258 return 0;
1259
Douglas Gregor4800d952010-02-11 19:21:55 +00001260 // Check whether we've already imported this declaration.
1261 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
1262 if (Pos != ImportedStmts.end())
1263 return Pos->second;
1264
1265 // Import the type
1266 ASTNodeImporter Importer(*this);
1267 Stmt *ToS = Importer.Visit(FromS);
1268 if (!ToS)
1269 return 0;
1270
1271 // Record the imported declaration.
1272 ImportedStmts[FromS] = ToS;
1273 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00001274}
1275
1276NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
1277 if (!FromNNS)
1278 return 0;
1279
1280 // FIXME: Implement!
1281 return 0;
1282}
1283
1284SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
1285 if (FromLoc.isInvalid())
1286 return SourceLocation();
1287
Douglas Gregor88523732010-02-10 00:15:17 +00001288 SourceManager &FromSM = FromContext.getSourceManager();
1289
1290 // For now, map everything down to its spelling location, so that we
1291 // don't have to import macro instantiations.
1292 // FIXME: Import macro instantiations!
1293 FromLoc = FromSM.getSpellingLoc(FromLoc);
1294 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
1295 SourceManager &ToSM = ToContext.getSourceManager();
1296 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
1297 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00001298}
1299
1300SourceRange ASTImporter::Import(SourceRange FromRange) {
1301 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
1302}
1303
Douglas Gregor88523732010-02-10 00:15:17 +00001304FileID ASTImporter::Import(FileID FromID) {
1305 llvm::DenseMap<unsigned, FileID>::iterator Pos
1306 = ImportedFileIDs.find(FromID.getHashValue());
1307 if (Pos != ImportedFileIDs.end())
1308 return Pos->second;
1309
1310 SourceManager &FromSM = FromContext.getSourceManager();
1311 SourceManager &ToSM = ToContext.getSourceManager();
1312 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
1313 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
1314
1315 // Include location of this file.
1316 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
1317
1318 // Map the FileID for to the "to" source manager.
1319 FileID ToID;
1320 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
1321 if (Cache->Entry) {
1322 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
1323 // disk again
1324 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
1325 // than mmap the files several times.
1326 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
1327 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
1328 FromSLoc.getFile().getFileCharacteristic());
1329 } else {
1330 // FIXME: We want to re-use the existing MemoryBuffer!
1331 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer();
1332 llvm::MemoryBuffer *ToBuf
1333 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(),
1334 FromBuf->getBufferEnd(),
1335 FromBuf->getBufferIdentifier());
1336 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
1337 }
1338
1339
1340 ImportedFileIDs[FromID.getHashValue()] = ToID;
1341 return ToID;
1342}
1343
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001344DeclarationName ASTImporter::Import(DeclarationName FromName) {
1345 if (!FromName)
1346 return DeclarationName();
1347
1348 switch (FromName.getNameKind()) {
1349 case DeclarationName::Identifier:
1350 return Import(FromName.getAsIdentifierInfo());
1351
1352 case DeclarationName::ObjCZeroArgSelector:
1353 case DeclarationName::ObjCOneArgSelector:
1354 case DeclarationName::ObjCMultiArgSelector:
1355 return Import(FromName.getObjCSelector());
1356
1357 case DeclarationName::CXXConstructorName: {
1358 QualType T = Import(FromName.getCXXNameType());
1359 if (T.isNull())
1360 return DeclarationName();
1361
1362 return ToContext.DeclarationNames.getCXXConstructorName(
1363 ToContext.getCanonicalType(T));
1364 }
1365
1366 case DeclarationName::CXXDestructorName: {
1367 QualType T = Import(FromName.getCXXNameType());
1368 if (T.isNull())
1369 return DeclarationName();
1370
1371 return ToContext.DeclarationNames.getCXXDestructorName(
1372 ToContext.getCanonicalType(T));
1373 }
1374
1375 case DeclarationName::CXXConversionFunctionName: {
1376 QualType T = Import(FromName.getCXXNameType());
1377 if (T.isNull())
1378 return DeclarationName();
1379
1380 return ToContext.DeclarationNames.getCXXConversionFunctionName(
1381 ToContext.getCanonicalType(T));
1382 }
1383
1384 case DeclarationName::CXXOperatorName:
1385 return ToContext.DeclarationNames.getCXXOperatorName(
1386 FromName.getCXXOverloadedOperator());
1387
1388 case DeclarationName::CXXLiteralOperatorName:
1389 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
1390 Import(FromName.getCXXLiteralIdentifier()));
1391
1392 case DeclarationName::CXXUsingDirective:
1393 // FIXME: STATICS!
1394 return DeclarationName::getUsingDirectiveName();
1395 }
1396
1397 // Silence bogus GCC warning
1398 return DeclarationName();
1399}
1400
1401IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
1402 if (!FromId)
1403 return 0;
1404
1405 return &ToContext.Idents.get(FromId->getName());
1406}
Douglas Gregor089459a2010-02-08 21:09:39 +00001407
1408DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
1409 DeclContext *DC,
1410 unsigned IDNS,
1411 NamedDecl **Decls,
1412 unsigned NumDecls) {
1413 return Name;
1414}
1415
1416DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor4800d952010-02-11 19:21:55 +00001417 return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
1418 DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00001419}
1420
1421DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor4800d952010-02-11 19:21:55 +00001422 return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
1423 DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00001424}