blob: dc3281afba4bdeb38fc67af9cd4ff48592895c95 [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"
17#include "clang/AST/DeclObjC.h"
18#include "clang/AST/TypeVisitor.h"
19
20using namespace clang;
21
22namespace {
23 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType> {
24 ASTImporter &Importer;
25
26 public:
27 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
28
29 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
30
31 // Importing types
32 QualType VisitBuiltinType(BuiltinType *T);
33 QualType VisitComplexType(ComplexType *T);
34 QualType VisitPointerType(PointerType *T);
35 QualType VisitBlockPointerType(BlockPointerType *T);
36 QualType VisitLValueReferenceType(LValueReferenceType *T);
37 QualType VisitRValueReferenceType(RValueReferenceType *T);
38 QualType VisitMemberPointerType(MemberPointerType *T);
39 QualType VisitConstantArrayType(ConstantArrayType *T);
40 QualType VisitIncompleteArrayType(IncompleteArrayType *T);
41 QualType VisitVariableArrayType(VariableArrayType *T);
42 // FIXME: DependentSizedArrayType
43 // FIXME: DependentSizedExtVectorType
44 QualType VisitVectorType(VectorType *T);
45 QualType VisitExtVectorType(ExtVectorType *T);
46 QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
47 QualType VisitFunctionProtoType(FunctionProtoType *T);
48 // FIXME: UnresolvedUsingType
49 QualType VisitTypedefType(TypedefType *T);
50 QualType VisitTypeOfExprType(TypeOfExprType *T);
51 // FIXME: DependentTypeOfExprType
52 QualType VisitTypeOfType(TypeOfType *T);
53 QualType VisitDecltypeType(DecltypeType *T);
54 // FIXME: DependentDecltypeType
55 QualType VisitRecordType(RecordType *T);
56 QualType VisitEnumType(EnumType *T);
57 QualType VisitElaboratedType(ElaboratedType *T);
58 // FIXME: TemplateTypeParmType
59 // FIXME: SubstTemplateTypeParmType
60 // FIXME: TemplateSpecializationType
61 QualType VisitQualifiedNameType(QualifiedNameType *T);
62 // FIXME: TypenameType
63 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
64 QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
65 };
66}
67
68//----------------------------------------------------------------------------
69// Import Types
70//----------------------------------------------------------------------------
71
72QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
73 switch (T->getKind()) {
74 case BuiltinType::Void: return Importer.getToContext().VoidTy;
75 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
76
77 case BuiltinType::Char_U:
78 // The context we're importing from has an unsigned 'char'. If we're
79 // importing into a context with a signed 'char', translate to
80 // 'unsigned char' instead.
81 if (Importer.getToContext().getLangOptions().CharIsSigned)
82 return Importer.getToContext().UnsignedCharTy;
83
84 return Importer.getToContext().CharTy;
85
86 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
87
88 case BuiltinType::Char16:
89 // FIXME: Make sure that the "to" context supports C++!
90 return Importer.getToContext().Char16Ty;
91
92 case BuiltinType::Char32:
93 // FIXME: Make sure that the "to" context supports C++!
94 return Importer.getToContext().Char32Ty;
95
96 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
97 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
98 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
99 case BuiltinType::ULongLong:
100 return Importer.getToContext().UnsignedLongLongTy;
101 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
102
103 case BuiltinType::Char_S:
104 // The context we're importing from has an unsigned 'char'. If we're
105 // importing into a context with a signed 'char', translate to
106 // 'unsigned char' instead.
107 if (!Importer.getToContext().getLangOptions().CharIsSigned)
108 return Importer.getToContext().SignedCharTy;
109
110 return Importer.getToContext().CharTy;
111
112 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
113 case BuiltinType::WChar:
114 // FIXME: If not in C++, shall we translate to the C equivalent of
115 // wchar_t?
116 return Importer.getToContext().WCharTy;
117
118 case BuiltinType::Short : return Importer.getToContext().ShortTy;
119 case BuiltinType::Int : return Importer.getToContext().IntTy;
120 case BuiltinType::Long : return Importer.getToContext().LongTy;
121 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
122 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
123 case BuiltinType::Float: return Importer.getToContext().FloatTy;
124 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
125 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
126
127 case BuiltinType::NullPtr:
128 // FIXME: Make sure that the "to" context supports C++0x!
129 return Importer.getToContext().NullPtrTy;
130
131 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
132 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
133 case BuiltinType::UndeducedAuto:
134 // FIXME: Make sure that the "to" context supports C++0x!
135 return Importer.getToContext().UndeducedAutoTy;
136
137 case BuiltinType::ObjCId:
138 // FIXME: Make sure that the "to" context supports Objective-C!
139 return Importer.getToContext().ObjCBuiltinIdTy;
140
141 case BuiltinType::ObjCClass:
142 return Importer.getToContext().ObjCBuiltinClassTy;
143
144 case BuiltinType::ObjCSel:
145 return Importer.getToContext().ObjCBuiltinSelTy;
146 }
147
148 return QualType();
149}
150
151QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
152 QualType ToElementType = Importer.Import(T->getElementType());
153 if (ToElementType.isNull())
154 return QualType();
155
156 return Importer.getToContext().getComplexType(ToElementType);
157}
158
159QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
160 QualType ToPointeeType = Importer.Import(T->getPointeeType());
161 if (ToPointeeType.isNull())
162 return QualType();
163
164 return Importer.getToContext().getPointerType(ToPointeeType);
165}
166
167QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
168 // FIXME: Check for blocks support in "to" context.
169 QualType ToPointeeType = Importer.Import(T->getPointeeType());
170 if (ToPointeeType.isNull())
171 return QualType();
172
173 return Importer.getToContext().getBlockPointerType(ToPointeeType);
174}
175
176QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
177 // FIXME: Check for C++ support in "to" context.
178 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
179 if (ToPointeeType.isNull())
180 return QualType();
181
182 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
183}
184
185QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
186 // FIXME: Check for C++0x support in "to" context.
187 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
188 if (ToPointeeType.isNull())
189 return QualType();
190
191 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
192}
193
194QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
195 // FIXME: Check for C++ support in "to" context.
196 QualType ToPointeeType = Importer.Import(T->getPointeeType());
197 if (ToPointeeType.isNull())
198 return QualType();
199
200 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
201 return Importer.getToContext().getMemberPointerType(ToPointeeType,
202 ClassType.getTypePtr());
203}
204
205QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
206 QualType ToElementType = Importer.Import(T->getElementType());
207 if (ToElementType.isNull())
208 return QualType();
209
210 return Importer.getToContext().getConstantArrayType(ToElementType,
211 T->getSize(),
212 T->getSizeModifier(),
213 T->getIndexTypeCVRQualifiers());
214}
215
216QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
217 QualType ToElementType = Importer.Import(T->getElementType());
218 if (ToElementType.isNull())
219 return QualType();
220
221 return Importer.getToContext().getIncompleteArrayType(ToElementType,
222 T->getSizeModifier(),
223 T->getIndexTypeCVRQualifiers());
224}
225
226QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
227 QualType ToElementType = Importer.Import(T->getElementType());
228 if (ToElementType.isNull())
229 return QualType();
230
231 Expr *Size = Importer.Import(T->getSizeExpr());
232 if (!Size)
233 return QualType();
234
235 SourceRange Brackets = Importer.Import(T->getBracketsRange());
236 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
237 T->getSizeModifier(),
238 T->getIndexTypeCVRQualifiers(),
239 Brackets);
240}
241
242QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
243 QualType ToElementType = Importer.Import(T->getElementType());
244 if (ToElementType.isNull())
245 return QualType();
246
247 return Importer.getToContext().getVectorType(ToElementType,
248 T->getNumElements(),
249 T->isAltiVec(),
250 T->isPixel());
251}
252
253QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
254 QualType ToElementType = Importer.Import(T->getElementType());
255 if (ToElementType.isNull())
256 return QualType();
257
258 return Importer.getToContext().getExtVectorType(ToElementType,
259 T->getNumElements());
260}
261
262QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
263 // FIXME: What happens if we're importing a function without a prototype
264 // into C++? Should we make it variadic?
265 QualType ToResultType = Importer.Import(T->getResultType());
266 if (ToResultType.isNull())
267 return QualType();
268
269 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
270 T->getNoReturnAttr(),
271 T->getCallConv());
272}
273
274QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
275 QualType ToResultType = Importer.Import(T->getResultType());
276 if (ToResultType.isNull())
277 return QualType();
278
279 // Import argument types
280 llvm::SmallVector<QualType, 4> ArgTypes;
281 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
282 AEnd = T->arg_type_end();
283 A != AEnd; ++A) {
284 QualType ArgType = Importer.Import(*A);
285 if (ArgType.isNull())
286 return QualType();
287 ArgTypes.push_back(ArgType);
288 }
289
290 // Import exception types
291 llvm::SmallVector<QualType, 4> ExceptionTypes;
292 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
293 EEnd = T->exception_end();
294 E != EEnd; ++E) {
295 QualType ExceptionType = Importer.Import(*E);
296 if (ExceptionType.isNull())
297 return QualType();
298 ExceptionTypes.push_back(ExceptionType);
299 }
300
301 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
302 ArgTypes.size(),
303 T->isVariadic(),
304 T->getTypeQuals(),
305 T->hasExceptionSpec(),
306 T->hasAnyExceptionSpec(),
307 ExceptionTypes.size(),
308 ExceptionTypes.data(),
309 T->getNoReturnAttr(),
310 T->getCallConv());
311}
312
313QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
314 TypedefDecl *ToDecl
315 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
316 if (!ToDecl)
317 return QualType();
318
319 return Importer.getToContext().getTypeDeclType(ToDecl);
320}
321
322QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
323 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
324 if (!ToExpr)
325 return QualType();
326
327 return Importer.getToContext().getTypeOfExprType(ToExpr);
328}
329
330QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
331 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
332 if (ToUnderlyingType.isNull())
333 return QualType();
334
335 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
336}
337
338QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
339 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
340 if (!ToExpr)
341 return QualType();
342
343 return Importer.getToContext().getDecltypeType(ToExpr);
344}
345
346QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
347 RecordDecl *ToDecl
348 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
349 if (!ToDecl)
350 return QualType();
351
352 return Importer.getToContext().getTagDeclType(ToDecl);
353}
354
355QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
356 EnumDecl *ToDecl
357 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
358 if (!ToDecl)
359 return QualType();
360
361 return Importer.getToContext().getTagDeclType(ToDecl);
362}
363
364QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
365 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
366 if (ToUnderlyingType.isNull())
367 return QualType();
368
369 return Importer.getToContext().getElaboratedType(ToUnderlyingType,
370 T->getTagKind());
371}
372
373QualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) {
374 NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier());
375 if (!ToQualifier)
376 return QualType();
377
378 QualType ToNamedType = Importer.Import(T->getNamedType());
379 if (ToNamedType.isNull())
380 return QualType();
381
382 return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType);
383}
384
385QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
386 ObjCInterfaceDecl *Class
387 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
388 if (!Class)
389 return QualType();
390
391 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
392 for (ObjCInterfaceType::qual_iterator P = T->qual_begin(),
393 PEnd = T->qual_end();
394 P != PEnd; ++P) {
395 ObjCProtocolDecl *Protocol
396 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
397 if (!Protocol)
398 return QualType();
399 Protocols.push_back(Protocol);
400 }
401
402 return Importer.getToContext().getObjCInterfaceType(Class,
403 Protocols.data(),
404 Protocols.size());
405}
406
407QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
408 QualType ToPointeeType = Importer.Import(T->getPointeeType());
409 if (ToPointeeType.isNull())
410 return QualType();
411
412 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
413 for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(),
414 PEnd = T->qual_end();
415 P != PEnd; ++P) {
416 ObjCProtocolDecl *Protocol
417 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
418 if (!Protocol)
419 return QualType();
420 Protocols.push_back(Protocol);
421 }
422
423 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType,
424 Protocols.data(),
425 Protocols.size());
426}
427
428ASTImporter::ASTImporter(ASTContext &ToContext, Diagnostic &ToDiags,
429 ASTContext &FromContext, Diagnostic &FromDiags)
430 : ToContext(ToContext), FromContext(FromContext),
431 ToDiags(ToDiags), FromDiags(FromDiags) { }
432
433QualType ASTImporter::Import(QualType FromT) {
434 if (FromT.isNull())
435 return QualType();
436
437 // FIXME: Cache type mappings?
438
439 ASTNodeImporter Importer(*this);
440 QualType ToT = Importer.Visit(FromT.getTypePtr());
441 if (ToT.isNull())
442 return ToT;
443
444 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
445}
446
447DeclarationName ASTImporter::Import(DeclarationName FromName) {
448 if (!FromName)
449 return DeclarationName();
450
451 switch (FromName.getNameKind()) {
452 case DeclarationName::Identifier:
453 return Import(FromName.getAsIdentifierInfo());
454
455 case DeclarationName::ObjCZeroArgSelector:
456 case DeclarationName::ObjCOneArgSelector:
457 case DeclarationName::ObjCMultiArgSelector:
458 return Import(FromName.getObjCSelector());
459
460 case DeclarationName::CXXConstructorName: {
461 QualType T = Import(FromName.getCXXNameType());
462 if (T.isNull())
463 return DeclarationName();
464
465 return ToContext.DeclarationNames.getCXXConstructorName(
466 ToContext.getCanonicalType(T));
467 }
468
469 case DeclarationName::CXXDestructorName: {
470 QualType T = Import(FromName.getCXXNameType());
471 if (T.isNull())
472 return DeclarationName();
473
474 return ToContext.DeclarationNames.getCXXDestructorName(
475 ToContext.getCanonicalType(T));
476 }
477
478 case DeclarationName::CXXConversionFunctionName: {
479 QualType T = Import(FromName.getCXXNameType());
480 if (T.isNull())
481 return DeclarationName();
482
483 return ToContext.DeclarationNames.getCXXConversionFunctionName(
484 ToContext.getCanonicalType(T));
485 }
486
487 case DeclarationName::CXXOperatorName:
488 return ToContext.DeclarationNames.getCXXOperatorName(
489 FromName.getCXXOverloadedOperator());
490
491 case DeclarationName::CXXLiteralOperatorName:
492 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
493 Import(FromName.getCXXLiteralIdentifier()));
494
495 case DeclarationName::CXXUsingDirective:
496 // FIXME: STATICS!
497 return DeclarationName::getUsingDirectiveName();
498 }
499
500 // Silence bogus GCC warning
501 return DeclarationName();
502}
503
504IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
505 if (!FromId)
506 return 0;
507
508 return &ToContext.Idents.get(FromId->getName());
509}