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