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