Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1 | //===--- 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 Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeVisitor.h" |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 23 | #include "clang/Basic/FileManager.h" |
| 24 | #include "clang/Basic/SourceManager.h" |
| 25 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 26 | #include <deque> |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | |
| 30 | namespace { |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 31 | class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>, |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 32 | public DeclVisitor<ASTNodeImporter, Decl *>, |
| 33 | public StmtVisitor<ASTNodeImporter, Stmt *> { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 34 | ASTImporter &Importer; |
| 35 | |
| 36 | public: |
| 37 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { } |
| 38 | |
| 39 | using TypeVisitor<ASTNodeImporter, QualType>::Visit; |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 40 | using DeclVisitor<ASTNodeImporter, Decl *>::Visit; |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 41 | using StmtVisitor<ASTNodeImporter, Stmt *>::Visit; |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 42 | |
| 43 | // Importing types |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 44 | QualType VisitType(const Type *T); |
| 45 | QualType VisitBuiltinType(const BuiltinType *T); |
| 46 | QualType VisitComplexType(const ComplexType *T); |
| 47 | QualType VisitPointerType(const PointerType *T); |
| 48 | QualType VisitBlockPointerType(const BlockPointerType *T); |
| 49 | QualType VisitLValueReferenceType(const LValueReferenceType *T); |
| 50 | QualType VisitRValueReferenceType(const RValueReferenceType *T); |
| 51 | QualType VisitMemberPointerType(const MemberPointerType *T); |
| 52 | QualType VisitConstantArrayType(const ConstantArrayType *T); |
| 53 | QualType VisitIncompleteArrayType(const IncompleteArrayType *T); |
| 54 | QualType VisitVariableArrayType(const VariableArrayType *T); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 55 | // FIXME: DependentSizedArrayType |
| 56 | // FIXME: DependentSizedExtVectorType |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 57 | QualType VisitVectorType(const VectorType *T); |
| 58 | QualType VisitExtVectorType(const ExtVectorType *T); |
| 59 | QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T); |
| 60 | QualType VisitFunctionProtoType(const FunctionProtoType *T); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 61 | // FIXME: UnresolvedUsingType |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 62 | QualType VisitTypedefType(const TypedefType *T); |
| 63 | QualType VisitTypeOfExprType(const TypeOfExprType *T); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 64 | // FIXME: DependentTypeOfExprType |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 65 | QualType VisitTypeOfType(const TypeOfType *T); |
| 66 | QualType VisitDecltypeType(const DecltypeType *T); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame^] | 67 | QualType VisitUnaryTransformType(const UnaryTransformType *T); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 68 | QualType VisitAutoType(const AutoType *T); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 69 | // FIXME: DependentDecltypeType |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 70 | QualType VisitRecordType(const RecordType *T); |
| 71 | QualType VisitEnumType(const EnumType *T); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 72 | // FIXME: TemplateTypeParmType |
| 73 | // FIXME: SubstTemplateTypeParmType |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 74 | QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T); |
| 75 | QualType VisitElaboratedType(const ElaboratedType *T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 76 | // FIXME: DependentNameType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 77 | // FIXME: DependentTemplateSpecializationType |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 78 | QualType VisitObjCInterfaceType(const ObjCInterfaceType *T); |
| 79 | QualType VisitObjCObjectType(const ObjCObjectType *T); |
| 80 | QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 81 | |
| 82 | // Importing declarations |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 83 | bool ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 84 | DeclContext *&LexicalDC, DeclarationName &Name, |
Douglas Gregor | 788c62d | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 85 | SourceLocation &Loc); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 86 | void ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 87 | DeclarationNameInfo& To); |
Douglas Gregor | d8868a6 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 88 | void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 89 | bool ImportDefinition(RecordDecl *From, RecordDecl *To); |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 90 | TemplateParameterList *ImportTemplateParameterList( |
| 91 | TemplateParameterList *Params); |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 92 | TemplateArgument ImportTemplateArgument(const TemplateArgument &From); |
| 93 | bool ImportTemplateArguments(const TemplateArgument *FromArgs, |
| 94 | unsigned NumFromArgs, |
| 95 | llvm::SmallVectorImpl<TemplateArgument> &ToArgs); |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 96 | bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 97 | bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 98 | bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To); |
Douglas Gregor | 89cc9d6 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 99 | Decl *VisitDecl(Decl *D); |
Douglas Gregor | 788c62d | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 100 | Decl *VisitNamespaceDecl(NamespaceDecl *D); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 101 | Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); |
Douglas Gregor | 9e5d996 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 102 | Decl *VisitTypedefDecl(TypedefDecl *D); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 103 | Decl *VisitTypeAliasDecl(TypeAliasDecl *D); |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 104 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 105 | Decl *VisitRecordDecl(RecordDecl *D); |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 106 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 107 | Decl *VisitFunctionDecl(FunctionDecl *D); |
Douglas Gregor | c144f35 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 108 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D); |
| 109 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
| 110 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
| 111 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 112 | Decl *VisitFieldDecl(FieldDecl *D); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 113 | Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); |
Douglas Gregor | 2e55e3a | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 114 | Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 115 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 2cd0093 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 116 | Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 117 | Decl *VisitParmVarDecl(ParmVarDecl *D); |
Douglas Gregor | c3f2d2b | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 118 | Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); |
Douglas Gregor | b4677b6 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 119 | Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
Douglas Gregor | 2e2a400 | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 120 | Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
Douglas Gregor | a12d294 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 121 | Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
Douglas Gregor | 3daef29 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 122 | Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
Douglas Gregor | dd182ff | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 123 | Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
Douglas Gregor | e326162 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 124 | Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
Douglas Gregor | 954e0c7 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 125 | Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
Douglas Gregor | 2b78502 | 2010-02-18 02:12:22 +0000 | [diff] [blame] | 126 | Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
Douglas Gregor | a2bc15b | 2010-02-18 02:04:09 +0000 | [diff] [blame] | 127 | Decl *VisitObjCClassDecl(ObjCClassDecl *D); |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 128 | Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
| 129 | Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
| 130 | Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
| 131 | Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 132 | Decl *VisitClassTemplateSpecializationDecl( |
| 133 | ClassTemplateSpecializationDecl *D); |
Douglas Gregor | a2bc15b | 2010-02-18 02:04:09 +0000 | [diff] [blame] | 134 | |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 135 | // Importing statements |
| 136 | Stmt *VisitStmt(Stmt *S); |
| 137 | |
| 138 | // Importing expressions |
| 139 | Expr *VisitExpr(Expr *E); |
Douglas Gregor | 4408063 | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 140 | Expr *VisitDeclRefExpr(DeclRefExpr *E); |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 141 | Expr *VisitIntegerLiteral(IntegerLiteral *E); |
Douglas Gregor | b2e400a | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 142 | Expr *VisitCharacterLiteral(CharacterLiteral *E); |
Douglas Gregor | f638f95 | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 143 | Expr *VisitParenExpr(ParenExpr *E); |
| 144 | Expr *VisitUnaryOperator(UnaryOperator *E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 145 | Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Douglas Gregor | f638f95 | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 146 | Expr *VisitBinaryOperator(BinaryOperator *E); |
| 147 | Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 148 | Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); |
Douglas Gregor | 008847a | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 149 | Expr *VisitCStyleCastExpr(CStyleCastExpr *E); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 150 | }; |
| 151 | } |
| 152 | |
| 153 | //---------------------------------------------------------------------------- |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 154 | // Structural Equivalence |
| 155 | //---------------------------------------------------------------------------- |
| 156 | |
| 157 | namespace { |
| 158 | struct StructuralEquivalenceContext { |
| 159 | /// \brief AST contexts for which we are checking structural equivalence. |
| 160 | ASTContext &C1, &C2; |
| 161 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 162 | /// \brief The set of "tentative" equivalences between two canonical |
| 163 | /// declarations, mapping from a declaration in the first context to the |
| 164 | /// declaration in the second context that we believe to be equivalent. |
| 165 | llvm::DenseMap<Decl *, Decl *> TentativeEquivalences; |
| 166 | |
| 167 | /// \brief Queue of declarations in the first context whose equivalence |
| 168 | /// with a declaration in the second context still needs to be verified. |
| 169 | std::deque<Decl *> DeclsToCheck; |
| 170 | |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 171 | /// \brief Declaration (from, to) pairs that are known not to be equivalent |
| 172 | /// (which we have already complained about). |
| 173 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls; |
| 174 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 175 | /// \brief Whether we're being strict about the spelling of types when |
| 176 | /// unifying two types. |
| 177 | bool StrictTypeSpelling; |
| 178 | |
| 179 | StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2, |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 180 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls, |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 181 | bool StrictTypeSpelling = false) |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 182 | : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls), |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 183 | StrictTypeSpelling(StrictTypeSpelling) { } |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 184 | |
| 185 | /// \brief Determine whether the two declarations are structurally |
| 186 | /// equivalent. |
| 187 | bool IsStructurallyEquivalent(Decl *D1, Decl *D2); |
| 188 | |
| 189 | /// \brief Determine whether the two types are structurally equivalent. |
| 190 | bool IsStructurallyEquivalent(QualType T1, QualType T2); |
| 191 | |
| 192 | private: |
| 193 | /// \brief Finish checking all of the structural equivalences. |
| 194 | /// |
| 195 | /// \returns true if an error occurred, false otherwise. |
| 196 | bool Finish(); |
| 197 | |
| 198 | public: |
| 199 | DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 200 | return C1.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 204 | return C2.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 205 | } |
| 206 | }; |
| 207 | } |
| 208 | |
| 209 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 210 | QualType T1, QualType T2); |
| 211 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 212 | Decl *D1, Decl *D2); |
| 213 | |
| 214 | /// \brief Determine if two APInts have the same value, after zero-extending |
| 215 | /// one of them (if needed!) to ensure that the bit-widths match. |
| 216 | static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) { |
| 217 | if (I1.getBitWidth() == I2.getBitWidth()) |
| 218 | return I1 == I2; |
| 219 | |
| 220 | if (I1.getBitWidth() > I2.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 221 | return I1 == I2.zext(I1.getBitWidth()); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 222 | |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 223 | return I1.zext(I2.getBitWidth()) == I2; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /// \brief Determine if two APSInts have the same value, zero- or sign-extending |
| 227 | /// as needed. |
| 228 | static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) { |
| 229 | if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned()) |
| 230 | return I1 == I2; |
| 231 | |
| 232 | // Check for a bit-width mismatch. |
| 233 | if (I1.getBitWidth() > I2.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 234 | return IsSameValue(I1, I2.extend(I1.getBitWidth())); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 235 | else if (I2.getBitWidth() > I1.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 236 | return IsSameValue(I1.extend(I2.getBitWidth()), I2); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 237 | |
| 238 | // We have a signedness mismatch. Turn the signed value into an unsigned |
| 239 | // value. |
| 240 | if (I1.isSigned()) { |
| 241 | if (I1.isNegative()) |
| 242 | return false; |
| 243 | |
| 244 | return llvm::APSInt(I1, true) == I2; |
| 245 | } |
| 246 | |
| 247 | if (I2.isNegative()) |
| 248 | return false; |
| 249 | |
| 250 | return I1 == llvm::APSInt(I2, true); |
| 251 | } |
| 252 | |
| 253 | /// \brief Determine structural equivalence of two expressions. |
| 254 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 255 | Expr *E1, Expr *E2) { |
| 256 | if (!E1 || !E2) |
| 257 | return E1 == E2; |
| 258 | |
| 259 | // FIXME: Actually perform a structural comparison! |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | /// \brief Determine whether two identifiers are equivalent. |
| 264 | static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, |
| 265 | const IdentifierInfo *Name2) { |
| 266 | if (!Name1 || !Name2) |
| 267 | return Name1 == Name2; |
| 268 | |
| 269 | return Name1->getName() == Name2->getName(); |
| 270 | } |
| 271 | |
| 272 | /// \brief Determine whether two nested-name-specifiers are equivalent. |
| 273 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 274 | NestedNameSpecifier *NNS1, |
| 275 | NestedNameSpecifier *NNS2) { |
| 276 | // FIXME: Implement! |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | /// \brief Determine whether two template arguments are equivalent. |
| 281 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 282 | const TemplateArgument &Arg1, |
| 283 | const TemplateArgument &Arg2) { |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 284 | if (Arg1.getKind() != Arg2.getKind()) |
| 285 | return false; |
| 286 | |
| 287 | switch (Arg1.getKind()) { |
| 288 | case TemplateArgument::Null: |
| 289 | return true; |
| 290 | |
| 291 | case TemplateArgument::Type: |
| 292 | return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType()); |
| 293 | |
| 294 | case TemplateArgument::Integral: |
| 295 | if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(), |
| 296 | Arg2.getIntegralType())) |
| 297 | return false; |
| 298 | |
| 299 | return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral()); |
| 300 | |
| 301 | case TemplateArgument::Declaration: |
| 302 | return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl()); |
| 303 | |
| 304 | case TemplateArgument::Template: |
| 305 | return IsStructurallyEquivalent(Context, |
| 306 | Arg1.getAsTemplate(), |
| 307 | Arg2.getAsTemplate()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 308 | |
| 309 | case TemplateArgument::TemplateExpansion: |
| 310 | return IsStructurallyEquivalent(Context, |
| 311 | Arg1.getAsTemplateOrTemplatePattern(), |
| 312 | Arg2.getAsTemplateOrTemplatePattern()); |
| 313 | |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 314 | case TemplateArgument::Expression: |
| 315 | return IsStructurallyEquivalent(Context, |
| 316 | Arg1.getAsExpr(), Arg2.getAsExpr()); |
| 317 | |
| 318 | case TemplateArgument::Pack: |
| 319 | if (Arg1.pack_size() != Arg2.pack_size()) |
| 320 | return false; |
| 321 | |
| 322 | for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I) |
| 323 | if (!IsStructurallyEquivalent(Context, |
| 324 | Arg1.pack_begin()[I], |
| 325 | Arg2.pack_begin()[I])) |
| 326 | return false; |
| 327 | |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | llvm_unreachable("Invalid template argument kind"); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 332 | return true; |
| 333 | } |
| 334 | |
| 335 | /// \brief Determine structural equivalence for the common part of array |
| 336 | /// types. |
| 337 | static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 338 | const ArrayType *Array1, |
| 339 | const ArrayType *Array2) { |
| 340 | if (!IsStructurallyEquivalent(Context, |
| 341 | Array1->getElementType(), |
| 342 | Array2->getElementType())) |
| 343 | return false; |
| 344 | if (Array1->getSizeModifier() != Array2->getSizeModifier()) |
| 345 | return false; |
| 346 | if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers()) |
| 347 | return false; |
| 348 | |
| 349 | return true; |
| 350 | } |
| 351 | |
| 352 | /// \brief Determine structural equivalence of two types. |
| 353 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 354 | QualType T1, QualType T2) { |
| 355 | if (T1.isNull() || T2.isNull()) |
| 356 | return T1.isNull() && T2.isNull(); |
| 357 | |
| 358 | if (!Context.StrictTypeSpelling) { |
| 359 | // We aren't being strict about token-to-token equivalence of types, |
| 360 | // so map down to the canonical type. |
| 361 | T1 = Context.C1.getCanonicalType(T1); |
| 362 | T2 = Context.C2.getCanonicalType(T2); |
| 363 | } |
| 364 | |
| 365 | if (T1.getQualifiers() != T2.getQualifiers()) |
| 366 | return false; |
| 367 | |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 368 | Type::TypeClass TC = T1->getTypeClass(); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 369 | |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 370 | if (T1->getTypeClass() != T2->getTypeClass()) { |
| 371 | // Compare function types with prototypes vs. without prototypes as if |
| 372 | // both did not have prototypes. |
| 373 | if (T1->getTypeClass() == Type::FunctionProto && |
| 374 | T2->getTypeClass() == Type::FunctionNoProto) |
| 375 | TC = Type::FunctionNoProto; |
| 376 | else if (T1->getTypeClass() == Type::FunctionNoProto && |
| 377 | T2->getTypeClass() == Type::FunctionProto) |
| 378 | TC = Type::FunctionNoProto; |
| 379 | else |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | switch (TC) { |
| 384 | case Type::Builtin: |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 385 | // FIXME: Deal with Char_S/Char_U. |
| 386 | if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind()) |
| 387 | return false; |
| 388 | break; |
| 389 | |
| 390 | case Type::Complex: |
| 391 | if (!IsStructurallyEquivalent(Context, |
| 392 | cast<ComplexType>(T1)->getElementType(), |
| 393 | cast<ComplexType>(T2)->getElementType())) |
| 394 | return false; |
| 395 | break; |
| 396 | |
| 397 | case Type::Pointer: |
| 398 | if (!IsStructurallyEquivalent(Context, |
| 399 | cast<PointerType>(T1)->getPointeeType(), |
| 400 | cast<PointerType>(T2)->getPointeeType())) |
| 401 | return false; |
| 402 | break; |
| 403 | |
| 404 | case Type::BlockPointer: |
| 405 | if (!IsStructurallyEquivalent(Context, |
| 406 | cast<BlockPointerType>(T1)->getPointeeType(), |
| 407 | cast<BlockPointerType>(T2)->getPointeeType())) |
| 408 | return false; |
| 409 | break; |
| 410 | |
| 411 | case Type::LValueReference: |
| 412 | case Type::RValueReference: { |
| 413 | const ReferenceType *Ref1 = cast<ReferenceType>(T1); |
| 414 | const ReferenceType *Ref2 = cast<ReferenceType>(T2); |
| 415 | if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue()) |
| 416 | return false; |
| 417 | if (Ref1->isInnerRef() != Ref2->isInnerRef()) |
| 418 | return false; |
| 419 | if (!IsStructurallyEquivalent(Context, |
| 420 | Ref1->getPointeeTypeAsWritten(), |
| 421 | Ref2->getPointeeTypeAsWritten())) |
| 422 | return false; |
| 423 | break; |
| 424 | } |
| 425 | |
| 426 | case Type::MemberPointer: { |
| 427 | const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1); |
| 428 | const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2); |
| 429 | if (!IsStructurallyEquivalent(Context, |
| 430 | MemPtr1->getPointeeType(), |
| 431 | MemPtr2->getPointeeType())) |
| 432 | return false; |
| 433 | if (!IsStructurallyEquivalent(Context, |
| 434 | QualType(MemPtr1->getClass(), 0), |
| 435 | QualType(MemPtr2->getClass(), 0))) |
| 436 | return false; |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | case Type::ConstantArray: { |
| 441 | const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1); |
| 442 | const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2); |
| 443 | if (!IsSameValue(Array1->getSize(), Array2->getSize())) |
| 444 | return false; |
| 445 | |
| 446 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 447 | return false; |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | case Type::IncompleteArray: |
| 452 | if (!IsArrayStructurallyEquivalent(Context, |
| 453 | cast<ArrayType>(T1), |
| 454 | cast<ArrayType>(T2))) |
| 455 | return false; |
| 456 | break; |
| 457 | |
| 458 | case Type::VariableArray: { |
| 459 | const VariableArrayType *Array1 = cast<VariableArrayType>(T1); |
| 460 | const VariableArrayType *Array2 = cast<VariableArrayType>(T2); |
| 461 | if (!IsStructurallyEquivalent(Context, |
| 462 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 463 | return false; |
| 464 | |
| 465 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 466 | return false; |
| 467 | |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | case Type::DependentSizedArray: { |
| 472 | const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1); |
| 473 | const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2); |
| 474 | if (!IsStructurallyEquivalent(Context, |
| 475 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 476 | return false; |
| 477 | |
| 478 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 479 | return false; |
| 480 | |
| 481 | break; |
| 482 | } |
| 483 | |
| 484 | case Type::DependentSizedExtVector: { |
| 485 | const DependentSizedExtVectorType *Vec1 |
| 486 | = cast<DependentSizedExtVectorType>(T1); |
| 487 | const DependentSizedExtVectorType *Vec2 |
| 488 | = cast<DependentSizedExtVectorType>(T2); |
| 489 | if (!IsStructurallyEquivalent(Context, |
| 490 | Vec1->getSizeExpr(), Vec2->getSizeExpr())) |
| 491 | return false; |
| 492 | if (!IsStructurallyEquivalent(Context, |
| 493 | Vec1->getElementType(), |
| 494 | Vec2->getElementType())) |
| 495 | return false; |
| 496 | break; |
| 497 | } |
| 498 | |
| 499 | case Type::Vector: |
| 500 | case Type::ExtVector: { |
| 501 | const VectorType *Vec1 = cast<VectorType>(T1); |
| 502 | const VectorType *Vec2 = cast<VectorType>(T2); |
| 503 | if (!IsStructurallyEquivalent(Context, |
| 504 | Vec1->getElementType(), |
| 505 | Vec2->getElementType())) |
| 506 | return false; |
| 507 | if (Vec1->getNumElements() != Vec2->getNumElements()) |
| 508 | return false; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 509 | if (Vec1->getVectorKind() != Vec2->getVectorKind()) |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 510 | return false; |
Douglas Gregor | 0e12b44 | 2010-02-19 01:36:36 +0000 | [diff] [blame] | 511 | break; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | case Type::FunctionProto: { |
| 515 | const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1); |
| 516 | const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2); |
| 517 | if (Proto1->getNumArgs() != Proto2->getNumArgs()) |
| 518 | return false; |
| 519 | for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) { |
| 520 | if (!IsStructurallyEquivalent(Context, |
| 521 | Proto1->getArgType(I), |
| 522 | Proto2->getArgType(I))) |
| 523 | return false; |
| 524 | } |
| 525 | if (Proto1->isVariadic() != Proto2->isVariadic()) |
| 526 | return false; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 527 | if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType()) |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 528 | return false; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 529 | if (Proto1->getExceptionSpecType() == EST_Dynamic) { |
| 530 | if (Proto1->getNumExceptions() != Proto2->getNumExceptions()) |
| 531 | return false; |
| 532 | for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) { |
| 533 | if (!IsStructurallyEquivalent(Context, |
| 534 | Proto1->getExceptionType(I), |
| 535 | Proto2->getExceptionType(I))) |
| 536 | return false; |
| 537 | } |
| 538 | } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) { |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 539 | if (!IsStructurallyEquivalent(Context, |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 540 | Proto1->getNoexceptExpr(), |
| 541 | Proto2->getNoexceptExpr())) |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 542 | return false; |
| 543 | } |
| 544 | if (Proto1->getTypeQuals() != Proto2->getTypeQuals()) |
| 545 | return false; |
| 546 | |
| 547 | // Fall through to check the bits common with FunctionNoProtoType. |
| 548 | } |
| 549 | |
| 550 | case Type::FunctionNoProto: { |
| 551 | const FunctionType *Function1 = cast<FunctionType>(T1); |
| 552 | const FunctionType *Function2 = cast<FunctionType>(T2); |
| 553 | if (!IsStructurallyEquivalent(Context, |
| 554 | Function1->getResultType(), |
| 555 | Function2->getResultType())) |
| 556 | return false; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 557 | if (Function1->getExtInfo() != Function2->getExtInfo()) |
| 558 | return false; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 559 | break; |
| 560 | } |
| 561 | |
| 562 | case Type::UnresolvedUsing: |
| 563 | if (!IsStructurallyEquivalent(Context, |
| 564 | cast<UnresolvedUsingType>(T1)->getDecl(), |
| 565 | cast<UnresolvedUsingType>(T2)->getDecl())) |
| 566 | return false; |
| 567 | |
| 568 | break; |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 569 | |
| 570 | case Type::Attributed: |
| 571 | if (!IsStructurallyEquivalent(Context, |
| 572 | cast<AttributedType>(T1)->getModifiedType(), |
| 573 | cast<AttributedType>(T2)->getModifiedType())) |
| 574 | return false; |
| 575 | if (!IsStructurallyEquivalent(Context, |
| 576 | cast<AttributedType>(T1)->getEquivalentType(), |
| 577 | cast<AttributedType>(T2)->getEquivalentType())) |
| 578 | return false; |
| 579 | break; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 580 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 581 | case Type::Paren: |
| 582 | if (!IsStructurallyEquivalent(Context, |
| 583 | cast<ParenType>(T1)->getInnerType(), |
| 584 | cast<ParenType>(T2)->getInnerType())) |
| 585 | return false; |
| 586 | break; |
| 587 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 588 | case Type::Typedef: |
| 589 | if (!IsStructurallyEquivalent(Context, |
| 590 | cast<TypedefType>(T1)->getDecl(), |
| 591 | cast<TypedefType>(T2)->getDecl())) |
| 592 | return false; |
| 593 | break; |
| 594 | |
| 595 | case Type::TypeOfExpr: |
| 596 | if (!IsStructurallyEquivalent(Context, |
| 597 | cast<TypeOfExprType>(T1)->getUnderlyingExpr(), |
| 598 | cast<TypeOfExprType>(T2)->getUnderlyingExpr())) |
| 599 | return false; |
| 600 | break; |
| 601 | |
| 602 | case Type::TypeOf: |
| 603 | if (!IsStructurallyEquivalent(Context, |
| 604 | cast<TypeOfType>(T1)->getUnderlyingType(), |
| 605 | cast<TypeOfType>(T2)->getUnderlyingType())) |
| 606 | return false; |
| 607 | break; |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame^] | 608 | |
| 609 | case Type::UnaryTransform: |
| 610 | if (!IsStructurallyEquivalent(Context, |
| 611 | cast<UnaryTransformType>(T1)->getUnderlyingType(), |
| 612 | cast<UnaryTransformType>(T1)->getUnderlyingType())) |
| 613 | return false; |
| 614 | break; |
| 615 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 616 | case Type::Decltype: |
| 617 | if (!IsStructurallyEquivalent(Context, |
| 618 | cast<DecltypeType>(T1)->getUnderlyingExpr(), |
| 619 | cast<DecltypeType>(T2)->getUnderlyingExpr())) |
| 620 | return false; |
| 621 | break; |
| 622 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 623 | case Type::Auto: |
| 624 | if (!IsStructurallyEquivalent(Context, |
| 625 | cast<AutoType>(T1)->getDeducedType(), |
| 626 | cast<AutoType>(T2)->getDeducedType())) |
| 627 | return false; |
| 628 | break; |
| 629 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 630 | case Type::Record: |
| 631 | case Type::Enum: |
| 632 | if (!IsStructurallyEquivalent(Context, |
| 633 | cast<TagType>(T1)->getDecl(), |
| 634 | cast<TagType>(T2)->getDecl())) |
| 635 | return false; |
| 636 | break; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 637 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 638 | case Type::TemplateTypeParm: { |
| 639 | const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1); |
| 640 | const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2); |
| 641 | if (Parm1->getDepth() != Parm2->getDepth()) |
| 642 | return false; |
| 643 | if (Parm1->getIndex() != Parm2->getIndex()) |
| 644 | return false; |
| 645 | if (Parm1->isParameterPack() != Parm2->isParameterPack()) |
| 646 | return false; |
| 647 | |
| 648 | // Names of template type parameters are never significant. |
| 649 | break; |
| 650 | } |
| 651 | |
| 652 | case Type::SubstTemplateTypeParm: { |
| 653 | const SubstTemplateTypeParmType *Subst1 |
| 654 | = cast<SubstTemplateTypeParmType>(T1); |
| 655 | const SubstTemplateTypeParmType *Subst2 |
| 656 | = cast<SubstTemplateTypeParmType>(T2); |
| 657 | if (!IsStructurallyEquivalent(Context, |
| 658 | QualType(Subst1->getReplacedParameter(), 0), |
| 659 | QualType(Subst2->getReplacedParameter(), 0))) |
| 660 | return false; |
| 661 | if (!IsStructurallyEquivalent(Context, |
| 662 | Subst1->getReplacementType(), |
| 663 | Subst2->getReplacementType())) |
| 664 | return false; |
| 665 | break; |
| 666 | } |
| 667 | |
Douglas Gregor | 0bc15d9 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 668 | case Type::SubstTemplateTypeParmPack: { |
| 669 | const SubstTemplateTypeParmPackType *Subst1 |
| 670 | = cast<SubstTemplateTypeParmPackType>(T1); |
| 671 | const SubstTemplateTypeParmPackType *Subst2 |
| 672 | = cast<SubstTemplateTypeParmPackType>(T2); |
| 673 | if (!IsStructurallyEquivalent(Context, |
| 674 | QualType(Subst1->getReplacedParameter(), 0), |
| 675 | QualType(Subst2->getReplacedParameter(), 0))) |
| 676 | return false; |
| 677 | if (!IsStructurallyEquivalent(Context, |
| 678 | Subst1->getArgumentPack(), |
| 679 | Subst2->getArgumentPack())) |
| 680 | return false; |
| 681 | break; |
| 682 | } |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 683 | case Type::TemplateSpecialization: { |
| 684 | const TemplateSpecializationType *Spec1 |
| 685 | = cast<TemplateSpecializationType>(T1); |
| 686 | const TemplateSpecializationType *Spec2 |
| 687 | = cast<TemplateSpecializationType>(T2); |
| 688 | if (!IsStructurallyEquivalent(Context, |
| 689 | Spec1->getTemplateName(), |
| 690 | Spec2->getTemplateName())) |
| 691 | return false; |
| 692 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 693 | return false; |
| 694 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 695 | if (!IsStructurallyEquivalent(Context, |
| 696 | Spec1->getArg(I), Spec2->getArg(I))) |
| 697 | return false; |
| 698 | } |
| 699 | break; |
| 700 | } |
| 701 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 702 | case Type::Elaborated: { |
| 703 | const ElaboratedType *Elab1 = cast<ElaboratedType>(T1); |
| 704 | const ElaboratedType *Elab2 = cast<ElaboratedType>(T2); |
| 705 | // CHECKME: what if a keyword is ETK_None or ETK_typename ? |
| 706 | if (Elab1->getKeyword() != Elab2->getKeyword()) |
| 707 | return false; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 708 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 709 | Elab1->getQualifier(), |
| 710 | Elab2->getQualifier())) |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 711 | return false; |
| 712 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 713 | Elab1->getNamedType(), |
| 714 | Elab2->getNamedType())) |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 715 | return false; |
| 716 | break; |
| 717 | } |
| 718 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 719 | case Type::InjectedClassName: { |
| 720 | const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1); |
| 721 | const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2); |
| 722 | if (!IsStructurallyEquivalent(Context, |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 723 | Inj1->getInjectedSpecializationType(), |
| 724 | Inj2->getInjectedSpecializationType())) |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 725 | return false; |
| 726 | break; |
| 727 | } |
| 728 | |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 729 | case Type::DependentName: { |
| 730 | const DependentNameType *Typename1 = cast<DependentNameType>(T1); |
| 731 | const DependentNameType *Typename2 = cast<DependentNameType>(T2); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 732 | if (!IsStructurallyEquivalent(Context, |
| 733 | Typename1->getQualifier(), |
| 734 | Typename2->getQualifier())) |
| 735 | return false; |
| 736 | if (!IsStructurallyEquivalent(Typename1->getIdentifier(), |
| 737 | Typename2->getIdentifier())) |
| 738 | return false; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 739 | |
| 740 | break; |
| 741 | } |
| 742 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 743 | case Type::DependentTemplateSpecialization: { |
| 744 | const DependentTemplateSpecializationType *Spec1 = |
| 745 | cast<DependentTemplateSpecializationType>(T1); |
| 746 | const DependentTemplateSpecializationType *Spec2 = |
| 747 | cast<DependentTemplateSpecializationType>(T2); |
| 748 | if (!IsStructurallyEquivalent(Context, |
| 749 | Spec1->getQualifier(), |
| 750 | Spec2->getQualifier())) |
| 751 | return false; |
| 752 | if (!IsStructurallyEquivalent(Spec1->getIdentifier(), |
| 753 | Spec2->getIdentifier())) |
| 754 | return false; |
| 755 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 756 | return false; |
| 757 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 758 | if (!IsStructurallyEquivalent(Context, |
| 759 | Spec1->getArg(I), Spec2->getArg(I))) |
| 760 | return false; |
| 761 | } |
| 762 | break; |
| 763 | } |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 764 | |
| 765 | case Type::PackExpansion: |
| 766 | if (!IsStructurallyEquivalent(Context, |
| 767 | cast<PackExpansionType>(T1)->getPattern(), |
| 768 | cast<PackExpansionType>(T2)->getPattern())) |
| 769 | return false; |
| 770 | break; |
| 771 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 772 | case Type::ObjCInterface: { |
| 773 | const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1); |
| 774 | const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2); |
| 775 | if (!IsStructurallyEquivalent(Context, |
| 776 | Iface1->getDecl(), Iface2->getDecl())) |
| 777 | return false; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 778 | break; |
| 779 | } |
| 780 | |
| 781 | case Type::ObjCObject: { |
| 782 | const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1); |
| 783 | const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2); |
| 784 | if (!IsStructurallyEquivalent(Context, |
| 785 | Obj1->getBaseType(), |
| 786 | Obj2->getBaseType())) |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 787 | return false; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 788 | if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) |
| 789 | return false; |
| 790 | for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 791 | if (!IsStructurallyEquivalent(Context, |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 792 | Obj1->getProtocol(I), |
| 793 | Obj2->getProtocol(I))) |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 794 | return false; |
| 795 | } |
| 796 | break; |
| 797 | } |
| 798 | |
| 799 | case Type::ObjCObjectPointer: { |
| 800 | const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1); |
| 801 | const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2); |
| 802 | if (!IsStructurallyEquivalent(Context, |
| 803 | Ptr1->getPointeeType(), |
| 804 | Ptr2->getPointeeType())) |
| 805 | return false; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 806 | break; |
| 807 | } |
| 808 | |
| 809 | } // end switch |
| 810 | |
| 811 | return true; |
| 812 | } |
| 813 | |
| 814 | /// \brief Determine structural equivalence of two records. |
| 815 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 816 | RecordDecl *D1, RecordDecl *D2) { |
| 817 | if (D1->isUnion() != D2->isUnion()) { |
| 818 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 819 | << Context.C2.getTypeDeclType(D2); |
| 820 | Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here) |
| 821 | << D1->getDeclName() << (unsigned)D1->getTagKind(); |
| 822 | return false; |
| 823 | } |
| 824 | |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 825 | // If both declarations are class template specializations, we know |
| 826 | // the ODR applies, so check the template and template arguments. |
| 827 | ClassTemplateSpecializationDecl *Spec1 |
| 828 | = dyn_cast<ClassTemplateSpecializationDecl>(D1); |
| 829 | ClassTemplateSpecializationDecl *Spec2 |
| 830 | = dyn_cast<ClassTemplateSpecializationDecl>(D2); |
| 831 | if (Spec1 && Spec2) { |
| 832 | // Check that the specialized templates are the same. |
| 833 | if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(), |
| 834 | Spec2->getSpecializedTemplate())) |
| 835 | return false; |
| 836 | |
| 837 | // Check that the template arguments are the same. |
| 838 | if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size()) |
| 839 | return false; |
| 840 | |
| 841 | for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I) |
| 842 | if (!IsStructurallyEquivalent(Context, |
| 843 | Spec1->getTemplateArgs().get(I), |
| 844 | Spec2->getTemplateArgs().get(I))) |
| 845 | return false; |
| 846 | } |
| 847 | // If one is a class template specialization and the other is not, these |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 848 | // structures are different. |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 849 | else if (Spec1 || Spec2) |
| 850 | return false; |
| 851 | |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 852 | // Compare the definitions of these two records. If either or both are |
| 853 | // incomplete, we assume that they are equivalent. |
| 854 | D1 = D1->getDefinition(); |
| 855 | D2 = D2->getDefinition(); |
| 856 | if (!D1 || !D2) |
| 857 | return true; |
| 858 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 859 | if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) { |
| 860 | if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) { |
| 861 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) { |
| 862 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 863 | << Context.C2.getTypeDeclType(D2); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 864 | Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases) |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 865 | << D2CXX->getNumBases(); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 866 | Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases) |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 867 | << D1CXX->getNumBases(); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 868 | return false; |
| 869 | } |
| 870 | |
| 871 | // Check the base classes. |
| 872 | for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(), |
| 873 | BaseEnd1 = D1CXX->bases_end(), |
| 874 | Base2 = D2CXX->bases_begin(); |
| 875 | Base1 != BaseEnd1; |
| 876 | ++Base1, ++Base2) { |
| 877 | if (!IsStructurallyEquivalent(Context, |
| 878 | Base1->getType(), Base2->getType())) { |
| 879 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 880 | << Context.C2.getTypeDeclType(D2); |
| 881 | Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base) |
| 882 | << Base2->getType() |
| 883 | << Base2->getSourceRange(); |
| 884 | Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base) |
| 885 | << Base1->getType() |
| 886 | << Base1->getSourceRange(); |
| 887 | return false; |
| 888 | } |
| 889 | |
| 890 | // Check virtual vs. non-virtual inheritance mismatch. |
| 891 | if (Base1->isVirtual() != Base2->isVirtual()) { |
| 892 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 893 | << Context.C2.getTypeDeclType(D2); |
| 894 | Context.Diag2(Base2->getSourceRange().getBegin(), |
| 895 | diag::note_odr_virtual_base) |
| 896 | << Base2->isVirtual() << Base2->getSourceRange(); |
| 897 | Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base) |
| 898 | << Base1->isVirtual() |
| 899 | << Base1->getSourceRange(); |
| 900 | return false; |
| 901 | } |
| 902 | } |
| 903 | } else if (D1CXX->getNumBases() > 0) { |
| 904 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 905 | << Context.C2.getTypeDeclType(D2); |
| 906 | const CXXBaseSpecifier *Base1 = D1CXX->bases_begin(); |
| 907 | Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base) |
| 908 | << Base1->getType() |
| 909 | << Base1->getSourceRange(); |
| 910 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_base); |
| 911 | return false; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | // Check the fields for consistency. |
| 916 | CXXRecordDecl::field_iterator Field2 = D2->field_begin(), |
| 917 | Field2End = D2->field_end(); |
| 918 | for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(), |
| 919 | Field1End = D1->field_end(); |
| 920 | Field1 != Field1End; |
| 921 | ++Field1, ++Field2) { |
| 922 | if (Field2 == Field2End) { |
| 923 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 924 | << Context.C2.getTypeDeclType(D2); |
| 925 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 926 | << Field1->getDeclName() << Field1->getType(); |
| 927 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_field); |
| 928 | return false; |
| 929 | } |
| 930 | |
| 931 | if (!IsStructurallyEquivalent(Context, |
| 932 | Field1->getType(), Field2->getType())) { |
| 933 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 934 | << Context.C2.getTypeDeclType(D2); |
| 935 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 936 | << Field2->getDeclName() << Field2->getType(); |
| 937 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 938 | << Field1->getDeclName() << Field1->getType(); |
| 939 | return false; |
| 940 | } |
| 941 | |
| 942 | if (Field1->isBitField() != Field2->isBitField()) { |
| 943 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 944 | << Context.C2.getTypeDeclType(D2); |
| 945 | if (Field1->isBitField()) { |
| 946 | llvm::APSInt Bits; |
| 947 | Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1); |
| 948 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 949 | << Field1->getDeclName() << Field1->getType() |
| 950 | << Bits.toString(10, false); |
| 951 | Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field) |
| 952 | << Field2->getDeclName(); |
| 953 | } else { |
| 954 | llvm::APSInt Bits; |
| 955 | Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2); |
| 956 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 957 | << Field2->getDeclName() << Field2->getType() |
| 958 | << Bits.toString(10, false); |
| 959 | Context.Diag1(Field1->getLocation(), |
| 960 | diag::note_odr_not_bit_field) |
| 961 | << Field1->getDeclName(); |
| 962 | } |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | if (Field1->isBitField()) { |
| 967 | // Make sure that the bit-fields are the same length. |
| 968 | llvm::APSInt Bits1, Bits2; |
| 969 | if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1)) |
| 970 | return false; |
| 971 | if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2)) |
| 972 | return false; |
| 973 | |
| 974 | if (!IsSameValue(Bits1, Bits2)) { |
| 975 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 976 | << Context.C2.getTypeDeclType(D2); |
| 977 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 978 | << Field2->getDeclName() << Field2->getType() |
| 979 | << Bits2.toString(10, false); |
| 980 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 981 | << Field1->getDeclName() << Field1->getType() |
| 982 | << Bits1.toString(10, false); |
| 983 | return false; |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | if (Field2 != Field2End) { |
| 989 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 990 | << Context.C2.getTypeDeclType(D2); |
| 991 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 992 | << Field2->getDeclName() << Field2->getType(); |
| 993 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_field); |
| 994 | return false; |
| 995 | } |
| 996 | |
| 997 | return true; |
| 998 | } |
| 999 | |
| 1000 | /// \brief Determine structural equivalence of two enums. |
| 1001 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1002 | EnumDecl *D1, EnumDecl *D2) { |
| 1003 | EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(), |
| 1004 | EC2End = D2->enumerator_end(); |
| 1005 | for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(), |
| 1006 | EC1End = D1->enumerator_end(); |
| 1007 | EC1 != EC1End; ++EC1, ++EC2) { |
| 1008 | if (EC2 == EC2End) { |
| 1009 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1010 | << Context.C2.getTypeDeclType(D2); |
| 1011 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 1012 | << EC1->getDeclName() |
| 1013 | << EC1->getInitVal().toString(10); |
| 1014 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
| 1018 | llvm::APSInt Val1 = EC1->getInitVal(); |
| 1019 | llvm::APSInt Val2 = EC2->getInitVal(); |
| 1020 | if (!IsSameValue(Val1, Val2) || |
| 1021 | !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) { |
| 1022 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1023 | << Context.C2.getTypeDeclType(D2); |
| 1024 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 1025 | << EC2->getDeclName() |
| 1026 | << EC2->getInitVal().toString(10); |
| 1027 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 1028 | << EC1->getDeclName() |
| 1029 | << EC1->getInitVal().toString(10); |
| 1030 | return false; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | if (EC2 != EC2End) { |
| 1035 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1036 | << Context.C2.getTypeDeclType(D2); |
| 1037 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 1038 | << EC2->getDeclName() |
| 1039 | << EC2->getInitVal().toString(10); |
| 1040 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); |
| 1041 | return false; |
| 1042 | } |
| 1043 | |
| 1044 | return true; |
| 1045 | } |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1046 | |
| 1047 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1048 | TemplateParameterList *Params1, |
| 1049 | TemplateParameterList *Params2) { |
| 1050 | if (Params1->size() != Params2->size()) { |
| 1051 | Context.Diag2(Params2->getTemplateLoc(), |
| 1052 | diag::err_odr_different_num_template_parameters) |
| 1053 | << Params1->size() << Params2->size(); |
| 1054 | Context.Diag1(Params1->getTemplateLoc(), |
| 1055 | diag::note_odr_template_parameter_list); |
| 1056 | return false; |
| 1057 | } |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1058 | |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1059 | for (unsigned I = 0, N = Params1->size(); I != N; ++I) { |
| 1060 | if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) { |
| 1061 | Context.Diag2(Params2->getParam(I)->getLocation(), |
| 1062 | diag::err_odr_different_template_parameter_kind); |
| 1063 | Context.Diag1(Params1->getParam(I)->getLocation(), |
| 1064 | diag::note_odr_template_parameter_here); |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
| 1068 | if (!Context.IsStructurallyEquivalent(Params1->getParam(I), |
| 1069 | Params2->getParam(I))) { |
| 1070 | |
| 1071 | return false; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | return true; |
| 1076 | } |
| 1077 | |
| 1078 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1079 | TemplateTypeParmDecl *D1, |
| 1080 | TemplateTypeParmDecl *D2) { |
| 1081 | if (D1->isParameterPack() != D2->isParameterPack()) { |
| 1082 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1083 | << D2->isParameterPack(); |
| 1084 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1085 | << D1->isParameterPack(); |
| 1086 | return false; |
| 1087 | } |
| 1088 | |
| 1089 | return true; |
| 1090 | } |
| 1091 | |
| 1092 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1093 | NonTypeTemplateParmDecl *D1, |
| 1094 | NonTypeTemplateParmDecl *D2) { |
| 1095 | // FIXME: Enable once we have variadic templates. |
| 1096 | #if 0 |
| 1097 | if (D1->isParameterPack() != D2->isParameterPack()) { |
| 1098 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1099 | << D2->isParameterPack(); |
| 1100 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1101 | << D1->isParameterPack(); |
| 1102 | return false; |
| 1103 | } |
| 1104 | #endif |
| 1105 | |
| 1106 | // Check types. |
| 1107 | if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) { |
| 1108 | Context.Diag2(D2->getLocation(), |
| 1109 | diag::err_odr_non_type_parameter_type_inconsistent) |
| 1110 | << D2->getType() << D1->getType(); |
| 1111 | Context.Diag1(D1->getLocation(), diag::note_odr_value_here) |
| 1112 | << D1->getType(); |
| 1113 | return false; |
| 1114 | } |
| 1115 | |
| 1116 | return true; |
| 1117 | } |
| 1118 | |
| 1119 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1120 | TemplateTemplateParmDecl *D1, |
| 1121 | TemplateTemplateParmDecl *D2) { |
| 1122 | // FIXME: Enable once we have variadic templates. |
| 1123 | #if 0 |
| 1124 | if (D1->isParameterPack() != D2->isParameterPack()) { |
| 1125 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1126 | << D2->isParameterPack(); |
| 1127 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1128 | << D1->isParameterPack(); |
| 1129 | return false; |
| 1130 | } |
| 1131 | #endif |
| 1132 | |
| 1133 | // Check template parameter lists. |
| 1134 | return IsStructurallyEquivalent(Context, D1->getTemplateParameters(), |
| 1135 | D2->getTemplateParameters()); |
| 1136 | } |
| 1137 | |
| 1138 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1139 | ClassTemplateDecl *D1, |
| 1140 | ClassTemplateDecl *D2) { |
| 1141 | // Check template parameters. |
| 1142 | if (!IsStructurallyEquivalent(Context, |
| 1143 | D1->getTemplateParameters(), |
| 1144 | D2->getTemplateParameters())) |
| 1145 | return false; |
| 1146 | |
| 1147 | // Check the templated declaration. |
| 1148 | return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(), |
| 1149 | D2->getTemplatedDecl()); |
| 1150 | } |
| 1151 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1152 | /// \brief Determine structural equivalence of two declarations. |
| 1153 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1154 | Decl *D1, Decl *D2) { |
| 1155 | // FIXME: Check for known structural equivalences via a callback of some sort. |
| 1156 | |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1157 | // Check whether we already know that these two declarations are not |
| 1158 | // structurally equivalent. |
| 1159 | if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(), |
| 1160 | D2->getCanonicalDecl()))) |
| 1161 | return false; |
| 1162 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1163 | // Determine whether we've already produced a tentative equivalence for D1. |
| 1164 | Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()]; |
| 1165 | if (EquivToD1) |
| 1166 | return EquivToD1 == D2->getCanonicalDecl(); |
| 1167 | |
| 1168 | // Produce a tentative equivalence D1 <-> D2, which will be checked later. |
| 1169 | EquivToD1 = D2->getCanonicalDecl(); |
| 1170 | Context.DeclsToCheck.push_back(D1->getCanonicalDecl()); |
| 1171 | return true; |
| 1172 | } |
| 1173 | |
| 1174 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1, |
| 1175 | Decl *D2) { |
| 1176 | if (!::IsStructurallyEquivalent(*this, D1, D2)) |
| 1177 | return false; |
| 1178 | |
| 1179 | return !Finish(); |
| 1180 | } |
| 1181 | |
| 1182 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1, |
| 1183 | QualType T2) { |
| 1184 | if (!::IsStructurallyEquivalent(*this, T1, T2)) |
| 1185 | return false; |
| 1186 | |
| 1187 | return !Finish(); |
| 1188 | } |
| 1189 | |
| 1190 | bool StructuralEquivalenceContext::Finish() { |
| 1191 | while (!DeclsToCheck.empty()) { |
| 1192 | // Check the next declaration. |
| 1193 | Decl *D1 = DeclsToCheck.front(); |
| 1194 | DeclsToCheck.pop_front(); |
| 1195 | |
| 1196 | Decl *D2 = TentativeEquivalences[D1]; |
| 1197 | assert(D2 && "Unrecorded tentative equivalence?"); |
| 1198 | |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1199 | bool Equivalent = true; |
| 1200 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1201 | // FIXME: Switch on all declaration kinds. For now, we're just going to |
| 1202 | // check the obvious ones. |
| 1203 | if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) { |
| 1204 | if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) { |
| 1205 | // Check for equivalent structure names. |
| 1206 | IdentifierInfo *Name1 = Record1->getIdentifier(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1207 | if (!Name1 && Record1->getTypedefNameForAnonDecl()) |
| 1208 | Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1209 | IdentifierInfo *Name2 = Record2->getIdentifier(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1210 | if (!Name2 && Record2->getTypedefNameForAnonDecl()) |
| 1211 | Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1212 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 1213 | !::IsStructurallyEquivalent(*this, Record1, Record2)) |
| 1214 | Equivalent = false; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1215 | } else { |
| 1216 | // Record/non-record mismatch. |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1217 | Equivalent = false; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1218 | } |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1219 | } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) { |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1220 | if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) { |
| 1221 | // Check for equivalent enum names. |
| 1222 | IdentifierInfo *Name1 = Enum1->getIdentifier(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1223 | if (!Name1 && Enum1->getTypedefNameForAnonDecl()) |
| 1224 | Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1225 | IdentifierInfo *Name2 = Enum2->getIdentifier(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1226 | if (!Name2 && Enum2->getTypedefNameForAnonDecl()) |
| 1227 | Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1228 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 1229 | !::IsStructurallyEquivalent(*this, Enum1, Enum2)) |
| 1230 | Equivalent = false; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1231 | } else { |
| 1232 | // Enum/non-enum mismatch |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1233 | Equivalent = false; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1234 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1235 | } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) { |
| 1236 | if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1237 | if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(), |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1238 | Typedef2->getIdentifier()) || |
| 1239 | !::IsStructurallyEquivalent(*this, |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1240 | Typedef1->getUnderlyingType(), |
| 1241 | Typedef2->getUnderlyingType())) |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1242 | Equivalent = false; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1243 | } else { |
| 1244 | // Typedef/non-typedef mismatch. |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1245 | Equivalent = false; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1246 | } |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1247 | } else if (ClassTemplateDecl *ClassTemplate1 |
| 1248 | = dyn_cast<ClassTemplateDecl>(D1)) { |
| 1249 | if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) { |
| 1250 | if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(), |
| 1251 | ClassTemplate2->getIdentifier()) || |
| 1252 | !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2)) |
| 1253 | Equivalent = false; |
| 1254 | } else { |
| 1255 | // Class template/non-class-template mismatch. |
| 1256 | Equivalent = false; |
| 1257 | } |
| 1258 | } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) { |
| 1259 | if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) { |
| 1260 | if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) |
| 1261 | Equivalent = false; |
| 1262 | } else { |
| 1263 | // Kind mismatch. |
| 1264 | Equivalent = false; |
| 1265 | } |
| 1266 | } else if (NonTypeTemplateParmDecl *NTTP1 |
| 1267 | = dyn_cast<NonTypeTemplateParmDecl>(D1)) { |
| 1268 | if (NonTypeTemplateParmDecl *NTTP2 |
| 1269 | = dyn_cast<NonTypeTemplateParmDecl>(D2)) { |
| 1270 | if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2)) |
| 1271 | Equivalent = false; |
| 1272 | } else { |
| 1273 | // Kind mismatch. |
| 1274 | Equivalent = false; |
| 1275 | } |
| 1276 | } else if (TemplateTemplateParmDecl *TTP1 |
| 1277 | = dyn_cast<TemplateTemplateParmDecl>(D1)) { |
| 1278 | if (TemplateTemplateParmDecl *TTP2 |
| 1279 | = dyn_cast<TemplateTemplateParmDecl>(D2)) { |
| 1280 | if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) |
| 1281 | Equivalent = false; |
| 1282 | } else { |
| 1283 | // Kind mismatch. |
| 1284 | Equivalent = false; |
| 1285 | } |
| 1286 | } |
| 1287 | |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1288 | if (!Equivalent) { |
| 1289 | // Note that these two declarations are not equivalent (and we already |
| 1290 | // know about it). |
| 1291 | NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(), |
| 1292 | D2->getCanonicalDecl())); |
| 1293 | return true; |
| 1294 | } |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1295 | // FIXME: Check other declaration kinds! |
| 1296 | } |
| 1297 | |
| 1298 | return false; |
| 1299 | } |
| 1300 | |
| 1301 | //---------------------------------------------------------------------------- |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1302 | // Import Types |
| 1303 | //---------------------------------------------------------------------------- |
| 1304 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1305 | QualType ASTNodeImporter::VisitType(const Type *T) { |
Douglas Gregor | 89cc9d6 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 1306 | Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) |
| 1307 | << T->getTypeClassName(); |
| 1308 | return QualType(); |
| 1309 | } |
| 1310 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1311 | QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1312 | switch (T->getKind()) { |
| 1313 | case BuiltinType::Void: return Importer.getToContext().VoidTy; |
| 1314 | case BuiltinType::Bool: return Importer.getToContext().BoolTy; |
| 1315 | |
| 1316 | case BuiltinType::Char_U: |
| 1317 | // The context we're importing from has an unsigned 'char'. If we're |
| 1318 | // importing into a context with a signed 'char', translate to |
| 1319 | // 'unsigned char' instead. |
| 1320 | if (Importer.getToContext().getLangOptions().CharIsSigned) |
| 1321 | return Importer.getToContext().UnsignedCharTy; |
| 1322 | |
| 1323 | return Importer.getToContext().CharTy; |
| 1324 | |
| 1325 | case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy; |
| 1326 | |
| 1327 | case BuiltinType::Char16: |
| 1328 | // FIXME: Make sure that the "to" context supports C++! |
| 1329 | return Importer.getToContext().Char16Ty; |
| 1330 | |
| 1331 | case BuiltinType::Char32: |
| 1332 | // FIXME: Make sure that the "to" context supports C++! |
| 1333 | return Importer.getToContext().Char32Ty; |
| 1334 | |
| 1335 | case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy; |
| 1336 | case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy; |
| 1337 | case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy; |
| 1338 | case BuiltinType::ULongLong: |
| 1339 | return Importer.getToContext().UnsignedLongLongTy; |
| 1340 | case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty; |
| 1341 | |
| 1342 | case BuiltinType::Char_S: |
| 1343 | // The context we're importing from has an unsigned 'char'. If we're |
| 1344 | // importing into a context with a signed 'char', translate to |
| 1345 | // 'unsigned char' instead. |
| 1346 | if (!Importer.getToContext().getLangOptions().CharIsSigned) |
| 1347 | return Importer.getToContext().SignedCharTy; |
| 1348 | |
| 1349 | return Importer.getToContext().CharTy; |
| 1350 | |
| 1351 | case BuiltinType::SChar: return Importer.getToContext().SignedCharTy; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1352 | case BuiltinType::WChar_S: |
| 1353 | case BuiltinType::WChar_U: |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1354 | // FIXME: If not in C++, shall we translate to the C equivalent of |
| 1355 | // wchar_t? |
| 1356 | return Importer.getToContext().WCharTy; |
| 1357 | |
| 1358 | case BuiltinType::Short : return Importer.getToContext().ShortTy; |
| 1359 | case BuiltinType::Int : return Importer.getToContext().IntTy; |
| 1360 | case BuiltinType::Long : return Importer.getToContext().LongTy; |
| 1361 | case BuiltinType::LongLong : return Importer.getToContext().LongLongTy; |
| 1362 | case BuiltinType::Int128 : return Importer.getToContext().Int128Ty; |
| 1363 | case BuiltinType::Float: return Importer.getToContext().FloatTy; |
| 1364 | case BuiltinType::Double: return Importer.getToContext().DoubleTy; |
| 1365 | case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy; |
| 1366 | |
| 1367 | case BuiltinType::NullPtr: |
| 1368 | // FIXME: Make sure that the "to" context supports C++0x! |
| 1369 | return Importer.getToContext().NullPtrTy; |
| 1370 | |
| 1371 | case BuiltinType::Overload: return Importer.getToContext().OverloadTy; |
| 1372 | case BuiltinType::Dependent: return Importer.getToContext().DependentTy; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 1373 | case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1374 | case BuiltinType::BoundMember: return Importer.getToContext().BoundMemberTy; |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1375 | |
| 1376 | case BuiltinType::ObjCId: |
| 1377 | // FIXME: Make sure that the "to" context supports Objective-C! |
| 1378 | return Importer.getToContext().ObjCBuiltinIdTy; |
| 1379 | |
| 1380 | case BuiltinType::ObjCClass: |
| 1381 | return Importer.getToContext().ObjCBuiltinClassTy; |
| 1382 | |
| 1383 | case BuiltinType::ObjCSel: |
| 1384 | return Importer.getToContext().ObjCBuiltinSelTy; |
| 1385 | } |
| 1386 | |
| 1387 | return QualType(); |
| 1388 | } |
| 1389 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1390 | QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1391 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1392 | if (ToElementType.isNull()) |
| 1393 | return QualType(); |
| 1394 | |
| 1395 | return Importer.getToContext().getComplexType(ToElementType); |
| 1396 | } |
| 1397 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1398 | QualType ASTNodeImporter::VisitPointerType(const PointerType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1399 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1400 | if (ToPointeeType.isNull()) |
| 1401 | return QualType(); |
| 1402 | |
| 1403 | return Importer.getToContext().getPointerType(ToPointeeType); |
| 1404 | } |
| 1405 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1406 | QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1407 | // FIXME: Check for blocks support in "to" context. |
| 1408 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1409 | if (ToPointeeType.isNull()) |
| 1410 | return QualType(); |
| 1411 | |
| 1412 | return Importer.getToContext().getBlockPointerType(ToPointeeType); |
| 1413 | } |
| 1414 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1415 | QualType |
| 1416 | ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1417 | // FIXME: Check for C++ support in "to" context. |
| 1418 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1419 | if (ToPointeeType.isNull()) |
| 1420 | return QualType(); |
| 1421 | |
| 1422 | return Importer.getToContext().getLValueReferenceType(ToPointeeType); |
| 1423 | } |
| 1424 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1425 | QualType |
| 1426 | ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1427 | // FIXME: Check for C++0x support in "to" context. |
| 1428 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1429 | if (ToPointeeType.isNull()) |
| 1430 | return QualType(); |
| 1431 | |
| 1432 | return Importer.getToContext().getRValueReferenceType(ToPointeeType); |
| 1433 | } |
| 1434 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1435 | QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1436 | // FIXME: Check for C++ support in "to" context. |
| 1437 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1438 | if (ToPointeeType.isNull()) |
| 1439 | return QualType(); |
| 1440 | |
| 1441 | QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); |
| 1442 | return Importer.getToContext().getMemberPointerType(ToPointeeType, |
| 1443 | ClassType.getTypePtr()); |
| 1444 | } |
| 1445 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1446 | QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1447 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1448 | if (ToElementType.isNull()) |
| 1449 | return QualType(); |
| 1450 | |
| 1451 | return Importer.getToContext().getConstantArrayType(ToElementType, |
| 1452 | T->getSize(), |
| 1453 | T->getSizeModifier(), |
| 1454 | T->getIndexTypeCVRQualifiers()); |
| 1455 | } |
| 1456 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1457 | QualType |
| 1458 | ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1459 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1460 | if (ToElementType.isNull()) |
| 1461 | return QualType(); |
| 1462 | |
| 1463 | return Importer.getToContext().getIncompleteArrayType(ToElementType, |
| 1464 | T->getSizeModifier(), |
| 1465 | T->getIndexTypeCVRQualifiers()); |
| 1466 | } |
| 1467 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1468 | QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1469 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1470 | if (ToElementType.isNull()) |
| 1471 | return QualType(); |
| 1472 | |
| 1473 | Expr *Size = Importer.Import(T->getSizeExpr()); |
| 1474 | if (!Size) |
| 1475 | return QualType(); |
| 1476 | |
| 1477 | SourceRange Brackets = Importer.Import(T->getBracketsRange()); |
| 1478 | return Importer.getToContext().getVariableArrayType(ToElementType, Size, |
| 1479 | T->getSizeModifier(), |
| 1480 | T->getIndexTypeCVRQualifiers(), |
| 1481 | Brackets); |
| 1482 | } |
| 1483 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1484 | QualType ASTNodeImporter::VisitVectorType(const VectorType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1485 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1486 | if (ToElementType.isNull()) |
| 1487 | return QualType(); |
| 1488 | |
| 1489 | return Importer.getToContext().getVectorType(ToElementType, |
| 1490 | T->getNumElements(), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1491 | T->getVectorKind()); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1494 | QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1495 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1496 | if (ToElementType.isNull()) |
| 1497 | return QualType(); |
| 1498 | |
| 1499 | return Importer.getToContext().getExtVectorType(ToElementType, |
| 1500 | T->getNumElements()); |
| 1501 | } |
| 1502 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1503 | QualType |
| 1504 | ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1505 | // FIXME: What happens if we're importing a function without a prototype |
| 1506 | // into C++? Should we make it variadic? |
| 1507 | QualType ToResultType = Importer.Import(T->getResultType()); |
| 1508 | if (ToResultType.isNull()) |
| 1509 | return QualType(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1510 | |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1511 | return Importer.getToContext().getFunctionNoProtoType(ToResultType, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1512 | T->getExtInfo()); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1515 | QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1516 | QualType ToResultType = Importer.Import(T->getResultType()); |
| 1517 | if (ToResultType.isNull()) |
| 1518 | return QualType(); |
| 1519 | |
| 1520 | // Import argument types |
| 1521 | llvm::SmallVector<QualType, 4> ArgTypes; |
| 1522 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
| 1523 | AEnd = T->arg_type_end(); |
| 1524 | A != AEnd; ++A) { |
| 1525 | QualType ArgType = Importer.Import(*A); |
| 1526 | if (ArgType.isNull()) |
| 1527 | return QualType(); |
| 1528 | ArgTypes.push_back(ArgType); |
| 1529 | } |
| 1530 | |
| 1531 | // Import exception types |
| 1532 | llvm::SmallVector<QualType, 4> ExceptionTypes; |
| 1533 | for (FunctionProtoType::exception_iterator E = T->exception_begin(), |
| 1534 | EEnd = T->exception_end(); |
| 1535 | E != EEnd; ++E) { |
| 1536 | QualType ExceptionType = Importer.Import(*E); |
| 1537 | if (ExceptionType.isNull()) |
| 1538 | return QualType(); |
| 1539 | ExceptionTypes.push_back(ExceptionType); |
| 1540 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1541 | |
| 1542 | FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo(); |
| 1543 | EPI.Exceptions = ExceptionTypes.data(); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1544 | |
| 1545 | return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1546 | ArgTypes.size(), EPI); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1549 | QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1550 | TypedefNameDecl *ToDecl |
| 1551 | = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl())); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1552 | if (!ToDecl) |
| 1553 | return QualType(); |
| 1554 | |
| 1555 | return Importer.getToContext().getTypeDeclType(ToDecl); |
| 1556 | } |
| 1557 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1558 | QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1559 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1560 | if (!ToExpr) |
| 1561 | return QualType(); |
| 1562 | |
| 1563 | return Importer.getToContext().getTypeOfExprType(ToExpr); |
| 1564 | } |
| 1565 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1566 | QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1567 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1568 | if (ToUnderlyingType.isNull()) |
| 1569 | return QualType(); |
| 1570 | |
| 1571 | return Importer.getToContext().getTypeOfType(ToUnderlyingType); |
| 1572 | } |
| 1573 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1574 | QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1575 | // FIXME: Make sure that the "to" context supports C++0x! |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1576 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1577 | if (!ToExpr) |
| 1578 | return QualType(); |
| 1579 | |
| 1580 | return Importer.getToContext().getDecltypeType(ToExpr); |
| 1581 | } |
| 1582 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame^] | 1583 | QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { |
| 1584 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 1585 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1586 | if (ToBaseType.isNull() || ToUnderlyingType.isNull()) |
| 1587 | return QualType(); |
| 1588 | |
| 1589 | return Importer.getToContext().getUnaryTransformType(ToBaseType, |
| 1590 | ToUnderlyingType, |
| 1591 | T->getUTTKind()); |
| 1592 | } |
| 1593 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1594 | QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { |
| 1595 | // FIXME: Make sure that the "to" context supports C++0x! |
| 1596 | QualType FromDeduced = T->getDeducedType(); |
| 1597 | QualType ToDeduced; |
| 1598 | if (!FromDeduced.isNull()) { |
| 1599 | ToDeduced = Importer.Import(FromDeduced); |
| 1600 | if (ToDeduced.isNull()) |
| 1601 | return QualType(); |
| 1602 | } |
| 1603 | |
| 1604 | return Importer.getToContext().getAutoType(ToDeduced); |
| 1605 | } |
| 1606 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1607 | QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1608 | RecordDecl *ToDecl |
| 1609 | = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); |
| 1610 | if (!ToDecl) |
| 1611 | return QualType(); |
| 1612 | |
| 1613 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 1614 | } |
| 1615 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1616 | QualType ASTNodeImporter::VisitEnumType(const EnumType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1617 | EnumDecl *ToDecl |
| 1618 | = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); |
| 1619 | if (!ToDecl) |
| 1620 | return QualType(); |
| 1621 | |
| 1622 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 1623 | } |
| 1624 | |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1625 | QualType ASTNodeImporter::VisitTemplateSpecializationType( |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1626 | const TemplateSpecializationType *T) { |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1627 | TemplateName ToTemplate = Importer.Import(T->getTemplateName()); |
| 1628 | if (ToTemplate.isNull()) |
| 1629 | return QualType(); |
| 1630 | |
| 1631 | llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs; |
| 1632 | if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs)) |
| 1633 | return QualType(); |
| 1634 | |
| 1635 | QualType ToCanonType; |
| 1636 | if (!QualType(T, 0).isCanonical()) { |
| 1637 | QualType FromCanonType |
| 1638 | = Importer.getFromContext().getCanonicalType(QualType(T, 0)); |
| 1639 | ToCanonType =Importer.Import(FromCanonType); |
| 1640 | if (ToCanonType.isNull()) |
| 1641 | return QualType(); |
| 1642 | } |
| 1643 | return Importer.getToContext().getTemplateSpecializationType(ToTemplate, |
| 1644 | ToTemplateArgs.data(), |
| 1645 | ToTemplateArgs.size(), |
| 1646 | ToCanonType); |
| 1647 | } |
| 1648 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1649 | QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1650 | NestedNameSpecifier *ToQualifier = 0; |
| 1651 | // Note: the qualifier in an ElaboratedType is optional. |
| 1652 | if (T->getQualifier()) { |
| 1653 | ToQualifier = Importer.Import(T->getQualifier()); |
| 1654 | if (!ToQualifier) |
| 1655 | return QualType(); |
| 1656 | } |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1657 | |
| 1658 | QualType ToNamedType = Importer.Import(T->getNamedType()); |
| 1659 | if (ToNamedType.isNull()) |
| 1660 | return QualType(); |
| 1661 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1662 | return Importer.getToContext().getElaboratedType(T->getKeyword(), |
| 1663 | ToQualifier, ToNamedType); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1666 | QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1667 | ObjCInterfaceDecl *Class |
| 1668 | = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); |
| 1669 | if (!Class) |
| 1670 | return QualType(); |
| 1671 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1672 | return Importer.getToContext().getObjCInterfaceType(Class); |
| 1673 | } |
| 1674 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1675 | QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1676 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 1677 | if (ToBaseType.isNull()) |
| 1678 | return QualType(); |
| 1679 | |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1680 | llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1681 | for (ObjCObjectType::qual_iterator P = T->qual_begin(), |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1682 | PEnd = T->qual_end(); |
| 1683 | P != PEnd; ++P) { |
| 1684 | ObjCProtocolDecl *Protocol |
| 1685 | = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P)); |
| 1686 | if (!Protocol) |
| 1687 | return QualType(); |
| 1688 | Protocols.push_back(Protocol); |
| 1689 | } |
| 1690 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1691 | return Importer.getToContext().getObjCObjectType(ToBaseType, |
| 1692 | Protocols.data(), |
| 1693 | Protocols.size()); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1696 | QualType |
| 1697 | ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1698 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1699 | if (ToPointeeType.isNull()) |
| 1700 | return QualType(); |
| 1701 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1702 | return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 1705 | //---------------------------------------------------------------------------- |
| 1706 | // Import Declarations |
| 1707 | //---------------------------------------------------------------------------- |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1708 | bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 1709 | DeclContext *&LexicalDC, |
| 1710 | DeclarationName &Name, |
| 1711 | SourceLocation &Loc) { |
| 1712 | // Import the context of this declaration. |
| 1713 | DC = Importer.ImportContext(D->getDeclContext()); |
| 1714 | if (!DC) |
| 1715 | return true; |
| 1716 | |
| 1717 | LexicalDC = DC; |
| 1718 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 1719 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 1720 | if (!LexicalDC) |
| 1721 | return true; |
| 1722 | } |
| 1723 | |
| 1724 | // Import the name of this declaration. |
| 1725 | Name = Importer.Import(D->getDeclName()); |
| 1726 | if (D->getDeclName() && !Name) |
| 1727 | return true; |
| 1728 | |
| 1729 | // Import the location of this declaration. |
| 1730 | Loc = Importer.Import(D->getLocation()); |
| 1731 | return false; |
| 1732 | } |
| 1733 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1734 | void |
| 1735 | ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 1736 | DeclarationNameInfo& To) { |
| 1737 | // NOTE: To.Name and To.Loc are already imported. |
| 1738 | // We only have to import To.LocInfo. |
| 1739 | switch (To.getName().getNameKind()) { |
| 1740 | case DeclarationName::Identifier: |
| 1741 | case DeclarationName::ObjCZeroArgSelector: |
| 1742 | case DeclarationName::ObjCOneArgSelector: |
| 1743 | case DeclarationName::ObjCMultiArgSelector: |
| 1744 | case DeclarationName::CXXUsingDirective: |
| 1745 | return; |
| 1746 | |
| 1747 | case DeclarationName::CXXOperatorName: { |
| 1748 | SourceRange Range = From.getCXXOperatorNameRange(); |
| 1749 | To.setCXXOperatorNameRange(Importer.Import(Range)); |
| 1750 | return; |
| 1751 | } |
| 1752 | case DeclarationName::CXXLiteralOperatorName: { |
| 1753 | SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); |
| 1754 | To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); |
| 1755 | return; |
| 1756 | } |
| 1757 | case DeclarationName::CXXConstructorName: |
| 1758 | case DeclarationName::CXXDestructorName: |
| 1759 | case DeclarationName::CXXConversionFunctionName: { |
| 1760 | TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); |
| 1761 | To.setNamedTypeInfo(Importer.Import(FromTInfo)); |
| 1762 | return; |
| 1763 | } |
| 1764 | assert(0 && "Unknown name kind."); |
| 1765 | } |
| 1766 | } |
| 1767 | |
Douglas Gregor | d8868a6 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 1768 | void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { |
| 1769 | if (Importer.isMinimalImport() && !ForceImport) { |
| 1770 | if (DeclContext *ToDC = Importer.ImportContext(FromDC)) { |
| 1771 | ToDC->setHasExternalLexicalStorage(); |
| 1772 | ToDC->setHasExternalVisibleStorage(); |
| 1773 | } |
| 1774 | return; |
| 1775 | } |
| 1776 | |
Douglas Gregor | 083a821 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 1777 | for (DeclContext::decl_iterator From = FromDC->decls_begin(), |
| 1778 | FromEnd = FromDC->decls_end(); |
| 1779 | From != FromEnd; |
| 1780 | ++From) |
| 1781 | Importer.Import(*From); |
| 1782 | } |
| 1783 | |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1784 | bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) { |
| 1785 | if (To->getDefinition()) |
| 1786 | return false; |
| 1787 | |
| 1788 | To->startDefinition(); |
| 1789 | |
| 1790 | // Add base classes. |
| 1791 | if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) { |
| 1792 | CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From); |
| 1793 | |
| 1794 | llvm::SmallVector<CXXBaseSpecifier *, 4> Bases; |
| 1795 | for (CXXRecordDecl::base_class_iterator |
| 1796 | Base1 = FromCXX->bases_begin(), |
| 1797 | FromBaseEnd = FromCXX->bases_end(); |
| 1798 | Base1 != FromBaseEnd; |
| 1799 | ++Base1) { |
| 1800 | QualType T = Importer.Import(Base1->getType()); |
| 1801 | if (T.isNull()) |
Douglas Gregor | c04d9d1 | 2010-12-02 19:33:37 +0000 | [diff] [blame] | 1802 | return true; |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1803 | |
| 1804 | SourceLocation EllipsisLoc; |
| 1805 | if (Base1->isPackExpansion()) |
| 1806 | EllipsisLoc = Importer.Import(Base1->getEllipsisLoc()); |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1807 | |
| 1808 | Bases.push_back( |
| 1809 | new (Importer.getToContext()) |
| 1810 | CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()), |
| 1811 | Base1->isVirtual(), |
| 1812 | Base1->isBaseOfClass(), |
| 1813 | Base1->getAccessSpecifierAsWritten(), |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1814 | Importer.Import(Base1->getTypeSourceInfo()), |
| 1815 | EllipsisLoc)); |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1816 | } |
| 1817 | if (!Bases.empty()) |
| 1818 | ToCXX->setBases(Bases.data(), Bases.size()); |
| 1819 | } |
| 1820 | |
| 1821 | ImportDeclContext(From); |
| 1822 | To->completeDefinition(); |
Douglas Gregor | c04d9d1 | 2010-12-02 19:33:37 +0000 | [diff] [blame] | 1823 | return false; |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1826 | TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList( |
| 1827 | TemplateParameterList *Params) { |
| 1828 | llvm::SmallVector<NamedDecl *, 4> ToParams; |
| 1829 | ToParams.reserve(Params->size()); |
| 1830 | for (TemplateParameterList::iterator P = Params->begin(), |
| 1831 | PEnd = Params->end(); |
| 1832 | P != PEnd; ++P) { |
| 1833 | Decl *To = Importer.Import(*P); |
| 1834 | if (!To) |
| 1835 | return 0; |
| 1836 | |
| 1837 | ToParams.push_back(cast<NamedDecl>(To)); |
| 1838 | } |
| 1839 | |
| 1840 | return TemplateParameterList::Create(Importer.getToContext(), |
| 1841 | Importer.Import(Params->getTemplateLoc()), |
| 1842 | Importer.Import(Params->getLAngleLoc()), |
| 1843 | ToParams.data(), ToParams.size(), |
| 1844 | Importer.Import(Params->getRAngleLoc())); |
| 1845 | } |
| 1846 | |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1847 | TemplateArgument |
| 1848 | ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { |
| 1849 | switch (From.getKind()) { |
| 1850 | case TemplateArgument::Null: |
| 1851 | return TemplateArgument(); |
| 1852 | |
| 1853 | case TemplateArgument::Type: { |
| 1854 | QualType ToType = Importer.Import(From.getAsType()); |
| 1855 | if (ToType.isNull()) |
| 1856 | return TemplateArgument(); |
| 1857 | return TemplateArgument(ToType); |
| 1858 | } |
| 1859 | |
| 1860 | case TemplateArgument::Integral: { |
| 1861 | QualType ToType = Importer.Import(From.getIntegralType()); |
| 1862 | if (ToType.isNull()) |
| 1863 | return TemplateArgument(); |
| 1864 | return TemplateArgument(*From.getAsIntegral(), ToType); |
| 1865 | } |
| 1866 | |
| 1867 | case TemplateArgument::Declaration: |
| 1868 | if (Decl *To = Importer.Import(From.getAsDecl())) |
| 1869 | return TemplateArgument(To); |
| 1870 | return TemplateArgument(); |
| 1871 | |
| 1872 | case TemplateArgument::Template: { |
| 1873 | TemplateName ToTemplate = Importer.Import(From.getAsTemplate()); |
| 1874 | if (ToTemplate.isNull()) |
| 1875 | return TemplateArgument(); |
| 1876 | |
| 1877 | return TemplateArgument(ToTemplate); |
| 1878 | } |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1879 | |
| 1880 | case TemplateArgument::TemplateExpansion: { |
| 1881 | TemplateName ToTemplate |
| 1882 | = Importer.Import(From.getAsTemplateOrTemplatePattern()); |
| 1883 | if (ToTemplate.isNull()) |
| 1884 | return TemplateArgument(); |
| 1885 | |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 1886 | return TemplateArgument(ToTemplate, From.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1889 | case TemplateArgument::Expression: |
| 1890 | if (Expr *ToExpr = Importer.Import(From.getAsExpr())) |
| 1891 | return TemplateArgument(ToExpr); |
| 1892 | return TemplateArgument(); |
| 1893 | |
| 1894 | case TemplateArgument::Pack: { |
| 1895 | llvm::SmallVector<TemplateArgument, 2> ToPack; |
| 1896 | ToPack.reserve(From.pack_size()); |
| 1897 | if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack)) |
| 1898 | return TemplateArgument(); |
| 1899 | |
| 1900 | TemplateArgument *ToArgs |
| 1901 | = new (Importer.getToContext()) TemplateArgument[ToPack.size()]; |
| 1902 | std::copy(ToPack.begin(), ToPack.end(), ToArgs); |
| 1903 | return TemplateArgument(ToArgs, ToPack.size()); |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | llvm_unreachable("Invalid template argument kind"); |
| 1908 | return TemplateArgument(); |
| 1909 | } |
| 1910 | |
| 1911 | bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs, |
| 1912 | unsigned NumFromArgs, |
| 1913 | llvm::SmallVectorImpl<TemplateArgument> &ToArgs) { |
| 1914 | for (unsigned I = 0; I != NumFromArgs; ++I) { |
| 1915 | TemplateArgument To = ImportTemplateArgument(FromArgs[I]); |
| 1916 | if (To.isNull() && !FromArgs[I].isNull()) |
| 1917 | return true; |
| 1918 | |
| 1919 | ToArgs.push_back(To); |
| 1920 | } |
| 1921 | |
| 1922 | return false; |
| 1923 | } |
| 1924 | |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1925 | bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1926 | RecordDecl *ToRecord) { |
Benjamin Kramer | bb2d176 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 1927 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1928 | Importer.getToContext(), |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1929 | Importer.getNonEquivalentDecls()); |
Benjamin Kramer | bb2d176 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 1930 | return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord); |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1933 | bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { |
Benjamin Kramer | bb2d176 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 1934 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1935 | Importer.getToContext(), |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1936 | Importer.getNonEquivalentDecls()); |
Benjamin Kramer | bb2d176 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 1937 | return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum); |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1940 | bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, |
| 1941 | ClassTemplateDecl *To) { |
| 1942 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| 1943 | Importer.getToContext(), |
| 1944 | Importer.getNonEquivalentDecls()); |
| 1945 | return Ctx.IsStructurallyEquivalent(From, To); |
| 1946 | } |
| 1947 | |
Douglas Gregor | 89cc9d6 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 1948 | Decl *ASTNodeImporter::VisitDecl(Decl *D) { |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 1949 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
Douglas Gregor | 89cc9d6 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 1950 | << D->getDeclKindName(); |
| 1951 | return 0; |
| 1952 | } |
| 1953 | |
Douglas Gregor | 788c62d | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 1954 | Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1955 | // Import the major distinguishing characteristics of this namespace. |
| 1956 | DeclContext *DC, *LexicalDC; |
| 1957 | DeclarationName Name; |
| 1958 | SourceLocation Loc; |
| 1959 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 1960 | return 0; |
| 1961 | |
| 1962 | NamespaceDecl *MergeWithNamespace = 0; |
| 1963 | if (!Name) { |
| 1964 | // This is an anonymous namespace. Adopt an existing anonymous |
| 1965 | // namespace if we can. |
| 1966 | // FIXME: Not testable. |
| 1967 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 1968 | MergeWithNamespace = TU->getAnonymousNamespace(); |
| 1969 | else |
| 1970 | MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); |
| 1971 | } else { |
| 1972 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 1973 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 1974 | Lookup.first != Lookup.second; |
| 1975 | ++Lookup.first) { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1976 | if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace)) |
Douglas Gregor | 788c62d | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 1977 | continue; |
| 1978 | |
| 1979 | if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) { |
| 1980 | MergeWithNamespace = FoundNS; |
| 1981 | ConflictingDecls.clear(); |
| 1982 | break; |
| 1983 | } |
| 1984 | |
| 1985 | ConflictingDecls.push_back(*Lookup.first); |
| 1986 | } |
| 1987 | |
| 1988 | if (!ConflictingDecls.empty()) { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1989 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, |
Douglas Gregor | 788c62d | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 1990 | ConflictingDecls.data(), |
| 1991 | ConflictingDecls.size()); |
| 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | // Create the "to" namespace, if needed. |
| 1996 | NamespaceDecl *ToNamespace = MergeWithNamespace; |
| 1997 | if (!ToNamespace) { |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 1998 | ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, |
| 1999 | Importer.Import(D->getLocStart()), |
| 2000 | Loc, Name.getAsIdentifierInfo()); |
Douglas Gregor | 788c62d | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2001 | ToNamespace->setLexicalDeclContext(LexicalDC); |
| 2002 | LexicalDC->addDecl(ToNamespace); |
| 2003 | |
| 2004 | // If this is an anonymous namespace, register it as the anonymous |
| 2005 | // namespace within its context. |
| 2006 | if (!Name) { |
| 2007 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 2008 | TU->setAnonymousNamespace(ToNamespace); |
| 2009 | else |
| 2010 | cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); |
| 2011 | } |
| 2012 | } |
| 2013 | Importer.Imported(D, ToNamespace); |
| 2014 | |
| 2015 | ImportDeclContext(D); |
| 2016 | |
| 2017 | return ToNamespace; |
| 2018 | } |
| 2019 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2020 | Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { |
Douglas Gregor | 9e5d996 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2021 | // Import the major distinguishing characteristics of this typedef. |
| 2022 | DeclContext *DC, *LexicalDC; |
| 2023 | DeclarationName Name; |
| 2024 | SourceLocation Loc; |
| 2025 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2026 | return 0; |
| 2027 | |
Douglas Gregor | 9e5d996 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2028 | // If this typedef is not in block scope, determine whether we've |
| 2029 | // seen a typedef with the same name (that we can merge with) or any |
| 2030 | // other entity by that name (which name lookup could conflict with). |
| 2031 | if (!DC->isFunctionOrMethod()) { |
| 2032 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 2033 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 2034 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2035 | Lookup.first != Lookup.second; |
| 2036 | ++Lookup.first) { |
| 2037 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 2038 | continue; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2039 | if (TypedefNameDecl *FoundTypedef = |
| 2040 | dyn_cast<TypedefNameDecl>(*Lookup.first)) { |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2041 | if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), |
| 2042 | FoundTypedef->getUnderlyingType())) |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2043 | return Importer.Imported(D, FoundTypedef); |
Douglas Gregor | 9e5d996 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | ConflictingDecls.push_back(*Lookup.first); |
| 2047 | } |
| 2048 | |
| 2049 | if (!ConflictingDecls.empty()) { |
| 2050 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2051 | ConflictingDecls.data(), |
| 2052 | ConflictingDecls.size()); |
| 2053 | if (!Name) |
| 2054 | return 0; |
| 2055 | } |
| 2056 | } |
| 2057 | |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2058 | // Import the underlying type of this typedef; |
| 2059 | QualType T = Importer.Import(D->getUnderlyingType()); |
| 2060 | if (T.isNull()) |
| 2061 | return 0; |
| 2062 | |
Douglas Gregor | 9e5d996 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2063 | // Create the new typedef node. |
| 2064 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 2065 | SourceLocation StartL = Importer.Import(D->getLocStart()); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2066 | TypedefNameDecl *ToTypedef; |
| 2067 | if (IsAlias) |
| 2068 | ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC, |
| 2069 | StartL, Loc, |
| 2070 | Name.getAsIdentifierInfo(), |
| 2071 | TInfo); |
| 2072 | else |
| 2073 | ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, |
| 2074 | StartL, Loc, |
| 2075 | Name.getAsIdentifierInfo(), |
| 2076 | TInfo); |
Douglas Gregor | 325bf17 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2077 | ToTypedef->setAccess(D->getAccess()); |
Douglas Gregor | 9e5d996 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2078 | ToTypedef->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2079 | Importer.Imported(D, ToTypedef); |
Douglas Gregor | 9e5d996 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2080 | LexicalDC->addDecl(ToTypedef); |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2081 | |
Douglas Gregor | 9e5d996 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2082 | return ToTypedef; |
| 2083 | } |
| 2084 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2085 | Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { |
| 2086 | return VisitTypedefNameDecl(D, /*IsAlias=*/false); |
| 2087 | } |
| 2088 | |
| 2089 | Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 2090 | return VisitTypedefNameDecl(D, /*IsAlias=*/true); |
| 2091 | } |
| 2092 | |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2093 | Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { |
| 2094 | // Import the major distinguishing characteristics of this enum. |
| 2095 | DeclContext *DC, *LexicalDC; |
| 2096 | DeclarationName Name; |
| 2097 | SourceLocation Loc; |
| 2098 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2099 | return 0; |
| 2100 | |
| 2101 | // Figure out what enum name we're looking for. |
| 2102 | unsigned IDNS = Decl::IDNS_Tag; |
| 2103 | DeclarationName SearchName = Name; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2104 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 2105 | SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2106 | IDNS = Decl::IDNS_Ordinary; |
| 2107 | } else if (Importer.getToContext().getLangOptions().CPlusPlus) |
| 2108 | IDNS |= Decl::IDNS_Ordinary; |
| 2109 | |
| 2110 | // We may already have an enum of the same name; try to find and match it. |
| 2111 | if (!DC->isFunctionOrMethod() && SearchName) { |
| 2112 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 2113 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2114 | Lookup.first != Lookup.second; |
| 2115 | ++Lookup.first) { |
| 2116 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 2117 | continue; |
| 2118 | |
| 2119 | Decl *Found = *Lookup.first; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2120 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2121 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 2122 | Found = Tag->getDecl(); |
| 2123 | } |
| 2124 | |
| 2125 | if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) { |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2126 | if (IsStructuralMatch(D, FoundEnum)) |
| 2127 | return Importer.Imported(D, FoundEnum); |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | ConflictingDecls.push_back(*Lookup.first); |
| 2131 | } |
| 2132 | |
| 2133 | if (!ConflictingDecls.empty()) { |
| 2134 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2135 | ConflictingDecls.data(), |
| 2136 | ConflictingDecls.size()); |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | // Create the enum declaration. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2141 | EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, |
| 2142 | Importer.Import(D->getLocStart()), |
| 2143 | Loc, Name.getAsIdentifierInfo(), 0, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2144 | D->isScoped(), D->isScopedUsingClassTag(), |
| 2145 | D->isFixed()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2146 | // Import the qualifier, if any. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2147 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | 325bf17 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2148 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2149 | D2->setLexicalDeclContext(LexicalDC); |
| 2150 | Importer.Imported(D, D2); |
| 2151 | LexicalDC->addDecl(D2); |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2152 | |
| 2153 | // Import the integer type. |
| 2154 | QualType ToIntegerType = Importer.Import(D->getIntegerType()); |
| 2155 | if (ToIntegerType.isNull()) |
| 2156 | return 0; |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2157 | D2->setIntegerType(ToIntegerType); |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2158 | |
| 2159 | // Import the definition |
| 2160 | if (D->isDefinition()) { |
| 2161 | QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D)); |
| 2162 | if (T.isNull()) |
| 2163 | return 0; |
| 2164 | |
| 2165 | QualType ToPromotionType = Importer.Import(D->getPromotionType()); |
| 2166 | if (ToPromotionType.isNull()) |
| 2167 | return 0; |
| 2168 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2169 | D2->startDefinition(); |
Douglas Gregor | 083a821 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 2170 | ImportDeclContext(D); |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 2171 | |
| 2172 | // FIXME: we might need to merge the number of positive or negative bits |
| 2173 | // if the enumerator lists don't match. |
| 2174 | D2->completeDefinition(T, ToPromotionType, |
| 2175 | D->getNumPositiveBits(), |
| 2176 | D->getNumNegativeBits()); |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2179 | return D2; |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2182 | Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { |
| 2183 | // If this record has a definition in the translation unit we're coming from, |
| 2184 | // but this particular declaration is not that definition, import the |
| 2185 | // definition and map to that. |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2186 | TagDecl *Definition = D->getDefinition(); |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2187 | if (Definition && Definition != D) { |
| 2188 | Decl *ImportedDef = Importer.Import(Definition); |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2189 | if (!ImportedDef) |
| 2190 | return 0; |
| 2191 | |
| 2192 | return Importer.Imported(D, ImportedDef); |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | // Import the major distinguishing characteristics of this record. |
| 2196 | DeclContext *DC, *LexicalDC; |
| 2197 | DeclarationName Name; |
| 2198 | SourceLocation Loc; |
| 2199 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2200 | return 0; |
| 2201 | |
| 2202 | // Figure out what structure name we're looking for. |
| 2203 | unsigned IDNS = Decl::IDNS_Tag; |
| 2204 | DeclarationName SearchName = Name; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2205 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 2206 | SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2207 | IDNS = Decl::IDNS_Ordinary; |
| 2208 | } else if (Importer.getToContext().getLangOptions().CPlusPlus) |
| 2209 | IDNS |= Decl::IDNS_Ordinary; |
| 2210 | |
| 2211 | // We may already have a record of the same name; try to find and match it. |
Douglas Gregor | e72b5dc | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2212 | RecordDecl *AdoptDecl = 0; |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2213 | if (!DC->isFunctionOrMethod() && SearchName) { |
| 2214 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 2215 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2216 | Lookup.first != Lookup.second; |
| 2217 | ++Lookup.first) { |
| 2218 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 2219 | continue; |
| 2220 | |
| 2221 | Decl *Found = *Lookup.first; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2222 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2223 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 2224 | Found = Tag->getDecl(); |
| 2225 | } |
| 2226 | |
| 2227 | if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) { |
Douglas Gregor | e72b5dc | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2228 | if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { |
| 2229 | if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) { |
| 2230 | // The record types structurally match, or the "from" translation |
| 2231 | // unit only had a forward declaration anyway; call it the same |
| 2232 | // function. |
| 2233 | // FIXME: For C++, we should also merge methods here. |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2234 | return Importer.Imported(D, FoundDef); |
Douglas Gregor | e72b5dc | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2235 | } |
| 2236 | } else { |
| 2237 | // We have a forward declaration of this type, so adopt that forward |
| 2238 | // declaration rather than building a new one. |
| 2239 | AdoptDecl = FoundRecord; |
| 2240 | continue; |
| 2241 | } |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | ConflictingDecls.push_back(*Lookup.first); |
| 2245 | } |
| 2246 | |
| 2247 | if (!ConflictingDecls.empty()) { |
| 2248 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2249 | ConflictingDecls.data(), |
| 2250 | ConflictingDecls.size()); |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | // Create the record declaration. |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2255 | RecordDecl *D2 = AdoptDecl; |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2256 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2257 | if (!D2) { |
John McCall | 5250f27 | 2010-06-03 19:28:45 +0000 | [diff] [blame] | 2258 | if (isa<CXXRecordDecl>(D)) { |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2259 | CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(), |
Douglas Gregor | e72b5dc | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2260 | D->getTagKind(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2261 | DC, StartLoc, Loc, |
| 2262 | Name.getAsIdentifierInfo()); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2263 | D2 = D2CXX; |
Douglas Gregor | 325bf17 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2264 | D2->setAccess(D->getAccess()); |
Douglas Gregor | e72b5dc | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2265 | } else { |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2266 | D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2267 | DC, StartLoc, Loc, Name.getAsIdentifierInfo()); |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2268 | } |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2269 | |
| 2270 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2271 | D2->setLexicalDeclContext(LexicalDC); |
| 2272 | LexicalDC->addDecl(D2); |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2273 | } |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2274 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2275 | Importer.Imported(D, D2); |
Douglas Gregor | e72b5dc | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2276 | |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2277 | if (D->isDefinition() && ImportDefinition(D, D2)) |
| 2278 | return 0; |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2279 | |
Douglas Gregor | 73dc30b | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2280 | return D2; |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2283 | Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 2284 | // Import the major distinguishing characteristics of this enumerator. |
| 2285 | DeclContext *DC, *LexicalDC; |
| 2286 | DeclarationName Name; |
| 2287 | SourceLocation Loc; |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2288 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2289 | return 0; |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2290 | |
| 2291 | QualType T = Importer.Import(D->getType()); |
| 2292 | if (T.isNull()) |
| 2293 | return 0; |
| 2294 | |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2295 | // Determine whether there are any other declarations with the same name and |
| 2296 | // in the same context. |
| 2297 | if (!LexicalDC->isFunctionOrMethod()) { |
| 2298 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 2299 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 2300 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2301 | Lookup.first != Lookup.second; |
| 2302 | ++Lookup.first) { |
| 2303 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 2304 | continue; |
| 2305 | |
| 2306 | ConflictingDecls.push_back(*Lookup.first); |
| 2307 | } |
| 2308 | |
| 2309 | if (!ConflictingDecls.empty()) { |
| 2310 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2311 | ConflictingDecls.data(), |
| 2312 | ConflictingDecls.size()); |
| 2313 | if (!Name) |
| 2314 | return 0; |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | Expr *Init = Importer.Import(D->getInitExpr()); |
| 2319 | if (D->getInitExpr() && !Init) |
| 2320 | return 0; |
| 2321 | |
| 2322 | EnumConstantDecl *ToEnumerator |
| 2323 | = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc, |
| 2324 | Name.getAsIdentifierInfo(), T, |
| 2325 | Init, D->getInitVal()); |
Douglas Gregor | 325bf17 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2326 | ToEnumerator->setAccess(D->getAccess()); |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2327 | ToEnumerator->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2328 | Importer.Imported(D, ToEnumerator); |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2329 | LexicalDC->addDecl(ToEnumerator); |
| 2330 | return ToEnumerator; |
| 2331 | } |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2332 | |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2333 | Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { |
| 2334 | // Import the major distinguishing characteristics of this function. |
| 2335 | DeclContext *DC, *LexicalDC; |
| 2336 | DeclarationName Name; |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2337 | SourceLocation Loc; |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2338 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2339 | return 0; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2341 | // Try to find a function in our own ("to") context with the same name, same |
| 2342 | // type, and in the same context as the function we're importing. |
| 2343 | if (!LexicalDC->isFunctionOrMethod()) { |
| 2344 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 2345 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 2346 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2347 | Lookup.first != Lookup.second; |
| 2348 | ++Lookup.first) { |
| 2349 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 2350 | continue; |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2351 | |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2352 | if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) { |
| 2353 | if (isExternalLinkage(FoundFunction->getLinkage()) && |
| 2354 | isExternalLinkage(D->getLinkage())) { |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2355 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2356 | FoundFunction->getType())) { |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2357 | // FIXME: Actually try to merge the body and other attributes. |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2358 | return Importer.Imported(D, FoundFunction); |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | // FIXME: Check for overloading more carefully, e.g., by boosting |
| 2362 | // Sema::IsOverload out to the AST library. |
| 2363 | |
| 2364 | // Function overloading is okay in C++. |
| 2365 | if (Importer.getToContext().getLangOptions().CPlusPlus) |
| 2366 | continue; |
| 2367 | |
| 2368 | // Complain about inconsistent function types. |
| 2369 | Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2370 | << Name << D->getType() << FoundFunction->getType(); |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2371 | Importer.ToDiag(FoundFunction->getLocation(), |
| 2372 | diag::note_odr_value_here) |
| 2373 | << FoundFunction->getType(); |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | ConflictingDecls.push_back(*Lookup.first); |
| 2378 | } |
| 2379 | |
| 2380 | if (!ConflictingDecls.empty()) { |
| 2381 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2382 | ConflictingDecls.data(), |
| 2383 | ConflictingDecls.size()); |
| 2384 | if (!Name) |
| 2385 | return 0; |
| 2386 | } |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2387 | } |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2388 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2389 | DeclarationNameInfo NameInfo(Name, Loc); |
| 2390 | // Import additional name location/type info. |
| 2391 | ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); |
| 2392 | |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2393 | // Import the type. |
| 2394 | QualType T = Importer.Import(D->getType()); |
| 2395 | if (T.isNull()) |
| 2396 | return 0; |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2397 | |
| 2398 | // Import the function parameters. |
| 2399 | llvm::SmallVector<ParmVarDecl *, 8> Parameters; |
| 2400 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 2401 | P != PEnd; ++P) { |
| 2402 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P)); |
| 2403 | if (!ToP) |
| 2404 | return 0; |
| 2405 | |
| 2406 | Parameters.push_back(ToP); |
| 2407 | } |
| 2408 | |
| 2409 | // Create the imported function. |
| 2410 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Douglas Gregor | c144f35 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2411 | FunctionDecl *ToFunction = 0; |
| 2412 | if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 2413 | ToFunction = CXXConstructorDecl::Create(Importer.getToContext(), |
| 2414 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2415 | D->getInnerLocStart(), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2416 | NameInfo, T, TInfo, |
Douglas Gregor | c144f35 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2417 | FromConstructor->isExplicit(), |
| 2418 | D->isInlineSpecified(), |
Sean Hunt | 5f802e5 | 2011-05-06 00:11:07 +0000 | [diff] [blame] | 2419 | D->isImplicit()); |
Douglas Gregor | c144f35 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2420 | } else if (isa<CXXDestructorDecl>(D)) { |
| 2421 | ToFunction = CXXDestructorDecl::Create(Importer.getToContext(), |
| 2422 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2423 | D->getInnerLocStart(), |
Craig Silverstein | b41d899 | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 2424 | NameInfo, T, TInfo, |
Douglas Gregor | c144f35 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2425 | D->isInlineSpecified(), |
| 2426 | D->isImplicit()); |
| 2427 | } else if (CXXConversionDecl *FromConversion |
| 2428 | = dyn_cast<CXXConversionDecl>(D)) { |
| 2429 | ToFunction = CXXConversionDecl::Create(Importer.getToContext(), |
| 2430 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2431 | D->getInnerLocStart(), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2432 | NameInfo, T, TInfo, |
Douglas Gregor | c144f35 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2433 | D->isInlineSpecified(), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2434 | FromConversion->isExplicit(), |
| 2435 | Importer.Import(D->getLocEnd())); |
Douglas Gregor | 0629cbe | 2010-11-29 16:04:58 +0000 | [diff] [blame] | 2436 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 2437 | ToFunction = CXXMethodDecl::Create(Importer.getToContext(), |
| 2438 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2439 | D->getInnerLocStart(), |
Douglas Gregor | 0629cbe | 2010-11-29 16:04:58 +0000 | [diff] [blame] | 2440 | NameInfo, T, TInfo, |
| 2441 | Method->isStatic(), |
| 2442 | Method->getStorageClassAsWritten(), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2443 | Method->isInlineSpecified(), |
| 2444 | Importer.Import(D->getLocEnd())); |
Douglas Gregor | c144f35 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2445 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2446 | ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2447 | D->getInnerLocStart(), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2448 | NameInfo, T, TInfo, D->getStorageClass(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2449 | D->getStorageClassAsWritten(), |
Douglas Gregor | c144f35 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2450 | D->isInlineSpecified(), |
| 2451 | D->hasWrittenPrototype()); |
| 2452 | } |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2453 | |
| 2454 | // Import the qualifier, if any. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2455 | ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | 325bf17 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2456 | ToFunction->setAccess(D->getAccess()); |
Douglas Gregor | c3f2d2b | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2457 | ToFunction->setLexicalDeclContext(LexicalDC); |
John McCall | f2eca2c | 2011-01-27 02:37:01 +0000 | [diff] [blame] | 2458 | ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); |
| 2459 | ToFunction->setTrivial(D->isTrivial()); |
| 2460 | ToFunction->setPure(D->isPure()); |
Douglas Gregor | c3f2d2b | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2461 | Importer.Imported(D, ToFunction); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2462 | |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2463 | // Set the parameters. |
| 2464 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) { |
Douglas Gregor | c3f2d2b | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2465 | Parameters[I]->setOwningFunction(ToFunction); |
| 2466 | ToFunction->addDecl(Parameters[I]); |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2467 | } |
Douglas Gregor | c3f2d2b | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2468 | ToFunction->setParams(Parameters.data(), Parameters.size()); |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2469 | |
| 2470 | // FIXME: Other bits to merge? |
Douglas Gregor | 81134ad | 2010-10-01 23:55:07 +0000 | [diff] [blame] | 2471 | |
| 2472 | // Add this function to the lexical context. |
| 2473 | LexicalDC->addDecl(ToFunction); |
| 2474 | |
Douglas Gregor | c3f2d2b | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2475 | return ToFunction; |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Douglas Gregor | c144f35 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2478 | Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 2479 | return VisitFunctionDecl(D); |
| 2480 | } |
| 2481 | |
| 2482 | Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| 2483 | return VisitCXXMethodDecl(D); |
| 2484 | } |
| 2485 | |
| 2486 | Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
| 2487 | return VisitCXXMethodDecl(D); |
| 2488 | } |
| 2489 | |
| 2490 | Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| 2491 | return VisitCXXMethodDecl(D); |
| 2492 | } |
| 2493 | |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2494 | Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { |
| 2495 | // Import the major distinguishing characteristics of a variable. |
| 2496 | DeclContext *DC, *LexicalDC; |
| 2497 | DeclarationName Name; |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2498 | SourceLocation Loc; |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2499 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2500 | return 0; |
| 2501 | |
| 2502 | // Import the type. |
| 2503 | QualType T = Importer.Import(D->getType()); |
| 2504 | if (T.isNull()) |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2505 | return 0; |
| 2506 | |
| 2507 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 2508 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 2509 | if (!BitWidth && D->getBitWidth()) |
| 2510 | return 0; |
| 2511 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2512 | FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, |
| 2513 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2514 | Loc, Name.getAsIdentifierInfo(), |
| 2515 | T, TInfo, BitWidth, D->isMutable()); |
Douglas Gregor | 325bf17 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2516 | ToField->setAccess(D->getAccess()); |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2517 | ToField->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2518 | Importer.Imported(D, ToField); |
Douglas Gregor | 96a01b4 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2519 | LexicalDC->addDecl(ToField); |
| 2520 | return ToField; |
| 2521 | } |
| 2522 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2523 | Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 2524 | // Import the major distinguishing characteristics of a variable. |
| 2525 | DeclContext *DC, *LexicalDC; |
| 2526 | DeclarationName Name; |
| 2527 | SourceLocation Loc; |
| 2528 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2529 | return 0; |
| 2530 | |
| 2531 | // Import the type. |
| 2532 | QualType T = Importer.Import(D->getType()); |
| 2533 | if (T.isNull()) |
| 2534 | return 0; |
| 2535 | |
| 2536 | NamedDecl **NamedChain = |
| 2537 | new (Importer.getToContext())NamedDecl*[D->getChainingSize()]; |
| 2538 | |
| 2539 | unsigned i = 0; |
| 2540 | for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(), |
| 2541 | PE = D->chain_end(); PI != PE; ++PI) { |
| 2542 | Decl* D = Importer.Import(*PI); |
| 2543 | if (!D) |
| 2544 | return 0; |
| 2545 | NamedChain[i++] = cast<NamedDecl>(D); |
| 2546 | } |
| 2547 | |
| 2548 | IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create( |
| 2549 | Importer.getToContext(), DC, |
| 2550 | Loc, Name.getAsIdentifierInfo(), T, |
| 2551 | NamedChain, D->getChainingSize()); |
| 2552 | ToIndirectField->setAccess(D->getAccess()); |
| 2553 | ToIndirectField->setLexicalDeclContext(LexicalDC); |
| 2554 | Importer.Imported(D, ToIndirectField); |
| 2555 | LexicalDC->addDecl(ToIndirectField); |
| 2556 | return ToIndirectField; |
| 2557 | } |
| 2558 | |
Douglas Gregor | 2e55e3a | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2559 | Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 2560 | // Import the major distinguishing characteristics of an ivar. |
| 2561 | DeclContext *DC, *LexicalDC; |
| 2562 | DeclarationName Name; |
| 2563 | SourceLocation Loc; |
| 2564 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2565 | return 0; |
| 2566 | |
| 2567 | // Determine whether we've already imported this ivar |
| 2568 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2569 | Lookup.first != Lookup.second; |
| 2570 | ++Lookup.first) { |
| 2571 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) { |
| 2572 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2573 | FoundIvar->getType())) { |
| 2574 | Importer.Imported(D, FoundIvar); |
| 2575 | return FoundIvar; |
| 2576 | } |
| 2577 | |
| 2578 | Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) |
| 2579 | << Name << D->getType() << FoundIvar->getType(); |
| 2580 | Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) |
| 2581 | << FoundIvar->getType(); |
| 2582 | return 0; |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | // Import the type. |
| 2587 | QualType T = Importer.Import(D->getType()); |
| 2588 | if (T.isNull()) |
| 2589 | return 0; |
| 2590 | |
| 2591 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 2592 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 2593 | if (!BitWidth && D->getBitWidth()) |
| 2594 | return 0; |
| 2595 | |
Daniel Dunbar | a065492 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 2596 | ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), |
| 2597 | cast<ObjCContainerDecl>(DC), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2598 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 2e55e3a | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2599 | Loc, Name.getAsIdentifierInfo(), |
| 2600 | T, TInfo, D->getAccessControl(), |
Fariborz Jahanian | ac0021b | 2010-07-17 18:35:47 +0000 | [diff] [blame] | 2601 | BitWidth, D->getSynthesize()); |
Douglas Gregor | 2e55e3a | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2602 | ToIvar->setLexicalDeclContext(LexicalDC); |
| 2603 | Importer.Imported(D, ToIvar); |
| 2604 | LexicalDC->addDecl(ToIvar); |
| 2605 | return ToIvar; |
| 2606 | |
| 2607 | } |
| 2608 | |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2609 | Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { |
| 2610 | // Import the major distinguishing characteristics of a variable. |
| 2611 | DeclContext *DC, *LexicalDC; |
| 2612 | DeclarationName Name; |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2613 | SourceLocation Loc; |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2614 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2615 | return 0; |
| 2616 | |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2617 | // Try to find a variable in our own ("to") context with the same name and |
| 2618 | // in the same context as the variable we're importing. |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2619 | if (D->isFileVarDecl()) { |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2620 | VarDecl *MergeWithVar = 0; |
| 2621 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 2622 | unsigned IDNS = Decl::IDNS_Ordinary; |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2623 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2624 | Lookup.first != Lookup.second; |
| 2625 | ++Lookup.first) { |
| 2626 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 2627 | continue; |
| 2628 | |
| 2629 | if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) { |
| 2630 | // We have found a variable that we may need to merge with. Check it. |
| 2631 | if (isExternalLinkage(FoundVar->getLinkage()) && |
| 2632 | isExternalLinkage(D->getLinkage())) { |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2633 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2634 | FoundVar->getType())) { |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2635 | MergeWithVar = FoundVar; |
| 2636 | break; |
| 2637 | } |
| 2638 | |
Douglas Gregor | d014542 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 2639 | const ArrayType *FoundArray |
| 2640 | = Importer.getToContext().getAsArrayType(FoundVar->getType()); |
| 2641 | const ArrayType *TArray |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2642 | = Importer.getToContext().getAsArrayType(D->getType()); |
Douglas Gregor | d014542 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 2643 | if (FoundArray && TArray) { |
| 2644 | if (isa<IncompleteArrayType>(FoundArray) && |
| 2645 | isa<ConstantArrayType>(TArray)) { |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2646 | // Import the type. |
| 2647 | QualType T = Importer.Import(D->getType()); |
| 2648 | if (T.isNull()) |
| 2649 | return 0; |
| 2650 | |
Douglas Gregor | d014542 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 2651 | FoundVar->setType(T); |
| 2652 | MergeWithVar = FoundVar; |
| 2653 | break; |
| 2654 | } else if (isa<IncompleteArrayType>(TArray) && |
| 2655 | isa<ConstantArrayType>(FoundArray)) { |
| 2656 | MergeWithVar = FoundVar; |
| 2657 | break; |
Douglas Gregor | 0f962a8 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 2658 | } |
| 2659 | } |
| 2660 | |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2661 | Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2662 | << Name << D->getType() << FoundVar->getType(); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2663 | Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) |
| 2664 | << FoundVar->getType(); |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | ConflictingDecls.push_back(*Lookup.first); |
| 2669 | } |
| 2670 | |
| 2671 | if (MergeWithVar) { |
| 2672 | // An equivalent variable with external linkage has been found. Link |
| 2673 | // the two declarations, then merge them. |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2674 | Importer.Imported(D, MergeWithVar); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2675 | |
| 2676 | if (VarDecl *DDef = D->getDefinition()) { |
| 2677 | if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { |
| 2678 | Importer.ToDiag(ExistingDef->getLocation(), |
| 2679 | diag::err_odr_variable_multiple_def) |
| 2680 | << Name; |
| 2681 | Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); |
| 2682 | } else { |
| 2683 | Expr *Init = Importer.Import(DDef->getInit()); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2684 | MergeWithVar->setInit(Init); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2685 | } |
| 2686 | } |
| 2687 | |
| 2688 | return MergeWithVar; |
| 2689 | } |
| 2690 | |
| 2691 | if (!ConflictingDecls.empty()) { |
| 2692 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2693 | ConflictingDecls.data(), |
| 2694 | ConflictingDecls.size()); |
| 2695 | if (!Name) |
| 2696 | return 0; |
| 2697 | } |
| 2698 | } |
Douglas Gregor | 82fc4bf | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 2699 | |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2700 | // Import the type. |
| 2701 | QualType T = Importer.Import(D->getType()); |
| 2702 | if (T.isNull()) |
| 2703 | return 0; |
| 2704 | |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2705 | // Create the imported variable. |
Douglas Gregor | 82fc4bf | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 2706 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2707 | VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, |
| 2708 | Importer.Import(D->getInnerLocStart()), |
| 2709 | Loc, Name.getAsIdentifierInfo(), |
| 2710 | T, TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2711 | D->getStorageClass(), |
| 2712 | D->getStorageClassAsWritten()); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2713 | ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | 325bf17 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2714 | ToVar->setAccess(D->getAccess()); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2715 | ToVar->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2716 | Importer.Imported(D, ToVar); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2717 | LexicalDC->addDecl(ToVar); |
| 2718 | |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2719 | // Merge the initializer. |
| 2720 | // FIXME: Can we really import any initializer? Alternatively, we could force |
| 2721 | // ourselves to import every declaration of a variable and then only use |
| 2722 | // getInit() here. |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2723 | ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer()))); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2724 | |
| 2725 | // FIXME: Other bits to merge? |
| 2726 | |
| 2727 | return ToVar; |
| 2728 | } |
| 2729 | |
Douglas Gregor | 2cd0093 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 2730 | Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 2731 | // Parameters are created in the translation unit's context, then moved |
| 2732 | // into the function declaration's context afterward. |
| 2733 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 2734 | |
| 2735 | // Import the name of this declaration. |
| 2736 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 2737 | if (D->getDeclName() && !Name) |
| 2738 | return 0; |
| 2739 | |
| 2740 | // Import the location of this declaration. |
| 2741 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 2742 | |
| 2743 | // Import the parameter's type. |
| 2744 | QualType T = Importer.Import(D->getType()); |
| 2745 | if (T.isNull()) |
| 2746 | return 0; |
| 2747 | |
| 2748 | // Create the imported parameter. |
| 2749 | ImplicitParamDecl *ToParm |
| 2750 | = ImplicitParamDecl::Create(Importer.getToContext(), DC, |
| 2751 | Loc, Name.getAsIdentifierInfo(), |
| 2752 | T); |
| 2753 | return Importer.Imported(D, ToParm); |
| 2754 | } |
| 2755 | |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2756 | Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { |
| 2757 | // Parameters are created in the translation unit's context, then moved |
| 2758 | // into the function declaration's context afterward. |
| 2759 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 2760 | |
Douglas Gregor | 82fc4bf | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 2761 | // Import the name of this declaration. |
| 2762 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 2763 | if (D->getDeclName() && !Name) |
| 2764 | return 0; |
| 2765 | |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2766 | // Import the location of this declaration. |
| 2767 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 2768 | |
| 2769 | // Import the parameter's type. |
| 2770 | QualType T = Importer.Import(D->getType()); |
| 2771 | if (T.isNull()) |
| 2772 | return 0; |
| 2773 | |
| 2774 | // Create the imported parameter. |
| 2775 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 2776 | ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2777 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2778 | Loc, Name.getAsIdentifierInfo(), |
| 2779 | T, TInfo, D->getStorageClass(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2780 | D->getStorageClassAsWritten(), |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2781 | /*FIXME: Default argument*/ 0); |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 2782 | ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2783 | return Importer.Imported(D, ToParm); |
Douglas Gregor | a404ea6 | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Douglas Gregor | c3f2d2b | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2786 | Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 2787 | // Import the major distinguishing characteristics of a method. |
| 2788 | DeclContext *DC, *LexicalDC; |
| 2789 | DeclarationName Name; |
| 2790 | SourceLocation Loc; |
| 2791 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2792 | return 0; |
| 2793 | |
| 2794 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2795 | Lookup.first != Lookup.second; |
| 2796 | ++Lookup.first) { |
| 2797 | if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) { |
| 2798 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) |
| 2799 | continue; |
| 2800 | |
| 2801 | // Check return types. |
| 2802 | if (!Importer.IsStructurallyEquivalent(D->getResultType(), |
| 2803 | FoundMethod->getResultType())) { |
| 2804 | Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) |
| 2805 | << D->isInstanceMethod() << Name |
| 2806 | << D->getResultType() << FoundMethod->getResultType(); |
| 2807 | Importer.ToDiag(FoundMethod->getLocation(), |
| 2808 | diag::note_odr_objc_method_here) |
| 2809 | << D->isInstanceMethod() << Name; |
| 2810 | return 0; |
| 2811 | } |
| 2812 | |
| 2813 | // Check the number of parameters. |
| 2814 | if (D->param_size() != FoundMethod->param_size()) { |
| 2815 | Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) |
| 2816 | << D->isInstanceMethod() << Name |
| 2817 | << D->param_size() << FoundMethod->param_size(); |
| 2818 | Importer.ToDiag(FoundMethod->getLocation(), |
| 2819 | diag::note_odr_objc_method_here) |
| 2820 | << D->isInstanceMethod() << Name; |
| 2821 | return 0; |
| 2822 | } |
| 2823 | |
| 2824 | // Check parameter types. |
| 2825 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
| 2826 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); |
| 2827 | P != PEnd; ++P, ++FoundP) { |
| 2828 | if (!Importer.IsStructurallyEquivalent((*P)->getType(), |
| 2829 | (*FoundP)->getType())) { |
| 2830 | Importer.FromDiag((*P)->getLocation(), |
| 2831 | diag::err_odr_objc_method_param_type_inconsistent) |
| 2832 | << D->isInstanceMethod() << Name |
| 2833 | << (*P)->getType() << (*FoundP)->getType(); |
| 2834 | Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) |
| 2835 | << (*FoundP)->getType(); |
| 2836 | return 0; |
| 2837 | } |
| 2838 | } |
| 2839 | |
| 2840 | // Check variadic/non-variadic. |
| 2841 | // Check the number of parameters. |
| 2842 | if (D->isVariadic() != FoundMethod->isVariadic()) { |
| 2843 | Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) |
| 2844 | << D->isInstanceMethod() << Name; |
| 2845 | Importer.ToDiag(FoundMethod->getLocation(), |
| 2846 | diag::note_odr_objc_method_here) |
| 2847 | << D->isInstanceMethod() << Name; |
| 2848 | return 0; |
| 2849 | } |
| 2850 | |
| 2851 | // FIXME: Any other bits we need to merge? |
| 2852 | return Importer.Imported(D, FoundMethod); |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | // Import the result type. |
| 2857 | QualType ResultTy = Importer.Import(D->getResultType()); |
| 2858 | if (ResultTy.isNull()) |
| 2859 | return 0; |
| 2860 | |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 2861 | TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo()); |
| 2862 | |
Douglas Gregor | c3f2d2b | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2863 | ObjCMethodDecl *ToMethod |
| 2864 | = ObjCMethodDecl::Create(Importer.getToContext(), |
| 2865 | Loc, |
| 2866 | Importer.Import(D->getLocEnd()), |
| 2867 | Name.getObjCSelector(), |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 2868 | ResultTy, ResultTInfo, DC, |
Douglas Gregor | c3f2d2b | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2869 | D->isInstanceMethod(), |
| 2870 | D->isVariadic(), |
| 2871 | D->isSynthesized(), |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 2872 | D->isDefined(), |
Douglas Gregor | c3f2d2b | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2873 | D->getImplementationControl()); |
| 2874 | |
| 2875 | // FIXME: When we decide to merge method definitions, we'll need to |
| 2876 | // deal with implicit parameters. |
| 2877 | |
| 2878 | // Import the parameters |
| 2879 | llvm::SmallVector<ParmVarDecl *, 5> ToParams; |
| 2880 | for (ObjCMethodDecl::param_iterator FromP = D->param_begin(), |
| 2881 | FromPEnd = D->param_end(); |
| 2882 | FromP != FromPEnd; |
| 2883 | ++FromP) { |
| 2884 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP)); |
| 2885 | if (!ToP) |
| 2886 | return 0; |
| 2887 | |
| 2888 | ToParams.push_back(ToP); |
| 2889 | } |
| 2890 | |
| 2891 | // Set the parameters. |
| 2892 | for (unsigned I = 0, N = ToParams.size(); I != N; ++I) { |
| 2893 | ToParams[I]->setOwningFunction(ToMethod); |
| 2894 | ToMethod->addDecl(ToParams[I]); |
| 2895 | } |
| 2896 | ToMethod->setMethodParams(Importer.getToContext(), |
Fariborz Jahanian | 4ecb25f | 2010-04-09 15:40:42 +0000 | [diff] [blame] | 2897 | ToParams.data(), ToParams.size(), |
| 2898 | ToParams.size()); |
Douglas Gregor | c3f2d2b | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2899 | |
| 2900 | ToMethod->setLexicalDeclContext(LexicalDC); |
| 2901 | Importer.Imported(D, ToMethod); |
| 2902 | LexicalDC->addDecl(ToMethod); |
| 2903 | return ToMethod; |
| 2904 | } |
| 2905 | |
Douglas Gregor | b4677b6 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 2906 | Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 2907 | // Import the major distinguishing characteristics of a category. |
| 2908 | DeclContext *DC, *LexicalDC; |
| 2909 | DeclarationName Name; |
| 2910 | SourceLocation Loc; |
| 2911 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2912 | return 0; |
| 2913 | |
| 2914 | ObjCInterfaceDecl *ToInterface |
| 2915 | = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); |
| 2916 | if (!ToInterface) |
| 2917 | return 0; |
| 2918 | |
| 2919 | // Determine if we've already encountered this category. |
| 2920 | ObjCCategoryDecl *MergeWithCategory |
| 2921 | = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); |
| 2922 | ObjCCategoryDecl *ToCategory = MergeWithCategory; |
| 2923 | if (!ToCategory) { |
| 2924 | ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, |
| 2925 | Importer.Import(D->getAtLoc()), |
| 2926 | Loc, |
| 2927 | Importer.Import(D->getCategoryNameLoc()), |
| 2928 | Name.getAsIdentifierInfo()); |
| 2929 | ToCategory->setLexicalDeclContext(LexicalDC); |
| 2930 | LexicalDC->addDecl(ToCategory); |
| 2931 | Importer.Imported(D, ToCategory); |
| 2932 | |
| 2933 | // Link this category into its class's category list. |
| 2934 | ToCategory->setClassInterface(ToInterface); |
| 2935 | ToCategory->insertNextClassCategory(); |
| 2936 | |
| 2937 | // Import protocols |
| 2938 | llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 2939 | llvm::SmallVector<SourceLocation, 4> ProtocolLocs; |
| 2940 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc |
| 2941 | = D->protocol_loc_begin(); |
| 2942 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 2943 | FromProtoEnd = D->protocol_end(); |
| 2944 | FromProto != FromProtoEnd; |
| 2945 | ++FromProto, ++FromProtoLoc) { |
| 2946 | ObjCProtocolDecl *ToProto |
| 2947 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 2948 | if (!ToProto) |
| 2949 | return 0; |
| 2950 | Protocols.push_back(ToProto); |
| 2951 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 2952 | } |
| 2953 | |
| 2954 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 2955 | ToCategory->setProtocolList(Protocols.data(), Protocols.size(), |
| 2956 | ProtocolLocs.data(), Importer.getToContext()); |
| 2957 | |
| 2958 | } else { |
| 2959 | Importer.Imported(D, ToCategory); |
| 2960 | } |
| 2961 | |
| 2962 | // Import all of the members of this category. |
Douglas Gregor | 083a821 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 2963 | ImportDeclContext(D); |
Douglas Gregor | b4677b6 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 2964 | |
| 2965 | // If we have an implementation, import it as well. |
| 2966 | if (D->getImplementation()) { |
| 2967 | ObjCCategoryImplDecl *Impl |
Douglas Gregor | cad2c59 | 2010-12-08 16:41:55 +0000 | [diff] [blame] | 2968 | = cast_or_null<ObjCCategoryImplDecl>( |
| 2969 | Importer.Import(D->getImplementation())); |
Douglas Gregor | b4677b6 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 2970 | if (!Impl) |
| 2971 | return 0; |
| 2972 | |
| 2973 | ToCategory->setImplementation(Impl); |
| 2974 | } |
| 2975 | |
| 2976 | return ToCategory; |
| 2977 | } |
| 2978 | |
Douglas Gregor | 2e2a400 | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 2979 | Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
Douglas Gregor | b4677b6 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 2980 | // Import the major distinguishing characteristics of a protocol. |
Douglas Gregor | 2e2a400 | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 2981 | DeclContext *DC, *LexicalDC; |
| 2982 | DeclarationName Name; |
| 2983 | SourceLocation Loc; |
| 2984 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2985 | return 0; |
| 2986 | |
| 2987 | ObjCProtocolDecl *MergeWithProtocol = 0; |
| 2988 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 2989 | Lookup.first != Lookup.second; |
| 2990 | ++Lookup.first) { |
| 2991 | if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) |
| 2992 | continue; |
| 2993 | |
| 2994 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first))) |
| 2995 | break; |
| 2996 | } |
| 2997 | |
| 2998 | ObjCProtocolDecl *ToProto = MergeWithProtocol; |
| 2999 | if (!ToProto || ToProto->isForwardDecl()) { |
| 3000 | if (!ToProto) { |
| 3001 | ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc, |
| 3002 | Name.getAsIdentifierInfo()); |
| 3003 | ToProto->setForwardDecl(D->isForwardDecl()); |
| 3004 | ToProto->setLexicalDeclContext(LexicalDC); |
| 3005 | LexicalDC->addDecl(ToProto); |
| 3006 | } |
| 3007 | Importer.Imported(D, ToProto); |
| 3008 | |
| 3009 | // Import protocols |
| 3010 | llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3011 | llvm::SmallVector<SourceLocation, 4> ProtocolLocs; |
| 3012 | ObjCProtocolDecl::protocol_loc_iterator |
| 3013 | FromProtoLoc = D->protocol_loc_begin(); |
| 3014 | for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 3015 | FromProtoEnd = D->protocol_end(); |
| 3016 | FromProto != FromProtoEnd; |
| 3017 | ++FromProto, ++FromProtoLoc) { |
| 3018 | ObjCProtocolDecl *ToProto |
| 3019 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3020 | if (!ToProto) |
| 3021 | return 0; |
| 3022 | Protocols.push_back(ToProto); |
| 3023 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3024 | } |
| 3025 | |
| 3026 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3027 | ToProto->setProtocolList(Protocols.data(), Protocols.size(), |
| 3028 | ProtocolLocs.data(), Importer.getToContext()); |
| 3029 | } else { |
| 3030 | Importer.Imported(D, ToProto); |
| 3031 | } |
| 3032 | |
Douglas Gregor | b4677b6 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3033 | // Import all of the members of this protocol. |
Douglas Gregor | 083a821 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 3034 | ImportDeclContext(D); |
Douglas Gregor | 2e2a400 | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3035 | |
| 3036 | return ToProto; |
| 3037 | } |
| 3038 | |
Douglas Gregor | a12d294 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3039 | Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 3040 | // Import the major distinguishing characteristics of an @interface. |
| 3041 | DeclContext *DC, *LexicalDC; |
| 3042 | DeclarationName Name; |
| 3043 | SourceLocation Loc; |
| 3044 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 3045 | return 0; |
| 3046 | |
| 3047 | ObjCInterfaceDecl *MergeWithIface = 0; |
| 3048 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 3049 | Lookup.first != Lookup.second; |
| 3050 | ++Lookup.first) { |
| 3051 | if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 3052 | continue; |
| 3053 | |
| 3054 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first))) |
| 3055 | break; |
| 3056 | } |
| 3057 | |
| 3058 | ObjCInterfaceDecl *ToIface = MergeWithIface; |
| 3059 | if (!ToIface || ToIface->isForwardDecl()) { |
| 3060 | if (!ToIface) { |
| 3061 | ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), |
| 3062 | DC, Loc, |
| 3063 | Name.getAsIdentifierInfo(), |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3064 | Importer.Import(D->getClassLoc()), |
Douglas Gregor | a12d294 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3065 | D->isForwardDecl(), |
| 3066 | D->isImplicitInterfaceDecl()); |
Douglas Gregor | 2e2a400 | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3067 | ToIface->setForwardDecl(D->isForwardDecl()); |
Douglas Gregor | a12d294 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3068 | ToIface->setLexicalDeclContext(LexicalDC); |
| 3069 | LexicalDC->addDecl(ToIface); |
| 3070 | } |
| 3071 | Importer.Imported(D, ToIface); |
| 3072 | |
Douglas Gregor | a12d294 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3073 | if (D->getSuperClass()) { |
| 3074 | ObjCInterfaceDecl *Super |
| 3075 | = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass())); |
| 3076 | if (!Super) |
| 3077 | return 0; |
| 3078 | |
| 3079 | ToIface->setSuperClass(Super); |
| 3080 | ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc())); |
| 3081 | } |
| 3082 | |
| 3083 | // Import protocols |
| 3084 | llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3085 | llvm::SmallVector<SourceLocation, 4> ProtocolLocs; |
| 3086 | ObjCInterfaceDecl::protocol_loc_iterator |
| 3087 | FromProtoLoc = D->protocol_loc_begin(); |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 3088 | |
| 3089 | // FIXME: Should we be usng all_referenced_protocol_begin() here? |
Douglas Gregor | a12d294 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3090 | for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 3091 | FromProtoEnd = D->protocol_end(); |
| 3092 | FromProto != FromProtoEnd; |
| 3093 | ++FromProto, ++FromProtoLoc) { |
| 3094 | ObjCProtocolDecl *ToProto |
| 3095 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3096 | if (!ToProto) |
| 3097 | return 0; |
| 3098 | Protocols.push_back(ToProto); |
| 3099 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3100 | } |
| 3101 | |
| 3102 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3103 | ToIface->setProtocolList(Protocols.data(), Protocols.size(), |
| 3104 | ProtocolLocs.data(), Importer.getToContext()); |
| 3105 | |
Douglas Gregor | a12d294 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3106 | // Import @end range |
| 3107 | ToIface->setAtEndRange(Importer.Import(D->getAtEndRange())); |
| 3108 | } else { |
| 3109 | Importer.Imported(D, ToIface); |
Douglas Gregor | 2e55e3a | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3110 | |
| 3111 | // Check for consistency of superclasses. |
| 3112 | DeclarationName FromSuperName, ToSuperName; |
| 3113 | if (D->getSuperClass()) |
| 3114 | FromSuperName = Importer.Import(D->getSuperClass()->getDeclName()); |
| 3115 | if (ToIface->getSuperClass()) |
| 3116 | ToSuperName = ToIface->getSuperClass()->getDeclName(); |
| 3117 | if (FromSuperName != ToSuperName) { |
| 3118 | Importer.ToDiag(ToIface->getLocation(), |
| 3119 | diag::err_odr_objc_superclass_inconsistent) |
| 3120 | << ToIface->getDeclName(); |
| 3121 | if (ToIface->getSuperClass()) |
| 3122 | Importer.ToDiag(ToIface->getSuperClassLoc(), |
| 3123 | diag::note_odr_objc_superclass) |
| 3124 | << ToIface->getSuperClass()->getDeclName(); |
| 3125 | else |
| 3126 | Importer.ToDiag(ToIface->getLocation(), |
| 3127 | diag::note_odr_objc_missing_superclass); |
| 3128 | if (D->getSuperClass()) |
| 3129 | Importer.FromDiag(D->getSuperClassLoc(), |
| 3130 | diag::note_odr_objc_superclass) |
| 3131 | << D->getSuperClass()->getDeclName(); |
| 3132 | else |
| 3133 | Importer.FromDiag(D->getLocation(), |
| 3134 | diag::note_odr_objc_missing_superclass); |
| 3135 | return 0; |
| 3136 | } |
Douglas Gregor | a12d294 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3137 | } |
| 3138 | |
Douglas Gregor | b4677b6 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3139 | // Import categories. When the categories themselves are imported, they'll |
| 3140 | // hook themselves into this interface. |
| 3141 | for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat; |
| 3142 | FromCat = FromCat->getNextClassCategory()) |
| 3143 | Importer.Import(FromCat); |
| 3144 | |
Douglas Gregor | a12d294 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3145 | // Import all of the members of this class. |
Douglas Gregor | 083a821 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 3146 | ImportDeclContext(D); |
Douglas Gregor | a12d294 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3147 | |
| 3148 | // If we have an @implementation, import it as well. |
| 3149 | if (D->getImplementation()) { |
Douglas Gregor | dd182ff | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3150 | ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>( |
| 3151 | Importer.Import(D->getImplementation())); |
Douglas Gregor | a12d294 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3152 | if (!Impl) |
| 3153 | return 0; |
| 3154 | |
| 3155 | ToIface->setImplementation(Impl); |
| 3156 | } |
| 3157 | |
Douglas Gregor | 2e2a400 | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3158 | return ToIface; |
Douglas Gregor | a12d294 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
Douglas Gregor | 3daef29 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3161 | Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 3162 | ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>( |
| 3163 | Importer.Import(D->getCategoryDecl())); |
| 3164 | if (!Category) |
| 3165 | return 0; |
| 3166 | |
| 3167 | ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); |
| 3168 | if (!ToImpl) { |
| 3169 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 3170 | if (!DC) |
| 3171 | return 0; |
| 3172 | |
| 3173 | ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC, |
| 3174 | Importer.Import(D->getLocation()), |
| 3175 | Importer.Import(D->getIdentifier()), |
| 3176 | Category->getClassInterface()); |
| 3177 | |
| 3178 | DeclContext *LexicalDC = DC; |
| 3179 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3180 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3181 | if (!LexicalDC) |
| 3182 | return 0; |
| 3183 | |
| 3184 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 3185 | } |
| 3186 | |
| 3187 | LexicalDC->addDecl(ToImpl); |
| 3188 | Category->setImplementation(ToImpl); |
| 3189 | } |
| 3190 | |
| 3191 | Importer.Imported(D, ToImpl); |
Douglas Gregor | cad2c59 | 2010-12-08 16:41:55 +0000 | [diff] [blame] | 3192 | ImportDeclContext(D); |
Douglas Gregor | 3daef29 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3193 | return ToImpl; |
| 3194 | } |
| 3195 | |
Douglas Gregor | dd182ff | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3196 | Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 3197 | // Find the corresponding interface. |
| 3198 | ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>( |
| 3199 | Importer.Import(D->getClassInterface())); |
| 3200 | if (!Iface) |
| 3201 | return 0; |
| 3202 | |
| 3203 | // Import the superclass, if any. |
| 3204 | ObjCInterfaceDecl *Super = 0; |
| 3205 | if (D->getSuperClass()) { |
| 3206 | Super = cast_or_null<ObjCInterfaceDecl>( |
| 3207 | Importer.Import(D->getSuperClass())); |
| 3208 | if (!Super) |
| 3209 | return 0; |
| 3210 | } |
| 3211 | |
| 3212 | ObjCImplementationDecl *Impl = Iface->getImplementation(); |
| 3213 | if (!Impl) { |
| 3214 | // We haven't imported an implementation yet. Create a new @implementation |
| 3215 | // now. |
| 3216 | Impl = ObjCImplementationDecl::Create(Importer.getToContext(), |
| 3217 | Importer.ImportContext(D->getDeclContext()), |
| 3218 | Importer.Import(D->getLocation()), |
| 3219 | Iface, Super); |
| 3220 | |
| 3221 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3222 | DeclContext *LexicalDC |
| 3223 | = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3224 | if (!LexicalDC) |
| 3225 | return 0; |
| 3226 | Impl->setLexicalDeclContext(LexicalDC); |
| 3227 | } |
| 3228 | |
| 3229 | // Associate the implementation with the class it implements. |
| 3230 | Iface->setImplementation(Impl); |
| 3231 | Importer.Imported(D, Iface->getImplementation()); |
| 3232 | } else { |
| 3233 | Importer.Imported(D, Iface->getImplementation()); |
| 3234 | |
| 3235 | // Verify that the existing @implementation has the same superclass. |
| 3236 | if ((Super && !Impl->getSuperClass()) || |
| 3237 | (!Super && Impl->getSuperClass()) || |
| 3238 | (Super && Impl->getSuperClass() && |
| 3239 | Super->getCanonicalDecl() != Impl->getSuperClass())) { |
| 3240 | Importer.ToDiag(Impl->getLocation(), |
| 3241 | diag::err_odr_objc_superclass_inconsistent) |
| 3242 | << Iface->getDeclName(); |
| 3243 | // FIXME: It would be nice to have the location of the superclass |
| 3244 | // below. |
| 3245 | if (Impl->getSuperClass()) |
| 3246 | Importer.ToDiag(Impl->getLocation(), |
| 3247 | diag::note_odr_objc_superclass) |
| 3248 | << Impl->getSuperClass()->getDeclName(); |
| 3249 | else |
| 3250 | Importer.ToDiag(Impl->getLocation(), |
| 3251 | diag::note_odr_objc_missing_superclass); |
| 3252 | if (D->getSuperClass()) |
| 3253 | Importer.FromDiag(D->getLocation(), |
| 3254 | diag::note_odr_objc_superclass) |
| 3255 | << D->getSuperClass()->getDeclName(); |
| 3256 | else |
| 3257 | Importer.FromDiag(D->getLocation(), |
| 3258 | diag::note_odr_objc_missing_superclass); |
| 3259 | return 0; |
| 3260 | } |
| 3261 | } |
| 3262 | |
| 3263 | // Import all of the members of this @implementation. |
| 3264 | ImportDeclContext(D); |
| 3265 | |
| 3266 | return Impl; |
| 3267 | } |
| 3268 | |
Douglas Gregor | e326162 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3269 | Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 3270 | // Import the major distinguishing characteristics of an @property. |
| 3271 | DeclContext *DC, *LexicalDC; |
| 3272 | DeclarationName Name; |
| 3273 | SourceLocation Loc; |
| 3274 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 3275 | return 0; |
| 3276 | |
| 3277 | // Check whether we have already imported this property. |
| 3278 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 3279 | Lookup.first != Lookup.second; |
| 3280 | ++Lookup.first) { |
| 3281 | if (ObjCPropertyDecl *FoundProp |
| 3282 | = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) { |
| 3283 | // Check property types. |
| 3284 | if (!Importer.IsStructurallyEquivalent(D->getType(), |
| 3285 | FoundProp->getType())) { |
| 3286 | Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) |
| 3287 | << Name << D->getType() << FoundProp->getType(); |
| 3288 | Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) |
| 3289 | << FoundProp->getType(); |
| 3290 | return 0; |
| 3291 | } |
| 3292 | |
| 3293 | // FIXME: Check property attributes, getters, setters, etc.? |
| 3294 | |
| 3295 | // Consider these properties to be equivalent. |
| 3296 | Importer.Imported(D, FoundProp); |
| 3297 | return FoundProp; |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | // Import the type. |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 3302 | TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo()); |
| 3303 | if (!T) |
Douglas Gregor | e326162 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3304 | return 0; |
| 3305 | |
| 3306 | // Create the new property. |
| 3307 | ObjCPropertyDecl *ToProperty |
| 3308 | = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, |
| 3309 | Name.getAsIdentifierInfo(), |
| 3310 | Importer.Import(D->getAtLoc()), |
| 3311 | T, |
| 3312 | D->getPropertyImplementation()); |
| 3313 | Importer.Imported(D, ToProperty); |
| 3314 | ToProperty->setLexicalDeclContext(LexicalDC); |
| 3315 | LexicalDC->addDecl(ToProperty); |
| 3316 | |
| 3317 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 3318 | ToProperty->setPropertyAttributesAsWritten( |
| 3319 | D->getPropertyAttributesAsWritten()); |
Douglas Gregor | e326162 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3320 | ToProperty->setGetterName(Importer.Import(D->getGetterName())); |
| 3321 | ToProperty->setSetterName(Importer.Import(D->getSetterName())); |
| 3322 | ToProperty->setGetterMethodDecl( |
| 3323 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); |
| 3324 | ToProperty->setSetterMethodDecl( |
| 3325 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); |
| 3326 | ToProperty->setPropertyIvarDecl( |
| 3327 | cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); |
| 3328 | return ToProperty; |
| 3329 | } |
| 3330 | |
Douglas Gregor | 954e0c7 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3331 | Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 3332 | ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>( |
| 3333 | Importer.Import(D->getPropertyDecl())); |
| 3334 | if (!Property) |
| 3335 | return 0; |
| 3336 | |
| 3337 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 3338 | if (!DC) |
| 3339 | return 0; |
| 3340 | |
| 3341 | // Import the lexical declaration context. |
| 3342 | DeclContext *LexicalDC = DC; |
| 3343 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3344 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3345 | if (!LexicalDC) |
| 3346 | return 0; |
| 3347 | } |
| 3348 | |
| 3349 | ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC); |
| 3350 | if (!InImpl) |
| 3351 | return 0; |
| 3352 | |
| 3353 | // Import the ivar (for an @synthesize). |
| 3354 | ObjCIvarDecl *Ivar = 0; |
| 3355 | if (D->getPropertyIvarDecl()) { |
| 3356 | Ivar = cast_or_null<ObjCIvarDecl>( |
| 3357 | Importer.Import(D->getPropertyIvarDecl())); |
| 3358 | if (!Ivar) |
| 3359 | return 0; |
| 3360 | } |
| 3361 | |
| 3362 | ObjCPropertyImplDecl *ToImpl |
| 3363 | = InImpl->FindPropertyImplDecl(Property->getIdentifier()); |
| 3364 | if (!ToImpl) { |
| 3365 | ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC, |
| 3366 | Importer.Import(D->getLocStart()), |
| 3367 | Importer.Import(D->getLocation()), |
| 3368 | Property, |
| 3369 | D->getPropertyImplementation(), |
| 3370 | Ivar, |
| 3371 | Importer.Import(D->getPropertyIvarDeclLoc())); |
| 3372 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 3373 | Importer.Imported(D, ToImpl); |
| 3374 | LexicalDC->addDecl(ToImpl); |
| 3375 | } else { |
| 3376 | // Check that we have the same kind of property implementation (@synthesize |
| 3377 | // vs. @dynamic). |
| 3378 | if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { |
| 3379 | Importer.ToDiag(ToImpl->getLocation(), |
| 3380 | diag::err_odr_objc_property_impl_kind_inconsistent) |
| 3381 | << Property->getDeclName() |
| 3382 | << (ToImpl->getPropertyImplementation() |
| 3383 | == ObjCPropertyImplDecl::Dynamic); |
| 3384 | Importer.FromDiag(D->getLocation(), |
| 3385 | diag::note_odr_objc_property_impl_kind) |
| 3386 | << D->getPropertyDecl()->getDeclName() |
| 3387 | << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
| 3388 | return 0; |
| 3389 | } |
| 3390 | |
| 3391 | // For @synthesize, check that we have the same |
| 3392 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && |
| 3393 | Ivar != ToImpl->getPropertyIvarDecl()) { |
| 3394 | Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), |
| 3395 | diag::err_odr_objc_synthesize_ivar_inconsistent) |
| 3396 | << Property->getDeclName() |
| 3397 | << ToImpl->getPropertyIvarDecl()->getDeclName() |
| 3398 | << Ivar->getDeclName(); |
| 3399 | Importer.FromDiag(D->getPropertyIvarDeclLoc(), |
| 3400 | diag::note_odr_objc_synthesize_ivar_here) |
| 3401 | << D->getPropertyIvarDecl()->getDeclName(); |
| 3402 | return 0; |
| 3403 | } |
| 3404 | |
| 3405 | // Merge the existing implementation with the new implementation. |
| 3406 | Importer.Imported(D, ToImpl); |
| 3407 | } |
| 3408 | |
| 3409 | return ToImpl; |
| 3410 | } |
| 3411 | |
Douglas Gregor | 2b78502 | 2010-02-18 02:12:22 +0000 | [diff] [blame] | 3412 | Decl * |
| 3413 | ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 3414 | // Import the context of this declaration. |
| 3415 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 3416 | if (!DC) |
| 3417 | return 0; |
| 3418 | |
| 3419 | DeclContext *LexicalDC = DC; |
| 3420 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3421 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3422 | if (!LexicalDC) |
| 3423 | return 0; |
| 3424 | } |
| 3425 | |
| 3426 | // Import the location of this declaration. |
| 3427 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3428 | |
| 3429 | llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3430 | llvm::SmallVector<SourceLocation, 4> Locations; |
| 3431 | ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc |
| 3432 | = D->protocol_loc_begin(); |
| 3433 | for (ObjCForwardProtocolDecl::protocol_iterator FromProto |
| 3434 | = D->protocol_begin(), FromProtoEnd = D->protocol_end(); |
| 3435 | FromProto != FromProtoEnd; |
| 3436 | ++FromProto, ++FromProtoLoc) { |
| 3437 | ObjCProtocolDecl *ToProto |
| 3438 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3439 | if (!ToProto) |
| 3440 | continue; |
| 3441 | |
| 3442 | Protocols.push_back(ToProto); |
| 3443 | Locations.push_back(Importer.Import(*FromProtoLoc)); |
| 3444 | } |
| 3445 | |
| 3446 | ObjCForwardProtocolDecl *ToForward |
| 3447 | = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc, |
| 3448 | Protocols.data(), Protocols.size(), |
| 3449 | Locations.data()); |
| 3450 | ToForward->setLexicalDeclContext(LexicalDC); |
| 3451 | LexicalDC->addDecl(ToForward); |
| 3452 | Importer.Imported(D, ToForward); |
| 3453 | return ToForward; |
| 3454 | } |
| 3455 | |
Douglas Gregor | a2bc15b | 2010-02-18 02:04:09 +0000 | [diff] [blame] | 3456 | Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 3457 | // Import the context of this declaration. |
| 3458 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 3459 | if (!DC) |
| 3460 | return 0; |
| 3461 | |
| 3462 | DeclContext *LexicalDC = DC; |
| 3463 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3464 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3465 | if (!LexicalDC) |
| 3466 | return 0; |
| 3467 | } |
| 3468 | |
| 3469 | // Import the location of this declaration. |
| 3470 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3471 | |
| 3472 | llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces; |
| 3473 | llvm::SmallVector<SourceLocation, 4> Locations; |
| 3474 | for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end(); |
| 3475 | From != FromEnd; ++From) { |
| 3476 | ObjCInterfaceDecl *ToIface |
| 3477 | = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface())); |
| 3478 | if (!ToIface) |
| 3479 | continue; |
| 3480 | |
| 3481 | Interfaces.push_back(ToIface); |
| 3482 | Locations.push_back(Importer.Import(From->getLocation())); |
| 3483 | } |
| 3484 | |
| 3485 | ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC, |
| 3486 | Loc, |
| 3487 | Interfaces.data(), |
| 3488 | Locations.data(), |
| 3489 | Interfaces.size()); |
| 3490 | ToClass->setLexicalDeclContext(LexicalDC); |
| 3491 | LexicalDC->addDecl(ToClass); |
| 3492 | Importer.Imported(D, ToClass); |
| 3493 | return ToClass; |
| 3494 | } |
| 3495 | |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3496 | Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 3497 | // For template arguments, we adopt the translation unit as our declaration |
| 3498 | // context. This context will be fixed when the actual template declaration |
| 3499 | // is created. |
| 3500 | |
| 3501 | // FIXME: Import default argument. |
| 3502 | return TemplateTypeParmDecl::Create(Importer.getToContext(), |
| 3503 | Importer.getToContext().getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3504 | Importer.Import(D->getLocStart()), |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3505 | Importer.Import(D->getLocation()), |
| 3506 | D->getDepth(), |
| 3507 | D->getIndex(), |
| 3508 | Importer.Import(D->getIdentifier()), |
| 3509 | D->wasDeclaredWithTypename(), |
| 3510 | D->isParameterPack()); |
| 3511 | } |
| 3512 | |
| 3513 | Decl * |
| 3514 | ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 3515 | // Import the name of this declaration. |
| 3516 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3517 | if (D->getDeclName() && !Name) |
| 3518 | return 0; |
| 3519 | |
| 3520 | // Import the location of this declaration. |
| 3521 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3522 | |
| 3523 | // Import the type of this declaration. |
| 3524 | QualType T = Importer.Import(D->getType()); |
| 3525 | if (T.isNull()) |
| 3526 | return 0; |
| 3527 | |
| 3528 | // Import type-source information. |
| 3529 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3530 | if (D->getTypeSourceInfo() && !TInfo) |
| 3531 | return 0; |
| 3532 | |
| 3533 | // FIXME: Import default argument. |
| 3534 | |
| 3535 | return NonTypeTemplateParmDecl::Create(Importer.getToContext(), |
| 3536 | Importer.getToContext().getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3537 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3538 | Loc, D->getDepth(), D->getPosition(), |
| 3539 | Name.getAsIdentifierInfo(), |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 3540 | T, D->isParameterPack(), TInfo); |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | Decl * |
| 3544 | ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 3545 | // Import the name of this declaration. |
| 3546 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3547 | if (D->getDeclName() && !Name) |
| 3548 | return 0; |
| 3549 | |
| 3550 | // Import the location of this declaration. |
| 3551 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3552 | |
| 3553 | // Import template parameters. |
| 3554 | TemplateParameterList *TemplateParams |
| 3555 | = ImportTemplateParameterList(D->getTemplateParameters()); |
| 3556 | if (!TemplateParams) |
| 3557 | return 0; |
| 3558 | |
| 3559 | // FIXME: Import default argument. |
| 3560 | |
| 3561 | return TemplateTemplateParmDecl::Create(Importer.getToContext(), |
| 3562 | Importer.getToContext().getTranslationUnitDecl(), |
| 3563 | Loc, D->getDepth(), D->getPosition(), |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 3564 | D->isParameterPack(), |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3565 | Name.getAsIdentifierInfo(), |
| 3566 | TemplateParams); |
| 3567 | } |
| 3568 | |
| 3569 | Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 3570 | // If this record has a definition in the translation unit we're coming from, |
| 3571 | // but this particular declaration is not that definition, import the |
| 3572 | // definition and map to that. |
| 3573 | CXXRecordDecl *Definition |
| 3574 | = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition()); |
| 3575 | if (Definition && Definition != D->getTemplatedDecl()) { |
| 3576 | Decl *ImportedDef |
| 3577 | = Importer.Import(Definition->getDescribedClassTemplate()); |
| 3578 | if (!ImportedDef) |
| 3579 | return 0; |
| 3580 | |
| 3581 | return Importer.Imported(D, ImportedDef); |
| 3582 | } |
| 3583 | |
| 3584 | // Import the major distinguishing characteristics of this class template. |
| 3585 | DeclContext *DC, *LexicalDC; |
| 3586 | DeclarationName Name; |
| 3587 | SourceLocation Loc; |
| 3588 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 3589 | return 0; |
| 3590 | |
| 3591 | // We may already have a template of the same name; try to find and match it. |
| 3592 | if (!DC->isFunctionOrMethod()) { |
| 3593 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 3594 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
| 3595 | Lookup.first != Lookup.second; |
| 3596 | ++Lookup.first) { |
| 3597 | if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 3598 | continue; |
| 3599 | |
| 3600 | Decl *Found = *Lookup.first; |
| 3601 | if (ClassTemplateDecl *FoundTemplate |
| 3602 | = dyn_cast<ClassTemplateDecl>(Found)) { |
| 3603 | if (IsStructuralMatch(D, FoundTemplate)) { |
| 3604 | // The class templates structurally match; call it the same template. |
| 3605 | // FIXME: We may be filling in a forward declaration here. Handle |
| 3606 | // this case! |
| 3607 | Importer.Imported(D->getTemplatedDecl(), |
| 3608 | FoundTemplate->getTemplatedDecl()); |
| 3609 | return Importer.Imported(D, FoundTemplate); |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | ConflictingDecls.push_back(*Lookup.first); |
| 3614 | } |
| 3615 | |
| 3616 | if (!ConflictingDecls.empty()) { |
| 3617 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, |
| 3618 | ConflictingDecls.data(), |
| 3619 | ConflictingDecls.size()); |
| 3620 | } |
| 3621 | |
| 3622 | if (!Name) |
| 3623 | return 0; |
| 3624 | } |
| 3625 | |
| 3626 | CXXRecordDecl *DTemplated = D->getTemplatedDecl(); |
| 3627 | |
| 3628 | // Create the declaration that is being templated. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3629 | SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); |
| 3630 | SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3631 | CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(), |
| 3632 | DTemplated->getTagKind(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3633 | DC, StartLoc, IdLoc, |
| 3634 | Name.getAsIdentifierInfo()); |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3635 | D2Templated->setAccess(DTemplated->getAccess()); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3636 | D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); |
Douglas Gregor | 040afae | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3637 | D2Templated->setLexicalDeclContext(LexicalDC); |
| 3638 | |
| 3639 | // Create the class template declaration itself. |
| 3640 | TemplateParameterList *TemplateParams |
| 3641 | = ImportTemplateParameterList(D->getTemplateParameters()); |
| 3642 | if (!TemplateParams) |
| 3643 | return 0; |
| 3644 | |
| 3645 | ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC, |
| 3646 | Loc, Name, TemplateParams, |
| 3647 | D2Templated, |
| 3648 | /*PrevDecl=*/0); |
| 3649 | D2Templated->setDescribedClassTemplate(D2); |
| 3650 | |
| 3651 | D2->setAccess(D->getAccess()); |
| 3652 | D2->setLexicalDeclContext(LexicalDC); |
| 3653 | LexicalDC->addDecl(D2); |
| 3654 | |
| 3655 | // Note the relationship between the class templates. |
| 3656 | Importer.Imported(D, D2); |
| 3657 | Importer.Imported(DTemplated, D2Templated); |
| 3658 | |
| 3659 | if (DTemplated->isDefinition() && !D2Templated->isDefinition()) { |
| 3660 | // FIXME: Import definition! |
| 3661 | } |
| 3662 | |
| 3663 | return D2; |
| 3664 | } |
| 3665 | |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 3666 | Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl( |
| 3667 | ClassTemplateSpecializationDecl *D) { |
| 3668 | // If this record has a definition in the translation unit we're coming from, |
| 3669 | // but this particular declaration is not that definition, import the |
| 3670 | // definition and map to that. |
| 3671 | TagDecl *Definition = D->getDefinition(); |
| 3672 | if (Definition && Definition != D) { |
| 3673 | Decl *ImportedDef = Importer.Import(Definition); |
| 3674 | if (!ImportedDef) |
| 3675 | return 0; |
| 3676 | |
| 3677 | return Importer.Imported(D, ImportedDef); |
| 3678 | } |
| 3679 | |
| 3680 | ClassTemplateDecl *ClassTemplate |
| 3681 | = cast_or_null<ClassTemplateDecl>(Importer.Import( |
| 3682 | D->getSpecializedTemplate())); |
| 3683 | if (!ClassTemplate) |
| 3684 | return 0; |
| 3685 | |
| 3686 | // Import the context of this declaration. |
| 3687 | DeclContext *DC = ClassTemplate->getDeclContext(); |
| 3688 | if (!DC) |
| 3689 | return 0; |
| 3690 | |
| 3691 | DeclContext *LexicalDC = DC; |
| 3692 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3693 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3694 | if (!LexicalDC) |
| 3695 | return 0; |
| 3696 | } |
| 3697 | |
| 3698 | // Import the location of this declaration. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3699 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
| 3700 | SourceLocation IdLoc = Importer.Import(D->getLocation()); |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 3701 | |
| 3702 | // Import template arguments. |
| 3703 | llvm::SmallVector<TemplateArgument, 2> TemplateArgs; |
| 3704 | if (ImportTemplateArguments(D->getTemplateArgs().data(), |
| 3705 | D->getTemplateArgs().size(), |
| 3706 | TemplateArgs)) |
| 3707 | return 0; |
| 3708 | |
| 3709 | // Try to find an existing specialization with these template arguments. |
| 3710 | void *InsertPos = 0; |
| 3711 | ClassTemplateSpecializationDecl *D2 |
| 3712 | = ClassTemplate->findSpecialization(TemplateArgs.data(), |
| 3713 | TemplateArgs.size(), InsertPos); |
| 3714 | if (D2) { |
| 3715 | // We already have a class template specialization with these template |
| 3716 | // arguments. |
| 3717 | |
| 3718 | // FIXME: Check for specialization vs. instantiation errors. |
| 3719 | |
| 3720 | if (RecordDecl *FoundDef = D2->getDefinition()) { |
| 3721 | if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) { |
| 3722 | // The record types structurally match, or the "from" translation |
| 3723 | // unit only had a forward declaration anyway; call it the same |
| 3724 | // function. |
| 3725 | return Importer.Imported(D, FoundDef); |
| 3726 | } |
| 3727 | } |
| 3728 | } else { |
| 3729 | // Create a new specialization. |
| 3730 | D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(), |
| 3731 | D->getTagKind(), DC, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3732 | StartLoc, IdLoc, |
| 3733 | ClassTemplate, |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 3734 | TemplateArgs.data(), |
| 3735 | TemplateArgs.size(), |
| 3736 | /*PrevDecl=*/0); |
| 3737 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 3738 | |
| 3739 | // Add this specialization to the class template. |
| 3740 | ClassTemplate->AddSpecialization(D2, InsertPos); |
| 3741 | |
| 3742 | // Import the qualifier, if any. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3743 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 3744 | |
| 3745 | // Add the specialization to this context. |
| 3746 | D2->setLexicalDeclContext(LexicalDC); |
| 3747 | LexicalDC->addDecl(D2); |
| 3748 | } |
| 3749 | Importer.Imported(D, D2); |
| 3750 | |
| 3751 | if (D->isDefinition() && ImportDefinition(D, D2)) |
| 3752 | return 0; |
| 3753 | |
| 3754 | return D2; |
| 3755 | } |
| 3756 | |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 3757 | //---------------------------------------------------------------------------- |
| 3758 | // Import Statements |
| 3759 | //---------------------------------------------------------------------------- |
| 3760 | |
| 3761 | Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { |
| 3762 | Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) |
| 3763 | << S->getStmtClassName(); |
| 3764 | return 0; |
| 3765 | } |
| 3766 | |
| 3767 | //---------------------------------------------------------------------------- |
| 3768 | // Import Expressions |
| 3769 | //---------------------------------------------------------------------------- |
| 3770 | Expr *ASTNodeImporter::VisitExpr(Expr *E) { |
| 3771 | Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) |
| 3772 | << E->getStmtClassName(); |
| 3773 | return 0; |
| 3774 | } |
| 3775 | |
Douglas Gregor | 4408063 | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 3776 | Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | 4408063 | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 3777 | ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); |
| 3778 | if (!ToD) |
| 3779 | return 0; |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 3780 | |
| 3781 | NamedDecl *FoundD = 0; |
| 3782 | if (E->getDecl() != E->getFoundDecl()) { |
| 3783 | FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl())); |
| 3784 | if (!FoundD) |
| 3785 | return 0; |
| 3786 | } |
Douglas Gregor | 4408063 | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 3787 | |
| 3788 | QualType T = Importer.Import(E->getType()); |
| 3789 | if (T.isNull()) |
| 3790 | return 0; |
| 3791 | |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 3792 | return DeclRefExpr::Create(Importer.getToContext(), |
| 3793 | Importer.Import(E->getQualifierLoc()), |
Douglas Gregor | 4408063 | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 3794 | ToD, |
| 3795 | Importer.Import(E->getLocation()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3796 | T, E->getValueKind(), |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 3797 | FoundD, |
Douglas Gregor | 4408063 | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 3798 | /*FIXME:TemplateArgs=*/0); |
| 3799 | } |
| 3800 | |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 3801 | Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { |
| 3802 | QualType T = Importer.Import(E->getType()); |
| 3803 | if (T.isNull()) |
| 3804 | return 0; |
| 3805 | |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3806 | return IntegerLiteral::Create(Importer.getToContext(), |
| 3807 | E->getValue(), T, |
| 3808 | Importer.Import(E->getLocation())); |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
Douglas Gregor | b2e400a | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 3811 | Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { |
| 3812 | QualType T = Importer.Import(E->getType()); |
| 3813 | if (T.isNull()) |
| 3814 | return 0; |
| 3815 | |
| 3816 | return new (Importer.getToContext()) CharacterLiteral(E->getValue(), |
| 3817 | E->isWide(), T, |
| 3818 | Importer.Import(E->getLocation())); |
| 3819 | } |
| 3820 | |
Douglas Gregor | f638f95 | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 3821 | Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { |
| 3822 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 3823 | if (!SubExpr) |
| 3824 | return 0; |
| 3825 | |
| 3826 | return new (Importer.getToContext()) |
| 3827 | ParenExpr(Importer.Import(E->getLParen()), |
| 3828 | Importer.Import(E->getRParen()), |
| 3829 | SubExpr); |
| 3830 | } |
| 3831 | |
| 3832 | Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { |
| 3833 | QualType T = Importer.Import(E->getType()); |
| 3834 | if (T.isNull()) |
| 3835 | return 0; |
| 3836 | |
| 3837 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 3838 | if (!SubExpr) |
| 3839 | return 0; |
| 3840 | |
| 3841 | return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3842 | T, E->getValueKind(), |
| 3843 | E->getObjectKind(), |
Douglas Gregor | f638f95 | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 3844 | Importer.Import(E->getOperatorLoc())); |
| 3845 | } |
| 3846 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3847 | Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr( |
| 3848 | UnaryExprOrTypeTraitExpr *E) { |
Douglas Gregor | bd249a5 | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 3849 | QualType ResultType = Importer.Import(E->getType()); |
| 3850 | |
| 3851 | if (E->isArgumentType()) { |
| 3852 | TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); |
| 3853 | if (!TInfo) |
| 3854 | return 0; |
| 3855 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3856 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), |
| 3857 | TInfo, ResultType, |
Douglas Gregor | bd249a5 | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 3858 | Importer.Import(E->getOperatorLoc()), |
| 3859 | Importer.Import(E->getRParenLoc())); |
| 3860 | } |
| 3861 | |
| 3862 | Expr *SubExpr = Importer.Import(E->getArgumentExpr()); |
| 3863 | if (!SubExpr) |
| 3864 | return 0; |
| 3865 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3866 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), |
| 3867 | SubExpr, ResultType, |
Douglas Gregor | bd249a5 | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 3868 | Importer.Import(E->getOperatorLoc()), |
| 3869 | Importer.Import(E->getRParenLoc())); |
| 3870 | } |
| 3871 | |
Douglas Gregor | f638f95 | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 3872 | Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { |
| 3873 | QualType T = Importer.Import(E->getType()); |
| 3874 | if (T.isNull()) |
| 3875 | return 0; |
| 3876 | |
| 3877 | Expr *LHS = Importer.Import(E->getLHS()); |
| 3878 | if (!LHS) |
| 3879 | return 0; |
| 3880 | |
| 3881 | Expr *RHS = Importer.Import(E->getRHS()); |
| 3882 | if (!RHS) |
| 3883 | return 0; |
| 3884 | |
| 3885 | return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3886 | T, E->getValueKind(), |
| 3887 | E->getObjectKind(), |
Douglas Gregor | f638f95 | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 3888 | Importer.Import(E->getOperatorLoc())); |
| 3889 | } |
| 3890 | |
| 3891 | Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { |
| 3892 | QualType T = Importer.Import(E->getType()); |
| 3893 | if (T.isNull()) |
| 3894 | return 0; |
| 3895 | |
| 3896 | QualType CompLHSType = Importer.Import(E->getComputationLHSType()); |
| 3897 | if (CompLHSType.isNull()) |
| 3898 | return 0; |
| 3899 | |
| 3900 | QualType CompResultType = Importer.Import(E->getComputationResultType()); |
| 3901 | if (CompResultType.isNull()) |
| 3902 | return 0; |
| 3903 | |
| 3904 | Expr *LHS = Importer.Import(E->getLHS()); |
| 3905 | if (!LHS) |
| 3906 | return 0; |
| 3907 | |
| 3908 | Expr *RHS = Importer.Import(E->getRHS()); |
| 3909 | if (!RHS) |
| 3910 | return 0; |
| 3911 | |
| 3912 | return new (Importer.getToContext()) |
| 3913 | CompoundAssignOperator(LHS, RHS, E->getOpcode(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3914 | T, E->getValueKind(), |
| 3915 | E->getObjectKind(), |
| 3916 | CompLHSType, CompResultType, |
Douglas Gregor | f638f95 | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 3917 | Importer.Import(E->getOperatorLoc())); |
| 3918 | } |
| 3919 | |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 3920 | static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3921 | if (E->path_empty()) return false; |
| 3922 | |
| 3923 | // TODO: import cast paths |
| 3924 | return true; |
| 3925 | } |
| 3926 | |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 3927 | Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 3928 | QualType T = Importer.Import(E->getType()); |
| 3929 | if (T.isNull()) |
| 3930 | return 0; |
| 3931 | |
| 3932 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 3933 | if (!SubExpr) |
| 3934 | return 0; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3935 | |
| 3936 | CXXCastPath BasePath; |
| 3937 | if (ImportCastPath(E, BasePath)) |
| 3938 | return 0; |
| 3939 | |
| 3940 | return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3941 | SubExpr, &BasePath, E->getValueKind()); |
Douglas Gregor | 36ead2e | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
Douglas Gregor | 008847a | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 3944 | Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) { |
| 3945 | QualType T = Importer.Import(E->getType()); |
| 3946 | if (T.isNull()) |
| 3947 | return 0; |
| 3948 | |
| 3949 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 3950 | if (!SubExpr) |
| 3951 | return 0; |
| 3952 | |
| 3953 | TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); |
| 3954 | if (!TInfo && E->getTypeInfoAsWritten()) |
| 3955 | return 0; |
| 3956 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3957 | CXXCastPath BasePath; |
| 3958 | if (ImportCastPath(E, BasePath)) |
| 3959 | return 0; |
| 3960 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3961 | return CStyleCastExpr::Create(Importer.getToContext(), T, |
| 3962 | E->getValueKind(), E->getCastKind(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3963 | SubExpr, &BasePath, TInfo, |
| 3964 | Importer.Import(E->getLParenLoc()), |
| 3965 | Importer.Import(E->getRParenLoc())); |
Douglas Gregor | 008847a | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 3968 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, |
Douglas Gregor | d8868a6 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 3969 | ASTContext &FromContext, FileManager &FromFileManager, |
| 3970 | bool MinimalImport) |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 3971 | : ToContext(ToContext), FromContext(FromContext), |
Douglas Gregor | d8868a6 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 3972 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), |
| 3973 | Minimal(MinimalImport) |
| 3974 | { |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3975 | ImportedDecls[FromContext.getTranslationUnitDecl()] |
| 3976 | = ToContext.getTranslationUnitDecl(); |
| 3977 | } |
| 3978 | |
| 3979 | ASTImporter::~ASTImporter() { } |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 3980 | |
| 3981 | QualType ASTImporter::Import(QualType FromT) { |
| 3982 | if (FromT.isNull()) |
| 3983 | return QualType(); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3984 | |
| 3985 | const Type *fromTy = FromT.getTypePtr(); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 3986 | |
Douglas Gregor | 169fba5 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 3987 | // Check whether we've already imported this type. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3988 | llvm::DenseMap<const Type *, const Type *>::iterator Pos |
| 3989 | = ImportedTypes.find(fromTy); |
Douglas Gregor | 169fba5 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 3990 | if (Pos != ImportedTypes.end()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3991 | return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers()); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 3992 | |
Douglas Gregor | 169fba5 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 3993 | // Import the type |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 3994 | ASTNodeImporter Importer(*this); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3995 | QualType ToT = Importer.Visit(fromTy); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 3996 | if (ToT.isNull()) |
| 3997 | return ToT; |
| 3998 | |
Douglas Gregor | 169fba5 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 3999 | // Record the imported type. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4000 | ImportedTypes[fromTy] = ToT.getTypePtr(); |
Douglas Gregor | 169fba5 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 4001 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4002 | return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers()); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4005 | TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { |
Douglas Gregor | 82fc4bf | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 4006 | if (!FromTSI) |
| 4007 | return FromTSI; |
| 4008 | |
| 4009 | // FIXME: For now we just create a "trivial" type source info based |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 4010 | // on the type and a single location. Implement a real version of this. |
Douglas Gregor | 82fc4bf | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 4011 | QualType T = Import(FromTSI->getType()); |
| 4012 | if (T.isNull()) |
| 4013 | return 0; |
| 4014 | |
| 4015 | return ToContext.getTrivialTypeSourceInfo(T, |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4016 | FromTSI->getTypeLoc().getSourceRange().getBegin()); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
| 4019 | Decl *ASTImporter::Import(Decl *FromD) { |
| 4020 | if (!FromD) |
| 4021 | return 0; |
| 4022 | |
| 4023 | // Check whether we've already imported this declaration. |
| 4024 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); |
| 4025 | if (Pos != ImportedDecls.end()) |
| 4026 | return Pos->second; |
| 4027 | |
| 4028 | // Import the type |
| 4029 | ASTNodeImporter Importer(*this); |
| 4030 | Decl *ToD = Importer.Visit(FromD); |
| 4031 | if (!ToD) |
| 4032 | return 0; |
| 4033 | |
| 4034 | // Record the imported declaration. |
| 4035 | ImportedDecls[FromD] = ToD; |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4036 | |
| 4037 | if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) { |
| 4038 | // Keep track of anonymous tags that have an associated typedef. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4039 | if (FromTag->getTypedefNameForAnonDecl()) |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4040 | AnonTagsWithPendingTypedefs.push_back(FromTag); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4041 | } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) { |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4042 | // When we've finished transforming a typedef, see whether it was the |
| 4043 | // typedef for an anonymous tag. |
| 4044 | for (llvm::SmallVector<TagDecl *, 4>::iterator |
| 4045 | FromTag = AnonTagsWithPendingTypedefs.begin(), |
| 4046 | FromTagEnd = AnonTagsWithPendingTypedefs.end(); |
| 4047 | FromTag != FromTagEnd; ++FromTag) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4048 | if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) { |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4049 | if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) { |
| 4050 | // We found the typedef for an anonymous tag; link them. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4051 | ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD)); |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4052 | AnonTagsWithPendingTypedefs.erase(FromTag); |
| 4053 | break; |
| 4054 | } |
| 4055 | } |
| 4056 | } |
| 4057 | } |
| 4058 | |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4059 | return ToD; |
| 4060 | } |
| 4061 | |
| 4062 | DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { |
| 4063 | if (!FromDC) |
| 4064 | return FromDC; |
| 4065 | |
| 4066 | return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); |
| 4067 | } |
| 4068 | |
| 4069 | Expr *ASTImporter::Import(Expr *FromE) { |
| 4070 | if (!FromE) |
| 4071 | return 0; |
| 4072 | |
| 4073 | return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); |
| 4074 | } |
| 4075 | |
| 4076 | Stmt *ASTImporter::Import(Stmt *FromS) { |
| 4077 | if (!FromS) |
| 4078 | return 0; |
| 4079 | |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 4080 | // Check whether we've already imported this declaration. |
| 4081 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); |
| 4082 | if (Pos != ImportedStmts.end()) |
| 4083 | return Pos->second; |
| 4084 | |
| 4085 | // Import the type |
| 4086 | ASTNodeImporter Importer(*this); |
| 4087 | Stmt *ToS = Importer.Visit(FromS); |
| 4088 | if (!ToS) |
| 4089 | return 0; |
| 4090 | |
| 4091 | // Record the imported declaration. |
| 4092 | ImportedStmts[FromS] = ToS; |
| 4093 | return ToS; |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { |
| 4097 | if (!FromNNS) |
| 4098 | return 0; |
| 4099 | |
Douglas Gregor | 8703b1c | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 4100 | NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); |
| 4101 | |
| 4102 | switch (FromNNS->getKind()) { |
| 4103 | case NestedNameSpecifier::Identifier: |
| 4104 | if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { |
| 4105 | return NestedNameSpecifier::Create(ToContext, prefix, II); |
| 4106 | } |
| 4107 | return 0; |
| 4108 | |
| 4109 | case NestedNameSpecifier::Namespace: |
| 4110 | if (NamespaceDecl *NS = |
| 4111 | cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { |
| 4112 | return NestedNameSpecifier::Create(ToContext, prefix, NS); |
| 4113 | } |
| 4114 | return 0; |
| 4115 | |
| 4116 | case NestedNameSpecifier::NamespaceAlias: |
| 4117 | if (NamespaceAliasDecl *NSAD = |
| 4118 | cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { |
| 4119 | return NestedNameSpecifier::Create(ToContext, prefix, NSAD); |
| 4120 | } |
| 4121 | return 0; |
| 4122 | |
| 4123 | case NestedNameSpecifier::Global: |
| 4124 | return NestedNameSpecifier::GlobalSpecifier(ToContext); |
| 4125 | |
| 4126 | case NestedNameSpecifier::TypeSpec: |
| 4127 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 4128 | QualType T = Import(QualType(FromNNS->getAsType(), 0u)); |
| 4129 | if (!T.isNull()) { |
| 4130 | bool bTemplate = FromNNS->getKind() == |
| 4131 | NestedNameSpecifier::TypeSpecWithTemplate; |
| 4132 | return NestedNameSpecifier::Create(ToContext, prefix, |
| 4133 | bTemplate, T.getTypePtr()); |
| 4134 | } |
| 4135 | } |
| 4136 | return 0; |
| 4137 | } |
| 4138 | |
| 4139 | llvm_unreachable("Invalid nested name specifier kind"); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4140 | return 0; |
| 4141 | } |
| 4142 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4143 | NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { |
| 4144 | // FIXME: Implement! |
| 4145 | return NestedNameSpecifierLoc(); |
| 4146 | } |
| 4147 | |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4148 | TemplateName ASTImporter::Import(TemplateName From) { |
| 4149 | switch (From.getKind()) { |
| 4150 | case TemplateName::Template: |
| 4151 | if (TemplateDecl *ToTemplate |
| 4152 | = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) |
| 4153 | return TemplateName(ToTemplate); |
| 4154 | |
| 4155 | return TemplateName(); |
| 4156 | |
| 4157 | case TemplateName::OverloadedTemplate: { |
| 4158 | OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); |
| 4159 | UnresolvedSet<2> ToTemplates; |
| 4160 | for (OverloadedTemplateStorage::iterator I = FromStorage->begin(), |
| 4161 | E = FromStorage->end(); |
| 4162 | I != E; ++I) { |
| 4163 | if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I))) |
| 4164 | ToTemplates.addDecl(To); |
| 4165 | else |
| 4166 | return TemplateName(); |
| 4167 | } |
| 4168 | return ToContext.getOverloadedTemplateName(ToTemplates.begin(), |
| 4169 | ToTemplates.end()); |
| 4170 | } |
| 4171 | |
| 4172 | case TemplateName::QualifiedTemplate: { |
| 4173 | QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); |
| 4174 | NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); |
| 4175 | if (!Qualifier) |
| 4176 | return TemplateName(); |
| 4177 | |
| 4178 | if (TemplateDecl *ToTemplate |
| 4179 | = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) |
| 4180 | return ToContext.getQualifiedTemplateName(Qualifier, |
| 4181 | QTN->hasTemplateKeyword(), |
| 4182 | ToTemplate); |
| 4183 | |
| 4184 | return TemplateName(); |
| 4185 | } |
| 4186 | |
| 4187 | case TemplateName::DependentTemplate: { |
| 4188 | DependentTemplateName *DTN = From.getAsDependentTemplateName(); |
| 4189 | NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); |
| 4190 | if (!Qualifier) |
| 4191 | return TemplateName(); |
| 4192 | |
| 4193 | if (DTN->isIdentifier()) { |
| 4194 | return ToContext.getDependentTemplateName(Qualifier, |
| 4195 | Import(DTN->getIdentifier())); |
| 4196 | } |
| 4197 | |
| 4198 | return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); |
| 4199 | } |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4200 | |
| 4201 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 4202 | SubstTemplateTemplateParmPackStorage *SubstPack |
| 4203 | = From.getAsSubstTemplateTemplateParmPack(); |
| 4204 | TemplateTemplateParmDecl *Param |
| 4205 | = cast_or_null<TemplateTemplateParmDecl>( |
| 4206 | Import(SubstPack->getParameterPack())); |
| 4207 | if (!Param) |
| 4208 | return TemplateName(); |
| 4209 | |
| 4210 | ASTNodeImporter Importer(*this); |
| 4211 | TemplateArgument ArgPack |
| 4212 | = Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); |
| 4213 | if (ArgPack.isNull()) |
| 4214 | return TemplateName(); |
| 4215 | |
| 4216 | return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack); |
| 4217 | } |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | llvm_unreachable("Invalid template name kind"); |
| 4221 | return TemplateName(); |
| 4222 | } |
| 4223 | |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4224 | SourceLocation ASTImporter::Import(SourceLocation FromLoc) { |
| 4225 | if (FromLoc.isInvalid()) |
| 4226 | return SourceLocation(); |
| 4227 | |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4228 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 4229 | |
| 4230 | // For now, map everything down to its spelling location, so that we |
| 4231 | // don't have to import macro instantiations. |
| 4232 | // FIXME: Import macro instantiations! |
| 4233 | FromLoc = FromSM.getSpellingLoc(FromLoc); |
| 4234 | std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); |
| 4235 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 4236 | return ToSM.getLocForStartOfFile(Import(Decomposed.first)) |
| 4237 | .getFileLocWithOffset(Decomposed.second); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
| 4240 | SourceRange ASTImporter::Import(SourceRange FromRange) { |
| 4241 | return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); |
| 4242 | } |
| 4243 | |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4244 | FileID ASTImporter::Import(FileID FromID) { |
Sebastian Redl | 535a3e2 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 4245 | llvm::DenseMap<FileID, FileID>::iterator Pos |
| 4246 | = ImportedFileIDs.find(FromID); |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4247 | if (Pos != ImportedFileIDs.end()) |
| 4248 | return Pos->second; |
| 4249 | |
| 4250 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 4251 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 4252 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); |
| 4253 | assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet"); |
| 4254 | |
| 4255 | // Include location of this file. |
| 4256 | SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); |
| 4257 | |
| 4258 | // Map the FileID for to the "to" source manager. |
| 4259 | FileID ToID; |
| 4260 | const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 4261 | if (Cache->OrigEntry) { |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4262 | // FIXME: We probably want to use getVirtualFile(), so we don't hit the |
| 4263 | // disk again |
| 4264 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather |
| 4265 | // than mmap the files several times. |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 4266 | const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName()); |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4267 | ToID = ToSM.createFileID(Entry, ToIncludeLoc, |
| 4268 | FromSLoc.getFile().getFileCharacteristic()); |
| 4269 | } else { |
| 4270 | // FIXME: We want to re-use the existing MemoryBuffer! |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 4271 | const llvm::MemoryBuffer * |
| 4272 | FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM); |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4273 | llvm::MemoryBuffer *ToBuf |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 4274 | = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4275 | FromBuf->getBufferIdentifier()); |
| 4276 | ToID = ToSM.createFileIDForMemBuffer(ToBuf); |
| 4277 | } |
| 4278 | |
| 4279 | |
Sebastian Redl | 535a3e2 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 4280 | ImportedFileIDs[FromID] = ToID; |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4281 | return ToID; |
| 4282 | } |
| 4283 | |
Douglas Gregor | d8868a6 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 4284 | void ASTImporter::ImportDefinition(Decl *From) { |
| 4285 | Decl *To = Import(From); |
| 4286 | if (!To) |
| 4287 | return; |
| 4288 | |
| 4289 | if (DeclContext *FromDC = cast<DeclContext>(From)) { |
| 4290 | ASTNodeImporter Importer(*this); |
| 4291 | Importer.ImportDeclContext(FromDC, true); |
| 4292 | } |
| 4293 | } |
| 4294 | |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4295 | DeclarationName ASTImporter::Import(DeclarationName FromName) { |
| 4296 | if (!FromName) |
| 4297 | return DeclarationName(); |
| 4298 | |
| 4299 | switch (FromName.getNameKind()) { |
| 4300 | case DeclarationName::Identifier: |
| 4301 | return Import(FromName.getAsIdentifierInfo()); |
| 4302 | |
| 4303 | case DeclarationName::ObjCZeroArgSelector: |
| 4304 | case DeclarationName::ObjCOneArgSelector: |
| 4305 | case DeclarationName::ObjCMultiArgSelector: |
| 4306 | return Import(FromName.getObjCSelector()); |
| 4307 | |
| 4308 | case DeclarationName::CXXConstructorName: { |
| 4309 | QualType T = Import(FromName.getCXXNameType()); |
| 4310 | if (T.isNull()) |
| 4311 | return DeclarationName(); |
| 4312 | |
| 4313 | return ToContext.DeclarationNames.getCXXConstructorName( |
| 4314 | ToContext.getCanonicalType(T)); |
| 4315 | } |
| 4316 | |
| 4317 | case DeclarationName::CXXDestructorName: { |
| 4318 | QualType T = Import(FromName.getCXXNameType()); |
| 4319 | if (T.isNull()) |
| 4320 | return DeclarationName(); |
| 4321 | |
| 4322 | return ToContext.DeclarationNames.getCXXDestructorName( |
| 4323 | ToContext.getCanonicalType(T)); |
| 4324 | } |
| 4325 | |
| 4326 | case DeclarationName::CXXConversionFunctionName: { |
| 4327 | QualType T = Import(FromName.getCXXNameType()); |
| 4328 | if (T.isNull()) |
| 4329 | return DeclarationName(); |
| 4330 | |
| 4331 | return ToContext.DeclarationNames.getCXXConversionFunctionName( |
| 4332 | ToContext.getCanonicalType(T)); |
| 4333 | } |
| 4334 | |
| 4335 | case DeclarationName::CXXOperatorName: |
| 4336 | return ToContext.DeclarationNames.getCXXOperatorName( |
| 4337 | FromName.getCXXOverloadedOperator()); |
| 4338 | |
| 4339 | case DeclarationName::CXXLiteralOperatorName: |
| 4340 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( |
| 4341 | Import(FromName.getCXXLiteralIdentifier())); |
| 4342 | |
| 4343 | case DeclarationName::CXXUsingDirective: |
| 4344 | // FIXME: STATICS! |
| 4345 | return DeclarationName::getUsingDirectiveName(); |
| 4346 | } |
| 4347 | |
| 4348 | // Silence bogus GCC warning |
| 4349 | return DeclarationName(); |
| 4350 | } |
| 4351 | |
Douglas Gregor | d5dc83a | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4352 | IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4353 | if (!FromId) |
| 4354 | return 0; |
| 4355 | |
| 4356 | return &ToContext.Idents.get(FromId->getName()); |
| 4357 | } |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 4358 | |
Douglas Gregor | c3f2d2b | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 4359 | Selector ASTImporter::Import(Selector FromSel) { |
| 4360 | if (FromSel.isNull()) |
| 4361 | return Selector(); |
| 4362 | |
| 4363 | llvm::SmallVector<IdentifierInfo *, 4> Idents; |
| 4364 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); |
| 4365 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) |
| 4366 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); |
| 4367 | return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); |
| 4368 | } |
| 4369 | |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 4370 | DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, |
| 4371 | DeclContext *DC, |
| 4372 | unsigned IDNS, |
| 4373 | NamedDecl **Decls, |
| 4374 | unsigned NumDecls) { |
| 4375 | return Name; |
| 4376 | } |
| 4377 | |
| 4378 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 4379 | return ToContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 4383 | return FromContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 4384 | } |
Douglas Gregor | 5ce5dab | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 4385 | |
| 4386 | Decl *ASTImporter::Imported(Decl *From, Decl *To) { |
| 4387 | ImportedDecls[From] = To; |
| 4388 | return To; |
Daniel Dunbar | af66758 | 2010-02-13 20:24:39 +0000 | [diff] [blame] | 4389 | } |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4390 | |
| 4391 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4392 | llvm::DenseMap<const Type *, const Type *>::iterator Pos |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4393 | = ImportedTypes.find(From.getTypePtr()); |
| 4394 | if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) |
| 4395 | return true; |
| 4396 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 4397 | StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls); |
Benjamin Kramer | bb2d176 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 4398 | return Ctx.IsStructurallyEquivalent(From, To); |
Douglas Gregor | ea35d11 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4399 | } |