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