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