blob: 5201f29b9036fc52434c5d2dd4030f37837d91ef [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 Gregor1b2949d2010-02-05 17:54:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor089459a2010-02-08 21:09:39 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor82fc4bf2010-02-10 17:47:19 +000020#include "clang/AST/TypeLoc.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000021#include "clang/AST/TypeVisitor.h"
Douglas Gregor88523732010-02-10 00:15:17 +000022#include "clang/Basic/FileManager.h"
23#include "clang/Basic/SourceManager.h"
24#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000025
26using namespace clang;
27
28namespace {
Douglas Gregor089459a2010-02-08 21:09:39 +000029 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
30 public DeclVisitor<ASTNodeImporter, Decl *> {
Douglas Gregor1b2949d2010-02-05 17:54:41 +000031 ASTImporter &Importer;
32
33 public:
34 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
35
36 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor9bed8792010-02-09 19:21:46 +000037 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor1b2949d2010-02-05 17:54:41 +000038
39 // Importing types
Douglas Gregor89cc9d62010-02-09 22:48:33 +000040 QualType VisitType(Type *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000041 QualType VisitBuiltinType(BuiltinType *T);
42 QualType VisitComplexType(ComplexType *T);
43 QualType VisitPointerType(PointerType *T);
44 QualType VisitBlockPointerType(BlockPointerType *T);
45 QualType VisitLValueReferenceType(LValueReferenceType *T);
46 QualType VisitRValueReferenceType(RValueReferenceType *T);
47 QualType VisitMemberPointerType(MemberPointerType *T);
48 QualType VisitConstantArrayType(ConstantArrayType *T);
49 QualType VisitIncompleteArrayType(IncompleteArrayType *T);
50 QualType VisitVariableArrayType(VariableArrayType *T);
51 // FIXME: DependentSizedArrayType
52 // FIXME: DependentSizedExtVectorType
53 QualType VisitVectorType(VectorType *T);
54 QualType VisitExtVectorType(ExtVectorType *T);
55 QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
56 QualType VisitFunctionProtoType(FunctionProtoType *T);
57 // FIXME: UnresolvedUsingType
58 QualType VisitTypedefType(TypedefType *T);
59 QualType VisitTypeOfExprType(TypeOfExprType *T);
60 // FIXME: DependentTypeOfExprType
61 QualType VisitTypeOfType(TypeOfType *T);
62 QualType VisitDecltypeType(DecltypeType *T);
63 // FIXME: DependentDecltypeType
64 QualType VisitRecordType(RecordType *T);
65 QualType VisitEnumType(EnumType *T);
66 QualType VisitElaboratedType(ElaboratedType *T);
67 // FIXME: TemplateTypeParmType
68 // FIXME: SubstTemplateTypeParmType
69 // FIXME: TemplateSpecializationType
70 QualType VisitQualifiedNameType(QualifiedNameType *T);
71 // FIXME: TypenameType
72 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
73 QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000074
75 // Importing declarations
Douglas Gregor89cc9d62010-02-09 22:48:33 +000076 Decl *VisitDecl(Decl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +000077 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor82fc4bf2010-02-10 17:47:19 +000078 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000079 };
80}
81
82//----------------------------------------------------------------------------
83// Import Types
84//----------------------------------------------------------------------------
85
Douglas Gregor89cc9d62010-02-09 22:48:33 +000086QualType ASTNodeImporter::VisitType(Type *T) {
87 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
88 << T->getTypeClassName();
89 return QualType();
90}
91
Douglas Gregor1b2949d2010-02-05 17:54:41 +000092QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
93 switch (T->getKind()) {
94 case BuiltinType::Void: return Importer.getToContext().VoidTy;
95 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
96
97 case BuiltinType::Char_U:
98 // The context we're importing from has an unsigned 'char'. If we're
99 // importing into a context with a signed 'char', translate to
100 // 'unsigned char' instead.
101 if (Importer.getToContext().getLangOptions().CharIsSigned)
102 return Importer.getToContext().UnsignedCharTy;
103
104 return Importer.getToContext().CharTy;
105
106 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
107
108 case BuiltinType::Char16:
109 // FIXME: Make sure that the "to" context supports C++!
110 return Importer.getToContext().Char16Ty;
111
112 case BuiltinType::Char32:
113 // FIXME: Make sure that the "to" context supports C++!
114 return Importer.getToContext().Char32Ty;
115
116 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
117 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
118 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
119 case BuiltinType::ULongLong:
120 return Importer.getToContext().UnsignedLongLongTy;
121 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
122
123 case BuiltinType::Char_S:
124 // The context we're importing from has an unsigned 'char'. If we're
125 // importing into a context with a signed 'char', translate to
126 // 'unsigned char' instead.
127 if (!Importer.getToContext().getLangOptions().CharIsSigned)
128 return Importer.getToContext().SignedCharTy;
129
130 return Importer.getToContext().CharTy;
131
132 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
133 case BuiltinType::WChar:
134 // FIXME: If not in C++, shall we translate to the C equivalent of
135 // wchar_t?
136 return Importer.getToContext().WCharTy;
137
138 case BuiltinType::Short : return Importer.getToContext().ShortTy;
139 case BuiltinType::Int : return Importer.getToContext().IntTy;
140 case BuiltinType::Long : return Importer.getToContext().LongTy;
141 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
142 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
143 case BuiltinType::Float: return Importer.getToContext().FloatTy;
144 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
145 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
146
147 case BuiltinType::NullPtr:
148 // FIXME: Make sure that the "to" context supports C++0x!
149 return Importer.getToContext().NullPtrTy;
150
151 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
152 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
153 case BuiltinType::UndeducedAuto:
154 // FIXME: Make sure that the "to" context supports C++0x!
155 return Importer.getToContext().UndeducedAutoTy;
156
157 case BuiltinType::ObjCId:
158 // FIXME: Make sure that the "to" context supports Objective-C!
159 return Importer.getToContext().ObjCBuiltinIdTy;
160
161 case BuiltinType::ObjCClass:
162 return Importer.getToContext().ObjCBuiltinClassTy;
163
164 case BuiltinType::ObjCSel:
165 return Importer.getToContext().ObjCBuiltinSelTy;
166 }
167
168 return QualType();
169}
170
171QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
172 QualType ToElementType = Importer.Import(T->getElementType());
173 if (ToElementType.isNull())
174 return QualType();
175
176 return Importer.getToContext().getComplexType(ToElementType);
177}
178
179QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
180 QualType ToPointeeType = Importer.Import(T->getPointeeType());
181 if (ToPointeeType.isNull())
182 return QualType();
183
184 return Importer.getToContext().getPointerType(ToPointeeType);
185}
186
187QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
188 // FIXME: Check for blocks support in "to" context.
189 QualType ToPointeeType = Importer.Import(T->getPointeeType());
190 if (ToPointeeType.isNull())
191 return QualType();
192
193 return Importer.getToContext().getBlockPointerType(ToPointeeType);
194}
195
196QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
197 // FIXME: Check for C++ support in "to" context.
198 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
199 if (ToPointeeType.isNull())
200 return QualType();
201
202 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
203}
204
205QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
206 // FIXME: Check for C++0x support in "to" context.
207 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
208 if (ToPointeeType.isNull())
209 return QualType();
210
211 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
212}
213
214QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
215 // FIXME: Check for C++ support in "to" context.
216 QualType ToPointeeType = Importer.Import(T->getPointeeType());
217 if (ToPointeeType.isNull())
218 return QualType();
219
220 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
221 return Importer.getToContext().getMemberPointerType(ToPointeeType,
222 ClassType.getTypePtr());
223}
224
225QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
226 QualType ToElementType = Importer.Import(T->getElementType());
227 if (ToElementType.isNull())
228 return QualType();
229
230 return Importer.getToContext().getConstantArrayType(ToElementType,
231 T->getSize(),
232 T->getSizeModifier(),
233 T->getIndexTypeCVRQualifiers());
234}
235
236QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
237 QualType ToElementType = Importer.Import(T->getElementType());
238 if (ToElementType.isNull())
239 return QualType();
240
241 return Importer.getToContext().getIncompleteArrayType(ToElementType,
242 T->getSizeModifier(),
243 T->getIndexTypeCVRQualifiers());
244}
245
246QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
247 QualType ToElementType = Importer.Import(T->getElementType());
248 if (ToElementType.isNull())
249 return QualType();
250
251 Expr *Size = Importer.Import(T->getSizeExpr());
252 if (!Size)
253 return QualType();
254
255 SourceRange Brackets = Importer.Import(T->getBracketsRange());
256 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
257 T->getSizeModifier(),
258 T->getIndexTypeCVRQualifiers(),
259 Brackets);
260}
261
262QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
263 QualType ToElementType = Importer.Import(T->getElementType());
264 if (ToElementType.isNull())
265 return QualType();
266
267 return Importer.getToContext().getVectorType(ToElementType,
268 T->getNumElements(),
269 T->isAltiVec(),
270 T->isPixel());
271}
272
273QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
274 QualType ToElementType = Importer.Import(T->getElementType());
275 if (ToElementType.isNull())
276 return QualType();
277
278 return Importer.getToContext().getExtVectorType(ToElementType,
279 T->getNumElements());
280}
281
282QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
283 // FIXME: What happens if we're importing a function without a prototype
284 // into C++? Should we make it variadic?
285 QualType ToResultType = Importer.Import(T->getResultType());
286 if (ToResultType.isNull())
287 return QualType();
288
289 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
290 T->getNoReturnAttr(),
291 T->getCallConv());
292}
293
294QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
295 QualType ToResultType = Importer.Import(T->getResultType());
296 if (ToResultType.isNull())
297 return QualType();
298
299 // Import argument types
300 llvm::SmallVector<QualType, 4> ArgTypes;
301 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
302 AEnd = T->arg_type_end();
303 A != AEnd; ++A) {
304 QualType ArgType = Importer.Import(*A);
305 if (ArgType.isNull())
306 return QualType();
307 ArgTypes.push_back(ArgType);
308 }
309
310 // Import exception types
311 llvm::SmallVector<QualType, 4> ExceptionTypes;
312 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
313 EEnd = T->exception_end();
314 E != EEnd; ++E) {
315 QualType ExceptionType = Importer.Import(*E);
316 if (ExceptionType.isNull())
317 return QualType();
318 ExceptionTypes.push_back(ExceptionType);
319 }
320
321 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
322 ArgTypes.size(),
323 T->isVariadic(),
324 T->getTypeQuals(),
325 T->hasExceptionSpec(),
326 T->hasAnyExceptionSpec(),
327 ExceptionTypes.size(),
328 ExceptionTypes.data(),
329 T->getNoReturnAttr(),
330 T->getCallConv());
331}
332
333QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
334 TypedefDecl *ToDecl
335 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
336 if (!ToDecl)
337 return QualType();
338
339 return Importer.getToContext().getTypeDeclType(ToDecl);
340}
341
342QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
343 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
344 if (!ToExpr)
345 return QualType();
346
347 return Importer.getToContext().getTypeOfExprType(ToExpr);
348}
349
350QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
351 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
352 if (ToUnderlyingType.isNull())
353 return QualType();
354
355 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
356}
357
358QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
359 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
360 if (!ToExpr)
361 return QualType();
362
363 return Importer.getToContext().getDecltypeType(ToExpr);
364}
365
366QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
367 RecordDecl *ToDecl
368 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
369 if (!ToDecl)
370 return QualType();
371
372 return Importer.getToContext().getTagDeclType(ToDecl);
373}
374
375QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
376 EnumDecl *ToDecl
377 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
378 if (!ToDecl)
379 return QualType();
380
381 return Importer.getToContext().getTagDeclType(ToDecl);
382}
383
384QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
385 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
386 if (ToUnderlyingType.isNull())
387 return QualType();
388
389 return Importer.getToContext().getElaboratedType(ToUnderlyingType,
390 T->getTagKind());
391}
392
393QualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) {
394 NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier());
395 if (!ToQualifier)
396 return QualType();
397
398 QualType ToNamedType = Importer.Import(T->getNamedType());
399 if (ToNamedType.isNull())
400 return QualType();
401
402 return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType);
403}
404
405QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
406 ObjCInterfaceDecl *Class
407 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
408 if (!Class)
409 return QualType();
410
411 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
412 for (ObjCInterfaceType::qual_iterator P = T->qual_begin(),
413 PEnd = T->qual_end();
414 P != PEnd; ++P) {
415 ObjCProtocolDecl *Protocol
416 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
417 if (!Protocol)
418 return QualType();
419 Protocols.push_back(Protocol);
420 }
421
422 return Importer.getToContext().getObjCInterfaceType(Class,
423 Protocols.data(),
424 Protocols.size());
425}
426
427QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
428 QualType ToPointeeType = Importer.Import(T->getPointeeType());
429 if (ToPointeeType.isNull())
430 return QualType();
431
432 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
433 for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(),
434 PEnd = T->qual_end();
435 P != PEnd; ++P) {
436 ObjCProtocolDecl *Protocol
437 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
438 if (!Protocol)
439 return QualType();
440 Protocols.push_back(Protocol);
441 }
442
443 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType,
444 Protocols.data(),
445 Protocols.size());
446}
447
Douglas Gregor089459a2010-02-08 21:09:39 +0000448//----------------------------------------------------------------------------
449// Import Declarations
450//----------------------------------------------------------------------------
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000451Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +0000452 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000453 << D->getDeclKindName();
454 return 0;
455}
456
Douglas Gregor089459a2010-02-08 21:09:39 +0000457Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
458 // Import the context of this declaration.
459 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
460 if (!DC)
461 return 0;
462
Douglas Gregor9bed8792010-02-09 19:21:46 +0000463 DeclContext *LexicalDC = DC;
464 if (D->getDeclContext() != D->getLexicalDeclContext()) {
465 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
466 if (!LexicalDC)
467 return 0;
468 }
469
Douglas Gregor089459a2010-02-08 21:09:39 +0000470 // Import the name of this declaration.
471 DeclarationName Name = Importer.Import(D->getDeclName());
472 if (D->getDeclName() && !Name)
473 return 0;
474
475 // Import the type of this declaration.
476 QualType T = Importer.Import(D->getType());
477 if (T.isNull())
478 return 0;
479
480 // Import the location of this declaration.
481 SourceLocation Loc = Importer.Import(D->getLocation());
482
483 // Try to find a variable in our own ("to") context with the same name and
484 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +0000485 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +0000486 VarDecl *MergeWithVar = 0;
487 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
488 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +0000489 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +0000490 Lookup.first != Lookup.second;
491 ++Lookup.first) {
492 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
493 continue;
494
495 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
496 // We have found a variable that we may need to merge with. Check it.
497 if (isExternalLinkage(FoundVar->getLinkage()) &&
498 isExternalLinkage(D->getLinkage())) {
499 if (Importer.getToContext().typesAreCompatible(T,
500 FoundVar->getType())) {
501 MergeWithVar = FoundVar;
502 break;
503 }
504
Douglas Gregor0f962a82010-02-10 17:16:49 +0000505 if (const IncompleteArrayType *FoundArray
506 = Importer.getToContext().getAsIncompleteArrayType(
507 FoundVar->getType())) {
508 if (const ConstantArrayType *TArray
509 = Importer.getToContext().getAsConstantArrayType(T)) {
510 if (Importer.getToContext().typesAreCompatible(
511 TArray->getElementType(),
512 FoundArray->getElementType())) {
513 FoundVar->setType(T);
514 MergeWithVar = FoundVar;
515 break;
516 }
517 }
518 } else if (const IncompleteArrayType *TArray
519 = Importer.getToContext().getAsIncompleteArrayType(T)) {
520 if (const ConstantArrayType *FoundArray
521 = Importer.getToContext().getAsConstantArrayType(
522 FoundVar->getType())) {
523 if (Importer.getToContext().typesAreCompatible(
524 TArray->getElementType(),
525 FoundArray->getElementType())) {
526 MergeWithVar = FoundVar;
527 break;
528 }
529 }
530 }
531
Douglas Gregor089459a2010-02-08 21:09:39 +0000532 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
533 << Name << T << FoundVar->getType();
534 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
535 << FoundVar->getType();
536 }
537 }
538
539 ConflictingDecls.push_back(*Lookup.first);
540 }
541
542 if (MergeWithVar) {
543 // An equivalent variable with external linkage has been found. Link
544 // the two declarations, then merge them.
545 Importer.getImportedDecls()[D] = MergeWithVar;
546
547 if (VarDecl *DDef = D->getDefinition()) {
548 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
549 Importer.ToDiag(ExistingDef->getLocation(),
550 diag::err_odr_variable_multiple_def)
551 << Name;
552 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
553 } else {
554 Expr *Init = Importer.Import(DDef->getInit());
555 MergeWithVar->setInit(Importer.getToContext(), Init);
556 }
557 }
558
559 return MergeWithVar;
560 }
561
562 if (!ConflictingDecls.empty()) {
563 Name = Importer.HandleNameConflict(Name, DC, IDNS,
564 ConflictingDecls.data(),
565 ConflictingDecls.size());
566 if (!Name)
567 return 0;
568 }
569 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +0000570
Douglas Gregor089459a2010-02-08 21:09:39 +0000571 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +0000572 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor089459a2010-02-08 21:09:39 +0000573 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
574 Name.getAsIdentifierInfo(), T, TInfo,
575 D->getStorageClass());
Douglas Gregor9bed8792010-02-09 19:21:46 +0000576 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor089459a2010-02-08 21:09:39 +0000577 Importer.getImportedDecls()[D] = ToVar;
Douglas Gregor9bed8792010-02-09 19:21:46 +0000578 LexicalDC->addDecl(ToVar);
579
Douglas Gregor089459a2010-02-08 21:09:39 +0000580 // Merge the initializer.
581 // FIXME: Can we really import any initializer? Alternatively, we could force
582 // ourselves to import every declaration of a variable and then only use
583 // getInit() here.
584 ToVar->setInit(Importer.getToContext(),
585 Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
586
587 // FIXME: Other bits to merge?
588
589 return ToVar;
590}
591
Douglas Gregor82fc4bf2010-02-10 17:47:19 +0000592Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
593 // Import the context of this declaration.
594 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
595 if (!DC)
596 return 0;
597
598 DeclContext *LexicalDC = DC;
599 if (D->getDeclContext() != D->getLexicalDeclContext()) {
600 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
601 if (!LexicalDC)
602 return 0;
603 }
604
605 // Import the name of this declaration.
606 DeclarationName Name = Importer.Import(D->getDeclName());
607 if (D->getDeclName() && !Name)
608 return 0;
609
610 // Import the type of this declaration.
611 QualType T = Importer.Import(D->getUnderlyingType());
612 if (T.isNull())
613 return 0;
614
615 // Import the location of this declaration.
616 SourceLocation Loc = Importer.Import(D->getLocation());
617
618 // If this typedef is not in block scope, determine whether we've
619 // seen a typedef with the same name (that we can merge with) or any
620 // other entity by that name (which name lookup could conflict with).
621 if (!DC->isFunctionOrMethod()) {
622 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
623 unsigned IDNS = Decl::IDNS_Ordinary;
624 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
625 Lookup.first != Lookup.second;
626 ++Lookup.first) {
627 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
628 continue;
629 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
630 if (Importer.getToContext().typesAreCompatible(T,
631 FoundTypedef->getUnderlyingType())) {
632 Importer.getImportedDecls()[D] = FoundTypedef;
633 return FoundTypedef;
634 }
635 }
636
637 ConflictingDecls.push_back(*Lookup.first);
638 }
639
640 if (!ConflictingDecls.empty()) {
641 Name = Importer.HandleNameConflict(Name, DC, IDNS,
642 ConflictingDecls.data(),
643 ConflictingDecls.size());
644 if (!Name)
645 return 0;
646 }
647 }
648
649 // Create the new typedef node.
650 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
651 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
652 Loc, Name.getAsIdentifierInfo(),
653 TInfo);
654 ToTypedef->setLexicalDeclContext(LexicalDC);
655 Importer.getImportedDecls()[D] = ToTypedef;
656 LexicalDC->addDecl(ToTypedef);
657 return ToTypedef;
658}
659
Douglas Gregor88523732010-02-10 00:15:17 +0000660ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
661 Diagnostic &ToDiags,
662 ASTContext &FromContext, FileManager &FromFileManager,
663 Diagnostic &FromDiags)
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000664 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor88523732010-02-10 00:15:17 +0000665 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Douglas Gregor9bed8792010-02-09 19:21:46 +0000666 ToDiags(ToDiags), FromDiags(FromDiags) {
667 ImportedDecls[FromContext.getTranslationUnitDecl()]
668 = ToContext.getTranslationUnitDecl();
669}
670
671ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000672
673QualType ASTImporter::Import(QualType FromT) {
674 if (FromT.isNull())
675 return QualType();
676
Douglas Gregor169fba52010-02-08 15:18:58 +0000677 // Check whether we've already imported this type.
678 llvm::DenseMap<Type *, Type *>::iterator Pos
679 = ImportedTypes.find(FromT.getTypePtr());
680 if (Pos != ImportedTypes.end())
681 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000682
Douglas Gregor169fba52010-02-08 15:18:58 +0000683 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000684 ASTNodeImporter Importer(*this);
685 QualType ToT = Importer.Visit(FromT.getTypePtr());
686 if (ToT.isNull())
687 return ToT;
688
Douglas Gregor169fba52010-02-08 15:18:58 +0000689 // Record the imported type.
690 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
691
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000692 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
693}
694
Douglas Gregor9bed8792010-02-09 19:21:46 +0000695TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +0000696 if (!FromTSI)
697 return FromTSI;
698
699 // FIXME: For now we just create a "trivial" type source info based
700 // on the type and a seingle location. Implement a real version of
701 // this.
702 QualType T = Import(FromTSI->getType());
703 if (T.isNull())
704 return 0;
705
706 return ToContext.getTrivialTypeSourceInfo(T,
707 FromTSI->getTypeLoc().getFullSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +0000708}
709
710Decl *ASTImporter::Import(Decl *FromD) {
711 if (!FromD)
712 return 0;
713
714 // Check whether we've already imported this declaration.
715 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
716 if (Pos != ImportedDecls.end())
717 return Pos->second;
718
719 // Import the type
720 ASTNodeImporter Importer(*this);
721 Decl *ToD = Importer.Visit(FromD);
722 if (!ToD)
723 return 0;
724
725 // Record the imported declaration.
726 ImportedDecls[FromD] = ToD;
727 return ToD;
728}
729
730DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
731 if (!FromDC)
732 return FromDC;
733
734 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
735}
736
737Expr *ASTImporter::Import(Expr *FromE) {
738 if (!FromE)
739 return 0;
740
741 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
742}
743
744Stmt *ASTImporter::Import(Stmt *FromS) {
745 if (!FromS)
746 return 0;
747
748 // FIXME: Implement!
749 return 0;
750}
751
752NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
753 if (!FromNNS)
754 return 0;
755
756 // FIXME: Implement!
757 return 0;
758}
759
760SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
761 if (FromLoc.isInvalid())
762 return SourceLocation();
763
Douglas Gregor88523732010-02-10 00:15:17 +0000764 SourceManager &FromSM = FromContext.getSourceManager();
765
766 // For now, map everything down to its spelling location, so that we
767 // don't have to import macro instantiations.
768 // FIXME: Import macro instantiations!
769 FromLoc = FromSM.getSpellingLoc(FromLoc);
770 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
771 SourceManager &ToSM = ToContext.getSourceManager();
772 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
773 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +0000774}
775
776SourceRange ASTImporter::Import(SourceRange FromRange) {
777 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
778}
779
Douglas Gregor88523732010-02-10 00:15:17 +0000780FileID ASTImporter::Import(FileID FromID) {
781 llvm::DenseMap<unsigned, FileID>::iterator Pos
782 = ImportedFileIDs.find(FromID.getHashValue());
783 if (Pos != ImportedFileIDs.end())
784 return Pos->second;
785
786 SourceManager &FromSM = FromContext.getSourceManager();
787 SourceManager &ToSM = ToContext.getSourceManager();
788 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
789 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
790
791 // Include location of this file.
792 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
793
794 // Map the FileID for to the "to" source manager.
795 FileID ToID;
796 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
797 if (Cache->Entry) {
798 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
799 // disk again
800 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
801 // than mmap the files several times.
802 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
803 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
804 FromSLoc.getFile().getFileCharacteristic());
805 } else {
806 // FIXME: We want to re-use the existing MemoryBuffer!
807 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer();
808 llvm::MemoryBuffer *ToBuf
809 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(),
810 FromBuf->getBufferEnd(),
811 FromBuf->getBufferIdentifier());
812 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
813 }
814
815
816 ImportedFileIDs[FromID.getHashValue()] = ToID;
817 return ToID;
818}
819
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000820DeclarationName ASTImporter::Import(DeclarationName FromName) {
821 if (!FromName)
822 return DeclarationName();
823
824 switch (FromName.getNameKind()) {
825 case DeclarationName::Identifier:
826 return Import(FromName.getAsIdentifierInfo());
827
828 case DeclarationName::ObjCZeroArgSelector:
829 case DeclarationName::ObjCOneArgSelector:
830 case DeclarationName::ObjCMultiArgSelector:
831 return Import(FromName.getObjCSelector());
832
833 case DeclarationName::CXXConstructorName: {
834 QualType T = Import(FromName.getCXXNameType());
835 if (T.isNull())
836 return DeclarationName();
837
838 return ToContext.DeclarationNames.getCXXConstructorName(
839 ToContext.getCanonicalType(T));
840 }
841
842 case DeclarationName::CXXDestructorName: {
843 QualType T = Import(FromName.getCXXNameType());
844 if (T.isNull())
845 return DeclarationName();
846
847 return ToContext.DeclarationNames.getCXXDestructorName(
848 ToContext.getCanonicalType(T));
849 }
850
851 case DeclarationName::CXXConversionFunctionName: {
852 QualType T = Import(FromName.getCXXNameType());
853 if (T.isNull())
854 return DeclarationName();
855
856 return ToContext.DeclarationNames.getCXXConversionFunctionName(
857 ToContext.getCanonicalType(T));
858 }
859
860 case DeclarationName::CXXOperatorName:
861 return ToContext.DeclarationNames.getCXXOperatorName(
862 FromName.getCXXOverloadedOperator());
863
864 case DeclarationName::CXXLiteralOperatorName:
865 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
866 Import(FromName.getCXXLiteralIdentifier()));
867
868 case DeclarationName::CXXUsingDirective:
869 // FIXME: STATICS!
870 return DeclarationName::getUsingDirectiveName();
871 }
872
873 // Silence bogus GCC warning
874 return DeclarationName();
875}
876
877IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
878 if (!FromId)
879 return 0;
880
881 return &ToContext.Idents.get(FromId->getName());
882}
Douglas Gregor089459a2010-02-08 21:09:39 +0000883
884DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
885 DeclContext *DC,
886 unsigned IDNS,
887 NamedDecl **Decls,
888 unsigned NumDecls) {
889 return Name;
890}
891
892DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
893 return ToDiags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
894 DiagID);
895}
896
897DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
898 return FromDiags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
899 DiagID);
900}