Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "CXXABI.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTMutationListener.h" |
| 17 | #include "clang/AST/Attr.h" |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 18 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/Comment.h" |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 20 | #include "clang/AST/CommentCommandTraits.h" |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExternalASTSource.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 27 | #include "clang/AST/Mangle.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 28 | #include "clang/AST/RecordLayout.h" |
Reid Kleckner | cff1512 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 29 | #include "clang/AST/RecursiveASTVisitor.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 30 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 31 | #include "clang/Basic/Builtins.h" |
Chris Lattner | a9376d4 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 32 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/StringExtras.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Capacity.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 39 | #include <map> |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 40 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | using namespace clang; |
| 42 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 43 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 44 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 45 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 46 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 47 | unsigned ASTContext::NumImplicitMoveConstructors; |
| 48 | unsigned ASTContext::NumImplicitMoveConstructorsDeclared; |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 49 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 50 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 51 | unsigned ASTContext::NumImplicitMoveAssignmentOperators; |
| 52 | unsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 53 | unsigned ASTContext::NumImplicitDestructors; |
| 54 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 55 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | enum FloatingRank { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 57 | HalfRank, FloatRank, DoubleRank, LongDoubleRank |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 60 | RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 61 | if (!CommentsLoaded && ExternalSource) { |
| 62 | ExternalSource->ReadComments(); |
| 63 | CommentsLoaded = true; |
| 64 | } |
| 65 | |
| 66 | assert(D); |
| 67 | |
Dmitri Gribenko | c3fee35 | 2012-06-28 16:19:39 +0000 | [diff] [blame] | 68 | // User can not attach documentation to implicit declarations. |
| 69 | if (D->isImplicit()) |
| 70 | return NULL; |
| 71 | |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 72 | // User can not attach documentation to implicit instantiations. |
| 73 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 74 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 75 | return NULL; |
| 76 | } |
| 77 | |
Dmitri Gribenko | dce750b | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 78 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 79 | if (VD->isStaticDataMember() && |
| 80 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) { |
| 85 | if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 86 | return NULL; |
| 87 | } |
| 88 | |
Dmitri Gribenko | d1e5c0d | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 89 | if (const ClassTemplateSpecializationDecl *CTSD = |
| 90 | dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 91 | TemplateSpecializationKind TSK = CTSD->getSpecializationKind(); |
| 92 | if (TSK == TSK_ImplicitInstantiation || |
| 93 | TSK == TSK_Undeclared) |
| 94 | return NULL; |
| 95 | } |
| 96 | |
Dmitri Gribenko | dce750b | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 97 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 98 | if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 99 | return NULL; |
| 100 | } |
Fariborz Jahanian | 099ecfb | 2013-04-17 21:05:20 +0000 | [diff] [blame] | 101 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 102 | // When tag declaration (but not definition!) is part of the |
| 103 | // decl-specifier-seq of some other declaration, it doesn't get comment |
| 104 | if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition()) |
| 105 | return NULL; |
| 106 | } |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 107 | // TODO: handle comments for function parameters properly. |
| 108 | if (isa<ParmVarDecl>(D)) |
| 109 | return NULL; |
| 110 | |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 111 | // TODO: we could look up template parameter documentation in the template |
| 112 | // documentation. |
| 113 | if (isa<TemplateTypeParmDecl>(D) || |
| 114 | isa<NonTypeTemplateParmDecl>(D) || |
| 115 | isa<TemplateTemplateParmDecl>(D)) |
| 116 | return NULL; |
| 117 | |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 118 | ArrayRef<RawComment *> RawComments = Comments.getComments(); |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 119 | |
| 120 | // If there are no comments anywhere, we won't find anything. |
| 121 | if (RawComments.empty()) |
| 122 | return NULL; |
| 123 | |
Dmitri Gribenko | abd56c8 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 124 | // Find declaration location. |
| 125 | // For Objective-C declarations we generally don't expect to have multiple |
| 126 | // declarators, thus use declaration starting location as the "declaration |
| 127 | // location". |
| 128 | // For all other declarations multiple declarators are used quite frequently, |
| 129 | // so we use the location of the identifier as the "declaration location". |
| 130 | SourceLocation DeclLoc; |
| 131 | if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) || |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 132 | isa<ObjCPropertyDecl>(D) || |
Dmitri Gribenko | c27bc80 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 133 | isa<RedeclarableTemplateDecl>(D) || |
| 134 | isa<ClassTemplateSpecializationDecl>(D)) |
Dmitri Gribenko | abd56c8 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 135 | DeclLoc = D->getLocStart(); |
| 136 | else |
| 137 | DeclLoc = D->getLocation(); |
| 138 | |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 139 | // If the declaration doesn't map directly to a location in a file, we |
| 140 | // can't find the comment. |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 141 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
| 142 | return NULL; |
| 143 | |
| 144 | // Find the comment that occurs just after this declaration. |
Dmitri Gribenko | a444f18 | 2012-07-17 22:01:09 +0000 | [diff] [blame] | 145 | ArrayRef<RawComment *>::iterator Comment; |
| 146 | { |
| 147 | // When searching for comments during parsing, the comment we are looking |
| 148 | // for is usually among the last two comments we parsed -- check them |
| 149 | // first. |
Dmitri Gribenko | 6fd7d30 | 2013-04-10 15:35:17 +0000 | [diff] [blame] | 150 | RawComment CommentAtDeclLoc( |
| 151 | SourceMgr, SourceRange(DeclLoc), false, |
| 152 | LangOpts.CommentOpts.ParseAllComments); |
Dmitri Gribenko | a444f18 | 2012-07-17 22:01:09 +0000 | [diff] [blame] | 153 | BeforeThanCompare<RawComment> Compare(SourceMgr); |
| 154 | ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1; |
| 155 | bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 156 | if (!Found && RawComments.size() >= 2) { |
| 157 | MaybeBeforeDecl--; |
| 158 | Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 159 | } |
| 160 | |
| 161 | if (Found) { |
| 162 | Comment = MaybeBeforeDecl + 1; |
| 163 | assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(), |
| 164 | &CommentAtDeclLoc, Compare)); |
| 165 | } else { |
| 166 | // Slow path. |
| 167 | Comment = std::lower_bound(RawComments.begin(), RawComments.end(), |
| 168 | &CommentAtDeclLoc, Compare); |
| 169 | } |
| 170 | } |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 171 | |
| 172 | // Decompose the location for the declaration and find the beginning of the |
| 173 | // file buffer. |
| 174 | std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc); |
| 175 | |
| 176 | // First check whether we have a trailing comment. |
| 177 | if (Comment != RawComments.end() && |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 178 | (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() && |
Dmitri Gribenko | 9c00676 | 2012-07-06 23:27:33 +0000 | [diff] [blame] | 179 | (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D))) { |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 180 | std::pair<FileID, unsigned> CommentBeginDecomp |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 181 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin()); |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 182 | // Check that Doxygen trailing comment comes after the declaration, starts |
| 183 | // on the same line and in the same file as the declaration. |
| 184 | if (DeclLocDecomp.first == CommentBeginDecomp.first && |
| 185 | SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) |
| 186 | == SourceMgr.getLineNumber(CommentBeginDecomp.first, |
| 187 | CommentBeginDecomp.second)) { |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 188 | return *Comment; |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | // The comment just after the declaration was not a trailing comment. |
| 193 | // Let's look at the previous comment. |
| 194 | if (Comment == RawComments.begin()) |
| 195 | return NULL; |
| 196 | --Comment; |
| 197 | |
| 198 | // Check that we actually have a non-member Doxygen comment. |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 199 | if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment()) |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 200 | return NULL; |
| 201 | |
| 202 | // Decompose the end of the comment. |
| 203 | std::pair<FileID, unsigned> CommentEndDecomp |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 204 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd()); |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 205 | |
| 206 | // If the comment and the declaration aren't in the same file, then they |
| 207 | // aren't related. |
| 208 | if (DeclLocDecomp.first != CommentEndDecomp.first) |
| 209 | return NULL; |
| 210 | |
| 211 | // Get the corresponding buffer. |
| 212 | bool Invalid = false; |
| 213 | const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first, |
| 214 | &Invalid).data(); |
| 215 | if (Invalid) |
| 216 | return NULL; |
| 217 | |
| 218 | // Extract text between the comment and declaration. |
| 219 | StringRef Text(Buffer + CommentEndDecomp.second, |
| 220 | DeclLocDecomp.second - CommentEndDecomp.second); |
| 221 | |
Dmitri Gribenko | 8bdb58a | 2012-06-27 23:43:37 +0000 | [diff] [blame] | 222 | // There should be no other declarations or preprocessor directives between |
| 223 | // comment and declaration. |
Dmitri Gribenko | abd56c8 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 224 | if (Text.find_first_of(",;{}#@") != StringRef::npos) |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 225 | return NULL; |
| 226 | |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 227 | return *Comment; |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 230 | namespace { |
| 231 | /// If we have a 'templated' declaration for a template, adjust 'D' to |
| 232 | /// refer to the actual template. |
Dmitri Gribenko | 2125c90 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 233 | /// If we have an implicit instantiation, adjust 'D' to refer to template. |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 234 | const Decl *adjustDeclToTemplate(const Decl *D) { |
Douglas Gregor | cd81df2 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 235 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Dmitri Gribenko | 2125c90 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 236 | // Is this function declaration part of a function template? |
Douglas Gregor | cd81df2 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 237 | if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) |
Dmitri Gribenko | 2125c90 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 238 | return FTD; |
| 239 | |
| 240 | // Nothing to do if function is not an implicit instantiation. |
| 241 | if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 242 | return D; |
| 243 | |
| 244 | // Function is an implicit instantiation of a function template? |
| 245 | if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate()) |
| 246 | return FTD; |
| 247 | |
| 248 | // Function is instantiated from a member definition of a class template? |
| 249 | if (const FunctionDecl *MemberDecl = |
| 250 | FD->getInstantiatedFromMemberFunction()) |
| 251 | return MemberDecl; |
| 252 | |
| 253 | return D; |
Douglas Gregor | cd81df2 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 254 | } |
Dmitri Gribenko | 2125c90 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 255 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 256 | // Static data member is instantiated from a member definition of a class |
| 257 | // template? |
| 258 | if (VD->isStaticDataMember()) |
| 259 | if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember()) |
| 260 | return MemberDecl; |
| 261 | |
| 262 | return D; |
| 263 | } |
| 264 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) { |
| 265 | // Is this class declaration part of a class template? |
| 266 | if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate()) |
| 267 | return CTD; |
| 268 | |
| 269 | // Class is an implicit instantiation of a class template or partial |
| 270 | // specialization? |
| 271 | if (const ClassTemplateSpecializationDecl *CTSD = |
| 272 | dyn_cast<ClassTemplateSpecializationDecl>(CRD)) { |
| 273 | if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation) |
| 274 | return D; |
| 275 | llvm::PointerUnion<ClassTemplateDecl *, |
| 276 | ClassTemplatePartialSpecializationDecl *> |
| 277 | PU = CTSD->getSpecializedTemplateOrPartial(); |
| 278 | return PU.is<ClassTemplateDecl*>() ? |
| 279 | static_cast<const Decl*>(PU.get<ClassTemplateDecl *>()) : |
| 280 | static_cast<const Decl*>( |
| 281 | PU.get<ClassTemplatePartialSpecializationDecl *>()); |
| 282 | } |
| 283 | |
| 284 | // Class is instantiated from a member definition of a class template? |
| 285 | if (const MemberSpecializationInfo *Info = |
| 286 | CRD->getMemberSpecializationInfo()) |
| 287 | return Info->getInstantiatedFrom(); |
| 288 | |
| 289 | return D; |
| 290 | } |
| 291 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 292 | // Enum is instantiated from a member definition of a class template? |
| 293 | if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum()) |
| 294 | return MemberDecl; |
| 295 | |
| 296 | return D; |
| 297 | } |
| 298 | // FIXME: Adjust alias templates? |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 299 | return D; |
| 300 | } |
| 301 | } // unnamed namespace |
| 302 | |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 303 | const RawComment *ASTContext::getRawCommentForAnyRedecl( |
| 304 | const Decl *D, |
| 305 | const Decl **OriginalDecl) const { |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 306 | D = adjustDeclToTemplate(D); |
Douglas Gregor | cd81df2 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 307 | |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 308 | // Check whether we have cached a comment for this declaration already. |
| 309 | { |
| 310 | llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos = |
| 311 | RedeclComments.find(D); |
| 312 | if (Pos != RedeclComments.end()) { |
| 313 | const RawCommentAndCacheFlags &Raw = Pos->second; |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 314 | if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) { |
| 315 | if (OriginalDecl) |
| 316 | *OriginalDecl = Raw.getOriginalDecl(); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 317 | return Raw.getRaw(); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 318 | } |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 319 | } |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 320 | } |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 321 | |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 322 | // Search for comments attached to declarations in the redeclaration chain. |
| 323 | const RawComment *RC = NULL; |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 324 | const Decl *OriginalDeclForRC = NULL; |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 325 | for (Decl::redecl_iterator I = D->redecls_begin(), |
| 326 | E = D->redecls_end(); |
| 327 | I != E; ++I) { |
| 328 | llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos = |
| 329 | RedeclComments.find(*I); |
| 330 | if (Pos != RedeclComments.end()) { |
| 331 | const RawCommentAndCacheFlags &Raw = Pos->second; |
| 332 | if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) { |
| 333 | RC = Raw.getRaw(); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 334 | OriginalDeclForRC = Raw.getOriginalDecl(); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | } else { |
| 338 | RC = getRawCommentForDeclNoCache(*I); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 339 | OriginalDeclForRC = *I; |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 340 | RawCommentAndCacheFlags Raw; |
| 341 | if (RC) { |
| 342 | Raw.setRaw(RC); |
| 343 | Raw.setKind(RawCommentAndCacheFlags::FromDecl); |
| 344 | } else |
| 345 | Raw.setKind(RawCommentAndCacheFlags::NoCommentInDecl); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 346 | Raw.setOriginalDecl(*I); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 347 | RedeclComments[*I] = Raw; |
| 348 | if (RC) |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | |
Dmitri Gribenko | 8376f59 | 2012-06-28 16:25:36 +0000 | [diff] [blame] | 353 | // If we found a comment, it should be a documentation comment. |
| 354 | assert(!RC || RC->isDocumentation()); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 355 | |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 356 | if (OriginalDecl) |
| 357 | *OriginalDecl = OriginalDeclForRC; |
| 358 | |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 359 | // Update cache for every declaration in the redeclaration chain. |
| 360 | RawCommentAndCacheFlags Raw; |
| 361 | Raw.setRaw(RC); |
| 362 | Raw.setKind(RawCommentAndCacheFlags::FromRedecl); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 363 | Raw.setOriginalDecl(OriginalDeclForRC); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 364 | |
| 365 | for (Decl::redecl_iterator I = D->redecls_begin(), |
| 366 | E = D->redecls_end(); |
| 367 | I != E; ++I) { |
| 368 | RawCommentAndCacheFlags &R = RedeclComments[*I]; |
| 369 | if (R.getKind() == RawCommentAndCacheFlags::NoCommentInDecl) |
| 370 | R = Raw; |
| 371 | } |
| 372 | |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 373 | return RC; |
| 374 | } |
| 375 | |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 376 | static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod, |
| 377 | SmallVectorImpl<const NamedDecl *> &Redeclared) { |
| 378 | const DeclContext *DC = ObjCMethod->getDeclContext(); |
| 379 | if (const ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(DC)) { |
| 380 | const ObjCInterfaceDecl *ID = IMD->getClassInterface(); |
| 381 | if (!ID) |
| 382 | return; |
| 383 | // Add redeclared method here. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 384 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 385 | Ext = ID->known_extensions_begin(), |
| 386 | ExtEnd = ID->known_extensions_end(); |
| 387 | Ext != ExtEnd; ++Ext) { |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 388 | if (ObjCMethodDecl *RedeclaredMethod = |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 389 | Ext->getMethod(ObjCMethod->getSelector(), |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 390 | ObjCMethod->isInstanceMethod())) |
| 391 | Redeclared.push_back(RedeclaredMethod); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Fariborz Jahanian | 749ace6 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 396 | comments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC, |
| 397 | const Decl *D) const { |
| 398 | comments::DeclInfo *ThisDeclInfo = new (*this) comments::DeclInfo; |
| 399 | ThisDeclInfo->CommentDecl = D; |
| 400 | ThisDeclInfo->IsFilled = false; |
| 401 | ThisDeclInfo->fill(); |
| 402 | ThisDeclInfo->CommentDecl = FC->getDecl(); |
| 403 | comments::FullComment *CFC = |
| 404 | new (*this) comments::FullComment(FC->getBlocks(), |
| 405 | ThisDeclInfo); |
| 406 | return CFC; |
| 407 | |
| 408 | } |
| 409 | |
Richard Smith | 0a74a4c | 2013-05-21 05:24:00 +0000 | [diff] [blame] | 410 | comments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const { |
| 411 | const RawComment *RC = getRawCommentForDeclNoCache(D); |
| 412 | return RC ? RC->parse(*this, 0, D) : 0; |
| 413 | } |
| 414 | |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 415 | comments::FullComment *ASTContext::getCommentForDecl( |
| 416 | const Decl *D, |
| 417 | const Preprocessor *PP) const { |
Fariborz Jahanian | fbff0c4 | 2013-05-13 17:27:00 +0000 | [diff] [blame] | 418 | if (D->isInvalidDecl()) |
| 419 | return NULL; |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 420 | D = adjustDeclToTemplate(D); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 421 | |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 422 | const Decl *Canonical = D->getCanonicalDecl(); |
| 423 | llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos = |
| 424 | ParsedComments.find(Canonical); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 425 | |
| 426 | if (Pos != ParsedComments.end()) { |
Fariborz Jahanian | 749ace6 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 427 | if (Canonical != D) { |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 428 | comments::FullComment *FC = Pos->second; |
Fariborz Jahanian | 749ace6 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 429 | comments::FullComment *CFC = cloneFullComment(FC, D); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 430 | return CFC; |
| 431 | } |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 432 | return Pos->second; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 435 | const Decl *OriginalDecl; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 436 | |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 437 | const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 438 | if (!RC) { |
| 439 | if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) { |
Dmitri Gribenko | 1e905da | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 440 | SmallVector<const NamedDecl*, 8> Overridden; |
Fariborz Jahanian | c328d9c | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 441 | const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 23799e3 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 442 | if (OMD && OMD->isPropertyAccessor()) |
| 443 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
| 444 | if (comments::FullComment *FC = getCommentForDecl(PDecl, PP)) |
| 445 | return cloneFullComment(FC, D); |
Fariborz Jahanian | c328d9c | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 446 | if (OMD) |
Dmitri Gribenko | 1e905da | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 447 | addRedeclaredMethods(OMD, Overridden); |
| 448 | getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden); |
Fariborz Jahanian | 23799e3 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 449 | for (unsigned i = 0, e = Overridden.size(); i < e; i++) |
| 450 | if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP)) |
| 451 | return cloneFullComment(FC, D); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 452 | } |
Fariborz Jahanian | 4857fdc | 2013-05-02 15:44:16 +0000 | [diff] [blame] | 453 | else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Dmitri Gribenko | d1e5c0d | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 454 | // Attach any tag type's documentation to its typedef if latter |
Fariborz Jahanian | 23799e3 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 455 | // does not have one of its own. |
Fariborz Jahanian | 41170b5 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 456 | QualType QT = TD->getUnderlyingType(); |
Dmitri Gribenko | d1e5c0d | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 457 | if (const TagType *TT = QT->getAs<TagType>()) |
| 458 | if (const Decl *TD = TT->getDecl()) |
| 459 | if (comments::FullComment *FC = getCommentForDecl(TD, PP)) |
Fariborz Jahanian | 23799e3 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 460 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 41170b5 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 461 | } |
Fariborz Jahanian | 622bb4a | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 462 | else if (const ObjCInterfaceDecl *IC = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 463 | while (IC->getSuperClass()) { |
| 464 | IC = IC->getSuperClass(); |
| 465 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 466 | return cloneFullComment(FC, D); |
| 467 | } |
| 468 | } |
| 469 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
| 470 | if (const ObjCInterfaceDecl *IC = CD->getClassInterface()) |
| 471 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 472 | return cloneFullComment(FC, D); |
| 473 | } |
| 474 | else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 475 | if (!(RD = RD->getDefinition())) |
| 476 | return NULL; |
| 477 | // Check non-virtual bases. |
| 478 | for (CXXRecordDecl::base_class_const_iterator I = |
| 479 | RD->bases_begin(), E = RD->bases_end(); I != E; ++I) { |
Fariborz Jahanian | 91efca0 | 2013-04-26 23:34:36 +0000 | [diff] [blame] | 480 | if (I->isVirtual() || (I->getAccessSpecifier() != AS_public)) |
Fariborz Jahanian | 622bb4a | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 481 | continue; |
| 482 | QualType Ty = I->getType(); |
| 483 | if (Ty.isNull()) |
| 484 | continue; |
| 485 | if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) { |
| 486 | if (!(NonVirtualBase= NonVirtualBase->getDefinition())) |
| 487 | continue; |
| 488 | |
| 489 | if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP)) |
| 490 | return cloneFullComment(FC, D); |
| 491 | } |
| 492 | } |
| 493 | // Check virtual bases. |
| 494 | for (CXXRecordDecl::base_class_const_iterator I = |
| 495 | RD->vbases_begin(), E = RD->vbases_end(); I != E; ++I) { |
Fariborz Jahanian | 91efca0 | 2013-04-26 23:34:36 +0000 | [diff] [blame] | 496 | if (I->getAccessSpecifier() != AS_public) |
| 497 | continue; |
Fariborz Jahanian | 622bb4a | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 498 | QualType Ty = I->getType(); |
| 499 | if (Ty.isNull()) |
| 500 | continue; |
| 501 | if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) { |
| 502 | if (!(VirtualBase= VirtualBase->getDefinition())) |
| 503 | continue; |
| 504 | if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP)) |
| 505 | return cloneFullComment(FC, D); |
| 506 | } |
| 507 | } |
| 508 | } |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 509 | return NULL; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Dmitri Gribenko | 4b41c65 | 2012-08-22 18:12:19 +0000 | [diff] [blame] | 512 | // If the RawComment was attached to other redeclaration of this Decl, we |
| 513 | // should parse the comment in context of that other Decl. This is important |
| 514 | // because comments can contain references to parameter names which can be |
| 515 | // different across redeclarations. |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 516 | if (D != OriginalDecl) |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 517 | return getCommentForDecl(OriginalDecl, PP); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 518 | |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 519 | comments::FullComment *FC = RC->parse(*this, PP, D); |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 520 | ParsedComments[Canonical] = FC; |
| 521 | return FC; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 524 | void |
| 525 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 526 | TemplateTemplateParmDecl *Parm) { |
| 527 | ID.AddInteger(Parm->getDepth()); |
| 528 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 529 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 530 | |
| 531 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 532 | ID.AddInteger(Params->size()); |
| 533 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 534 | PEnd = Params->end(); |
| 535 | P != PEnd; ++P) { |
| 536 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 537 | ID.AddInteger(0); |
| 538 | ID.AddBoolean(TTP->isParameterPack()); |
| 539 | continue; |
| 540 | } |
| 541 | |
| 542 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 543 | ID.AddInteger(1); |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 544 | ID.AddBoolean(NTTP->isParameterPack()); |
Eli Friedman | 9e9c454 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 545 | ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr()); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 546 | if (NTTP->isExpandedParameterPack()) { |
| 547 | ID.AddBoolean(true); |
| 548 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
Eli Friedman | 9e9c454 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 549 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 550 | QualType T = NTTP->getExpansionType(I); |
| 551 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); |
| 552 | } |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 553 | } else |
| 554 | ID.AddBoolean(false); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 555 | continue; |
| 556 | } |
| 557 | |
| 558 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 559 | ID.AddInteger(2); |
| 560 | Profile(ID, TTP); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | TemplateTemplateParmDecl * |
| 565 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 566 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 567 | // Check if we already have a canonical template template parameter. |
| 568 | llvm::FoldingSetNodeID ID; |
| 569 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 570 | void *InsertPos = 0; |
| 571 | CanonicalTemplateTemplateParm *Canonical |
| 572 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 573 | if (Canonical) |
| 574 | return Canonical->getParam(); |
| 575 | |
| 576 | // Build a canonical template parameter list. |
| 577 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 578 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 579 | CanonParams.reserve(Params->size()); |
| 580 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 581 | PEnd = Params->end(); |
| 582 | P != PEnd; ++P) { |
| 583 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 584 | CanonParams.push_back( |
| 585 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 586 | SourceLocation(), |
| 587 | SourceLocation(), |
| 588 | TTP->getDepth(), |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 589 | TTP->getIndex(), 0, false, |
| 590 | TTP->isParameterPack())); |
| 591 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 592 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 593 | QualType T = getCanonicalType(NTTP->getType()); |
| 594 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 595 | NonTypeTemplateParmDecl *Param; |
| 596 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 597 | SmallVector<QualType, 2> ExpandedTypes; |
| 598 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 599 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 600 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 601 | ExpandedTInfos.push_back( |
| 602 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 603 | } |
| 604 | |
| 605 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 606 | SourceLocation(), |
| 607 | SourceLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 608 | NTTP->getDepth(), |
| 609 | NTTP->getPosition(), 0, |
| 610 | T, |
| 611 | TInfo, |
| 612 | ExpandedTypes.data(), |
| 613 | ExpandedTypes.size(), |
| 614 | ExpandedTInfos.data()); |
| 615 | } else { |
| 616 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 617 | SourceLocation(), |
| 618 | SourceLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 619 | NTTP->getDepth(), |
| 620 | NTTP->getPosition(), 0, |
| 621 | T, |
| 622 | NTTP->isParameterPack(), |
| 623 | TInfo); |
| 624 | } |
| 625 | CanonParams.push_back(Param); |
| 626 | |
| 627 | } else |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 628 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 629 | cast<TemplateTemplateParmDecl>(*P))); |
| 630 | } |
| 631 | |
| 632 | TemplateTemplateParmDecl *CanonTTP |
| 633 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 634 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 635 | TTP->getPosition(), |
| 636 | TTP->isParameterPack(), |
| 637 | 0, |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 638 | TemplateParameterList::Create(*this, SourceLocation(), |
| 639 | SourceLocation(), |
| 640 | CanonParams.data(), |
| 641 | CanonParams.size(), |
| 642 | SourceLocation())); |
| 643 | |
| 644 | // Get the new insert position for the node we care about. |
| 645 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 646 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 647 | (void)Canonical; |
| 648 | |
| 649 | // Create the canonical template template parameter entry. |
| 650 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 651 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 652 | return CanonTTP; |
| 653 | } |
| 654 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 655 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 656 | if (!LangOpts.CPlusPlus) return 0; |
| 657 | |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 658 | switch (T.getCXXABI().getKind()) { |
| 659 | case TargetCXXABI::GenericARM: |
| 660 | case TargetCXXABI::iOS: |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 661 | return CreateARMCXXABI(*this); |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 662 | case TargetCXXABI::GenericAArch64: // Same as Itanium at this level |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 663 | case TargetCXXABI::GenericItanium: |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 664 | return CreateItaniumCXXABI(*this); |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 665 | case TargetCXXABI::Microsoft: |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 666 | return CreateMicrosoftCXXABI(*this); |
| 667 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 668 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 671 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 672 | const LangOptions &LOpts) { |
| 673 | if (LOpts.FakeAddressSpaceMap) { |
| 674 | // The fake address space map must have a distinct entry for each |
| 675 | // language-specific address space. |
| 676 | static const unsigned FakeAddrSpaceMap[] = { |
| 677 | 1, // opencl_global |
| 678 | 2, // opencl_local |
Peter Collingbourne | 4dc34eb | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 679 | 3, // opencl_constant |
| 680 | 4, // cuda_device |
| 681 | 5, // cuda_constant |
| 682 | 6 // cuda_shared |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 683 | }; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 684 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 685 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 686 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
Douglas Gregor | 3e3cd93 | 2011-09-01 20:23:19 +0000 | [diff] [blame] | 690 | ASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 691 | const TargetInfo *t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 692 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 693 | Builtin::Context &builtins, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 694 | unsigned size_reserve, |
| 695 | bool DelayInitialization) |
| 696 | : FunctionProtoTypes(this_()), |
| 697 | TemplateSpecializationTypes(this_()), |
| 698 | DependentTemplateSpecializationTypes(this_()), |
| 699 | SubstTemplateTemplateParmPacks(this_()), |
| 700 | GlobalNestedNameSpecifier(0), |
Nico Weber | cac18ad | 2013-06-20 21:44:55 +0000 | [diff] [blame] | 701 | Int128Decl(0), UInt128Decl(0), Float128StubDecl(0), |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 702 | BuiltinVaListDecl(0), |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 703 | ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0), |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 704 | BOOLDecl(0), |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 705 | CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 706 | FILEDecl(0), |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 707 | jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0), |
| 708 | BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 709 | cudaConfigureCallDecl(0), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 710 | NullTypeSourceInfo(QualType()), |
| 711 | FirstLocalImport(), LastLocalImport(), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 712 | SourceMgr(SM), LangOpts(LOpts), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 713 | AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 714 | Idents(idents), Selectors(sels), |
| 715 | BuiltinInfo(builtins), |
| 716 | DeclarationNames(*this), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 717 | ExternalSource(0), Listener(0), |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 718 | Comments(SM), CommentsLoaded(false), |
Dmitri Gribenko | 6ebf091 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 719 | CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), |
Rafael Espindola | 42b7861 | 2013-05-29 19:51:12 +0000 | [diff] [blame] | 720 | LastSDM(0, 0) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 721 | { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 723 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 724 | |
| 725 | if (!DelayInitialization) { |
| 726 | assert(t && "No target supplied for ASTContext initialization"); |
| 727 | InitBuiltinTypes(*t); |
| 728 | } |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 731 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 732 | // Release the DenseMaps associated with DeclContext objects. |
| 733 | // FIXME: Is this the ideal solution? |
| 734 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 735 | |
Manuel Klimek | f0f353b | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 736 | // Call all of the deallocation functions on all of their targets. |
| 737 | for (DeallocationMap::const_iterator I = Deallocations.begin(), |
| 738 | E = Deallocations.end(); I != E; ++I) |
| 739 | for (unsigned J = 0, N = I->second.size(); J != N; ++J) |
| 740 | (I->first)((I->second)[J]); |
| 741 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 742 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 743 | // because they can contain DenseMaps. |
| 744 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 745 | const ASTRecordLayout*>::iterator |
| 746 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 747 | // Increment in loop to prevent using deallocated memory. |
| 748 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 749 | R->Destroy(*this); |
| 750 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 751 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 752 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 753 | // Increment in loop to prevent using deallocated memory. |
| 754 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 755 | R->Destroy(*this); |
| 756 | } |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 757 | |
| 758 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 759 | AEnd = DeclAttrs.end(); |
| 760 | A != AEnd; ++A) |
| 761 | A->second->~AttrVec(); |
| 762 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 763 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 764 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
Manuel Klimek | f0f353b | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 765 | Deallocations[Callback].push_back(Data); |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | void |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 769 | ASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 770 | ExternalSource.reset(Source.take()); |
| 771 | } |
| 772 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 773 | void ASTContext::PrintStats() const { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 774 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 775 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 776 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 777 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 779 | #define ABSTRACT_TYPE(Name, Parent) |
| 780 | #include "clang/AST/TypeNodes.def" |
| 781 | 0 // Extra |
| 782 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 783 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 784 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 785 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 786 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 789 | unsigned Idx = 0; |
| 790 | unsigned TotalBytes = 0; |
| 791 | #define TYPE(Name, Parent) \ |
| 792 | if (counts[Idx]) \ |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 793 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 794 | << " types\n"; \ |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 795 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 796 | ++Idx; |
| 797 | #define ABSTRACT_TYPE(Name, Parent) |
| 798 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 800 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 801 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 802 | // Implicit special member functions. |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 803 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 804 | << NumImplicitDefaultConstructors |
| 805 | << " implicit default constructors created\n"; |
| 806 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 807 | << NumImplicitCopyConstructors |
| 808 | << " implicit copy constructors created\n"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 809 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 810 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 811 | << NumImplicitMoveConstructors |
| 812 | << " implicit move constructors created\n"; |
| 813 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 814 | << NumImplicitCopyAssignmentOperators |
| 815 | << " implicit copy assignment operators created\n"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 816 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 817 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 818 | << NumImplicitMoveAssignmentOperators |
| 819 | << " implicit move assignment operators created\n"; |
| 820 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 821 | << NumImplicitDestructors |
| 822 | << " implicit destructors created\n"; |
| 823 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 824 | if (ExternalSource.get()) { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 825 | llvm::errs() << "\n"; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 826 | ExternalSource->PrintStats(); |
| 827 | } |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 828 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 829 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Douglas Gregor | 772eeae | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 832 | TypedefDecl *ASTContext::getInt128Decl() const { |
| 833 | if (!Int128Decl) { |
| 834 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty); |
| 835 | Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 836 | getTranslationUnitDecl(), |
| 837 | SourceLocation(), |
| 838 | SourceLocation(), |
| 839 | &Idents.get("__int128_t"), |
| 840 | TInfo); |
| 841 | } |
| 842 | |
| 843 | return Int128Decl; |
| 844 | } |
| 845 | |
| 846 | TypedefDecl *ASTContext::getUInt128Decl() const { |
| 847 | if (!UInt128Decl) { |
| 848 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty); |
| 849 | UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 850 | getTranslationUnitDecl(), |
| 851 | SourceLocation(), |
| 852 | SourceLocation(), |
| 853 | &Idents.get("__uint128_t"), |
| 854 | TInfo); |
| 855 | } |
| 856 | |
| 857 | return UInt128Decl; |
| 858 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 859 | |
Nico Weber | cac18ad | 2013-06-20 21:44:55 +0000 | [diff] [blame] | 860 | TypeDecl *ASTContext::getFloat128StubType() const { |
Nico Weber | 3f7c1b1 | 2013-06-21 01:29:36 +0000 | [diff] [blame] | 861 | assert(LangOpts.CPlusPlus && "should only be called for c++"); |
Nico Weber | cac18ad | 2013-06-20 21:44:55 +0000 | [diff] [blame] | 862 | if (!Float128StubDecl) { |
Nico Weber | 9b9bdba | 2013-06-20 23:30:30 +0000 | [diff] [blame] | 863 | Float128StubDecl = CXXRecordDecl::Create(const_cast<ASTContext &>(*this), |
| 864 | TTK_Struct, |
| 865 | getTranslationUnitDecl(), |
| 866 | SourceLocation(), |
| 867 | SourceLocation(), |
| 868 | &Idents.get("__float128")); |
Nico Weber | cac18ad | 2013-06-20 21:44:55 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | return Float128StubDecl; |
| 872 | } |
| 873 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 874 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 875 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 876 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 877 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 880 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target) { |
| 881 | assert((!this->Target || this->Target == &Target) && |
| 882 | "Incorrect target reinitialization"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 883 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 885 | this->Target = &Target; |
| 886 | |
| 887 | ABI.reset(createCXXABI(Target)); |
| 888 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
| 889 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 890 | // C99 6.2.5p19. |
| 891 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 893 | // C99 6.2.5p2. |
| 894 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 895 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 896 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 897 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 898 | else |
| 899 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 900 | // C99 6.2.5p4. |
| 901 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 902 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 903 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 904 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 905 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 906 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 907 | // C99 6.2.5p6. |
| 908 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 909 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 910 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 911 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 912 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 914 | // C99 6.2.5p10. |
| 915 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 916 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 917 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 918 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 919 | // GNU extension, 128-bit integers. |
| 920 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 921 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 922 | |
Hans Wennborg | 15f92ba | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 923 | // C++ 3.9.1p5 |
| 924 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
| 925 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 926 | else // -fshort-wchar makes wchar_t be unsigned. |
| 927 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 928 | if (LangOpts.CPlusPlus && LangOpts.WChar) |
| 929 | WideCharTy = WCharTy; |
| 930 | else { |
| 931 | // C99 (or C++ using -fno-wchar). |
| 932 | WideCharTy = getFromTargetType(Target.getWCharType()); |
| 933 | } |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 934 | |
James Molloy | 392da48 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 935 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 936 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 937 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 938 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 939 | else // C99 |
| 940 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 941 | |
| 942 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 943 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 944 | else // C99 |
| 945 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 946 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 947 | // Placeholder type for type-dependent expressions whose type is |
| 948 | // completely unknown. No code should ever check a type against |
| 949 | // DependentTy and users should never see it; however, it is here to |
| 950 | // help diagnose failures to properly check for type-dependent |
| 951 | // expressions. |
| 952 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 953 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 954 | // Placeholder type for functions. |
| 955 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 956 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 957 | // Placeholder type for bound members. |
| 958 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 959 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 960 | // Placeholder type for pseudo-objects. |
| 961 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 962 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 963 | // "any" type; useful for debugger-like clients. |
| 964 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 965 | |
John McCall | 0ddaeb9 | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 966 | // Placeholder type for unbridged ARC casts. |
| 967 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 968 | |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 969 | // Placeholder type for builtin functions. |
| 970 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
| 971 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 972 | // C99 6.2.5p11. |
| 973 | FloatComplexTy = getComplexType(FloatTy); |
| 974 | DoubleComplexTy = getComplexType(DoubleTy); |
| 975 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 976 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 977 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 978 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 979 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 980 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 981 | |
| 982 | if (LangOpts.OpenCL) { |
| 983 | InitBuiltinType(OCLImage1dTy, BuiltinType::OCLImage1d); |
| 984 | InitBuiltinType(OCLImage1dArrayTy, BuiltinType::OCLImage1dArray); |
| 985 | InitBuiltinType(OCLImage1dBufferTy, BuiltinType::OCLImage1dBuffer); |
| 986 | InitBuiltinType(OCLImage2dTy, BuiltinType::OCLImage2d); |
| 987 | InitBuiltinType(OCLImage2dArrayTy, BuiltinType::OCLImage2dArray); |
| 988 | InitBuiltinType(OCLImage3dTy, BuiltinType::OCLImage3d); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 989 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 990 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 991 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 992 | } |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 993 | |
| 994 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 93a4994 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 995 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 996 | SignedCharTy : BoolTy); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 997 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 998 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 999 | |
| 1000 | ObjCSuperType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1002 | // void * type |
| 1003 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1004 | |
| 1005 | // nullptr type (C++0x 2.14.7) |
| 1006 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1007 | |
| 1008 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 1009 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 1010 | |
| 1011 | // Builtin type used to help define __builtin_va_list. |
| 1012 | VaListTagTy = QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1015 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1016 | return SourceMgr.getDiagnostics(); |
| 1017 | } |
| 1018 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 1019 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 1020 | AttrVec *&Result = DeclAttrs[D]; |
| 1021 | if (!Result) { |
| 1022 | void *Mem = Allocate(sizeof(AttrVec)); |
| 1023 | Result = new (Mem) AttrVec; |
| 1024 | } |
| 1025 | |
| 1026 | return *Result; |
| 1027 | } |
| 1028 | |
| 1029 | /// \brief Erase the attributes corresponding to the given declaration. |
| 1030 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 1031 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 1032 | if (Pos != DeclAttrs.end()) { |
| 1033 | Pos->second->~AttrVec(); |
| 1034 | DeclAttrs.erase(Pos); |
| 1035 | } |
| 1036 | } |
| 1037 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1038 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1039 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1040 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1041 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1042 | = InstantiatedFromStaticDataMember.find(Var); |
| 1043 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 1044 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1046 | return Pos->second; |
| 1047 | } |
| 1048 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1050 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 1051 | TemplateSpecializationKind TSK, |
| 1052 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1053 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 1054 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 1055 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 1056 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1057 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 1058 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1061 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 1062 | const FunctionDecl *FD){ |
| 1063 | assert(FD && "Specialization is 0"); |
| 1064 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1065 | = ClassScopeSpecializationPattern.find(FD); |
| 1066 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1067 | return 0; |
| 1068 | |
| 1069 | return Pos->second; |
| 1070 | } |
| 1071 | |
| 1072 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 1073 | FunctionDecl *Pattern) { |
| 1074 | assert(FD && "Specialization is 0"); |
| 1075 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1076 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1079 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1080 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1081 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1082 | = InstantiatedFromUsingDecl.find(UUD); |
| 1083 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1084 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1086 | return Pos->second; |
| 1087 | } |
| 1088 | |
| 1089 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1090 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 1091 | assert((isa<UsingDecl>(Pattern) || |
| 1092 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 1093 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 1094 | "pattern decl is not a using decl"); |
| 1095 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 1096 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 1097 | } |
| 1098 | |
| 1099 | UsingShadowDecl * |
| 1100 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 1101 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 1102 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 1103 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 1104 | return 0; |
| 1105 | |
| 1106 | return Pos->second; |
| 1107 | } |
| 1108 | |
| 1109 | void |
| 1110 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 1111 | UsingShadowDecl *Pattern) { |
| 1112 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 1113 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1116 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 1117 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 1118 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 1119 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 1120 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1122 | return Pos->second; |
| 1123 | } |
| 1124 | |
| 1125 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 1126 | FieldDecl *Tmpl) { |
| 1127 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 1128 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 1129 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 1130 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1132 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 1133 | } |
| 1134 | |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 1135 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 1136 | const FieldDecl *LastFD) const { |
| 1137 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1138 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 1141 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 1142 | const FieldDecl *LastFD) const { |
| 1143 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1144 | FD->getBitWidthValue(*this) == 0 && |
| 1145 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1148 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 1149 | const FieldDecl *LastFD) const { |
| 1150 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1151 | FD->getBitWidthValue(*this) && |
| 1152 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1155 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1156 | const FieldDecl *LastFD) const { |
| 1157 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1158 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1161 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1162 | const FieldDecl *LastFD) const { |
| 1163 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1164 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1167 | ASTContext::overridden_cxx_method_iterator |
| 1168 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 1169 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1170 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1171 | if (Pos == OverriddenMethods.end()) |
| 1172 | return 0; |
| 1173 | |
| 1174 | return Pos->second.begin(); |
| 1175 | } |
| 1176 | |
| 1177 | ASTContext::overridden_cxx_method_iterator |
| 1178 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 1179 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1180 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1181 | if (Pos == OverriddenMethods.end()) |
| 1182 | return 0; |
| 1183 | |
| 1184 | return Pos->second.end(); |
| 1185 | } |
| 1186 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1187 | unsigned |
| 1188 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 1189 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1190 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1191 | if (Pos == OverriddenMethods.end()) |
| 1192 | return 0; |
| 1193 | |
| 1194 | return Pos->second.size(); |
| 1195 | } |
| 1196 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1197 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 1198 | const CXXMethodDecl *Overridden) { |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1199 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1200 | OverriddenMethods[Method].push_back(Overridden); |
| 1201 | } |
| 1202 | |
Dmitri Gribenko | 1e905da | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 1203 | void ASTContext::getOverriddenMethods( |
| 1204 | const NamedDecl *D, |
| 1205 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1206 | assert(D); |
| 1207 | |
| 1208 | if (const CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 685d104 | 2013-04-17 00:09:03 +0000 | [diff] [blame] | 1209 | Overridden.append(overridden_methods_begin(CXXMethod), |
| 1210 | overridden_methods_end(CXXMethod)); |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); |
| 1215 | if (!Method) |
| 1216 | return; |
| 1217 | |
Argyrios Kyrtzidis | 740ae67 | 2012-10-09 18:19:01 +0000 | [diff] [blame] | 1218 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
| 1219 | Method->getOverriddenMethods(OverDecls); |
Argyrios Kyrtzidis | bc0a2bb | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1220 | Overridden.append(OverDecls.begin(), OverDecls.end()); |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1223 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 1224 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 1225 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 1226 | if (!FirstLocalImport) { |
| 1227 | FirstLocalImport = Import; |
| 1228 | LastLocalImport = Import; |
| 1229 | return; |
| 1230 | } |
| 1231 | |
| 1232 | LastLocalImport->NextLocalImport = Import; |
| 1233 | LastLocalImport = Import; |
| 1234 | } |
| 1235 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1236 | //===----------------------------------------------------------------------===// |
| 1237 | // Type Sizing and Analysis |
| 1238 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1239 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1240 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 1241 | /// scalar floating point type. |
| 1242 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1243 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1244 | assert(BT && "Not a floating point type!"); |
| 1245 | switch (BT->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1246 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1247 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1248 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 1249 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 1250 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1254 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1255 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 1256 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1257 | /// If @p RefAsPointee, references are treated like their underlying type |
| 1258 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1259 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1260 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1261 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1262 | bool UseAlignAttrOnly = false; |
| 1263 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 1264 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1265 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1266 | // __attribute__((aligned)) can increase or decrease alignment |
| 1267 | // *except* on a struct or struct member, where it only increases |
| 1268 | // alignment unless 'packed' is also specified. |
| 1269 | // |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1270 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1271 | // ignore that possibility; Sema should diagnose it. |
| 1272 | if (isa<FieldDecl>(D)) { |
| 1273 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 1274 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 1275 | } else { |
| 1276 | UseAlignAttrOnly = true; |
| 1277 | } |
| 1278 | } |
Fariborz Jahanian | 78a7d7d | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1279 | else if (isa<FieldDecl>(D)) |
| 1280 | UseAlignAttrOnly = |
| 1281 | D->hasAttr<PackedAttr>() || |
| 1282 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1283 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1284 | // If we're using the align attribute only, just ignore everything |
| 1285 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1286 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1287 | // do nothing |
| 1288 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1289 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1290 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1291 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1292 | if (RefAsPointee) |
| 1293 | T = RT->getPointeeType(); |
| 1294 | else |
| 1295 | T = getPointerType(RT->getPointeeType()); |
| 1296 | } |
| 1297 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1298 | // Adjust alignments of declarations with array type by the |
| 1299 | // large-array alignment on the target. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1300 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1301 | const ArrayType *arrayType; |
| 1302 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 1303 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1304 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1305 | else if (isa<ConstantArrayType>(arrayType) && |
| 1306 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1307 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1308 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1309 | // Walk through any array types while we're at it. |
| 1310 | T = getBaseElementType(arrayType); |
| 1311 | } |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 1312 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Ulrich Weigand | 6b20351 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1313 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1314 | if (VD->hasGlobalStorage()) |
| 1315 | Align = std::max(Align, getTargetInfo().getMinGlobalAlign()); |
| 1316 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1317 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1318 | |
| 1319 | // Fields can be subject to extra alignment constraints, like if |
| 1320 | // the field is packed, the struct is packed, or the struct has a |
| 1321 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 1322 | // the actual alignment of the field within the struct, and then |
| 1323 | // (as we're expected to) constrain that by the alignment of the type. |
Matt Beaumont-Gay | 147fab9 | 2013-06-25 22:19:15 +0000 | [diff] [blame^] | 1324 | if (const FieldDecl *Field = dyn_cast<FieldDecl>(VD)) { |
| 1325 | const RecordDecl *Parent = Field->getParent(); |
| 1326 | // We can only produce a sensible answer if the record is valid. |
| 1327 | if (!Parent->isInvalidDecl()) { |
| 1328 | const ASTRecordLayout &Layout = getASTRecordLayout(Parent); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1329 | |
Matt Beaumont-Gay | 147fab9 | 2013-06-25 22:19:15 +0000 | [diff] [blame^] | 1330 | // Start with the record's overall alignment. |
| 1331 | unsigned FieldAlign = toBits(Layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1332 | |
Matt Beaumont-Gay | 147fab9 | 2013-06-25 22:19:15 +0000 | [diff] [blame^] | 1333 | // Use the GCD of that and the offset within the record. |
| 1334 | uint64_t Offset = Layout.getFieldOffset(Field->getFieldIndex()); |
| 1335 | if (Offset > 0) { |
| 1336 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 1337 | // which means we get to do this crazy thing instead of Euclid's. |
| 1338 | uint64_t LowBitOfOffset = Offset & (~Offset + 1); |
| 1339 | if (LowBitOfOffset < FieldAlign) |
| 1340 | FieldAlign = static_cast<unsigned>(LowBitOfOffset); |
| 1341 | } |
| 1342 | |
| 1343 | Align = std::min(Align, FieldAlign); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1344 | } |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 1345 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1346 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1347 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1348 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1349 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1350 | |
John McCall | 929bbfb | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1351 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
| 1352 | // chars. If the type is a record, its data size is returned. This is |
| 1353 | // the size of the memcpy that's performed when assigning this type |
| 1354 | // using a trivial copy/move assignment operator. |
| 1355 | std::pair<CharUnits, CharUnits> |
| 1356 | ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
| 1357 | std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T); |
| 1358 | |
| 1359 | // In C++, objects can sometimes be allocated into the tail padding |
| 1360 | // of a base-class subobject. We decide whether that's possible |
| 1361 | // during class layout, so here we can just trust the layout results. |
| 1362 | if (getLangOpts().CPlusPlus) { |
| 1363 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1364 | const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl()); |
| 1365 | sizeAndAlign.first = layout.getDataSize(); |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | return sizeAndAlign; |
| 1370 | } |
| 1371 | |
Richard Trieu | 910f17e | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1372 | /// getConstantArrayInfoInChars - Performing the computation in CharUnits |
| 1373 | /// instead of in bits prevents overflowing the uint64_t for some large arrays. |
| 1374 | std::pair<CharUnits, CharUnits> |
| 1375 | static getConstantArrayInfoInChars(const ASTContext &Context, |
| 1376 | const ConstantArrayType *CAT) { |
| 1377 | std::pair<CharUnits, CharUnits> EltInfo = |
| 1378 | Context.getTypeInfoInChars(CAT->getElementType()); |
| 1379 | uint64_t Size = CAT->getSize().getZExtValue(); |
Richard Trieu | 1069b73 | 2013-05-14 23:41:50 +0000 | [diff] [blame] | 1380 | assert((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <= |
| 1381 | (uint64_t)(-1)/Size) && |
Richard Trieu | 910f17e | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1382 | "Overflow in array type char size evaluation"); |
| 1383 | uint64_t Width = EltInfo.first.getQuantity() * Size; |
| 1384 | unsigned Align = EltInfo.second.getQuantity(); |
| 1385 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 1386 | return std::make_pair(CharUnits::fromQuantity(Width), |
| 1387 | CharUnits::fromQuantity(Align)); |
| 1388 | } |
| 1389 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1390 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1391 | ASTContext::getTypeInfoInChars(const Type *T) const { |
Richard Trieu | 910f17e | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1392 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) |
| 1393 | return getConstantArrayInfoInChars(*this, CAT); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1394 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1395 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 1396 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1400 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1401 | return getTypeInfoInChars(T.getTypePtr()); |
| 1402 | } |
| 1403 | |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1404 | std::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const { |
| 1405 | TypeInfoMap::iterator it = MemoizedTypeInfo.find(T); |
| 1406 | if (it != MemoizedTypeInfo.end()) |
| 1407 | return it->second; |
| 1408 | |
| 1409 | std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T); |
| 1410 | MemoizedTypeInfo.insert(std::make_pair(T, Info)); |
| 1411 | return Info; |
| 1412 | } |
| 1413 | |
| 1414 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 1415 | /// method does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1416 | /// |
| 1417 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 1418 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1419 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1420 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1421 | ASTContext::getTypeInfoImpl(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 1422 | uint64_t Width=0; |
| 1423 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1424 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1425 | #define TYPE(Class, Base) |
| 1426 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1427 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1428 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1429 | #include "clang/AST/TypeNodes.def" |
John McCall | d3d49bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1430 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1431 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1432 | case Type::FunctionNoProto: |
| 1433 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1434 | // GCC extension: alignof(function) = 32 bits |
| 1435 | Width = 0; |
| 1436 | Align = 32; |
| 1437 | break; |
| 1438 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1439 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1440 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1441 | Width = 0; |
| 1442 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1443 | break; |
| 1444 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1445 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1446 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1448 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1449 | uint64_t Size = CAT->getSize().getZExtValue(); |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1450 | assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && |
| 1451 | "Overflow in array type bit size evaluation"); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1452 | Width = EltInfo.first*Size; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1453 | Align = EltInfo.second; |
Argyrios Kyrtzidis | cd88b41 | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 1454 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1455 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1456 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1457 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1458 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1459 | const VectorType *VT = cast<VectorType>(T); |
| 1460 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 1461 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1462 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1463 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1464 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1465 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1466 | Align = llvm::NextPowerOf2(Align); |
| 1467 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 1468 | } |
Chad Rosier | f9e9af7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1469 | // Adjust the alignment based on the target max. |
| 1470 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1471 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1472 | Align = TargetVectorAlign; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1473 | break; |
| 1474 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1475 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1476 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1477 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1478 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1479 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1480 | // GCC extension: alignof(void) = 8 bits. |
| 1481 | Width = 0; |
| 1482 | Align = 8; |
| 1483 | break; |
| 1484 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1485 | case BuiltinType::Bool: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1486 | Width = Target->getBoolWidth(); |
| 1487 | Align = Target->getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1488 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1489 | case BuiltinType::Char_S: |
| 1490 | case BuiltinType::Char_U: |
| 1491 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1492 | case BuiltinType::SChar: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1493 | Width = Target->getCharWidth(); |
| 1494 | Align = Target->getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1495 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1496 | case BuiltinType::WChar_S: |
| 1497 | case BuiltinType::WChar_U: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1498 | Width = Target->getWCharWidth(); |
| 1499 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1500 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1501 | case BuiltinType::Char16: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1502 | Width = Target->getChar16Width(); |
| 1503 | Align = Target->getChar16Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1504 | break; |
| 1505 | case BuiltinType::Char32: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1506 | Width = Target->getChar32Width(); |
| 1507 | Align = Target->getChar32Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1508 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1509 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1510 | case BuiltinType::Short: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1511 | Width = Target->getShortWidth(); |
| 1512 | Align = Target->getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1513 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1514 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1515 | case BuiltinType::Int: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1516 | Width = Target->getIntWidth(); |
| 1517 | Align = Target->getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1518 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1519 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1520 | case BuiltinType::Long: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1521 | Width = Target->getLongWidth(); |
| 1522 | Align = Target->getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1523 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1524 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1525 | case BuiltinType::LongLong: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1526 | Width = Target->getLongLongWidth(); |
| 1527 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1528 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1529 | case BuiltinType::Int128: |
| 1530 | case BuiltinType::UInt128: |
| 1531 | Width = 128; |
| 1532 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1533 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1534 | case BuiltinType::Half: |
| 1535 | Width = Target->getHalfWidth(); |
| 1536 | Align = Target->getHalfAlign(); |
| 1537 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1538 | case BuiltinType::Float: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1539 | Width = Target->getFloatWidth(); |
| 1540 | Align = Target->getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1541 | break; |
| 1542 | case BuiltinType::Double: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1543 | Width = Target->getDoubleWidth(); |
| 1544 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1545 | break; |
| 1546 | case BuiltinType::LongDouble: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1547 | Width = Target->getLongDoubleWidth(); |
| 1548 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1549 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1550 | case BuiltinType::NullPtr: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1551 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1552 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1553 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1554 | case BuiltinType::ObjCId: |
| 1555 | case BuiltinType::ObjCClass: |
| 1556 | case BuiltinType::ObjCSel: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1557 | Width = Target->getPointerWidth(0); |
| 1558 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1559 | break; |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1560 | case BuiltinType::OCLSampler: |
| 1561 | // Samplers are modeled as integers. |
| 1562 | Width = Target->getIntWidth(); |
| 1563 | Align = Target->getIntAlign(); |
| 1564 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1565 | case BuiltinType::OCLEvent: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1566 | case BuiltinType::OCLImage1d: |
| 1567 | case BuiltinType::OCLImage1dArray: |
| 1568 | case BuiltinType::OCLImage1dBuffer: |
| 1569 | case BuiltinType::OCLImage2d: |
| 1570 | case BuiltinType::OCLImage2dArray: |
| 1571 | case BuiltinType::OCLImage3d: |
| 1572 | // Currently these types are pointers to opaque types. |
| 1573 | Width = Target->getPointerWidth(0); |
| 1574 | Align = Target->getPointerAlign(0); |
| 1575 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1576 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1577 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1578 | case Type::ObjCObjectPointer: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1579 | Width = Target->getPointerWidth(0); |
| 1580 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1581 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1582 | case Type::BlockPointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1583 | unsigned AS = getTargetAddressSpace( |
| 1584 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1585 | Width = Target->getPointerWidth(AS); |
| 1586 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1587 | break; |
| 1588 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1589 | case Type::LValueReference: |
| 1590 | case Type::RValueReference: { |
| 1591 | // alignof and sizeof should never enter this code path here, so we go |
| 1592 | // the pointer route. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1593 | unsigned AS = getTargetAddressSpace( |
| 1594 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1595 | Width = Target->getPointerWidth(AS); |
| 1596 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1597 | break; |
| 1598 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1599 | case Type::Pointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1600 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1601 | Width = Target->getPointerWidth(AS); |
| 1602 | Align = Target->getPointerAlign(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1603 | break; |
| 1604 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1605 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1606 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 1607 | llvm::tie(Width, Align) = ABI->getMemberPointerWidthAndAlign(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1608 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1609 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1610 | case Type::Complex: { |
| 1611 | // Complex types have the same alignment as their elements, but twice the |
| 1612 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1614 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1615 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1616 | Align = EltInfo.second; |
| 1617 | break; |
| 1618 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1619 | case Type::ObjCObject: |
| 1620 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Reid Kleckner | 12df246 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1621 | case Type::Decayed: |
| 1622 | return getTypeInfo(cast<DecayedType>(T)->getDecayedType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1623 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1624 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1625 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1626 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1627 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1630 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1631 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1632 | const TagType *TT = cast<TagType>(T); |
| 1633 | |
| 1634 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 22ce41d | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1635 | Width = 8; |
| 1636 | Align = 8; |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1637 | break; |
| 1638 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1640 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1641 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1642 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1643 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1644 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1645 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1646 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1647 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1648 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1649 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1650 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1651 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1652 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1653 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1654 | case Type::Auto: { |
| 1655 | const AutoType *A = cast<AutoType>(T); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 1656 | assert(!A->getDeducedType().isNull() && |
| 1657 | "cannot request the size of an undeduced or dependent auto type"); |
Matt Beaumont-Gay | dc856af | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1658 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1661 | case Type::Paren: |
| 1662 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1663 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1664 | case Type::Typedef: { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1665 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1666 | std::pair<uint64_t, unsigned> Info |
| 1667 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1668 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1669 | // alignment we have. This violates the GCC documentation (which says that |
| 1670 | // attribute(aligned) can only round up) but matches its implementation. |
| 1671 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1672 | Align = AttrAlign; |
| 1673 | else |
| 1674 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1675 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1676 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1677 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1678 | |
| 1679 | case Type::TypeOfExpr: |
| 1680 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1681 | .getTypePtr()); |
| 1682 | |
| 1683 | case Type::TypeOf: |
| 1684 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1685 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1686 | case Type::Decltype: |
| 1687 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1688 | .getTypePtr()); |
| 1689 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1690 | case Type::UnaryTransform: |
| 1691 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1692 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1693 | case Type::Elaborated: |
| 1694 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1696 | case Type::Attributed: |
| 1697 | return getTypeInfo( |
| 1698 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1699 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1700 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1702 | "Cannot request the size of a dependent type"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1703 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1704 | // A type alias template specialization may refer to a typedef with the |
| 1705 | // aligned attribute on it. |
| 1706 | if (TST->isTypeAlias()) |
| 1707 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1708 | else |
| 1709 | return getTypeInfo(getCanonicalType(T)); |
| 1710 | } |
| 1711 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1712 | case Type::Atomic: { |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1713 | // Start with the base type information. |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1714 | std::pair<uint64_t, unsigned> Info |
| 1715 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1716 | Width = Info.first; |
| 1717 | Align = Info.second; |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1718 | |
| 1719 | // If the size of the type doesn't exceed the platform's max |
| 1720 | // atomic promotion width, make the size and alignment more |
| 1721 | // favorable to atomic operations: |
| 1722 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth()) { |
| 1723 | // Round the size up to a power of 2. |
| 1724 | if (!llvm::isPowerOf2_64(Width)) |
| 1725 | Width = llvm::NextPowerOf2(Width); |
| 1726 | |
| 1727 | // Set the alignment equal to the size. |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1728 | Align = static_cast<unsigned>(Width); |
| 1729 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1732 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1734 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1735 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1738 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1739 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1740 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1741 | } |
| 1742 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1743 | /// toBits - Convert a size in characters to a size in characters. |
| 1744 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1745 | return CharSize.getQuantity() * getCharWidth(); |
| 1746 | } |
| 1747 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1748 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1749 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1750 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Richard Trieu | 910f17e | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1751 | return getTypeInfoInChars(T).first; |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1752 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1753 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Richard Trieu | 910f17e | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1754 | return getTypeInfoInChars(T).first; |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1757 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1758 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1759 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1760 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1761 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1762 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1763 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1766 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1767 | /// type for the current target in bits. This can be different than the ABI |
| 1768 | /// alignment in cases where it is beneficial for performance to overalign |
| 1769 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1770 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1771 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1772 | |
| 1773 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1774 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1775 | T = CT->getElementType().getTypePtr(); |
| 1776 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | cde7a1d | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1777 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1778 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1779 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1780 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1781 | return ABIAlign; |
| 1782 | } |
| 1783 | |
Ulrich Weigand | 6b20351 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1784 | /// getAlignOfGlobalVar - Return the alignment in bits that should be given |
| 1785 | /// to a global variable of the specified type. |
| 1786 | unsigned ASTContext::getAlignOfGlobalVar(QualType T) const { |
| 1787 | return std::max(getTypeAlign(T), getTargetInfo().getMinGlobalAlign()); |
| 1788 | } |
| 1789 | |
| 1790 | /// getAlignOfGlobalVarInChars - Return the alignment in characters that |
| 1791 | /// should be given to a global variable of the specified type. |
| 1792 | CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const { |
| 1793 | return toCharUnitsFromBits(getAlignOfGlobalVar(T)); |
| 1794 | } |
| 1795 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1796 | /// DeepCollectObjCIvars - |
| 1797 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1798 | /// super class and then collects all ivars, including those synthesized for |
| 1799 | /// current class. This routine is used for implementation of current class |
| 1800 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1801 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1802 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1803 | bool leafClass, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1804 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1805 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1806 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1807 | if (!leafClass) { |
| 1808 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1809 | E = OI->ivar_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1810 | Ivars.push_back(*I); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1811 | } else { |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1812 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1813 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1814 | Iv= Iv->getNextIvar()) |
| 1815 | Ivars.push_back(Iv); |
| 1816 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1819 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1820 | /// those inherited by it. |
| 1821 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1822 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1823 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1824 | // We can use protocol_iterator here instead of |
| 1825 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1826 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1827 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1828 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1829 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1830 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1831 | PE = Proto->protocol_end(); P != PE; ++P) { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1832 | Protocols.insert((*P)->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1833 | CollectInheritedProtocols(*P, Protocols); |
| 1834 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1835 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1836 | |
| 1837 | // Categories of this Interface. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1838 | for (ObjCInterfaceDecl::visible_categories_iterator |
| 1839 | Cat = OI->visible_categories_begin(), |
| 1840 | CatEnd = OI->visible_categories_end(); |
| 1841 | Cat != CatEnd; ++Cat) { |
| 1842 | CollectInheritedProtocols(*Cat, Protocols); |
| 1843 | } |
| 1844 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1845 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1846 | while (SD) { |
| 1847 | CollectInheritedProtocols(SD, Protocols); |
| 1848 | SD = SD->getSuperClass(); |
| 1849 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1850 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1851 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1852 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1853 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1854 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1855 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1856 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1857 | CollectInheritedProtocols(*P, Protocols); |
| 1858 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1859 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1860 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1861 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1862 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1863 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1864 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1865 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1866 | CollectInheritedProtocols(*P, Protocols); |
| 1867 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1871 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1872 | unsigned count = 0; |
| 1873 | // Count ivars declared in class extension. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1874 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 1875 | Ext = OI->known_extensions_begin(), |
| 1876 | ExtEnd = OI->known_extensions_end(); |
| 1877 | Ext != ExtEnd; ++Ext) { |
| 1878 | count += Ext->ivar_size(); |
| 1879 | } |
| 1880 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1881 | // Count ivar defined in this class's implementation. This |
| 1882 | // includes synthesized ivars. |
| 1883 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1884 | count += ImplDecl->ivar_size(); |
| 1885 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1886 | return count; |
| 1887 | } |
| 1888 | |
Argyrios Kyrtzidis | 8deabc1 | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 1889 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 1890 | if (!E) |
| 1891 | return false; |
| 1892 | |
| 1893 | // nullptr_t is always treated as null. |
| 1894 | if (E->getType()->isNullPtrType()) return true; |
| 1895 | |
| 1896 | if (E->getType()->isAnyPointerType() && |
| 1897 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 1898 | Expr::NPC_ValueDependentIsNull)) |
| 1899 | return true; |
| 1900 | |
| 1901 | // Unfortunately, __null has type 'int'. |
| 1902 | if (isa<GNUNullExpr>(E)) return true; |
| 1903 | |
| 1904 | return false; |
| 1905 | } |
| 1906 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1907 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1908 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1909 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1910 | I = ObjCImpls.find(D); |
| 1911 | if (I != ObjCImpls.end()) |
| 1912 | return cast<ObjCImplementationDecl>(I->second); |
| 1913 | return 0; |
| 1914 | } |
| 1915 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1916 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1917 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1918 | I = ObjCImpls.find(D); |
| 1919 | if (I != ObjCImpls.end()) |
| 1920 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
| 1924 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1925 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1926 | ObjCImplementationDecl *ImplD) { |
| 1927 | assert(IFaceD && ImplD && "Passed null params"); |
| 1928 | ObjCImpls[IFaceD] = ImplD; |
| 1929 | } |
| 1930 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1931 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1932 | ObjCCategoryImplDecl *ImplD) { |
| 1933 | assert(CatD && ImplD && "Passed null params"); |
| 1934 | ObjCImpls[CatD] = ImplD; |
| 1935 | } |
| 1936 | |
Dmitri Gribenko | b35cc2d | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1937 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( |
| 1938 | const NamedDecl *ND) const { |
| 1939 | if (const ObjCInterfaceDecl *ID = |
| 1940 | dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1941 | return ID; |
Dmitri Gribenko | b35cc2d | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1942 | if (const ObjCCategoryDecl *CD = |
| 1943 | dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1944 | return CD->getClassInterface(); |
Dmitri Gribenko | b35cc2d | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1945 | if (const ObjCImplDecl *IMD = |
| 1946 | dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1947 | return IMD->getClassInterface(); |
| 1948 | |
| 1949 | return 0; |
| 1950 | } |
| 1951 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1952 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1953 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1954 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1955 | assert(VD && "Passed null params"); |
| 1956 | assert(VD->hasAttr<BlocksAttr>() && |
| 1957 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1958 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1959 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1960 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1961 | } |
| 1962 | |
| 1963 | /// \brief Set the copy inialization expression of a block var decl. |
| 1964 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1965 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1966 | assert(VD->hasAttr<BlocksAttr>() && |
| 1967 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1968 | BlockVarCopyInits[VD] = Init; |
| 1969 | } |
| 1970 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1971 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1972 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1973 | if (!DataSize) |
| 1974 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1975 | else |
| 1976 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1977 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1978 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1979 | TypeSourceInfo *TInfo = |
| 1980 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1981 | new (TInfo) TypeSourceInfo(T); |
| 1982 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1985 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1986 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1987 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1988 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1989 | return DI; |
| 1990 | } |
| 1991 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1992 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1993 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1994 | return getObjCLayout(D, 0); |
| 1995 | } |
| 1996 | |
| 1997 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1998 | ASTContext::getASTObjCImplementationLayout( |
| 1999 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2000 | return getObjCLayout(D->getClassInterface(), D); |
| 2001 | } |
| 2002 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2003 | //===----------------------------------------------------------------------===// |
| 2004 | // Type creation/memoization methods |
| 2005 | //===----------------------------------------------------------------------===// |
| 2006 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2007 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2008 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 2009 | unsigned fastQuals = quals.getFastQualifiers(); |
| 2010 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2011 | |
| 2012 | // Check if we've already instantiated this type. |
| 2013 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2014 | ExtQuals::Profile(ID, baseType, quals); |
| 2015 | void *insertPos = 0; |
| 2016 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 2017 | assert(eq->getQualifiers() == quals); |
| 2018 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2021 | // If the base type is not canonical, make the appropriate canonical type. |
| 2022 | QualType canon; |
| 2023 | if (!baseType->isCanonicalUnqualified()) { |
| 2024 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2025 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 2026 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2027 | |
| 2028 | // Re-find the insert position. |
| 2029 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 2030 | } |
| 2031 | |
| 2032 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 2033 | ExtQualNodes.InsertNode(eq, insertPos); |
| 2034 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2037 | QualType |
| 2038 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2039 | QualType CanT = getCanonicalType(T); |
| 2040 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2041 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2042 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2043 | // If we are composing extended qualifiers together, merge together |
| 2044 | // into one ExtQuals node. |
| 2045 | QualifierCollector Quals; |
| 2046 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2048 | // If this type already has an address space specified, it cannot get |
| 2049 | // another one. |
| 2050 | assert(!Quals.hasAddressSpace() && |
| 2051 | "Type cannot be in multiple addr spaces!"); |
| 2052 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2054 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2057 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2058 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2059 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2060 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2061 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2062 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2063 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 2064 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 2065 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 2066 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 2067 | return getPointerType(ResultType); |
| 2068 | } |
| 2069 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2071 | // If we are composing extended qualifiers together, merge together |
| 2072 | // into one ExtQuals node. |
| 2073 | QualifierCollector Quals; |
| 2074 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2076 | // If this type already has an ObjCGC specified, it cannot get |
| 2077 | // another one. |
| 2078 | assert(!Quals.hasObjCGCAttr() && |
| 2079 | "Type cannot have multiple ObjCGCs!"); |
| 2080 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2082 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2083 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2084 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2085 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 2086 | FunctionType::ExtInfo Info) { |
| 2087 | if (T->getExtInfo() == Info) |
| 2088 | return T; |
| 2089 | |
| 2090 | QualType Result; |
| 2091 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 2092 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 2093 | } else { |
| 2094 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 2095 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 2096 | EPI.ExtInfo = Info; |
Reid Kleckner | 0567a79 | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 2097 | Result = getFunctionType(FPT->getResultType(), FPT->getArgTypes(), EPI); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | return cast<FunctionType>(Result.getTypePtr()); |
| 2101 | } |
| 2102 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2103 | void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD, |
| 2104 | QualType ResultType) { |
Richard Smith | 9dadfab | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2105 | FD = FD->getMostRecentDecl(); |
| 2106 | while (true) { |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2107 | const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>(); |
| 2108 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 2109 | FD->setType(getFunctionType(ResultType, FPT->getArgTypes(), EPI)); |
Richard Smith | 9dadfab | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2110 | if (FunctionDecl *Next = FD->getPreviousDecl()) |
| 2111 | FD = Next; |
| 2112 | else |
| 2113 | break; |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2114 | } |
Richard Smith | 9dadfab | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2115 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2116 | L->DeducedReturnType(FD, ResultType); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2119 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 2120 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2121 | QualType ASTContext::getComplexType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2122 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2123 | // structure. |
| 2124 | llvm::FoldingSetNodeID ID; |
| 2125 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2127 | void *InsertPos = 0; |
| 2128 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2129 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2130 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2131 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2132 | // so fill in the canonical type field. |
| 2133 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2134 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2135 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2137 | // Get the new insert position for the node we care about. |
| 2138 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2139 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2140 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2141 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2142 | Types.push_back(New); |
| 2143 | ComplexTypes.InsertNode(New, InsertPos); |
| 2144 | return QualType(New, 0); |
| 2145 | } |
| 2146 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2147 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 2148 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2149 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2150 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2151 | // structure. |
| 2152 | llvm::FoldingSetNodeID ID; |
| 2153 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2154 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2155 | void *InsertPos = 0; |
| 2156 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2157 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2159 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2160 | // so fill in the canonical type field. |
| 2161 | QualType Canonical; |
Bob Wilson | c90cc93 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2162 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2163 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2164 | |
Bob Wilson | c90cc93 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2165 | // Get the new insert position for the node we care about. |
| 2166 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2167 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 2168 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2169 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2170 | Types.push_back(New); |
| 2171 | PointerTypes.InsertNode(New, InsertPos); |
| 2172 | return QualType(New, 0); |
| 2173 | } |
| 2174 | |
Reid Kleckner | 12df246 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2175 | QualType ASTContext::getDecayedType(QualType T) const { |
| 2176 | assert((T->isArrayType() || T->isFunctionType()) && "T does not decay"); |
| 2177 | |
| 2178 | llvm::FoldingSetNodeID ID; |
| 2179 | DecayedType::Profile(ID, T); |
| 2180 | void *InsertPos = 0; |
| 2181 | if (DecayedType *DT = DecayedTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2182 | return QualType(DT, 0); |
| 2183 | |
| 2184 | QualType Decayed; |
| 2185 | |
| 2186 | // C99 6.7.5.3p7: |
| 2187 | // A declaration of a parameter as "array of type" shall be |
| 2188 | // adjusted to "qualified pointer to type", where the type |
| 2189 | // qualifiers (if any) are those specified within the [ and ] of |
| 2190 | // the array type derivation. |
| 2191 | if (T->isArrayType()) |
| 2192 | Decayed = getArrayDecayedType(T); |
| 2193 | |
| 2194 | // C99 6.7.5.3p8: |
| 2195 | // A declaration of a parameter as "function returning type" |
| 2196 | // shall be adjusted to "pointer to function returning type", as |
| 2197 | // in 6.3.2.1. |
| 2198 | if (T->isFunctionType()) |
| 2199 | Decayed = getPointerType(T); |
| 2200 | |
| 2201 | QualType Canonical = getCanonicalType(Decayed); |
| 2202 | |
| 2203 | // Get the new insert position for the node we care about. |
| 2204 | DecayedType *NewIP = DecayedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2205 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 2206 | |
| 2207 | DecayedType *New = |
| 2208 | new (*this, TypeAlignment) DecayedType(T, Decayed, Canonical); |
| 2209 | Types.push_back(New); |
| 2210 | DecayedTypes.InsertNode(New, InsertPos); |
| 2211 | return QualType(New, 0); |
| 2212 | } |
| 2213 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2215 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2216 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2217 | assert(T->isFunctionType() && "block of function types only"); |
| 2218 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2219 | // structure. |
| 2220 | llvm::FoldingSetNodeID ID; |
| 2221 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2222 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2223 | void *InsertPos = 0; |
| 2224 | if (BlockPointerType *PT = |
| 2225 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2226 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2227 | |
| 2228 | // If the block pointee type isn't canonical, this won't be a canonical |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2229 | // type either so fill in the canonical type field. |
| 2230 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2231 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2232 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2234 | // Get the new insert position for the node we care about. |
| 2235 | BlockPointerType *NewIP = |
| 2236 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2237 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2238 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2239 | BlockPointerType *New |
| 2240 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2241 | Types.push_back(New); |
| 2242 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 2243 | return QualType(New, 0); |
| 2244 | } |
| 2245 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2246 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 2247 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2248 | QualType |
| 2249 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 2250 | assert(getCanonicalType(T) != OverloadTy && |
| 2251 | "Unresolved overloaded function type"); |
| 2252 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2253 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2254 | // structure. |
| 2255 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2256 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2257 | |
| 2258 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2259 | if (LValueReferenceType *RT = |
| 2260 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2261 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2262 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2263 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2264 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2265 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2266 | // either, so fill in the canonical type field. |
| 2267 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2268 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 2269 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2270 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2271 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2272 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2273 | LValueReferenceType *NewIP = |
| 2274 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2275 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2278 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2279 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 2280 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2281 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2282 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2283 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2284 | return QualType(New, 0); |
| 2285 | } |
| 2286 | |
| 2287 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 2288 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2289 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2290 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2291 | // structure. |
| 2292 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2293 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2294 | |
| 2295 | void *InsertPos = 0; |
| 2296 | if (RValueReferenceType *RT = |
| 2297 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2298 | return QualType(RT, 0); |
| 2299 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2300 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2301 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2302 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2303 | // either, so fill in the canonical type field. |
| 2304 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2305 | if (InnerRef || !T.isCanonical()) { |
| 2306 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2307 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2308 | |
| 2309 | // Get the new insert position for the node we care about. |
| 2310 | RValueReferenceType *NewIP = |
| 2311 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2312 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2315 | RValueReferenceType *New |
| 2316 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2317 | Types.push_back(New); |
| 2318 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2319 | return QualType(New, 0); |
| 2320 | } |
| 2321 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2322 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 2323 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2324 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2325 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2326 | // structure. |
| 2327 | llvm::FoldingSetNodeID ID; |
| 2328 | MemberPointerType::Profile(ID, T, Cls); |
| 2329 | |
| 2330 | void *InsertPos = 0; |
| 2331 | if (MemberPointerType *PT = |
| 2332 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2333 | return QualType(PT, 0); |
| 2334 | |
| 2335 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 2336 | // type either, so fill in the canonical type field. |
| 2337 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2338 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2339 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 2340 | |
| 2341 | // Get the new insert position for the node we care about. |
| 2342 | MemberPointerType *NewIP = |
| 2343 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2344 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2345 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2346 | MemberPointerType *New |
| 2347 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2348 | Types.push_back(New); |
| 2349 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 2350 | return QualType(New, 0); |
| 2351 | } |
| 2352 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2354 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2356 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2357 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2358 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 2359 | assert((EltTy->isDependentType() || |
| 2360 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 2361 | "Constant array of VLAs is illegal!"); |
| 2362 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2363 | // Convert the array size into a canonical width matching the pointer size for |
| 2364 | // the target. |
| 2365 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2366 | ArySize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2367 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2368 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2369 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2370 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2372 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2374 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2375 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2376 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2377 | // If the element type isn't canonical or has qualifiers, this won't |
| 2378 | // be a canonical type either, so fill in the canonical type field. |
| 2379 | QualType Canon; |
| 2380 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2381 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2382 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2383 | ASM, IndexTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2384 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2385 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2386 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2387 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2388 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2389 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2390 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2392 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2393 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2394 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2395 | Types.push_back(New); |
| 2396 | return QualType(New, 0); |
| 2397 | } |
| 2398 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2399 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 2400 | /// variably-modified, into the corresponding type with all the known |
| 2401 | /// sizes replaced with [*]. |
| 2402 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 2403 | // Vastly most common case. |
| 2404 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2405 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2406 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2407 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2408 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2409 | const Type *ty = split.Ty; |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2410 | switch (ty->getTypeClass()) { |
| 2411 | #define TYPE(Class, Base) |
| 2412 | #define ABSTRACT_TYPE(Class, Base) |
| 2413 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2414 | #include "clang/AST/TypeNodes.def" |
| 2415 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 2416 | |
| 2417 | // These types should never be variably-modified. |
| 2418 | case Type::Builtin: |
| 2419 | case Type::Complex: |
| 2420 | case Type::Vector: |
| 2421 | case Type::ExtVector: |
| 2422 | case Type::DependentSizedExtVector: |
| 2423 | case Type::ObjCObject: |
| 2424 | case Type::ObjCInterface: |
| 2425 | case Type::ObjCObjectPointer: |
| 2426 | case Type::Record: |
| 2427 | case Type::Enum: |
| 2428 | case Type::UnresolvedUsing: |
| 2429 | case Type::TypeOfExpr: |
| 2430 | case Type::TypeOf: |
| 2431 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2432 | case Type::UnaryTransform: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2433 | case Type::DependentName: |
| 2434 | case Type::InjectedClassName: |
| 2435 | case Type::TemplateSpecialization: |
| 2436 | case Type::DependentTemplateSpecialization: |
| 2437 | case Type::TemplateTypeParm: |
| 2438 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2439 | case Type::Auto: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2440 | case Type::PackExpansion: |
| 2441 | llvm_unreachable("type should never be variably-modified"); |
| 2442 | |
| 2443 | // These types can be variably-modified but should never need to |
| 2444 | // further decay. |
| 2445 | case Type::FunctionNoProto: |
| 2446 | case Type::FunctionProto: |
| 2447 | case Type::BlockPointer: |
| 2448 | case Type::MemberPointer: |
| 2449 | return type; |
| 2450 | |
| 2451 | // These types can be variably-modified. All these modifications |
| 2452 | // preserve structure except as noted by comments. |
| 2453 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 2454 | // optimizations available here. |
| 2455 | case Type::Pointer: |
| 2456 | result = getPointerType(getVariableArrayDecayedType( |
| 2457 | cast<PointerType>(ty)->getPointeeType())); |
| 2458 | break; |
| 2459 | |
| 2460 | case Type::LValueReference: { |
| 2461 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 2462 | result = getLValueReferenceType( |
| 2463 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 2464 | lv->isSpelledAsLValue()); |
| 2465 | break; |
| 2466 | } |
| 2467 | |
| 2468 | case Type::RValueReference: { |
| 2469 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 2470 | result = getRValueReferenceType( |
| 2471 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 2472 | break; |
| 2473 | } |
| 2474 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2475 | case Type::Atomic: { |
| 2476 | const AtomicType *at = cast<AtomicType>(ty); |
| 2477 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 2478 | break; |
| 2479 | } |
| 2480 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2481 | case Type::ConstantArray: { |
| 2482 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 2483 | result = getConstantArrayType( |
| 2484 | getVariableArrayDecayedType(cat->getElementType()), |
| 2485 | cat->getSize(), |
| 2486 | cat->getSizeModifier(), |
| 2487 | cat->getIndexTypeCVRQualifiers()); |
| 2488 | break; |
| 2489 | } |
| 2490 | |
| 2491 | case Type::DependentSizedArray: { |
| 2492 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 2493 | result = getDependentSizedArrayType( |
| 2494 | getVariableArrayDecayedType(dat->getElementType()), |
| 2495 | dat->getSizeExpr(), |
| 2496 | dat->getSizeModifier(), |
| 2497 | dat->getIndexTypeCVRQualifiers(), |
| 2498 | dat->getBracketsRange()); |
| 2499 | break; |
| 2500 | } |
| 2501 | |
| 2502 | // Turn incomplete types into [*] types. |
| 2503 | case Type::IncompleteArray: { |
| 2504 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 2505 | result = getVariableArrayType( |
| 2506 | getVariableArrayDecayedType(iat->getElementType()), |
| 2507 | /*size*/ 0, |
| 2508 | ArrayType::Normal, |
| 2509 | iat->getIndexTypeCVRQualifiers(), |
| 2510 | SourceRange()); |
| 2511 | break; |
| 2512 | } |
| 2513 | |
| 2514 | // Turn VLA types into [*] types. |
| 2515 | case Type::VariableArray: { |
| 2516 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 2517 | result = getVariableArrayType( |
| 2518 | getVariableArrayDecayedType(vat->getElementType()), |
| 2519 | /*size*/ 0, |
| 2520 | ArrayType::Star, |
| 2521 | vat->getIndexTypeCVRQualifiers(), |
| 2522 | vat->getBracketsRange()); |
| 2523 | break; |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | // Apply the top-level qualifiers from the original. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2528 | return getQualifiedType(result, split.Quals); |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2529 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2530 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 2531 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 2532 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2533 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 2534 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2535 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2536 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2537 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2538 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 2539 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2540 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2541 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2542 | // Be sure to pull qualifiers off the element type. |
| 2543 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2544 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2545 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2546 | IndexTypeQuals, Brackets); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2547 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2550 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2551 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2552 | |
| 2553 | VariableArrayTypes.push_back(New); |
| 2554 | Types.push_back(New); |
| 2555 | return QualType(New, 0); |
| 2556 | } |
| 2557 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2558 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 2559 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 2560 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2561 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 2562 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2563 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2564 | unsigned elementTypeQuals, |
| 2565 | SourceRange brackets) const { |
| 2566 | assert((!numElements || numElements->isTypeDependent() || |
| 2567 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2568 | "Size must be type- or value-dependent!"); |
| 2569 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2570 | // Dependently-sized array types that do not have a specified number |
| 2571 | // of elements will have their sizes deduced from a dependent |
| 2572 | // initializer. We do no canonicalization here at all, which is okay |
| 2573 | // because they can't be used in most locations. |
| 2574 | if (!numElements) { |
| 2575 | DependentSizedArrayType *newType |
| 2576 | = new (*this, TypeAlignment) |
| 2577 | DependentSizedArrayType(*this, elementType, QualType(), |
| 2578 | numElements, ASM, elementTypeQuals, |
| 2579 | brackets); |
| 2580 | Types.push_back(newType); |
| 2581 | return QualType(newType, 0); |
| 2582 | } |
| 2583 | |
| 2584 | // Otherwise, we actually build a new type every time, but we |
| 2585 | // also build a canonical type. |
| 2586 | |
| 2587 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 2588 | |
| 2589 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2590 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2591 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2592 | QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2593 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2594 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2595 | // Look for an existing type with these properties. |
| 2596 | DependentSizedArrayType *canonTy = |
| 2597 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2598 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2599 | // If we don't have one, build one. |
| 2600 | if (!canonTy) { |
| 2601 | canonTy = new (*this, TypeAlignment) |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2602 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2603 | QualType(), numElements, ASM, elementTypeQuals, |
| 2604 | brackets); |
| 2605 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 2606 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2607 | } |
| 2608 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2609 | // Apply qualifiers from the element type to the array. |
| 2610 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2611 | canonElementType.Quals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2612 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2613 | // If we didn't need extra canonicalization for the element type, |
| 2614 | // then just use that as our result. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2615 | if (QualType(canonElementType.Ty, 0) == elementType) |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2616 | return canon; |
| 2617 | |
| 2618 | // Otherwise, we need to build a type which follows the spelling |
| 2619 | // of the element type. |
| 2620 | DependentSizedArrayType *sugaredType |
| 2621 | = new (*this, TypeAlignment) |
| 2622 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 2623 | ASM, elementTypeQuals, brackets); |
| 2624 | Types.push_back(sugaredType); |
| 2625 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2628 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2629 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2630 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2631 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2632 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2633 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2634 | void *insertPos = 0; |
| 2635 | if (IncompleteArrayType *iat = |
| 2636 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 2637 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2638 | |
| 2639 | // If the element type isn't canonical, this won't be a canonical type |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2640 | // either, so fill in the canonical type field. We also have to pull |
| 2641 | // qualifiers off the element type. |
| 2642 | QualType canon; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2643 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2644 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 2645 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2646 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2647 | ASM, elementTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2648 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2649 | |
| 2650 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2651 | IncompleteArrayType *existing = |
| 2652 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 2653 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 2654 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2655 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2656 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 2657 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2658 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2659 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 2660 | Types.push_back(newType); |
| 2661 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2664 | /// getVectorType - Return the unique reference to a vector type of |
| 2665 | /// the specified element type and size. VectorType must be a built-in type. |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2666 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2667 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2668 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2669 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2670 | // Check if we've already instantiated a vector of this type. |
| 2671 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2672 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2673 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2674 | void *InsertPos = 0; |
| 2675 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2676 | return QualType(VTP, 0); |
| 2677 | |
| 2678 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2679 | // so fill in the canonical type field. |
| 2680 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2681 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2682 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2683 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2684 | // Get the new insert position for the node we care about. |
| 2685 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2686 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2687 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2688 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2689 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2690 | VectorTypes.InsertNode(New, InsertPos); |
| 2691 | Types.push_back(New); |
| 2692 | return QualType(New, 0); |
| 2693 | } |
| 2694 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2695 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2696 | /// the specified element type and size. VectorType must be a built-in type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2697 | QualType |
| 2698 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2699 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2700 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2701 | // Check if we've already instantiated a vector of this type. |
| 2702 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2703 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2704 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2705 | void *InsertPos = 0; |
| 2706 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2707 | return QualType(VTP, 0); |
| 2708 | |
| 2709 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2710 | // so fill in the canonical type field. |
| 2711 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2712 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2713 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2715 | // Get the new insert position for the node we care about. |
| 2716 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2717 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2718 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2719 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2720 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2721 | VectorTypes.InsertNode(New, InsertPos); |
| 2722 | Types.push_back(New); |
| 2723 | return QualType(New, 0); |
| 2724 | } |
| 2725 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2726 | QualType |
| 2727 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2728 | Expr *SizeExpr, |
| 2729 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2730 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2731 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2732 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2733 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2734 | void *InsertPos = 0; |
| 2735 | DependentSizedExtVectorType *Canon |
| 2736 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2737 | DependentSizedExtVectorType *New; |
| 2738 | if (Canon) { |
| 2739 | // We already have a canonical version of this array type; use it as |
| 2740 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2741 | New = new (*this, TypeAlignment) |
| 2742 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2743 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2744 | } else { |
| 2745 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2746 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2747 | New = new (*this, TypeAlignment) |
| 2748 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2749 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2750 | |
| 2751 | DependentSizedExtVectorType *CanonCheck |
| 2752 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2753 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2754 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2755 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2756 | } else { |
| 2757 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2758 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2759 | New = new (*this, TypeAlignment) |
| 2760 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2761 | } |
| 2762 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2763 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2764 | Types.push_back(New); |
| 2765 | return QualType(New, 0); |
| 2766 | } |
| 2767 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2768 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2769 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2770 | QualType |
| 2771 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2772 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2773 | const CallingConv DefaultCC = Info.getCC(); |
| 2774 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2775 | CC_X86StdCall : DefaultCC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2776 | // Unique functions, to guarantee there is only one function of a particular |
| 2777 | // structure. |
| 2778 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2779 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2780 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2781 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2783 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2784 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2785 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2786 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2787 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2788 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2789 | Canonical = |
| 2790 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2791 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2793 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2794 | FunctionNoProtoType *NewIP = |
| 2795 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2796 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2797 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2798 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2799 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2800 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2801 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2802 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2803 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2804 | return QualType(New, 0); |
| 2805 | } |
| 2806 | |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2807 | /// \brief Determine whether \p T is canonical as the result type of a function. |
| 2808 | static bool isCanonicalResultType(QualType T) { |
| 2809 | return T.isCanonical() && |
| 2810 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
| 2811 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
| 2812 | } |
| 2813 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2814 | /// getFunctionType - Return a normal function type with a typed argument |
| 2815 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2816 | QualType |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2817 | ASTContext::getFunctionType(QualType ResultTy, ArrayRef<QualType> ArgArray, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2818 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2819 | size_t NumArgs = ArgArray.size(); |
| 2820 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2821 | // Unique functions, to guarantee there is only one function of a particular |
| 2822 | // structure. |
| 2823 | llvm::FoldingSetNodeID ID; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2824 | FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI, |
| 2825 | *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2826 | |
| 2827 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2828 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2829 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2830 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2831 | |
| 2832 | // Determine whether the type being created is already canonical or not. |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2833 | bool isCanonical = |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2834 | EPI.ExceptionSpecType == EST_None && isCanonicalResultType(ResultTy) && |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2835 | !EPI.HasTrailingReturn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2836 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2837 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2838 | isCanonical = false; |
| 2839 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2840 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2841 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2842 | CC_X86StdCall : DefaultCC; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2843 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2844 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2845 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2846 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2847 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2848 | SmallVector<QualType, 16> CanonicalArgs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2849 | CanonicalArgs.reserve(NumArgs); |
| 2850 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2851 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2852 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2853 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2854 | CanonicalEPI.HasTrailingReturn = false; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2855 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2856 | CanonicalEPI.NumExceptions = 0; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2857 | CanonicalEPI.ExtInfo |
| 2858 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2859 | |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2860 | // Result types do not have ARC lifetime qualifiers. |
| 2861 | QualType CanResultTy = getCanonicalType(ResultTy); |
| 2862 | if (ResultTy.getQualifiers().hasObjCLifetime()) { |
| 2863 | Qualifiers Qs = CanResultTy.getQualifiers(); |
| 2864 | Qs.removeObjCLifetime(); |
| 2865 | CanResultTy = getQualifiedType(CanResultTy.getUnqualifiedType(), Qs); |
| 2866 | } |
| 2867 | |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2868 | Canonical = getFunctionType(CanResultTy, CanonicalArgs, CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2869 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2870 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2871 | FunctionProtoType *NewIP = |
| 2872 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2873 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2874 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2875 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2876 | // FunctionProtoType objects are allocated with extra bytes after |
| 2877 | // them for three variable size arrays at the end: |
| 2878 | // - parameter types |
| 2879 | // - exception types |
| 2880 | // - consumed-arguments flags |
| 2881 | // Instead of the exception types, there could be a noexcept |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2882 | // expression, or information used to resolve the exception |
| 2883 | // specification. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2884 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2885 | NumArgs * sizeof(QualType); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2886 | if (EPI.ExceptionSpecType == EST_Dynamic) { |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2887 | Size += EPI.NumExceptions * sizeof(QualType); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2888 | } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2889 | Size += sizeof(Expr*); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2890 | } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2891 | Size += 2 * sizeof(FunctionDecl*); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2892 | } else if (EPI.ExceptionSpecType == EST_Unevaluated) { |
| 2893 | Size += sizeof(FunctionDecl*); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2894 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2895 | if (EPI.ConsumedArguments) |
| 2896 | Size += NumArgs * sizeof(bool); |
| 2897 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2898 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2899 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2900 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2901 | new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2902 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2903 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2904 | return QualType(FTP, 0); |
| 2905 | } |
| 2906 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2907 | #ifndef NDEBUG |
| 2908 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2909 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2910 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2911 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2912 | return true; |
| 2913 | if (RD->getDescribedClassTemplate() && |
| 2914 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2915 | return true; |
| 2916 | return false; |
| 2917 | } |
| 2918 | #endif |
| 2919 | |
| 2920 | /// getInjectedClassNameType - Return the unique reference to the |
| 2921 | /// injected class name type for the specified templated declaration. |
| 2922 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2923 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2924 | assert(NeedsInjectedClassNameType(Decl)); |
| 2925 | if (Decl->TypeForDecl) { |
| 2926 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2927 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2928 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2929 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2930 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2931 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2932 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2933 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2934 | Decl->TypeForDecl = newType; |
| 2935 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2936 | } |
| 2937 | return QualType(Decl->TypeForDecl, 0); |
| 2938 | } |
| 2939 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2940 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2941 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2942 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2943 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2944 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2945 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2946 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2947 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2948 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2949 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2950 | "Template type parameter types are always available."); |
| 2951 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2952 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2953 | assert(!Record->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2954 | "struct/union has previous declaration"); |
| 2955 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2956 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2957 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2958 | assert(!Enum->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2959 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2960 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2961 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2962 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2963 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2964 | Decl->TypeForDecl = newType; |
| 2965 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2966 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2967 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2968 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2969 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2972 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2973 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2974 | QualType |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2975 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2976 | QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2977 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2978 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2979 | if (Canonical.isNull()) |
| 2980 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2981 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2982 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2983 | Decl->TypeForDecl = newType; |
| 2984 | Types.push_back(newType); |
| 2985 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2988 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2989 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2990 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2991 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2992 | if (PrevDecl->TypeForDecl) |
| 2993 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2994 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2995 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2996 | Decl->TypeForDecl = newType; |
| 2997 | Types.push_back(newType); |
| 2998 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3001 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3002 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 3003 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3004 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3005 | if (PrevDecl->TypeForDecl) |
| 3006 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 3007 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3008 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 3009 | Decl->TypeForDecl = newType; |
| 3010 | Types.push_back(newType); |
| 3011 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 3014 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 3015 | QualType modifiedType, |
| 3016 | QualType equivalentType) { |
| 3017 | llvm::FoldingSetNodeID id; |
| 3018 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 3019 | |
| 3020 | void *insertPos = 0; |
| 3021 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 3022 | if (type) return QualType(type, 0); |
| 3023 | |
| 3024 | QualType canon = getCanonicalType(equivalentType); |
| 3025 | type = new (*this, TypeAlignment) |
| 3026 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 3027 | |
| 3028 | Types.push_back(type); |
| 3029 | AttributedTypes.InsertNode(type, insertPos); |
| 3030 | |
| 3031 | return QualType(type, 0); |
| 3032 | } |
| 3033 | |
| 3034 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3035 | /// \brief Retrieve a substitution-result type. |
| 3036 | QualType |
| 3037 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3038 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3039 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3040 | && "replacement types must always be canonical"); |
| 3041 | |
| 3042 | llvm::FoldingSetNodeID ID; |
| 3043 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 3044 | void *InsertPos = 0; |
| 3045 | SubstTemplateTypeParmType *SubstParm |
| 3046 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3047 | |
| 3048 | if (!SubstParm) { |
| 3049 | SubstParm = new (*this, TypeAlignment) |
| 3050 | SubstTemplateTypeParmType(Parm, Replacement); |
| 3051 | Types.push_back(SubstParm); |
| 3052 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 3053 | } |
| 3054 | |
| 3055 | return QualType(SubstParm, 0); |
| 3056 | } |
| 3057 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3058 | /// \brief Retrieve a |
| 3059 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 3060 | const TemplateTypeParmType *Parm, |
| 3061 | const TemplateArgument &ArgPack) { |
| 3062 | #ifndef NDEBUG |
| 3063 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 3064 | PEnd = ArgPack.pack_end(); |
| 3065 | P != PEnd; ++P) { |
| 3066 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 3067 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 3068 | } |
| 3069 | #endif |
| 3070 | |
| 3071 | llvm::FoldingSetNodeID ID; |
| 3072 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 3073 | void *InsertPos = 0; |
| 3074 | if (SubstTemplateTypeParmPackType *SubstParm |
| 3075 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3076 | return QualType(SubstParm, 0); |
| 3077 | |
| 3078 | QualType Canon; |
| 3079 | if (!Parm->isCanonicalUnqualified()) { |
| 3080 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 3081 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 3082 | ArgPack); |
| 3083 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3084 | } |
| 3085 | |
| 3086 | SubstTemplateTypeParmPackType *SubstParm |
| 3087 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 3088 | ArgPack); |
| 3089 | Types.push_back(SubstParm); |
| 3090 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 3091 | return QualType(SubstParm, 0); |
| 3092 | } |
| 3093 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3094 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3095 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3096 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3097 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3098 | bool ParameterPack, |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3099 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3100 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3101 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3102 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3104 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3105 | |
| 3106 | if (TypeParm) |
| 3107 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3108 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3109 | if (TTPDecl) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3110 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3111 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3112 | |
| 3113 | TemplateTypeParmType *TypeCheck |
| 3114 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3115 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 3116 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3117 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3118 | TypeParm = new (*this, TypeAlignment) |
| 3119 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3120 | |
| 3121 | Types.push_back(TypeParm); |
| 3122 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 3123 | |
| 3124 | return QualType(TypeParm, 0); |
| 3125 | } |
| 3126 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3127 | TypeSourceInfo * |
| 3128 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 3129 | SourceLocation NameLoc, |
| 3130 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3131 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3132 | assert(!Name.getAsDependentTemplateName() && |
| 3133 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3134 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3135 | |
| 3136 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3137 | TemplateSpecializationTypeLoc TL = |
| 3138 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3139 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3140 | TL.setTemplateNameLoc(NameLoc); |
| 3141 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 3142 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 3143 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 3144 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 3145 | return DI; |
| 3146 | } |
| 3147 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3148 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3149 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3150 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3151 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3152 | assert(!Template.getAsDependentTemplateName() && |
| 3153 | "No dependent template names here!"); |
| 3154 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3155 | unsigned NumArgs = Args.size(); |
| 3156 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3157 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3158 | ArgVec.reserve(NumArgs); |
| 3159 | for (unsigned i = 0; i != NumArgs; ++i) |
| 3160 | ArgVec.push_back(Args[i].getArgument()); |
| 3161 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3162 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3163 | Underlying); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3166 | #ifndef NDEBUG |
| 3167 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 3168 | unsigned NumArgs) { |
| 3169 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3170 | if (Args[I].isPackExpansion()) |
| 3171 | return true; |
| 3172 | |
| 3173 | return true; |
| 3174 | } |
| 3175 | #endif |
| 3176 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3177 | QualType |
| 3178 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3179 | const TemplateArgument *Args, |
| 3180 | unsigned NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3181 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3182 | assert(!Template.getAsDependentTemplateName() && |
| 3183 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3184 | // Look through qualified template names. |
| 3185 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3186 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3187 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3188 | bool IsTypeAlias = |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3189 | Template.getAsTemplateDecl() && |
| 3190 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3191 | QualType CanonType; |
| 3192 | if (!Underlying.isNull()) |
| 3193 | CanonType = getCanonicalType(Underlying); |
| 3194 | else { |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3195 | // We can get here with an alias template when the specialization contains |
| 3196 | // a pack expansion that does not match up with a parameter pack. |
| 3197 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 3198 | "Caller must compute aliased type"); |
| 3199 | IsTypeAlias = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3200 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 3201 | NumArgs); |
| 3202 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3203 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3204 | // Allocate the (non-canonical) template specialization type, but don't |
| 3205 | // try to unique it: these types typically have location information that |
| 3206 | // we don't unique and don't want to lose. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3207 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 3208 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3209 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3210 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3211 | TemplateSpecializationType *Spec |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3212 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 3213 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3214 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3215 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3216 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3219 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3220 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 3221 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3222 | unsigned NumArgs) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3223 | assert(!Template.getAsDependentTemplateName() && |
| 3224 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3225 | |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3226 | // Look through qualified template names. |
| 3227 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3228 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3229 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3230 | // Build the canonical template specialization type. |
| 3231 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3232 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3233 | CanonArgs.reserve(NumArgs); |
| 3234 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3235 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 3236 | |
| 3237 | // Determine whether this canonical template specialization type already |
| 3238 | // exists. |
| 3239 | llvm::FoldingSetNodeID ID; |
| 3240 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 3241 | CanonArgs.data(), NumArgs, *this); |
| 3242 | |
| 3243 | void *InsertPos = 0; |
| 3244 | TemplateSpecializationType *Spec |
| 3245 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3246 | |
| 3247 | if (!Spec) { |
| 3248 | // Allocate a new canonical template specialization type. |
| 3249 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 3250 | sizeof(TemplateArgument) * NumArgs), |
| 3251 | TypeAlignment); |
| 3252 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 3253 | CanonArgs.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3254 | QualType(), QualType()); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3255 | Types.push_back(Spec); |
| 3256 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 3257 | } |
| 3258 | |
| 3259 | assert(Spec->isDependentType() && |
| 3260 | "Non-dependent template-id type must have a canonical type"); |
| 3261 | return QualType(Spec, 0); |
| 3262 | } |
| 3263 | |
| 3264 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3265 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 3266 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3267 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3268 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3269 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3270 | |
| 3271 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3272 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3273 | if (T) |
| 3274 | return QualType(T, 0); |
| 3275 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3276 | QualType Canon = NamedType; |
| 3277 | if (!Canon.isCanonical()) { |
| 3278 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3279 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3280 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3281 | (void)CheckT; |
| 3282 | } |
| 3283 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3284 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3285 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3286 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3287 | return QualType(T, 0); |
| 3288 | } |
| 3289 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3290 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3291 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3292 | llvm::FoldingSetNodeID ID; |
| 3293 | ParenType::Profile(ID, InnerType); |
| 3294 | |
| 3295 | void *InsertPos = 0; |
| 3296 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3297 | if (T) |
| 3298 | return QualType(T, 0); |
| 3299 | |
| 3300 | QualType Canon = InnerType; |
| 3301 | if (!Canon.isCanonical()) { |
| 3302 | Canon = getCanonicalType(InnerType); |
| 3303 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3304 | assert(!CheckT && "Paren canonical type broken"); |
| 3305 | (void)CheckT; |
| 3306 | } |
| 3307 | |
| 3308 | T = new (*this) ParenType(InnerType, Canon); |
| 3309 | Types.push_back(T); |
| 3310 | ParenTypes.InsertNode(T, InsertPos); |
| 3311 | return QualType(T, 0); |
| 3312 | } |
| 3313 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3314 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 3315 | NestedNameSpecifier *NNS, |
| 3316 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3317 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3318 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 3319 | |
| 3320 | if (Canon.isNull()) { |
| 3321 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3322 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3323 | if (Keyword == ETK_None) |
| 3324 | CanonKeyword = ETK_Typename; |
| 3325 | |
| 3326 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 3327 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3331 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3332 | |
| 3333 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3334 | DependentNameType *T |
| 3335 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3336 | if (T) |
| 3337 | return QualType(T, 0); |
| 3338 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3339 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3340 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3341 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3342 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3345 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3346 | ASTContext::getDependentTemplateSpecializationType( |
| 3347 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3348 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3349 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3350 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3351 | // TODO: avoid this copy |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3352 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3353 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 3354 | ArgCopy.push_back(Args[I].getArgument()); |
| 3355 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 3356 | ArgCopy.size(), |
| 3357 | ArgCopy.data()); |
| 3358 | } |
| 3359 | |
| 3360 | QualType |
| 3361 | ASTContext::getDependentTemplateSpecializationType( |
| 3362 | ElaboratedTypeKeyword Keyword, |
| 3363 | NestedNameSpecifier *NNS, |
| 3364 | const IdentifierInfo *Name, |
| 3365 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3366 | const TemplateArgument *Args) const { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3367 | assert((!NNS || NNS->isDependent()) && |
| 3368 | "nested-name-specifier must be dependent"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3369 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3370 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3371 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 3372 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3373 | |
| 3374 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3375 | DependentTemplateSpecializationType *T |
| 3376 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3377 | if (T) |
| 3378 | return QualType(T, 0); |
| 3379 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3380 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3381 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3382 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3383 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 3384 | |
| 3385 | bool AnyNonCanonArgs = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3386 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3387 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 3388 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 3389 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 3390 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3393 | QualType Canon; |
| 3394 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 3395 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 3396 | Name, NumArgs, |
| 3397 | CanonArgs.data()); |
| 3398 | |
| 3399 | // Find the insert position again. |
| 3400 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3401 | } |
| 3402 | |
| 3403 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 3404 | sizeof(TemplateArgument) * NumArgs), |
| 3405 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 3406 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3407 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3408 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3409 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3410 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3413 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3414 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3415 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3416 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3417 | |
| 3418 | assert(Pattern->containsUnexpandedParameterPack() && |
| 3419 | "Pack expansions must expand one or more parameter packs"); |
| 3420 | void *InsertPos = 0; |
| 3421 | PackExpansionType *T |
| 3422 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3423 | if (T) |
| 3424 | return QualType(T, 0); |
| 3425 | |
| 3426 | QualType Canon; |
| 3427 | if (!Pattern.isCanonical()) { |
Richard Smith | d8672ef | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3428 | Canon = getCanonicalType(Pattern); |
| 3429 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 3430 | // contains an alias template specialization which ignores one of its |
| 3431 | // parameters. |
| 3432 | if (Canon->containsUnexpandedParameterPack()) { |
| 3433 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3434 | |
Richard Smith | d8672ef | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3435 | // Find the insert position again, in case we inserted an element into |
| 3436 | // PackExpansionTypes and invalidated our insert position. |
| 3437 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3438 | } |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3439 | } |
| 3440 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3441 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3442 | Types.push_back(T); |
| 3443 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 3444 | return QualType(T, 0); |
| 3445 | } |
| 3446 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3447 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 3448 | /// alphabetically. |
| 3449 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 3450 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3451 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3454 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3455 | unsigned NumProtocols) { |
| 3456 | if (NumProtocols == 0) return true; |
| 3457 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3458 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 3459 | return false; |
| 3460 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3461 | for (unsigned i = 1; i != NumProtocols; ++i) |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3462 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) || |
| 3463 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3464 | return false; |
| 3465 | return true; |
| 3466 | } |
| 3467 | |
| 3468 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3469 | unsigned &NumProtocols) { |
| 3470 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3472 | // Sort protocols, keyed by name. |
| 3473 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 3474 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3475 | // Canonicalize. |
| 3476 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 3477 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 3478 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3479 | // Remove duplicates. |
| 3480 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 3481 | NumProtocols = ProtocolsEnd-Protocols; |
| 3482 | } |
| 3483 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3484 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 3485 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3486 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3487 | // If the base type is an interface and there aren't any protocols |
| 3488 | // to add, then the interface type will do just fine. |
| 3489 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 3490 | return BaseType; |
| 3491 | |
| 3492 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3493 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3494 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3495 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3496 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3497 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3498 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3499 | // Build the canonical type, which has the canonical base type and |
| 3500 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3501 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3502 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 3503 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 3504 | if (!ProtocolsSorted) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3505 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3506 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3507 | unsigned UniqueCount = NumProtocols; |
| 3508 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3509 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3510 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3511 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3512 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3513 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3514 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3518 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3519 | } |
| 3520 | |
| 3521 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 3522 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 3523 | void *Mem = Allocate(Size, TypeAlignment); |
| 3524 | ObjCObjectTypeImpl *T = |
| 3525 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 3526 | |
| 3527 | Types.push_back(T); |
| 3528 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 3529 | return QualType(T, 0); |
| 3530 | } |
| 3531 | |
| 3532 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 3533 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3534 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3535 | llvm::FoldingSetNodeID ID; |
| 3536 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 3537 | |
| 3538 | void *InsertPos = 0; |
| 3539 | if (ObjCObjectPointerType *QT = |
| 3540 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3541 | return QualType(QT, 0); |
| 3542 | |
| 3543 | // Find the canonical object type. |
| 3544 | QualType Canonical; |
| 3545 | if (!ObjectT.isCanonical()) { |
| 3546 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 3547 | |
| 3548 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3549 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3550 | } |
| 3551 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 3552 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3553 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 3554 | ObjCObjectPointerType *QType = |
| 3555 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3556 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3557 | Types.push_back(QType); |
| 3558 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3559 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3560 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3561 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3562 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 3563 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3564 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 3565 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3566 | if (Decl->TypeForDecl) |
| 3567 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3568 | |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3569 | if (PrevDecl) { |
| 3570 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 3571 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3572 | return QualType(PrevDecl->TypeForDecl, 0); |
| 3573 | } |
| 3574 | |
Douglas Gregor | 8d2dbbf | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 3575 | // Prefer the definition, if there is one. |
| 3576 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 3577 | Decl = Def; |
| 3578 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3579 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 3580 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 3581 | Decl->TypeForDecl = T; |
| 3582 | Types.push_back(T); |
| 3583 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3586 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 3587 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3588 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3589 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3590 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3591 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3592 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3593 | if (tofExpr->isTypeDependent()) { |
| 3594 | llvm::FoldingSetNodeID ID; |
| 3595 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3596 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3597 | void *InsertPos = 0; |
| 3598 | DependentTypeOfExprType *Canon |
| 3599 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3600 | if (Canon) { |
| 3601 | // We already have a "canonical" version of an identical, dependent |
| 3602 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3603 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3604 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3605 | } else { |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3606 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3607 | Canon |
| 3608 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3609 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 3610 | toe = Canon; |
| 3611 | } |
| 3612 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3613 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3614 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3615 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3616 | Types.push_back(toe); |
| 3617 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3620 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 3621 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 3622 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3623 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3624 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3625 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3626 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3627 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3628 | Types.push_back(tot); |
| 3629 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 3632 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3633 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 3634 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 3635 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3636 | /// an issue. This doesn't effect the type checker, since it operates |
David Blaikie | 39e0203 | 2011-11-06 22:28:03 +0000 | [diff] [blame] | 3637 | /// on canonical types (which are always unique). |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3638 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3639 | DecltypeType *dt; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3640 | |
| 3641 | // C++0x [temp.type]p2: |
| 3642 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 3643 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 3644 | // type only if their expressions are equivalent (14.5.6.1). |
| 3645 | if (e->isInstantiationDependent()) { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3646 | llvm::FoldingSetNodeID ID; |
| 3647 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3648 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3649 | void *InsertPos = 0; |
| 3650 | DependentDecltypeType *Canon |
| 3651 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3652 | if (Canon) { |
| 3653 | // We already have a "canonical" version of an equivalent, dependent |
| 3654 | // decltype type. Use that as our canonical type. |
Richard Smith | 0d72910 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 3655 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3656 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3657 | } else { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3658 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3659 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3660 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 3661 | dt = Canon; |
| 3662 | } |
| 3663 | } else { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3664 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
| 3665 | getCanonicalType(UnderlyingType)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3666 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3667 | Types.push_back(dt); |
| 3668 | return QualType(dt, 0); |
| 3669 | } |
| 3670 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3671 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 3672 | /// savings are minimal and these are rare. |
| 3673 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 3674 | QualType UnderlyingType, |
| 3675 | UnaryTransformType::UTTKind Kind) |
| 3676 | const { |
| 3677 | UnaryTransformType *Ty = |
Douglas Gregor | 69d9775 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 3678 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 3679 | Kind, |
| 3680 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 12fc4b0 | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 3681 | QualType() : getCanonicalType(UnderlyingType)); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3682 | Types.push_back(Ty); |
| 3683 | return QualType(Ty, 0); |
| 3684 | } |
| 3685 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3686 | /// getAutoType - Return the uniqued reference to the 'auto' type which has been |
| 3687 | /// deduced to the given type, or to the canonical undeduced 'auto' type, or the |
| 3688 | /// canonical deduced-but-dependent 'auto' type. |
| 3689 | QualType ASTContext::getAutoType(QualType DeducedType, bool IsDecltypeAuto, |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3690 | bool IsDependent) const { |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3691 | if (DeducedType.isNull() && !IsDecltypeAuto && !IsDependent) |
| 3692 | return getAutoDeductType(); |
| 3693 | |
| 3694 | // Look in the folding set for an existing type. |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3695 | void *InsertPos = 0; |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3696 | llvm::FoldingSetNodeID ID; |
| 3697 | AutoType::Profile(ID, DeducedType, IsDecltypeAuto, IsDependent); |
| 3698 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3699 | return QualType(AT, 0); |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3700 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3701 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType, |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3702 | IsDecltypeAuto, |
| 3703 | IsDependent); |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3704 | Types.push_back(AT); |
| 3705 | if (InsertPos) |
| 3706 | AutoTypes.InsertNode(AT, InsertPos); |
| 3707 | return QualType(AT, 0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3710 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3711 | /// the given value type. |
| 3712 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3713 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3714 | // structure. |
| 3715 | llvm::FoldingSetNodeID ID; |
| 3716 | AtomicType::Profile(ID, T); |
| 3717 | |
| 3718 | void *InsertPos = 0; |
| 3719 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3720 | return QualType(AT, 0); |
| 3721 | |
| 3722 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3723 | // either, so fill in the canonical type field. |
| 3724 | QualType Canonical; |
| 3725 | if (!T.isCanonical()) { |
| 3726 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3727 | |
| 3728 | // Get the new insert position for the node we care about. |
| 3729 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3730 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 3731 | } |
| 3732 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3733 | Types.push_back(New); |
| 3734 | AtomicTypes.InsertNode(New, InsertPos); |
| 3735 | return QualType(New, 0); |
| 3736 | } |
| 3737 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3738 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3739 | QualType ASTContext::getAutoDeductType() const { |
| 3740 | if (AutoDeductTy.isNull()) |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3741 | AutoDeductTy = QualType( |
| 3742 | new (*this, TypeAlignment) AutoType(QualType(), /*decltype(auto)*/false, |
| 3743 | /*dependent*/false), |
| 3744 | 0); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3745 | return AutoDeductTy; |
| 3746 | } |
| 3747 | |
| 3748 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3749 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3750 | if (AutoRRefDeductTy.isNull()) |
| 3751 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3752 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3753 | return AutoRRefDeductTy; |
| 3754 | } |
| 3755 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3756 | /// getTagDeclType - Return the unique reference to the type for the |
| 3757 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3758 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3759 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3760 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3761 | // away const? mutable? |
| 3762 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3765 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3766 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3767 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3768 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3769 | return getFromTargetType(Target->getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3772 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3773 | CanQualType ASTContext::getIntMaxType() const { |
| 3774 | return getFromTargetType(Target->getIntMaxType()); |
| 3775 | } |
| 3776 | |
| 3777 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3778 | CanQualType ASTContext::getUIntMaxType() const { |
| 3779 | return getFromTargetType(Target->getUIntMaxType()); |
| 3780 | } |
| 3781 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3782 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3783 | /// Used when in C++, as a GCC extension. |
| 3784 | QualType ASTContext::getSignedWCharType() const { |
| 3785 | // FIXME: derive from "Target" ? |
| 3786 | return WCharTy; |
| 3787 | } |
| 3788 | |
| 3789 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3790 | /// Used when in C++, as a GCC extension. |
| 3791 | QualType ASTContext::getUnsignedWCharType() const { |
| 3792 | // FIXME: derive from "Target" ? |
| 3793 | return UnsignedIntTy; |
| 3794 | } |
| 3795 | |
Enea Zaffanella | 9677eb8 | 2013-01-26 17:08:37 +0000 | [diff] [blame] | 3796 | QualType ASTContext::getIntPtrType() const { |
| 3797 | return getFromTargetType(Target->getIntPtrType()); |
| 3798 | } |
| 3799 | |
| 3800 | QualType ASTContext::getUIntPtrType() const { |
| 3801 | return getCorrespondingUnsignedType(getIntPtrType()); |
| 3802 | } |
| 3803 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3804 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3805 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3806 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3807 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
Eli Friedman | 6902e41 | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 3810 | /// \brief Return the unique type for "pid_t" defined in |
| 3811 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
| 3812 | QualType ASTContext::getProcessIDType() const { |
| 3813 | return getFromTargetType(Target->getProcessIDType()); |
| 3814 | } |
| 3815 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3816 | //===----------------------------------------------------------------------===// |
| 3817 | // Type Operators |
| 3818 | //===----------------------------------------------------------------------===// |
| 3819 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3820 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3821 | // Push qualifiers into arrays, and then discard any remaining |
| 3822 | // qualifiers. |
| 3823 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3824 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3825 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3826 | QualType Result; |
| 3827 | if (isa<ArrayType>(Ty)) { |
| 3828 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3829 | } else if (isa<FunctionType>(Ty)) { |
| 3830 | Result = getPointerType(QualType(Ty, 0)); |
| 3831 | } else { |
| 3832 | Result = QualType(Ty, 0); |
| 3833 | } |
| 3834 | |
| 3835 | return CanQualType::CreateUnsafe(Result); |
| 3836 | } |
| 3837 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3838 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3839 | Qualifiers &quals) { |
| 3840 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3841 | |
| 3842 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3843 | // the unqualified desugared type and then drops it on the floor. |
| 3844 | // We then have to strip that sugar back off with |
| 3845 | // getUnqualifiedDesugaredType(), which is silly. |
| 3846 | const ArrayType *AT = |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3847 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3848 | |
| 3849 | // If we don't have an array, just use the results in splitType. |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3850 | if (!AT) { |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3851 | quals = splitType.Quals; |
| 3852 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3855 | // Otherwise, recurse on the array's element type. |
| 3856 | QualType elementType = AT->getElementType(); |
| 3857 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3858 | |
| 3859 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3860 | // can just use the results in splitType. |
| 3861 | if (elementType == unqualElementType) { |
| 3862 | assert(quals.empty()); // from the recursive call |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3863 | quals = splitType.Quals; |
| 3864 | return QualType(splitType.Ty, 0); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3868 | // build the type back up. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3869 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3870 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3871 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3872 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3873 | CAT->getSizeModifier(), 0); |
| 3874 | } |
| 3875 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3876 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3877 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3880 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3881 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3882 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3883 | VAT->getSizeModifier(), |
| 3884 | VAT->getIndexTypeCVRQualifiers(), |
| 3885 | VAT->getBracketsRange()); |
| 3886 | } |
| 3887 | |
| 3888 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3889 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3890 | DSAT->getSizeModifier(), 0, |
| 3891 | SourceRange()); |
| 3892 | } |
| 3893 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3894 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3895 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3896 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3897 | /// or pointer-to-member types, or if they are not similar at this |
| 3898 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3899 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3900 | /// be called in a loop that successively "unwraps" pointer and |
| 3901 | /// pointer-to-member types to compare them at each level. |
| 3902 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3903 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3904 | *T2PtrType = T2->getAs<PointerType>(); |
| 3905 | if (T1PtrType && T2PtrType) { |
| 3906 | T1 = T1PtrType->getPointeeType(); |
| 3907 | T2 = T2PtrType->getPointeeType(); |
| 3908 | return true; |
| 3909 | } |
| 3910 | |
| 3911 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3912 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3913 | if (T1MPType && T2MPType && |
| 3914 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3915 | QualType(T2MPType->getClass(), 0))) { |
| 3916 | T1 = T1MPType->getPointeeType(); |
| 3917 | T2 = T2MPType->getPointeeType(); |
| 3918 | return true; |
| 3919 | } |
| 3920 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3921 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3922 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3923 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3924 | if (T1OPType && T2OPType) { |
| 3925 | T1 = T1OPType->getPointeeType(); |
| 3926 | T2 = T2OPType->getPointeeType(); |
| 3927 | return true; |
| 3928 | } |
| 3929 | } |
| 3930 | |
| 3931 | // FIXME: Block pointers, too? |
| 3932 | |
| 3933 | return false; |
| 3934 | } |
| 3935 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3936 | DeclarationNameInfo |
| 3937 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3938 | SourceLocation NameLoc) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3939 | switch (Name.getKind()) { |
| 3940 | case TemplateName::QualifiedTemplate: |
| 3941 | case TemplateName::Template: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3942 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3943 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3944 | NameLoc); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3945 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3946 | case TemplateName::OverloadedTemplate: { |
| 3947 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3948 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3949 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3950 | } |
| 3951 | |
| 3952 | case TemplateName::DependentTemplate: { |
| 3953 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3954 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3955 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3956 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3957 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3958 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3959 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3960 | // DNInfo work in progress: FIXME: source locations? |
| 3961 | DeclarationNameLoc DNLoc; |
| 3962 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3963 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3964 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3965 | } |
| 3966 | } |
| 3967 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3968 | case TemplateName::SubstTemplateTemplateParm: { |
| 3969 | SubstTemplateTemplateParmStorage *subst |
| 3970 | = Name.getAsSubstTemplateTemplateParm(); |
| 3971 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3972 | NameLoc); |
| 3973 | } |
| 3974 | |
| 3975 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3976 | SubstTemplateTemplateParmPackStorage *subst |
| 3977 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3978 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3979 | NameLoc); |
| 3980 | } |
| 3981 | } |
| 3982 | |
| 3983 | llvm_unreachable("bad template name kind!"); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3986 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3987 | switch (Name.getKind()) { |
| 3988 | case TemplateName::QualifiedTemplate: |
| 3989 | case TemplateName::Template: { |
| 3990 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3991 | if (TemplateTemplateParmDecl *TTP |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3992 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3993 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3994 | |
| 3995 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3996 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3997 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3998 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3999 | case TemplateName::OverloadedTemplate: |
| 4000 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4001 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4002 | case TemplateName::DependentTemplate: { |
| 4003 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 4004 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 4005 | return DTN->CanonicalTemplateName; |
| 4006 | } |
| 4007 | |
| 4008 | case TemplateName::SubstTemplateTemplateParm: { |
| 4009 | SubstTemplateTemplateParmStorage *subst |
| 4010 | = Name.getAsSubstTemplateTemplateParm(); |
| 4011 | return getCanonicalTemplateName(subst->getReplacement()); |
| 4012 | } |
| 4013 | |
| 4014 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 4015 | SubstTemplateTemplateParmPackStorage *subst |
| 4016 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 4017 | TemplateTemplateParmDecl *canonParameter |
| 4018 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 4019 | TemplateArgument canonArgPack |
| 4020 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 4021 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 4022 | } |
| 4023 | } |
| 4024 | |
| 4025 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 4028 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 4029 | X = getCanonicalTemplateName(X); |
| 4030 | Y = getCanonicalTemplateName(Y); |
| 4031 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 4032 | } |
| 4033 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4034 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4035 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4036 | switch (Arg.getKind()) { |
| 4037 | case TemplateArgument::Null: |
| 4038 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4039 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4040 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4041 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4042 | |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4043 | case TemplateArgument::Declaration: { |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4044 | ValueDecl *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
| 4045 | return TemplateArgument(D, Arg.isDeclForReferenceParam()); |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4046 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4047 | |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4048 | case TemplateArgument::NullPtr: |
| 4049 | return TemplateArgument(getCanonicalType(Arg.getNullPtrType()), |
| 4050 | /*isNullPtr*/true); |
| 4051 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4052 | case TemplateArgument::Template: |
| 4053 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4054 | |
| 4055 | case TemplateArgument::TemplateExpansion: |
| 4056 | return TemplateArgument(getCanonicalTemplateName( |
| 4057 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 4058 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4059 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4060 | case TemplateArgument::Integral: |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4061 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4062 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4063 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4064 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4065 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4066 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 4067 | if (Arg.pack_size() == 0) |
| 4068 | return Arg; |
| 4069 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4070 | TemplateArgument *CanonArgs |
| 4071 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4072 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4073 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4074 | AEnd = Arg.pack_end(); |
| 4075 | A != AEnd; (void)++A, ++Idx) |
| 4076 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4077 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4078 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4079 | } |
| 4080 | } |
| 4081 | |
| 4082 | // Silence GCC warning |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4083 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4086 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4087 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4088 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4089 | return 0; |
| 4090 | |
| 4091 | switch (NNS->getKind()) { |
| 4092 | case NestedNameSpecifier::Identifier: |
| 4093 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4094 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4095 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 4096 | NNS->getAsIdentifier()); |
| 4097 | |
| 4098 | case NestedNameSpecifier::Namespace: |
| 4099 | // A namespace is canonical; build a nested-name-specifier with |
| 4100 | // this namespace and no prefix. |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4101 | return NestedNameSpecifier::Create(*this, 0, |
| 4102 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 4103 | |
| 4104 | case NestedNameSpecifier::NamespaceAlias: |
| 4105 | // A namespace is canonical; build a nested-name-specifier with |
| 4106 | // this namespace and no prefix. |
| 4107 | return NestedNameSpecifier::Create(*this, 0, |
| 4108 | NNS->getAsNamespaceAlias()->getNamespace() |
| 4109 | ->getOriginalNamespace()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4110 | |
| 4111 | case NestedNameSpecifier::TypeSpec: |
| 4112 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 4113 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4114 | |
| 4115 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 4116 | // break it apart into its prefix and identifier, then reconsititute those |
| 4117 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 4118 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 4119 | // types, e.g., |
| 4120 | // typedef typename T::type T1; |
| 4121 | // typedef typename T1::type T2; |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 4122 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 4123 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4124 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4125 | |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 4126 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 4127 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 4128 | // first place? |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4129 | return NestedNameSpecifier::Create(*this, 0, false, |
| 4130 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | case NestedNameSpecifier::Global: |
| 4134 | // The global specifier is canonical and unique. |
| 4135 | return NNS; |
| 4136 | } |
| 4137 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4138 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4141 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4142 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4143 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4144 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4145 | // Handle the common positive case fast. |
| 4146 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 4147 | return AT; |
| 4148 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4149 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4150 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4151 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4152 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4153 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4154 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4155 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 4156 | // any type qualifiers, the element type is so qualified, not the array type." |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4157 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4158 | // If we get here, we either have type qualifiers on the type, or we have |
| 4159 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 4160 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4161 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4162 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4163 | Qualifiers qs = split.Quals; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4164 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4165 | // If we have a simple case, just return now. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4166 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4167 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4168 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4169 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4170 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 4171 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4172 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4173 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4174 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 4175 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 4176 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4177 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4178 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 4179 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 4180 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4181 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4182 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4183 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4184 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 4185 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4186 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4187 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4188 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4189 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4190 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4192 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4193 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4194 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4195 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4196 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4197 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
Abramo Bagnara | ad9689f | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4200 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Reid Kleckner | 12df246 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 4201 | if (T->isArrayType() || T->isFunctionType()) |
| 4202 | return getDecayedType(T); |
| 4203 | return T; |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
Abramo Bagnara | ad9689f | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4206 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4207 | T = getVariableArrayDecayedType(T); |
| 4208 | T = getAdjustedParameterType(T); |
| 4209 | return T.getUnqualifiedType(); |
| 4210 | } |
| 4211 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4212 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 4213 | /// specified array type to a pointer. This operation is non-trivial when |
| 4214 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 4215 | /// this returns a pointer to a properly qualified element of the array. |
| 4216 | /// |
| 4217 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4218 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4219 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 4220 | // typedefs in the element type of the array. This also handles propagation |
| 4221 | // of type qualifiers from the array type into the element type if present |
| 4222 | // (C99 6.7.3p8). |
| 4223 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 4224 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4225 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4226 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4227 | |
| 4228 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4229 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4232 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 4233 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 4234 | } |
| 4235 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4236 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 4237 | Qualifiers qs; |
| 4238 | while (true) { |
| 4239 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4240 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4241 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4242 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4243 | type = array->getElementType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4244 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4245 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4246 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4247 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4250 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4251 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4252 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 4253 | uint64_t ElementCount = 1; |
| 4254 | do { |
| 4255 | ElementCount *= CA->getSize().getZExtValue(); |
Richard Smith | d5e8394 | 2012-12-06 03:04:50 +0000 | [diff] [blame] | 4256 | CA = dyn_cast_or_null<ConstantArrayType>( |
| 4257 | CA->getElementType()->getAsArrayTypeUnsafe()); |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4258 | } while (CA); |
| 4259 | return ElementCount; |
| 4260 | } |
| 4261 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4262 | /// getFloatingRank - Return a relative rank for floating point types. |
| 4263 | /// This routine will assert if passed a built-in type that isn't a float. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4264 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4265 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4266 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4267 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4268 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 4269 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4270 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4271 | case BuiltinType::Half: return HalfRank; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4272 | case BuiltinType::Float: return FloatRank; |
| 4273 | case BuiltinType::Double: return DoubleRank; |
| 4274 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 4275 | } |
| 4276 | } |
| 4277 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4278 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 4279 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 4280 | /// 'typeDomain' is a real floating point or complex type. |
| 4281 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4282 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 4283 | QualType Domain) const { |
| 4284 | FloatingRank EltRank = getFloatingRank(Size); |
| 4285 | if (Domain->isComplexType()) { |
| 4286 | switch (EltRank) { |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4287 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4288 | case FloatRank: return FloatComplexTy; |
| 4289 | case DoubleRank: return DoubleComplexTy; |
| 4290 | case LongDoubleRank: return LongDoubleComplexTy; |
| 4291 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4292 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4293 | |
| 4294 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 4295 | switch (EltRank) { |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 4296 | case HalfRank: return HalfTy; |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4297 | case FloatRank: return FloatTy; |
| 4298 | case DoubleRank: return DoubleTy; |
| 4299 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4300 | } |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4301 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4302 | } |
| 4303 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4304 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 4305 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 4306 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4307 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4308 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4309 | FloatingRank LHSR = getFloatingRank(LHS); |
| 4310 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4311 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4312 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4313 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4314 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4315 | return 1; |
| 4316 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4317 | } |
| 4318 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4319 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 4320 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 4321 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4322 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4323 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4324 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4325 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4326 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4327 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4328 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4329 | case BuiltinType::Char_S: |
| 4330 | case BuiltinType::Char_U: |
| 4331 | case BuiltinType::SChar: |
| 4332 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4333 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4334 | case BuiltinType::Short: |
| 4335 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4336 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4337 | case BuiltinType::Int: |
| 4338 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4339 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4340 | case BuiltinType::Long: |
| 4341 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4342 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4343 | case BuiltinType::LongLong: |
| 4344 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4345 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4346 | case BuiltinType::Int128: |
| 4347 | case BuiltinType::UInt128: |
| 4348 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4349 | } |
| 4350 | } |
| 4351 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4352 | /// \brief Whether this is a promotable bitfield reference according |
| 4353 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 4354 | /// |
| 4355 | /// \returns the type this bit-field will promote to, or NULL if no |
| 4356 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4357 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 4358 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4359 | return QualType(); |
| 4360 | |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 4361 | FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields? |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4362 | if (!Field) |
| 4363 | return QualType(); |
| 4364 | |
| 4365 | QualType FT = Field->getType(); |
| 4366 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4367 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4368 | uint64_t IntSize = getTypeSize(IntTy); |
| 4369 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 4370 | // to the size of int, it gets promoted no matter what its type is. |
| 4371 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 4372 | if (BitWidth < IntSize) |
| 4373 | return IntTy; |
| 4374 | |
| 4375 | if (BitWidth == IntSize) |
| 4376 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 4377 | |
| 4378 | // Types bigger than int are not subject to promotions, and therefore act |
| 4379 | // like the base type. |
| 4380 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 4381 | // is ridiculous. |
| 4382 | return QualType(); |
| 4383 | } |
| 4384 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4385 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 4386 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 4387 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4388 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4389 | assert(!Promotable.isNull()); |
| 4390 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4391 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 4392 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 68a2dc4 | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 4393 | |
| 4394 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 4395 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 4396 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 4397 | // types that can represent all the values of its underlying type: |
| 4398 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 4399 | // unsigned long long int [...] |
| 4400 | // FIXME: Is there some better way to compute this? |
| 4401 | if (BT->getKind() == BuiltinType::WChar_S || |
| 4402 | BT->getKind() == BuiltinType::WChar_U || |
| 4403 | BT->getKind() == BuiltinType::Char16 || |
| 4404 | BT->getKind() == BuiltinType::Char32) { |
| 4405 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 4406 | uint64_t FromSize = getTypeSize(BT); |
| 4407 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 4408 | LongLongTy, UnsignedLongLongTy }; |
| 4409 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 4410 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 4411 | if (FromSize < ToSize || |
| 4412 | (FromSize == ToSize && |
| 4413 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 4414 | return PromoteTypes[Idx]; |
| 4415 | } |
| 4416 | llvm_unreachable("char type should fit into long long"); |
| 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4421 | if (Promotable->isSignedIntegerType()) |
| 4422 | return IntTy; |
Eli Friedman | 5b64e77 | 2012-11-15 01:21:59 +0000 | [diff] [blame] | 4423 | uint64_t PromotableSize = getIntWidth(Promotable); |
| 4424 | uint64_t IntSize = getIntWidth(IntTy); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4425 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 4426 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 4427 | } |
| 4428 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4429 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 4430 | /// type and returns its ownership. |
| 4431 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 4432 | while (!T.isNull()) { |
| 4433 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 4434 | return T.getObjCLifetime(); |
| 4435 | if (T->isArrayType()) |
| 4436 | T = getBaseElementType(T); |
| 4437 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 4438 | T = PT->getPointeeType(); |
| 4439 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 28445f0 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 4440 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4441 | else |
| 4442 | break; |
| 4443 | } |
| 4444 | |
| 4445 | return Qualifiers::OCL_None; |
| 4446 | } |
| 4447 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4448 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4449 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4450 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4451 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4452 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 4453 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4454 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4455 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4456 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 4457 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4458 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4459 | unsigned LHSRank = getIntegerRank(LHSC); |
| 4460 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4461 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4462 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 4463 | if (LHSRank == RHSRank) return 0; |
| 4464 | return LHSRank > RHSRank ? 1 : -1; |
| 4465 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4466 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4467 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 4468 | if (LHSUnsigned) { |
| 4469 | // If the unsigned [LHS] type is larger, return it. |
| 4470 | if (LHSRank >= RHSRank) |
| 4471 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4472 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4473 | // If the signed type can represent all values of the unsigned type, it |
| 4474 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4475 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4476 | return -1; |
| 4477 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4478 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4479 | // If the unsigned [RHS] type is larger, return it. |
| 4480 | if (RHSRank >= LHSRank) |
| 4481 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4482 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4483 | // If the signed type can represent all values of the unsigned type, it |
| 4484 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4485 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4486 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4487 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4488 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4489 | static RecordDecl * |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4490 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 4491 | DeclContext *DC, IdentifierInfo *Id) { |
| 4492 | SourceLocation Loc; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4493 | if (Ctx.getLangOpts().CPlusPlus) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4494 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4495 | else |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4496 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4497 | } |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4498 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4499 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4500 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4501 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4502 | CFConstantStringTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4503 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4504 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4505 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4506 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4507 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4508 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4509 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4510 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4511 | // int flags; |
| 4512 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4513 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4514 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4515 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4516 | FieldTypes[3] = LongTy; |
| 4517 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4518 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4519 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4520 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4521 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4522 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4523 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4524 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4525 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4526 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4527 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4528 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4531 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4532 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4534 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 4535 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4536 | |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 4537 | QualType ASTContext::getObjCSuperType() const { |
| 4538 | if (ObjCSuperType.isNull()) { |
| 4539 | RecordDecl *ObjCSuperTypeDecl = |
| 4540 | CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get("objc_super")); |
| 4541 | TUDecl->addDecl(ObjCSuperTypeDecl); |
| 4542 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
| 4543 | } |
| 4544 | return ObjCSuperType; |
| 4545 | } |
| 4546 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4547 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4548 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4549 | assert(Rec && "Invalid CFConstantStringType"); |
| 4550 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 4551 | } |
| 4552 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4553 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4554 | if (BlockDescriptorType) |
| 4555 | return getTagDeclType(BlockDescriptorType); |
| 4556 | |
| 4557 | RecordDecl *T; |
| 4558 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4559 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4560 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4561 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4562 | |
| 4563 | QualType FieldTypes[] = { |
| 4564 | UnsignedLongTy, |
| 4565 | UnsignedLongTy, |
| 4566 | }; |
| 4567 | |
| 4568 | const char *FieldNames[] = { |
| 4569 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4570 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4571 | }; |
| 4572 | |
| 4573 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4574 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4575 | SourceLocation(), |
| 4576 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4577 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4578 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4579 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4580 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4581 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4582 | T->addDecl(Field); |
| 4583 | } |
| 4584 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4585 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4586 | |
| 4587 | BlockDescriptorType = T; |
| 4588 | |
| 4589 | return getTagDeclType(BlockDescriptorType); |
| 4590 | } |
| 4591 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4592 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4593 | if (BlockDescriptorExtendedType) |
| 4594 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4595 | |
| 4596 | RecordDecl *T; |
| 4597 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4598 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4599 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4600 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4601 | |
| 4602 | QualType FieldTypes[] = { |
| 4603 | UnsignedLongTy, |
| 4604 | UnsignedLongTy, |
| 4605 | getPointerType(VoidPtrTy), |
| 4606 | getPointerType(VoidPtrTy) |
| 4607 | }; |
| 4608 | |
| 4609 | const char *FieldNames[] = { |
| 4610 | "reserved", |
| 4611 | "Size", |
| 4612 | "CopyFuncPtr", |
| 4613 | "DestroyFuncPtr" |
| 4614 | }; |
| 4615 | |
| 4616 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4617 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4618 | SourceLocation(), |
| 4619 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4620 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4621 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4622 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4623 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4624 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4625 | T->addDecl(Field); |
| 4626 | } |
| 4627 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4628 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4629 | |
| 4630 | BlockDescriptorExtendedType = T; |
| 4631 | |
| 4632 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4633 | } |
| 4634 | |
Fariborz Jahanian | b15c898 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4635 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
| 4636 | /// requires copy/dispose. Note that this must match the logic |
| 4637 | /// in buildByrefHelpers. |
| 4638 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
| 4639 | const VarDecl *D) { |
| 4640 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
| 4641 | const Expr *copyExpr = getBlockVarCopyInits(D); |
| 4642 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
| 4643 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4644 | return true; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4645 | } |
Fariborz Jahanian | b15c898 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4646 | |
| 4647 | if (!Ty->isObjCRetainableType()) return false; |
| 4648 | |
| 4649 | Qualifiers qs = Ty.getQualifiers(); |
| 4650 | |
| 4651 | // If we have lifetime, that dominates. |
| 4652 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
| 4653 | assert(getLangOpts().ObjCAutoRefCount); |
| 4654 | |
| 4655 | switch (lifetime) { |
| 4656 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
| 4657 | |
| 4658 | // These are just bits as far as the runtime is concerned. |
| 4659 | case Qualifiers::OCL_ExplicitNone: |
| 4660 | case Qualifiers::OCL_Autoreleasing: |
| 4661 | return false; |
| 4662 | |
| 4663 | // Tell the runtime that this is ARC __weak, called by the |
| 4664 | // byref routines. |
| 4665 | case Qualifiers::OCL_Weak: |
| 4666 | // ARC __strong __block variables need to be retained. |
| 4667 | case Qualifiers::OCL_Strong: |
| 4668 | return true; |
| 4669 | } |
| 4670 | llvm_unreachable("fell out of lifetime switch!"); |
| 4671 | } |
| 4672 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
| 4673 | Ty->isObjCObjectPointerType()); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4674 | } |
| 4675 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4676 | bool ASTContext::getByrefLifetime(QualType Ty, |
| 4677 | Qualifiers::ObjCLifetime &LifeTime, |
| 4678 | bool &HasByrefExtendedLayout) const { |
| 4679 | |
| 4680 | if (!getLangOpts().ObjC1 || |
| 4681 | getLangOpts().getGC() != LangOptions::NonGC) |
| 4682 | return false; |
| 4683 | |
| 4684 | HasByrefExtendedLayout = false; |
Fariborz Jahanian | 34db84f | 2012-12-11 19:58:01 +0000 | [diff] [blame] | 4685 | if (Ty->isRecordType()) { |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4686 | HasByrefExtendedLayout = true; |
| 4687 | LifeTime = Qualifiers::OCL_None; |
| 4688 | } |
| 4689 | else if (getLangOpts().ObjCAutoRefCount) |
| 4690 | LifeTime = Ty.getObjCLifetime(); |
| 4691 | // MRR. |
| 4692 | else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 4693 | LifeTime = Qualifiers::OCL_ExplicitNone; |
| 4694 | else |
| 4695 | LifeTime = Qualifiers::OCL_None; |
| 4696 | return true; |
| 4697 | } |
| 4698 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4699 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 4700 | if (!ObjCInstanceTypeDecl) |
| 4701 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 4702 | getTranslationUnitDecl(), |
| 4703 | SourceLocation(), |
| 4704 | SourceLocation(), |
| 4705 | &Idents.get("instancetype"), |
| 4706 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 4707 | return ObjCInstanceTypeDecl; |
| 4708 | } |
| 4709 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4710 | // This returns true if a type has been typedefed to BOOL: |
| 4711 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 4712 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4713 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 4714 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 4715 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4716 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4717 | return false; |
| 4718 | } |
| 4719 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4720 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4721 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4722 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4723 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4724 | return CharUnits::Zero(); |
| 4725 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4726 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4728 | // Make all integer and enum types at least as large as an int |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4729 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4730 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4731 | // Treat arrays as pointers, since that's how they're passed in. |
| 4732 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4733 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4734 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | static inline |
| 4738 | std::string charUnitsToString(const CharUnits &CU) { |
| 4739 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4742 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4743 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4744 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4745 | std::string S; |
| 4746 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4747 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4748 | QualType BlockTy = |
| 4749 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4750 | // Encode result type. |
Fariborz Jahanian | 3d145f6 | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 4751 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 06cffc0 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 4752 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, |
| 4753 | BlockTy->getAs<FunctionType>()->getResultType(), |
| 4754 | S, true /*Extended*/); |
| 4755 | else |
| 4756 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), |
| 4757 | S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4758 | // Compute size of all parameters. |
| 4759 | // Start with computing size of a pointer in number of bytes. |
| 4760 | // FIXME: There might(should) be a better way of doing this computation! |
| 4761 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4762 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4763 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 4764 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4765 | E = Decl->param_end(); PI != E; ++PI) { |
| 4766 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4767 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 075a543 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 4768 | if (sz.isZero()) |
| 4769 | continue; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4770 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4771 | ParmOffset += sz; |
| 4772 | } |
| 4773 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4774 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4775 | // Block pointer and offset. |
| 4776 | S += "@?0"; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4777 | |
| 4778 | // Argument types. |
| 4779 | ParmOffset = PtrSize; |
| 4780 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 4781 | Decl->param_end(); PI != E; ++PI) { |
| 4782 | ParmVarDecl *PVDecl = *PI; |
| 4783 | QualType PType = PVDecl->getOriginalType(); |
| 4784 | if (const ArrayType *AT = |
| 4785 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4786 | // Use array's original type only if it has known number of |
| 4787 | // elements. |
| 4788 | if (!isa<ConstantArrayType>(AT)) |
| 4789 | PType = PVDecl->getType(); |
| 4790 | } else if (PType->isFunctionType()) |
| 4791 | PType = PVDecl->getType(); |
Fariborz Jahanian | 3d145f6 | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 4792 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 06cffc0 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 4793 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType, |
| 4794 | S, true /*Extended*/); |
| 4795 | else |
| 4796 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4797 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4798 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4799 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4800 | |
| 4801 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4802 | } |
| 4803 | |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4804 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4805 | std::string& S) { |
| 4806 | // Encode result type. |
| 4807 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4808 | CharUnits ParmOffset; |
| 4809 | // Compute size of all parameters. |
| 4810 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4811 | E = Decl->param_end(); PI != E; ++PI) { |
| 4812 | QualType PType = (*PI)->getType(); |
| 4813 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4814 | if (sz.isZero()) |
Fariborz Jahanian | 7e68ba5 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4815 | continue; |
| 4816 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4817 | assert (sz.isPositive() && |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4818 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4819 | ParmOffset += sz; |
| 4820 | } |
| 4821 | S += charUnitsToString(ParmOffset); |
| 4822 | ParmOffset = CharUnits::Zero(); |
| 4823 | |
| 4824 | // Argument types. |
| 4825 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4826 | E = Decl->param_end(); PI != E; ++PI) { |
| 4827 | ParmVarDecl *PVDecl = *PI; |
| 4828 | QualType PType = PVDecl->getOriginalType(); |
| 4829 | if (const ArrayType *AT = |
| 4830 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4831 | // Use array's original type only if it has known number of |
| 4832 | // elements. |
| 4833 | if (!isa<ConstantArrayType>(AT)) |
| 4834 | PType = PVDecl->getType(); |
| 4835 | } else if (PType->isFunctionType()) |
| 4836 | PType = PVDecl->getType(); |
| 4837 | getObjCEncodingForType(PType, S); |
| 4838 | S += charUnitsToString(ParmOffset); |
| 4839 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4840 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4841 | |
| 4842 | return false; |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4845 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 4846 | /// method parameter or return type. If Extended, include class names and |
| 4847 | /// block object types. |
| 4848 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 4849 | QualType T, std::string& S, |
| 4850 | bool Extended) const { |
| 4851 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 4852 | getObjCEncodingForTypeQualifier(QT, S); |
| 4853 | // Encode parameter type. |
| 4854 | getObjCEncodingForTypeImpl(T, S, true, true, 0, |
| 4855 | true /*OutermostType*/, |
| 4856 | false /*EncodingProperty*/, |
| 4857 | false /*StructField*/, |
| 4858 | Extended /*EncodeBlockParameters*/, |
| 4859 | Extended /*EncodeClassNames*/); |
| 4860 | } |
| 4861 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4862 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4863 | /// declaration. |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4864 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4865 | std::string& S, |
| 4866 | bool Extended) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4867 | // FIXME: This is not very efficient. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4868 | // Encode return type. |
| 4869 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 4870 | Decl->getResultType(), S, Extended); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4871 | // Compute size of all parameters. |
| 4872 | // Start with computing size of a pointer in number of bytes. |
| 4873 | // FIXME: There might(should) be a better way of doing this computation! |
| 4874 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4875 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4876 | // The first two arguments (self and _cmd) are pointers; account for |
| 4877 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4878 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4879 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4880 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4881 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4882 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4883 | if (sz.isZero()) |
Fariborz Jahanian | 7e68ba5 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4884 | continue; |
| 4885 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4886 | assert (sz.isPositive() && |
| 4887 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4888 | ParmOffset += sz; |
| 4889 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4890 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4891 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4892 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4893 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4894 | // Argument types. |
| 4895 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4896 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4897 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4898 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4899 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4900 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4901 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4902 | // Use array's original type only if it has known number of |
| 4903 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4904 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4905 | PType = PVDecl->getType(); |
| 4906 | } else if (PType->isFunctionType()) |
| 4907 | PType = PVDecl->getType(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4908 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 4909 | PType, S, Extended); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4910 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4911 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4912 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4913 | |
| 4914 | return false; |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4917 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4918 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4919 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4920 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4921 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4922 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4923 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4924 | /// encoded as single characters, followed by an identifier. Property types |
| 4925 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4926 | /// these attributes are defined by the following enumeration: |
| 4927 | /// @code |
| 4928 | /// enum PropertyAttributes { |
| 4929 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4930 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4931 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4932 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4933 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4934 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4935 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 0d4cb85 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 4936 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4937 | /// kPropertyWeak = 'W' // 'weak' property |
| 4938 | /// kPropertyStrong = 'P' // property GC'able |
| 4939 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4940 | /// }; |
| 4941 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4942 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4943 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4944 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4945 | // Collect information from the property implementation decl(s). |
| 4946 | bool Dynamic = false; |
| 4947 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4948 | |
| 4949 | // FIXME: Duplicated code due to poor abstraction. |
| 4950 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4951 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4952 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4953 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4954 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4955 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4956 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4957 | if (PID->getPropertyDecl() == PD) { |
| 4958 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4959 | Dynamic = true; |
| 4960 | } else { |
| 4961 | SynthesizePID = PID; |
| 4962 | } |
| 4963 | } |
| 4964 | } |
| 4965 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4966 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4967 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4968 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4969 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4970 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4971 | if (PID->getPropertyDecl() == PD) { |
| 4972 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4973 | Dynamic = true; |
| 4974 | } else { |
| 4975 | SynthesizePID = PID; |
| 4976 | } |
| 4977 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4978 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4979 | } |
| 4980 | } |
| 4981 | |
| 4982 | // FIXME: This is not very efficient. |
| 4983 | S = "T"; |
| 4984 | |
| 4985 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4986 | // GCC has some special rules regarding encoding of properties which |
| 4987 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4988 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4989 | true /* outermost type */, |
| 4990 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4991 | |
| 4992 | if (PD->isReadOnly()) { |
| 4993 | S += ",R"; |
Nico Weber | d7ceab3 | 2013-05-08 23:47:40 +0000 | [diff] [blame] | 4994 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) |
| 4995 | S += ",C"; |
| 4996 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) |
| 4997 | S += ",&"; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4998 | } else { |
| 4999 | switch (PD->getSetterKind()) { |
| 5000 | case ObjCPropertyDecl::Assign: break; |
| 5001 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5002 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 3a02b44 | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 5003 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5004 | } |
| 5005 | } |
| 5006 | |
| 5007 | // It really isn't clear at all what this means, since properties |
| 5008 | // are "dynamic by default". |
| 5009 | if (Dynamic) |
| 5010 | S += ",D"; |
| 5011 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 5012 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 5013 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5014 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5015 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 5016 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5017 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
| 5020 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 5021 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5022 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | if (SynthesizePID) { |
| 5026 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 5027 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 5028 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
| 5031 | // FIXME: OBJCGC: weak & strong |
| 5032 | } |
| 5033 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5034 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5035 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 5036 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5037 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 5038 | /// |
| 5039 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 5040 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5041 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5042 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5043 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5044 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5045 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5046 | PointeeTy = IntTy; |
| 5047 | } |
| 5048 | } |
| 5049 | } |
| 5050 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5051 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5052 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5053 | // We follow the behavior of gcc, expanding structures which are |
| 5054 | // directly pointed to, and expanding embedded structures. Note that |
| 5055 | // these rules are sufficient to prevent recursive encoding of the |
| 5056 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5057 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 5058 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5061 | static char getObjCEncodingForPrimitiveKind(const ASTContext *C, |
| 5062 | BuiltinType::Kind kind) { |
| 5063 | switch (kind) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5064 | case BuiltinType::Void: return 'v'; |
| 5065 | case BuiltinType::Bool: return 'B'; |
| 5066 | case BuiltinType::Char_U: |
| 5067 | case BuiltinType::UChar: return 'C'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5068 | case BuiltinType::Char16: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5069 | case BuiltinType::UShort: return 'S'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5070 | case BuiltinType::Char32: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5071 | case BuiltinType::UInt: return 'I'; |
| 5072 | case BuiltinType::ULong: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5073 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5074 | case BuiltinType::UInt128: return 'T'; |
| 5075 | case BuiltinType::ULongLong: return 'Q'; |
| 5076 | case BuiltinType::Char_S: |
| 5077 | case BuiltinType::SChar: return 'c'; |
| 5078 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 5079 | case BuiltinType::WChar_S: |
| 5080 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5081 | case BuiltinType::Int: return 'i'; |
| 5082 | case BuiltinType::Long: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5083 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5084 | case BuiltinType::LongLong: return 'q'; |
| 5085 | case BuiltinType::Int128: return 't'; |
| 5086 | case BuiltinType::Float: return 'f'; |
| 5087 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 5088 | case BuiltinType::LongDouble: return 'D'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5089 | case BuiltinType::NullPtr: return '*'; // like char* |
| 5090 | |
| 5091 | case BuiltinType::Half: |
| 5092 | // FIXME: potentially need @encodes for these! |
| 5093 | return ' '; |
| 5094 | |
| 5095 | case BuiltinType::ObjCId: |
| 5096 | case BuiltinType::ObjCClass: |
| 5097 | case BuiltinType::ObjCSel: |
| 5098 | llvm_unreachable("@encoding ObjC primitive type"); |
| 5099 | |
| 5100 | // OpenCL and placeholder types don't need @encodings. |
| 5101 | case BuiltinType::OCLImage1d: |
| 5102 | case BuiltinType::OCLImage1dArray: |
| 5103 | case BuiltinType::OCLImage1dBuffer: |
| 5104 | case BuiltinType::OCLImage2d: |
| 5105 | case BuiltinType::OCLImage2dArray: |
| 5106 | case BuiltinType::OCLImage3d: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5107 | case BuiltinType::OCLEvent: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5108 | case BuiltinType::OCLSampler: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5109 | case BuiltinType::Dependent: |
| 5110 | #define BUILTIN_TYPE(KIND, ID) |
| 5111 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
| 5112 | case BuiltinType::KIND: |
| 5113 | #include "clang/AST/BuiltinTypes.def" |
| 5114 | llvm_unreachable("invalid builtin type for @encode"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5115 | } |
David Blaikie | 719e53f | 2013-01-09 17:48:41 +0000 | [diff] [blame] | 5116 | llvm_unreachable("invalid BuiltinType::Kind value"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5119 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 5120 | EnumDecl *Enum = ET->getDecl(); |
| 5121 | |
| 5122 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 5123 | if (!Enum->isFixed()) |
| 5124 | return 'i'; |
| 5125 | |
| 5126 | // The encoding of a fixed enum type matches its fixed underlying type. |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5127 | const BuiltinType *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
| 5128 | return getObjCEncodingForPrimitiveKind(C, BT->getKind()); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5131 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5132 | QualType T, const FieldDecl *FD) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5133 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 5134 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5135 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 5136 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 5137 | // then the offset (in bits) of the first element, then the type of the |
| 5138 | // bitfield, then the size in bits. For example, in this structure: |
| 5139 | // |
| 5140 | // struct |
| 5141 | // { |
| 5142 | // int integer; |
| 5143 | // int flags:2; |
| 5144 | // }; |
| 5145 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 5146 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 5147 | // information is not especially sensible, but we're stuck with it for |
| 5148 | // compatibility with GCC, although providing it breaks anything that |
| 5149 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 5150 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5151 | const RecordDecl *RD = FD->getParent(); |
| 5152 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | 8290574 | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 5153 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5154 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 5155 | S += ObjCEncodingForEnumType(Ctx, ET); |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5156 | else { |
| 5157 | const BuiltinType *BT = T->castAs<BuiltinType>(); |
| 5158 | S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind()); |
| 5159 | } |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5160 | } |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5161 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 5162 | } |
| 5163 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 5164 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5165 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 5166 | bool ExpandPointedToStructures, |
| 5167 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 5168 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 5169 | bool OutermostType, |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5170 | bool EncodingProperty, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5171 | bool StructField, |
| 5172 | bool EncodeBlockParameters, |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5173 | bool EncodeClassNames, |
| 5174 | bool EncodePointerToObjCTypedef) const { |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5175 | CanQualType CT = getCanonicalType(T); |
| 5176 | switch (CT->getTypeClass()) { |
| 5177 | case Type::Builtin: |
| 5178 | case Type::Enum: |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5179 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5180 | return EncodeBitField(this, S, T, FD); |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5181 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CT)) |
| 5182 | S += getObjCEncodingForPrimitiveKind(this, BT->getKind()); |
| 5183 | else |
| 5184 | S += ObjCEncodingForEnumType(this, cast<EnumType>(CT)); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5185 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5186 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5187 | case Type::Complex: { |
| 5188 | const ComplexType *CT = T->castAs<ComplexType>(); |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 5189 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5190 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 5191 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5192 | return; |
| 5193 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5194 | |
| 5195 | case Type::Atomic: { |
| 5196 | const AtomicType *AT = T->castAs<AtomicType>(); |
| 5197 | S += 'A'; |
| 5198 | getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, 0, |
| 5199 | false, false); |
| 5200 | return; |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 5201 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5202 | |
| 5203 | // encoding for pointer or reference types. |
| 5204 | case Type::Pointer: |
| 5205 | case Type::LValueReference: |
| 5206 | case Type::RValueReference: { |
| 5207 | QualType PointeeTy; |
| 5208 | if (isa<PointerType>(CT)) { |
| 5209 | const PointerType *PT = T->castAs<PointerType>(); |
| 5210 | if (PT->isObjCSelType()) { |
| 5211 | S += ':'; |
| 5212 | return; |
| 5213 | } |
| 5214 | PointeeTy = PT->getPointeeType(); |
| 5215 | } else { |
| 5216 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
| 5217 | } |
| 5218 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5219 | bool isReadOnly = false; |
| 5220 | // For historical/compatibility reasons, the read-only qualifier of the |
| 5221 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 5222 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5223 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 5224 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5225 | if (OutermostType && T.isConstQualified()) { |
| 5226 | isReadOnly = true; |
| 5227 | S += 'r'; |
| 5228 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 5229 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5230 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5231 | while (P->getAs<PointerType>()) |
| 5232 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5233 | if (P.isConstQualified()) { |
| 5234 | isReadOnly = true; |
| 5235 | S += 'r'; |
| 5236 | } |
| 5237 | } |
| 5238 | if (isReadOnly) { |
| 5239 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 5240 | // combinations need to be rearranged. |
| 5241 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5242 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 5243 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5244 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5245 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5246 | if (PointeeTy->isCharType()) { |
| 5247 | // char pointer types should be encoded as '*' unless it is a |
| 5248 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 5249 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5250 | S += '*'; |
| 5251 | return; |
| 5252 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5253 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 5254 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 5255 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 5256 | S += '#'; |
| 5257 | return; |
| 5258 | } |
| 5259 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 5260 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 5261 | S += '@'; |
| 5262 | return; |
| 5263 | } |
| 5264 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5265 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5266 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5267 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 5268 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5269 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5270 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5271 | return; |
| 5272 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5273 | |
| 5274 | case Type::ConstantArray: |
| 5275 | case Type::IncompleteArray: |
| 5276 | case Type::VariableArray: { |
| 5277 | const ArrayType *AT = cast<ArrayType>(CT); |
| 5278 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5279 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5280 | // Incomplete arrays are encoded as a pointer to the array element. |
| 5281 | S += '^'; |
| 5282 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5283 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5284 | false, ExpandStructures, FD); |
| 5285 | } else { |
| 5286 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5287 | |
Fariborz Jahanian | 48eff6c | 2013-06-04 16:04:37 +0000 | [diff] [blame] | 5288 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 5289 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 5290 | else { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5291 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5292 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 5293 | "Unknown array type!"); |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5294 | S += '0'; |
| 5295 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5296 | |
| 5297 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5298 | false, ExpandStructures, FD); |
| 5299 | S += ']'; |
| 5300 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5301 | return; |
| 5302 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5303 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5304 | case Type::FunctionNoProto: |
| 5305 | case Type::FunctionProto: |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 5306 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5307 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5308 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5309 | case Type::Record: { |
| 5310 | RecordDecl *RDecl = cast<RecordType>(CT)->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5311 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5312 | // Anonymous structures print as '?' |
| 5313 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 5314 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5315 | if (ClassTemplateSpecializationDecl *Spec |
| 5316 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 5317 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Benjamin Kramer | 5eada84 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 5318 | llvm::raw_string_ostream OS(S); |
| 5319 | TemplateSpecializationType::PrintTemplateArgumentList(OS, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5320 | TemplateArgs.data(), |
| 5321 | TemplateArgs.size(), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 5322 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5323 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5324 | } else { |
| 5325 | S += '?'; |
| 5326 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 5327 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5328 | S += '='; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5329 | if (!RDecl->isUnion()) { |
| 5330 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 5331 | } else { |
| 5332 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 5333 | FieldEnd = RDecl->field_end(); |
| 5334 | Field != FieldEnd; ++Field) { |
| 5335 | if (FD) { |
| 5336 | S += '"'; |
| 5337 | S += Field->getNameAsString(); |
| 5338 | S += '"'; |
| 5339 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5340 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5341 | // Special case bit-fields. |
| 5342 | if (Field->isBitField()) { |
| 5343 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5344 | *Field); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5345 | } else { |
| 5346 | QualType qt = Field->getType(); |
| 5347 | getLegacyIntegralTypeEncoding(qt); |
| 5348 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 5349 | FD, /*OutermostType*/false, |
| 5350 | /*EncodingProperty*/false, |
| 5351 | /*StructField*/true); |
| 5352 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5353 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5354 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 5355 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5356 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5357 | return; |
| 5358 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5359 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5360 | case Type::BlockPointer: { |
| 5361 | const BlockPointerType *BT = T->castAs<BlockPointerType>(); |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 5362 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5363 | if (EncodeBlockParameters) { |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5364 | const FunctionType *FT = BT->getPointeeType()->castAs<FunctionType>(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5365 | |
| 5366 | S += '<'; |
| 5367 | // Block return type |
| 5368 | getObjCEncodingForTypeImpl(FT->getResultType(), S, |
| 5369 | ExpandPointedToStructures, ExpandStructures, |
| 5370 | FD, |
| 5371 | false /* OutermostType */, |
| 5372 | EncodingProperty, |
| 5373 | false /* StructField */, |
| 5374 | EncodeBlockParameters, |
| 5375 | EncodeClassNames); |
| 5376 | // Block self |
| 5377 | S += "@?"; |
| 5378 | // Block parameters |
| 5379 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 5380 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(), |
| 5381 | E = FPT->arg_type_end(); I && (I != E); ++I) { |
| 5382 | getObjCEncodingForTypeImpl(*I, S, |
| 5383 | ExpandPointedToStructures, |
| 5384 | ExpandStructures, |
| 5385 | FD, |
| 5386 | false /* OutermostType */, |
| 5387 | EncodingProperty, |
| 5388 | false /* StructField */, |
| 5389 | EncodeBlockParameters, |
| 5390 | EncodeClassNames); |
| 5391 | } |
| 5392 | } |
| 5393 | S += '>'; |
| 5394 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5395 | return; |
| 5396 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5397 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5398 | case Type::ObjCObject: |
| 5399 | case Type::ObjCInterface: { |
| 5400 | // Ignore protocol qualifiers when mangling at this level. |
| 5401 | T = T->castAs<ObjCObjectType>()->getBaseType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5402 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5403 | // The assumption seems to be that this assert will succeed |
| 5404 | // because nested levels will have filtered out 'id' and 'Class'. |
| 5405 | const ObjCInterfaceType *OIT = T->castAs<ObjCInterfaceType>(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5406 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5407 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5408 | S += '{'; |
| 5409 | const IdentifierInfo *II = OI->getIdentifier(); |
| 5410 | S += II->getName(); |
| 5411 | S += '='; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5412 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5413 | DeepCollectObjCIvars(OI, true, Ivars); |
| 5414 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5415 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5416 | if (Field->isBitField()) |
| 5417 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5418 | else |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5419 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD, |
| 5420 | false, false, false, false, false, |
| 5421 | EncodePointerToObjCTypedef); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5422 | } |
| 5423 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5424 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5425 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5426 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5427 | case Type::ObjCObjectPointer: { |
| 5428 | const ObjCObjectPointerType *OPT = T->castAs<ObjCObjectPointerType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5429 | if (OPT->isObjCIdType()) { |
| 5430 | S += '@'; |
| 5431 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5432 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5433 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5434 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 5435 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 5436 | // Since this is a binary compatibility issue, need to consult with runtime |
| 5437 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5438 | S += '#'; |
| 5439 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5440 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5441 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5442 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5443 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5444 | ExpandPointedToStructures, |
| 5445 | ExpandStructures, FD); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5446 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5447 | // Note that we do extended encoding of protocol qualifer list |
| 5448 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5449 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5450 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 5451 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5452 | S += '<'; |
| 5453 | S += (*I)->getNameAsString(); |
| 5454 | S += '>'; |
| 5455 | } |
| 5456 | S += '"'; |
| 5457 | } |
| 5458 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5459 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5460 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5461 | QualType PointeeTy = OPT->getPointeeType(); |
| 5462 | if (!EncodingProperty && |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5463 | isa<TypedefType>(PointeeTy.getTypePtr()) && |
| 5464 | !EncodePointerToObjCTypedef) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5465 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5466 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5467 | // {...}; |
| 5468 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5469 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 5470 | false, ExpandPointedToStructures, |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5471 | NULL, |
| 5472 | false, false, false, false, false, |
| 5473 | /*EncodePointerToObjCTypedef*/true); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5474 | return; |
| 5475 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5476 | |
| 5477 | S += '@'; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5478 | if (OPT->getInterfaceDecl() && |
| 5479 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5480 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5481 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5482 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 5483 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5484 | S += '<'; |
| 5485 | S += (*I)->getNameAsString(); |
| 5486 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5487 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5488 | S += '"'; |
| 5489 | } |
| 5490 | return; |
| 5491 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5492 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 5493 | // gcc just blithely ignores member pointers. |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5494 | // FIXME: we shoul do better than that. 'M' is available. |
| 5495 | case Type::MemberPointer: |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 5496 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5497 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5498 | case Type::Vector: |
| 5499 | case Type::ExtVector: |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5500 | // This matches gcc's encoding, even though technically it is |
| 5501 | // insufficient. |
| 5502 | // FIXME. We should do a better job than gcc. |
| 5503 | return; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5504 | |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5505 | case Type::Auto: |
| 5506 | // We could see an undeduced auto type here during error recovery. |
| 5507 | // Just ignore it. |
| 5508 | return; |
| 5509 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5510 | #define ABSTRACT_TYPE(KIND, BASE) |
| 5511 | #define TYPE(KIND, BASE) |
| 5512 | #define DEPENDENT_TYPE(KIND, BASE) \ |
| 5513 | case Type::KIND: |
| 5514 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
| 5515 | case Type::KIND: |
| 5516 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
| 5517 | case Type::KIND: |
| 5518 | #include "clang/AST/TypeNodes.def" |
| 5519 | llvm_unreachable("@encode for dependent type!"); |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5520 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5521 | llvm_unreachable("bad type kind!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5522 | } |
| 5523 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5524 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 5525 | std::string &S, |
| 5526 | const FieldDecl *FD, |
| 5527 | bool includeVBases) const { |
| 5528 | assert(RDecl && "Expected non-null RecordDecl"); |
| 5529 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 5530 | if (!RDecl->getDefinition()) |
| 5531 | return; |
| 5532 | |
| 5533 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 5534 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 5535 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 5536 | |
| 5537 | if (CXXRec) { |
| 5538 | for (CXXRecordDecl::base_class_iterator |
| 5539 | BI = CXXRec->bases_begin(), |
| 5540 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 5541 | if (!BI->isVirtual()) { |
| 5542 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5543 | if (base->isEmpty()) |
| 5544 | continue; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5545 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5546 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 5547 | std::make_pair(offs, base)); |
| 5548 | } |
| 5549 | } |
| 5550 | } |
| 5551 | |
| 5552 | unsigned i = 0; |
| 5553 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 5554 | FieldEnd = RDecl->field_end(); |
| 5555 | Field != FieldEnd; ++Field, ++i) { |
| 5556 | uint64_t offs = layout.getFieldOffset(i); |
| 5557 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5558 | std::make_pair(offs, *Field)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5559 | } |
| 5560 | |
| 5561 | if (CXXRec && includeVBases) { |
| 5562 | for (CXXRecordDecl::base_class_iterator |
| 5563 | BI = CXXRec->vbases_begin(), |
| 5564 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 5565 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5566 | if (base->isEmpty()) |
| 5567 | continue; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5568 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Argyrios Kyrtzidis | 19aa860 | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 5569 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 5570 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 5571 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5572 | } |
| 5573 | } |
| 5574 | |
| 5575 | CharUnits size; |
| 5576 | if (CXXRec) { |
| 5577 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 5578 | } else { |
| 5579 | size = layout.getSize(); |
| 5580 | } |
| 5581 | |
| 5582 | uint64_t CurOffs = 0; |
| 5583 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 5584 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 5585 | |
Douglas Gregor | 58db7a5 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 5586 | if (CXXRec && CXXRec->isDynamicClass() && |
| 5587 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5588 | if (FD) { |
| 5589 | S += "\"_vptr$"; |
| 5590 | std::string recname = CXXRec->getNameAsString(); |
| 5591 | if (recname.empty()) recname = "?"; |
| 5592 | S += recname; |
| 5593 | S += '"'; |
| 5594 | } |
| 5595 | S += "^^?"; |
| 5596 | CurOffs += getTypeSize(VoidPtrTy); |
| 5597 | } |
| 5598 | |
| 5599 | if (!RDecl->hasFlexibleArrayMember()) { |
| 5600 | // Mark the end of the structure. |
| 5601 | uint64_t offs = toBits(size); |
| 5602 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 5603 | std::make_pair(offs, (NamedDecl*)0)); |
| 5604 | } |
| 5605 | |
| 5606 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 5607 | assert(CurOffs <= CurLayObj->first); |
| 5608 | |
| 5609 | if (CurOffs < CurLayObj->first) { |
| 5610 | uint64_t padding = CurLayObj->first - CurOffs; |
| 5611 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 5612 | // packing/alignment of members is different that normal, in which case |
| 5613 | // the encoding will be out-of-sync with the real layout. |
| 5614 | // If the runtime switches to just consider the size of types without |
| 5615 | // taking into account alignment, we could make padding explicit in the |
| 5616 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 5617 | // longer then though. |
| 5618 | CurOffs += padding; |
| 5619 | } |
| 5620 | |
| 5621 | NamedDecl *dcl = CurLayObj->second; |
| 5622 | if (dcl == 0) |
| 5623 | break; // reached end of structure. |
| 5624 | |
| 5625 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 5626 | // We expand the bases without their virtual bases since those are going |
| 5627 | // in the initial structure. Note that this differs from gcc which |
| 5628 | // expands virtual bases each time one is encountered in the hierarchy, |
| 5629 | // making the encoding type bigger than it really is. |
| 5630 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5631 | assert(!base->isEmpty()); |
| 5632 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5633 | } else { |
| 5634 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 5635 | if (FD) { |
| 5636 | S += '"'; |
| 5637 | S += field->getNameAsString(); |
| 5638 | S += '"'; |
| 5639 | } |
| 5640 | |
| 5641 | if (field->isBitField()) { |
| 5642 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5643 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5644 | } else { |
| 5645 | QualType qt = field->getType(); |
| 5646 | getLegacyIntegralTypeEncoding(qt); |
| 5647 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 5648 | /*OutermostType*/false, |
| 5649 | /*EncodingProperty*/false, |
| 5650 | /*StructField*/true); |
| 5651 | CurOffs += getTypeSize(field->getType()); |
| 5652 | } |
| 5653 | } |
| 5654 | } |
| 5655 | } |
| 5656 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5657 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 5658 | std::string& S) const { |
| 5659 | if (QT & Decl::OBJC_TQ_In) |
| 5660 | S += 'n'; |
| 5661 | if (QT & Decl::OBJC_TQ_Inout) |
| 5662 | S += 'N'; |
| 5663 | if (QT & Decl::OBJC_TQ_Out) |
| 5664 | S += 'o'; |
| 5665 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 5666 | S += 'O'; |
| 5667 | if (QT & Decl::OBJC_TQ_Byref) |
| 5668 | S += 'R'; |
| 5669 | if (QT & Decl::OBJC_TQ_Oneway) |
| 5670 | S += 'V'; |
| 5671 | } |
| 5672 | |
Douglas Gregor | 4dfd02a | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 5673 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 5674 | if (!ObjCIdDecl) { |
| 5675 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 5676 | T = getObjCObjectPointerType(T); |
| 5677 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 5678 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5679 | getTranslationUnitDecl(), |
| 5680 | SourceLocation(), SourceLocation(), |
| 5681 | &Idents.get("id"), IdInfo); |
| 5682 | } |
| 5683 | |
| 5684 | return ObjCIdDecl; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 5685 | } |
| 5686 | |
Douglas Gregor | 7a27ea5 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 5687 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 5688 | if (!ObjCSelDecl) { |
| 5689 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 5690 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 5691 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5692 | getTranslationUnitDecl(), |
| 5693 | SourceLocation(), SourceLocation(), |
| 5694 | &Idents.get("SEL"), SelInfo); |
| 5695 | } |
| 5696 | return ObjCSelDecl; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 5697 | } |
| 5698 | |
Douglas Gregor | 79d6726 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 5699 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 5700 | if (!ObjCClassDecl) { |
| 5701 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 5702 | T = getObjCObjectPointerType(T); |
| 5703 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 5704 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5705 | getTranslationUnitDecl(), |
| 5706 | SourceLocation(), SourceLocation(), |
| 5707 | &Idents.get("Class"), ClassInfo); |
| 5708 | } |
| 5709 | |
| 5710 | return ObjCClassDecl; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 5713 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 5714 | if (!ObjCProtocolClassDecl) { |
| 5715 | ObjCProtocolClassDecl |
| 5716 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 5717 | SourceLocation(), |
| 5718 | &Idents.get("Protocol"), |
| 5719 | /*PrevDecl=*/0, |
| 5720 | SourceLocation(), true); |
| 5721 | } |
| 5722 | |
| 5723 | return ObjCProtocolClassDecl; |
| 5724 | } |
| 5725 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5726 | //===----------------------------------------------------------------------===// |
| 5727 | // __builtin_va_list Construction Functions |
| 5728 | //===----------------------------------------------------------------------===// |
| 5729 | |
| 5730 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5731 | // typedef char* __builtin_va_list; |
| 5732 | QualType CharPtrType = Context->getPointerType(Context->CharTy); |
| 5733 | TypeSourceInfo *TInfo |
| 5734 | = Context->getTrivialTypeSourceInfo(CharPtrType); |
| 5735 | |
| 5736 | TypedefDecl *VaListTypeDecl |
| 5737 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5738 | Context->getTranslationUnitDecl(), |
| 5739 | SourceLocation(), SourceLocation(), |
| 5740 | &Context->Idents.get("__builtin_va_list"), |
| 5741 | TInfo); |
| 5742 | return VaListTypeDecl; |
| 5743 | } |
| 5744 | |
| 5745 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5746 | // typedef void* __builtin_va_list; |
| 5747 | QualType VoidPtrType = Context->getPointerType(Context->VoidTy); |
| 5748 | TypeSourceInfo *TInfo |
| 5749 | = Context->getTrivialTypeSourceInfo(VoidPtrType); |
| 5750 | |
| 5751 | TypedefDecl *VaListTypeDecl |
| 5752 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5753 | Context->getTranslationUnitDecl(), |
| 5754 | SourceLocation(), SourceLocation(), |
| 5755 | &Context->Idents.get("__builtin_va_list"), |
| 5756 | TInfo); |
| 5757 | return VaListTypeDecl; |
| 5758 | } |
| 5759 | |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5760 | static TypedefDecl * |
| 5761 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5762 | RecordDecl *VaListTagDecl; |
| 5763 | if (Context->getLangOpts().CPlusPlus) { |
| 5764 | // namespace std { struct __va_list { |
| 5765 | NamespaceDecl *NS; |
| 5766 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 5767 | Context->getTranslationUnitDecl(), |
| 5768 | /*Inline*/false, SourceLocation(), |
| 5769 | SourceLocation(), &Context->Idents.get("std"), |
| 5770 | /*PrevDecl*/0); |
| 5771 | |
| 5772 | VaListTagDecl = CXXRecordDecl::Create(*Context, TTK_Struct, |
| 5773 | Context->getTranslationUnitDecl(), |
| 5774 | SourceLocation(), SourceLocation(), |
| 5775 | &Context->Idents.get("__va_list")); |
| 5776 | VaListTagDecl->setDeclContext(NS); |
| 5777 | } else { |
| 5778 | // struct __va_list |
| 5779 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5780 | Context->getTranslationUnitDecl(), |
| 5781 | &Context->Idents.get("__va_list")); |
| 5782 | } |
| 5783 | |
| 5784 | VaListTagDecl->startDefinition(); |
| 5785 | |
| 5786 | const size_t NumFields = 5; |
| 5787 | QualType FieldTypes[NumFields]; |
| 5788 | const char *FieldNames[NumFields]; |
| 5789 | |
| 5790 | // void *__stack; |
| 5791 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
| 5792 | FieldNames[0] = "__stack"; |
| 5793 | |
| 5794 | // void *__gr_top; |
| 5795 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
| 5796 | FieldNames[1] = "__gr_top"; |
| 5797 | |
| 5798 | // void *__vr_top; |
| 5799 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5800 | FieldNames[2] = "__vr_top"; |
| 5801 | |
| 5802 | // int __gr_offs; |
| 5803 | FieldTypes[3] = Context->IntTy; |
| 5804 | FieldNames[3] = "__gr_offs"; |
| 5805 | |
| 5806 | // int __vr_offs; |
| 5807 | FieldTypes[4] = Context->IntTy; |
| 5808 | FieldNames[4] = "__vr_offs"; |
| 5809 | |
| 5810 | // Create fields |
| 5811 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5812 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5813 | VaListTagDecl, |
| 5814 | SourceLocation(), |
| 5815 | SourceLocation(), |
| 5816 | &Context->Idents.get(FieldNames[i]), |
| 5817 | FieldTypes[i], /*TInfo=*/0, |
| 5818 | /*BitWidth=*/0, |
| 5819 | /*Mutable=*/false, |
| 5820 | ICIS_NoInit); |
| 5821 | Field->setAccess(AS_public); |
| 5822 | VaListTagDecl->addDecl(Field); |
| 5823 | } |
| 5824 | VaListTagDecl->completeDefinition(); |
| 5825 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 5826 | Context->VaListTagTy = VaListTagType; |
| 5827 | |
| 5828 | // } __builtin_va_list; |
| 5829 | TypedefDecl *VaListTypedefDecl |
| 5830 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5831 | Context->getTranslationUnitDecl(), |
| 5832 | SourceLocation(), SourceLocation(), |
| 5833 | &Context->Idents.get("__builtin_va_list"), |
| 5834 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5835 | |
| 5836 | return VaListTypedefDecl; |
| 5837 | } |
| 5838 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5839 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5840 | // typedef struct __va_list_tag { |
| 5841 | RecordDecl *VaListTagDecl; |
| 5842 | |
| 5843 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5844 | Context->getTranslationUnitDecl(), |
| 5845 | &Context->Idents.get("__va_list_tag")); |
| 5846 | VaListTagDecl->startDefinition(); |
| 5847 | |
| 5848 | const size_t NumFields = 5; |
| 5849 | QualType FieldTypes[NumFields]; |
| 5850 | const char *FieldNames[NumFields]; |
| 5851 | |
| 5852 | // unsigned char gpr; |
| 5853 | FieldTypes[0] = Context->UnsignedCharTy; |
| 5854 | FieldNames[0] = "gpr"; |
| 5855 | |
| 5856 | // unsigned char fpr; |
| 5857 | FieldTypes[1] = Context->UnsignedCharTy; |
| 5858 | FieldNames[1] = "fpr"; |
| 5859 | |
| 5860 | // unsigned short reserved; |
| 5861 | FieldTypes[2] = Context->UnsignedShortTy; |
| 5862 | FieldNames[2] = "reserved"; |
| 5863 | |
| 5864 | // void* overflow_arg_area; |
| 5865 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5866 | FieldNames[3] = "overflow_arg_area"; |
| 5867 | |
| 5868 | // void* reg_save_area; |
| 5869 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 5870 | FieldNames[4] = "reg_save_area"; |
| 5871 | |
| 5872 | // Create fields |
| 5873 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5874 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 5875 | SourceLocation(), |
| 5876 | SourceLocation(), |
| 5877 | &Context->Idents.get(FieldNames[i]), |
| 5878 | FieldTypes[i], /*TInfo=*/0, |
| 5879 | /*BitWidth=*/0, |
| 5880 | /*Mutable=*/false, |
| 5881 | ICIS_NoInit); |
| 5882 | Field->setAccess(AS_public); |
| 5883 | VaListTagDecl->addDecl(Field); |
| 5884 | } |
| 5885 | VaListTagDecl->completeDefinition(); |
| 5886 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5887 | Context->VaListTagTy = VaListTagType; |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5888 | |
| 5889 | // } __va_list_tag; |
| 5890 | TypedefDecl *VaListTagTypedefDecl |
| 5891 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5892 | Context->getTranslationUnitDecl(), |
| 5893 | SourceLocation(), SourceLocation(), |
| 5894 | &Context->Idents.get("__va_list_tag"), |
| 5895 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5896 | QualType VaListTagTypedefType = |
| 5897 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5898 | |
| 5899 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5900 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5901 | QualType VaListTagArrayType |
| 5902 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5903 | Size, ArrayType::Normal, 0); |
| 5904 | TypeSourceInfo *TInfo |
| 5905 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5906 | TypedefDecl *VaListTypedefDecl |
| 5907 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5908 | Context->getTranslationUnitDecl(), |
| 5909 | SourceLocation(), SourceLocation(), |
| 5910 | &Context->Idents.get("__builtin_va_list"), |
| 5911 | TInfo); |
| 5912 | |
| 5913 | return VaListTypedefDecl; |
| 5914 | } |
| 5915 | |
| 5916 | static TypedefDecl * |
| 5917 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5918 | // typedef struct __va_list_tag { |
| 5919 | RecordDecl *VaListTagDecl; |
| 5920 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5921 | Context->getTranslationUnitDecl(), |
| 5922 | &Context->Idents.get("__va_list_tag")); |
| 5923 | VaListTagDecl->startDefinition(); |
| 5924 | |
| 5925 | const size_t NumFields = 4; |
| 5926 | QualType FieldTypes[NumFields]; |
| 5927 | const char *FieldNames[NumFields]; |
| 5928 | |
| 5929 | // unsigned gp_offset; |
| 5930 | FieldTypes[0] = Context->UnsignedIntTy; |
| 5931 | FieldNames[0] = "gp_offset"; |
| 5932 | |
| 5933 | // unsigned fp_offset; |
| 5934 | FieldTypes[1] = Context->UnsignedIntTy; |
| 5935 | FieldNames[1] = "fp_offset"; |
| 5936 | |
| 5937 | // void* overflow_arg_area; |
| 5938 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5939 | FieldNames[2] = "overflow_arg_area"; |
| 5940 | |
| 5941 | // void* reg_save_area; |
| 5942 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5943 | FieldNames[3] = "reg_save_area"; |
| 5944 | |
| 5945 | // Create fields |
| 5946 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5947 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5948 | VaListTagDecl, |
| 5949 | SourceLocation(), |
| 5950 | SourceLocation(), |
| 5951 | &Context->Idents.get(FieldNames[i]), |
| 5952 | FieldTypes[i], /*TInfo=*/0, |
| 5953 | /*BitWidth=*/0, |
| 5954 | /*Mutable=*/false, |
| 5955 | ICIS_NoInit); |
| 5956 | Field->setAccess(AS_public); |
| 5957 | VaListTagDecl->addDecl(Field); |
| 5958 | } |
| 5959 | VaListTagDecl->completeDefinition(); |
| 5960 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5961 | Context->VaListTagTy = VaListTagType; |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5962 | |
| 5963 | // } __va_list_tag; |
| 5964 | TypedefDecl *VaListTagTypedefDecl |
| 5965 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5966 | Context->getTranslationUnitDecl(), |
| 5967 | SourceLocation(), SourceLocation(), |
| 5968 | &Context->Idents.get("__va_list_tag"), |
| 5969 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5970 | QualType VaListTagTypedefType = |
| 5971 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5972 | |
| 5973 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5974 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5975 | QualType VaListTagArrayType |
| 5976 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5977 | Size, ArrayType::Normal,0); |
| 5978 | TypeSourceInfo *TInfo |
| 5979 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5980 | TypedefDecl *VaListTypedefDecl |
| 5981 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5982 | Context->getTranslationUnitDecl(), |
| 5983 | SourceLocation(), SourceLocation(), |
| 5984 | &Context->Idents.get("__builtin_va_list"), |
| 5985 | TInfo); |
| 5986 | |
| 5987 | return VaListTypedefDecl; |
| 5988 | } |
| 5989 | |
| 5990 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5991 | // typedef int __builtin_va_list[4]; |
| 5992 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
| 5993 | QualType IntArrayType |
| 5994 | = Context->getConstantArrayType(Context->IntTy, |
| 5995 | Size, ArrayType::Normal, 0); |
| 5996 | TypedefDecl *VaListTypedefDecl |
| 5997 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5998 | Context->getTranslationUnitDecl(), |
| 5999 | SourceLocation(), SourceLocation(), |
| 6000 | &Context->Idents.get("__builtin_va_list"), |
| 6001 | Context->getTrivialTypeSourceInfo(IntArrayType)); |
| 6002 | |
| 6003 | return VaListTypedefDecl; |
| 6004 | } |
| 6005 | |
Logan Chien | eae5a820 | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6006 | static TypedefDecl * |
| 6007 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
| 6008 | RecordDecl *VaListDecl; |
| 6009 | if (Context->getLangOpts().CPlusPlus) { |
| 6010 | // namespace std { struct __va_list { |
| 6011 | NamespaceDecl *NS; |
| 6012 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 6013 | Context->getTranslationUnitDecl(), |
| 6014 | /*Inline*/false, SourceLocation(), |
| 6015 | SourceLocation(), &Context->Idents.get("std"), |
| 6016 | /*PrevDecl*/0); |
| 6017 | |
| 6018 | VaListDecl = CXXRecordDecl::Create(*Context, TTK_Struct, |
| 6019 | Context->getTranslationUnitDecl(), |
| 6020 | SourceLocation(), SourceLocation(), |
| 6021 | &Context->Idents.get("__va_list")); |
| 6022 | |
| 6023 | VaListDecl->setDeclContext(NS); |
| 6024 | |
| 6025 | } else { |
| 6026 | // struct __va_list { |
| 6027 | VaListDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 6028 | Context->getTranslationUnitDecl(), |
| 6029 | &Context->Idents.get("__va_list")); |
| 6030 | } |
| 6031 | |
| 6032 | VaListDecl->startDefinition(); |
| 6033 | |
| 6034 | // void * __ap; |
| 6035 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6036 | VaListDecl, |
| 6037 | SourceLocation(), |
| 6038 | SourceLocation(), |
| 6039 | &Context->Idents.get("__ap"), |
| 6040 | Context->getPointerType(Context->VoidTy), |
| 6041 | /*TInfo=*/0, |
| 6042 | /*BitWidth=*/0, |
| 6043 | /*Mutable=*/false, |
| 6044 | ICIS_NoInit); |
| 6045 | Field->setAccess(AS_public); |
| 6046 | VaListDecl->addDecl(Field); |
| 6047 | |
| 6048 | // }; |
| 6049 | VaListDecl->completeDefinition(); |
| 6050 | |
| 6051 | // typedef struct __va_list __builtin_va_list; |
| 6052 | TypeSourceInfo *TInfo |
| 6053 | = Context->getTrivialTypeSourceInfo(Context->getRecordType(VaListDecl)); |
| 6054 | |
| 6055 | TypedefDecl *VaListTypeDecl |
| 6056 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 6057 | Context->getTranslationUnitDecl(), |
| 6058 | SourceLocation(), SourceLocation(), |
| 6059 | &Context->Idents.get("__builtin_va_list"), |
| 6060 | TInfo); |
| 6061 | |
| 6062 | return VaListTypeDecl; |
| 6063 | } |
| 6064 | |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6065 | static TypedefDecl * |
| 6066 | CreateSystemZBuiltinVaListDecl(const ASTContext *Context) { |
| 6067 | // typedef struct __va_list_tag { |
| 6068 | RecordDecl *VaListTagDecl; |
| 6069 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 6070 | Context->getTranslationUnitDecl(), |
| 6071 | &Context->Idents.get("__va_list_tag")); |
| 6072 | VaListTagDecl->startDefinition(); |
| 6073 | |
| 6074 | const size_t NumFields = 4; |
| 6075 | QualType FieldTypes[NumFields]; |
| 6076 | const char *FieldNames[NumFields]; |
| 6077 | |
| 6078 | // long __gpr; |
| 6079 | FieldTypes[0] = Context->LongTy; |
| 6080 | FieldNames[0] = "__gpr"; |
| 6081 | |
| 6082 | // long __fpr; |
| 6083 | FieldTypes[1] = Context->LongTy; |
| 6084 | FieldNames[1] = "__fpr"; |
| 6085 | |
| 6086 | // void *__overflow_arg_area; |
| 6087 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 6088 | FieldNames[2] = "__overflow_arg_area"; |
| 6089 | |
| 6090 | // void *__reg_save_area; |
| 6091 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 6092 | FieldNames[3] = "__reg_save_area"; |
| 6093 | |
| 6094 | // Create fields |
| 6095 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6096 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6097 | VaListTagDecl, |
| 6098 | SourceLocation(), |
| 6099 | SourceLocation(), |
| 6100 | &Context->Idents.get(FieldNames[i]), |
| 6101 | FieldTypes[i], /*TInfo=*/0, |
| 6102 | /*BitWidth=*/0, |
| 6103 | /*Mutable=*/false, |
| 6104 | ICIS_NoInit); |
| 6105 | Field->setAccess(AS_public); |
| 6106 | VaListTagDecl->addDecl(Field); |
| 6107 | } |
| 6108 | VaListTagDecl->completeDefinition(); |
| 6109 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 6110 | Context->VaListTagTy = VaListTagType; |
| 6111 | |
| 6112 | // } __va_list_tag; |
| 6113 | TypedefDecl *VaListTagTypedefDecl |
| 6114 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 6115 | Context->getTranslationUnitDecl(), |
| 6116 | SourceLocation(), SourceLocation(), |
| 6117 | &Context->Idents.get("__va_list_tag"), |
| 6118 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 6119 | QualType VaListTagTypedefType = |
| 6120 | Context->getTypedefType(VaListTagTypedefDecl); |
| 6121 | |
| 6122 | // typedef __va_list_tag __builtin_va_list[1]; |
| 6123 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 6124 | QualType VaListTagArrayType |
| 6125 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 6126 | Size, ArrayType::Normal,0); |
| 6127 | TypeSourceInfo *TInfo |
| 6128 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 6129 | TypedefDecl *VaListTypedefDecl |
| 6130 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 6131 | Context->getTranslationUnitDecl(), |
| 6132 | SourceLocation(), SourceLocation(), |
| 6133 | &Context->Idents.get("__builtin_va_list"), |
| 6134 | TInfo); |
| 6135 | |
| 6136 | return VaListTypedefDecl; |
| 6137 | } |
| 6138 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6139 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 6140 | TargetInfo::BuiltinVaListKind Kind) { |
| 6141 | switch (Kind) { |
| 6142 | case TargetInfo::CharPtrBuiltinVaList: |
| 6143 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 6144 | case TargetInfo::VoidPtrBuiltinVaList: |
| 6145 | return CreateVoidPtrBuiltinVaListDecl(Context); |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6146 | case TargetInfo::AArch64ABIBuiltinVaList: |
| 6147 | return CreateAArch64ABIBuiltinVaListDecl(Context); |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6148 | case TargetInfo::PowerABIBuiltinVaList: |
| 6149 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 6150 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 6151 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 6152 | case TargetInfo::PNaClABIBuiltinVaList: |
| 6153 | return CreatePNaClABIBuiltinVaListDecl(Context); |
Logan Chien | eae5a820 | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6154 | case TargetInfo::AAPCSABIBuiltinVaList: |
| 6155 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6156 | case TargetInfo::SystemZBuiltinVaList: |
| 6157 | return CreateSystemZBuiltinVaListDecl(Context); |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6158 | } |
| 6159 | |
| 6160 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 6161 | } |
| 6162 | |
| 6163 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
| 6164 | if (!BuiltinVaListDecl) |
| 6165 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
| 6166 | |
| 6167 | return BuiltinVaListDecl; |
| 6168 | } |
| 6169 | |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 6170 | QualType ASTContext::getVaListTagType() const { |
| 6171 | // Force the creation of VaListTagTy by building the __builtin_va_list |
| 6172 | // declaration. |
| 6173 | if (VaListTagTy.isNull()) |
| 6174 | (void) getBuiltinVaListDecl(); |
| 6175 | |
| 6176 | return VaListTagTy; |
| 6177 | } |
| 6178 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6179 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6180 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 6181 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6182 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6183 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 6184 | } |
| 6185 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6186 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 6187 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6188 | TemplateName |
| 6189 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 6190 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6191 | unsigned size = End - Begin; |
| 6192 | assert(size > 1 && "set is not overloaded!"); |
| 6193 | |
| 6194 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 6195 | size * sizeof(FunctionTemplateDecl*)); |
| 6196 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 6197 | |
| 6198 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6199 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6200 | NamedDecl *D = *I; |
| 6201 | assert(isa<FunctionTemplateDecl>(D) || |
| 6202 | (isa<UsingShadowDecl>(D) && |
| 6203 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 6204 | *Storage++ = D; |
| 6205 | } |
| 6206 | |
| 6207 | return TemplateName(OT); |
| 6208 | } |
| 6209 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6210 | /// \brief Retrieve the template name that represents a qualified |
| 6211 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6212 | TemplateName |
| 6213 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 6214 | bool TemplateKeyword, |
| 6215 | TemplateDecl *Template) const { |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 6216 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 6217 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6218 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6219 | llvm::FoldingSetNodeID ID; |
| 6220 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 6221 | |
| 6222 | void *InsertPos = 0; |
| 6223 | QualifiedTemplateName *QTN = |
| 6224 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6225 | if (!QTN) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6226 | QTN = new (*this, llvm::alignOf<QualifiedTemplateName>()) |
| 6227 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6228 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 6229 | } |
| 6230 | |
| 6231 | return TemplateName(QTN); |
| 6232 | } |
| 6233 | |
| 6234 | /// \brief Retrieve the template name that represents a dependent |
| 6235 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6236 | TemplateName |
| 6237 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 6238 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6239 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 6240 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6241 | |
| 6242 | llvm::FoldingSetNodeID ID; |
| 6243 | DependentTemplateName::Profile(ID, NNS, Name); |
| 6244 | |
| 6245 | void *InsertPos = 0; |
| 6246 | DependentTemplateName *QTN = |
| 6247 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6248 | |
| 6249 | if (QTN) |
| 6250 | return TemplateName(QTN); |
| 6251 | |
| 6252 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 6253 | if (CanonNNS == NNS) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6254 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6255 | DependentTemplateName(NNS, Name); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6256 | } else { |
| 6257 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6258 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6259 | DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6260 | DependentTemplateName *CheckQTN = |
| 6261 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6262 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 6263 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6264 | } |
| 6265 | |
| 6266 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 6267 | return TemplateName(QTN); |
| 6268 | } |
| 6269 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6270 | /// \brief Retrieve the template name that represents a dependent |
| 6271 | /// template name such as \c MetaFun::template operator+. |
| 6272 | TemplateName |
| 6273 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6274 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6275 | assert((!NNS || NNS->isDependent()) && |
| 6276 | "Nested name specifier must be dependent"); |
| 6277 | |
| 6278 | llvm::FoldingSetNodeID ID; |
| 6279 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 6280 | |
| 6281 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6282 | DependentTemplateName *QTN |
| 6283 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6284 | |
| 6285 | if (QTN) |
| 6286 | return TemplateName(QTN); |
| 6287 | |
| 6288 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 6289 | if (CanonNNS == NNS) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6290 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6291 | DependentTemplateName(NNS, Operator); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6292 | } else { |
| 6293 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6294 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6295 | DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6296 | |
| 6297 | DependentTemplateName *CheckQTN |
| 6298 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6299 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 6300 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6301 | } |
| 6302 | |
| 6303 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 6304 | return TemplateName(QTN); |
| 6305 | } |
| 6306 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6307 | TemplateName |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6308 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 6309 | TemplateName replacement) const { |
| 6310 | llvm::FoldingSetNodeID ID; |
| 6311 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 6312 | |
| 6313 | void *insertPos = 0; |
| 6314 | SubstTemplateTemplateParmStorage *subst |
| 6315 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 6316 | |
| 6317 | if (!subst) { |
| 6318 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 6319 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 6320 | } |
| 6321 | |
| 6322 | return TemplateName(subst); |
| 6323 | } |
| 6324 | |
| 6325 | TemplateName |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6326 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 6327 | const TemplateArgument &ArgPack) const { |
| 6328 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 6329 | llvm::FoldingSetNodeID ID; |
| 6330 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 6331 | |
| 6332 | void *InsertPos = 0; |
| 6333 | SubstTemplateTemplateParmPackStorage *Subst |
| 6334 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 6335 | |
| 6336 | if (!Subst) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6337 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6338 | ArgPack.pack_size(), |
| 6339 | ArgPack.pack_begin()); |
| 6340 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 6341 | } |
| 6342 | |
| 6343 | return TemplateName(Subst); |
| 6344 | } |
| 6345 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6346 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 6347 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 6348 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6349 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6350 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6351 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6352 | case TargetInfo::SignedShort: return ShortTy; |
| 6353 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 6354 | case TargetInfo::SignedInt: return IntTy; |
| 6355 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 6356 | case TargetInfo::SignedLong: return LongTy; |
| 6357 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 6358 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 6359 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 6360 | } |
| 6361 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6362 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6363 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 6364 | |
| 6365 | //===----------------------------------------------------------------------===// |
| 6366 | // Type Predicates. |
| 6367 | //===----------------------------------------------------------------------===// |
| 6368 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6369 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 6370 | /// garbage collection attribute. |
| 6371 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6372 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6373 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6374 | return Qualifiers::GCNone; |
| 6375 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6376 | assert(getLangOpts().ObjC1); |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6377 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 6378 | |
| 6379 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 6380 | // (or pointers to them) be treated as though they were declared |
| 6381 | // as __strong. |
| 6382 | if (GCAttrs == Qualifiers::GCNone) { |
| 6383 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 6384 | return Qualifiers::Strong; |
| 6385 | else if (Ty->isPointerType()) |
| 6386 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 6387 | } else { |
| 6388 | // It's not valid to set GC attributes on anything that isn't a |
| 6389 | // pointer. |
| 6390 | #ifndef NDEBUG |
| 6391 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 6392 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 6393 | CT = AT->getElementType(); |
| 6394 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 6395 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6396 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 6397 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6398 | } |
| 6399 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6400 | //===----------------------------------------------------------------------===// |
| 6401 | // Type Compatibility Testing |
| 6402 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 6403 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6404 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6405 | /// compatible. |
| 6406 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 6407 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 6408 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6409 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6410 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6411 | } |
| 6412 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6413 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 6414 | QualType SecondVec) { |
| 6415 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 6416 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 6417 | |
| 6418 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 6419 | return true; |
| 6420 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6421 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 6422 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6423 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 6424 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6425 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6426 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6427 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 6428 | First->getVectorKind() != VectorType::AltiVecBool && |
| 6429 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 6430 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6431 | return true; |
| 6432 | |
| 6433 | return false; |
| 6434 | } |
| 6435 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6436 | //===----------------------------------------------------------------------===// |
| 6437 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 6438 | //===----------------------------------------------------------------------===// |
| 6439 | |
| 6440 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 6441 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6442 | bool |
| 6443 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 6444 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 6445 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6446 | return true; |
| 6447 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 6448 | E = rProto->protocol_end(); PI != E; ++PI) |
| 6449 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 6450 | return true; |
| 6451 | return false; |
| 6452 | } |
| 6453 | |
Dmitri Gribenko | 4c3b8a3 | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 6454 | /// QualifiedIdConformsQualifiedId - compare id<pr,...> with id<pr1,...> |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6455 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 6456 | /// otherwise. |
| 6457 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 6458 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 6459 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 6460 | return false; |
| 6461 | } |
| 6462 | |
Dmitri Gribenko | 4c3b8a3 | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 6463 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
| 6464 | /// Class<pr1, ...>. |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6465 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 6466 | QualType rhs) { |
| 6467 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 6468 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 6469 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 6470 | |
| 6471 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6472 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6473 | bool match = false; |
| 6474 | ObjCProtocolDecl *lhsProto = *I; |
| 6475 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 6476 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 6477 | ObjCProtocolDecl *rhsProto = *J; |
| 6478 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 6479 | match = true; |
| 6480 | break; |
| 6481 | } |
| 6482 | } |
| 6483 | if (!match) |
| 6484 | return false; |
| 6485 | } |
| 6486 | return true; |
| 6487 | } |
| 6488 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6489 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 6490 | /// ObjCQualifiedIDType. |
| 6491 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 6492 | bool compare) { |
| 6493 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6494 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6495 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 6496 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6497 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6498 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 6499 | return true; |
| 6500 | |
| 6501 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6502 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6503 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6504 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6505 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6506 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6507 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6508 | // make sure we check the class hierarchy. |
| 6509 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 6510 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6511 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6512 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6513 | // see if static class implements all of id's protocols, directly or |
| 6514 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 6515 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6516 | return false; |
| 6517 | } |
| 6518 | } |
| 6519 | // If there are no qualifiers and no interface, we have an 'id'. |
| 6520 | return true; |
| 6521 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6522 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6523 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6524 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6525 | ObjCProtocolDecl *lhsProto = *I; |
| 6526 | bool match = false; |
| 6527 | |
| 6528 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6529 | // see if static class implements all of id's protocols, directly or |
| 6530 | // through its super class and categories. |
| 6531 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 6532 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 6533 | ObjCProtocolDecl *rhsProto = *J; |
| 6534 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6535 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6536 | match = true; |
| 6537 | break; |
| 6538 | } |
| 6539 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6540 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6541 | // make sure we check the class hierarchy. |
| 6542 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 6543 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6544 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6545 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6546 | // see if static class implements all of id's protocols, directly or |
| 6547 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 6548 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6549 | match = true; |
| 6550 | break; |
| 6551 | } |
| 6552 | } |
| 6553 | } |
| 6554 | if (!match) |
| 6555 | return false; |
| 6556 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6557 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6558 | return true; |
| 6559 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6560 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6561 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 6562 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 6563 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6564 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6565 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6566 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6567 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 6568 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 6569 | ObjCProtocolDecl *lhsProto = *I; |
| 6570 | bool match = false; |
| 6571 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6572 | // when comparing an id<P> on rhs with a static type on lhs, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6573 | // see if static class implements all of id's protocols, directly or |
| 6574 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6575 | // First, lhs protocols in the qualifier list must be found, direct |
| 6576 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6577 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 6578 | E = rhsQID->qual_end(); J != E; ++J) { |
| 6579 | ObjCProtocolDecl *rhsProto = *J; |
| 6580 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6581 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6582 | match = true; |
| 6583 | break; |
| 6584 | } |
| 6585 | } |
| 6586 | if (!match) |
| 6587 | return false; |
| 6588 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6589 | |
| 6590 | // Static class's protocols, or its super class or category protocols |
| 6591 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 6592 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 6593 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 6594 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 6595 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 6596 | // no type qualifier and its class has no static protocol(s) |
| 6597 | // assume that it is mismatch. |
| 6598 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 6599 | return false; |
| 6600 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6601 | LHSInheritedProtocols.begin(), |
| 6602 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 6603 | bool match = false; |
| 6604 | ObjCProtocolDecl *lhsProto = (*I); |
| 6605 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 6606 | E = rhsQID->qual_end(); J != E; ++J) { |
| 6607 | ObjCProtocolDecl *rhsProto = *J; |
| 6608 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6609 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6610 | match = true; |
| 6611 | break; |
| 6612 | } |
| 6613 | } |
| 6614 | if (!match) |
| 6615 | return false; |
| 6616 | } |
| 6617 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6618 | return true; |
| 6619 | } |
| 6620 | return false; |
| 6621 | } |
| 6622 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6623 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6624 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 6625 | /// protocol qualifiers on the LHS or RHS. |
| 6626 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6627 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 6628 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6629 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6630 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6631 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 6632 | // If either type represents the built-in 'id' or 'Class' types, return true. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6633 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 6634 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6635 | return true; |
| 6636 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6637 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6638 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6639 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6640 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6641 | |
| 6642 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 6643 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 6644 | QualType(RHSOPT,0)); |
| 6645 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6646 | // If we have 2 user-defined types, fall into that path. |
| 6647 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6648 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6649 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6650 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6651 | } |
| 6652 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6653 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 6654 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6655 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 6656 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 6657 | /// not OK. For the return type, the opposite is not OK. |
| 6658 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 6659 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6660 | const ObjCObjectPointerType *RHSOPT, |
| 6661 | bool BlockReturnType) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6662 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6663 | return true; |
| 6664 | |
| 6665 | if (LHSOPT->isObjCBuiltinType()) { |
| 6666 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 6667 | } |
| 6668 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6669 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6670 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6671 | QualType(RHSOPT,0), |
| 6672 | false); |
| 6673 | |
| 6674 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 6675 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 6676 | if (LHS && RHS) { // We have 2 user-defined types. |
| 6677 | if (LHS != RHS) { |
| 6678 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6679 | return BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6680 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6681 | return !BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6682 | } |
| 6683 | else |
| 6684 | return true; |
| 6685 | } |
| 6686 | return false; |
| 6687 | } |
| 6688 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6689 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 6690 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 6691 | /// It is used to build composite qualifier list of the composite type of |
| 6692 | /// the conditional expression involving two objective-c pointer objects. |
| 6693 | static |
| 6694 | void getIntersectionOfProtocols(ASTContext &Context, |
| 6695 | const ObjCObjectPointerType *LHSOPT, |
| 6696 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6697 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6698 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6699 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6700 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6701 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 6702 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6703 | |
| 6704 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 6705 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 6706 | if (LHSNumProtocols > 0) |
| 6707 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 6708 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6709 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6710 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 6711 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6712 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 6713 | LHSInheritedProtocols.end()); |
| 6714 | } |
| 6715 | |
| 6716 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 6717 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 6718 | ObjCProtocolDecl **RHSProtocols = |
| 6719 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6720 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 6721 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 6722 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 6723 | } else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6724 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6725 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 6726 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6727 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6728 | RHSInheritedProtocols.begin(), |
| 6729 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 6730 | if (InheritedProtocolSet.count((*I))) |
| 6731 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6732 | } |
| 6733 | } |
| 6734 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6735 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 6736 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 6737 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 6738 | /// warning is issued. So, its invokation is extremely rare. |
| 6739 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6740 | const ObjCObjectPointerType *Lptr, |
| 6741 | const ObjCObjectPointerType *Rptr) { |
| 6742 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 6743 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 6744 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 6745 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 6746 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6747 | return QualType(); |
| 6748 | |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 6749 | do { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6750 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6751 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6752 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6753 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 6754 | |
| 6755 | QualType Result = QualType(LHS, 0); |
| 6756 | if (!Protocols.empty()) |
| 6757 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 6758 | Result = getObjCObjectPointerType(Result); |
| 6759 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6760 | } |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 6761 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6762 | |
| 6763 | return QualType(); |
| 6764 | } |
| 6765 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6766 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 6767 | const ObjCObjectType *RHS) { |
| 6768 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 6769 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 6770 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6771 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 6772 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6773 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6774 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6775 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6776 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 6777 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 6778 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6779 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6780 | |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6781 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 6782 | // more detailed analysis is required. |
| 6783 | if (RHS->getNumProtocols() == 0) { |
| 6784 | // OK, if LHS is a superclass of RHS *and* |
| 6785 | // this superclass is assignment compatible with LHS. |
| 6786 | // false otherwise. |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 6787 | bool IsSuperClass = |
| 6788 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 6789 | if (IsSuperClass) { |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6790 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 6791 | // ; i.e., SuperClass may implement at least one of the protocols |
| 6792 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 6793 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 6794 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 6795 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6796 | // If super class has no protocols, it is not a match. |
| 6797 | if (SuperClassInheritedProtocols.empty()) |
| 6798 | return false; |
| 6799 | |
| 6800 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 6801 | LHSPE = LHS->qual_end(); |
| 6802 | LHSPI != LHSPE; LHSPI++) { |
| 6803 | bool SuperImplementsProtocol = false; |
| 6804 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 6805 | |
| 6806 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6807 | SuperClassInheritedProtocols.begin(), |
| 6808 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 6809 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 6810 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 6811 | SuperImplementsProtocol = true; |
| 6812 | break; |
| 6813 | } |
| 6814 | } |
| 6815 | if (!SuperImplementsProtocol) |
| 6816 | return false; |
| 6817 | } |
| 6818 | return true; |
| 6819 | } |
| 6820 | return false; |
| 6821 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6822 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6823 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 6824 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6825 | LHSPI != LHSPE; LHSPI++) { |
| 6826 | bool RHSImplementsProtocol = false; |
| 6827 | |
| 6828 | // If the RHS doesn't implement the protocol on the left, the types |
| 6829 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6830 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 6831 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 6832 | RHSPI != RHSPE; RHSPI++) { |
| 6833 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6834 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 6835 | break; |
| 6836 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6837 | } |
| 6838 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 6839 | if (!RHSImplementsProtocol) |
| 6840 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6841 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6842 | // The RHS implements all protocols listed on the LHS. |
| 6843 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6844 | } |
| 6845 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6846 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 6847 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6848 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 6849 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6850 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6851 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6852 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6853 | |
| 6854 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 6855 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6856 | } |
| 6857 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6858 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 6859 | return canAssignObjCInterfaces( |
| 6860 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 6861 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 6862 | } |
| 6863 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6864 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6865 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6866 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6867 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6868 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 6869 | bool CompareUnqualified) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6870 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6871 | return hasSameType(LHS, RHS); |
| 6872 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6873 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6874 | } |
| 6875 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6876 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | 8237839 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 6877 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6878 | } |
| 6879 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6880 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 6881 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 6882 | } |
| 6883 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6884 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 6885 | /// of T is compatible with SubType, return the merged type, else return |
| 6886 | /// QualType() |
| 6887 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 6888 | bool OfBlockPointer, |
| 6889 | bool Unqualified) { |
| 6890 | if (const RecordType *UT = T->getAsUnionType()) { |
| 6891 | RecordDecl *UD = UT->getDecl(); |
| 6892 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 6893 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6894 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 6895 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6896 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 6897 | if (!MT.isNull()) |
| 6898 | return MT; |
| 6899 | } |
| 6900 | } |
| 6901 | } |
| 6902 | |
| 6903 | return QualType(); |
| 6904 | } |
| 6905 | |
| 6906 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 6907 | /// argument types |
| 6908 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 6909 | bool OfBlockPointer, |
| 6910 | bool Unqualified) { |
| 6911 | // GNU extension: two types are compatible if they appear as a function |
| 6912 | // argument, one of the types is a transparent union type and the other |
| 6913 | // type is compatible with a union member |
| 6914 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 6915 | Unqualified); |
| 6916 | if (!lmerge.isNull()) |
| 6917 | return lmerge; |
| 6918 | |
| 6919 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 6920 | Unqualified); |
| 6921 | if (!rmerge.isNull()) |
| 6922 | return rmerge; |
| 6923 | |
| 6924 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 6925 | } |
| 6926 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6927 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6928 | bool OfBlockPointer, |
| 6929 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6930 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 6931 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6932 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 6933 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6934 | bool allLTypes = true; |
| 6935 | bool allRTypes = true; |
| 6936 | |
| 6937 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6938 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6939 | if (OfBlockPointer) { |
| 6940 | QualType RHS = rbase->getResultType(); |
| 6941 | QualType LHS = lbase->getResultType(); |
| 6942 | bool UnqualifiedResult = Unqualified; |
| 6943 | if (!UnqualifiedResult) |
| 6944 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6945 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6946 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6947 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6948 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 6949 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6950 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6951 | |
| 6952 | if (Unqualified) |
| 6953 | retType = retType.getUnqualifiedType(); |
| 6954 | |
| 6955 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 6956 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 6957 | if (Unqualified) { |
| 6958 | LRetType = LRetType.getUnqualifiedType(); |
| 6959 | RRetType = RRetType.getUnqualifiedType(); |
| 6960 | } |
| 6961 | |
| 6962 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6963 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6964 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6965 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6966 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 6967 | // FIXME: double check this |
| 6968 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 6969 | // rbase->getRegParmAttr() != 0 && |
| 6970 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 6971 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 6972 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6973 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6974 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6975 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6976 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6977 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6978 | // Regparm is part of the calling convention. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 6979 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 6980 | return QualType(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6981 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 6982 | return QualType(); |
| 6983 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6984 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 6985 | return QualType(); |
| 6986 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6987 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 6988 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6989 | |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 6990 | if (lbaseInfo.getNoReturn() != NoReturn) |
| 6991 | allLTypes = false; |
| 6992 | if (rbaseInfo.getNoReturn() != NoReturn) |
| 6993 | allRTypes = false; |
| 6994 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6995 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6996 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6997 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6998 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 6999 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7000 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 7001 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 7002 | |
| 7003 | // Compatible functions must have the same number of arguments |
| 7004 | if (lproto_nargs != rproto_nargs) |
| 7005 | return QualType(); |
| 7006 | |
| 7007 | // Variadic and non-variadic functions aren't compatible |
| 7008 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 7009 | return QualType(); |
| 7010 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 7011 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 7012 | return QualType(); |
| 7013 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7014 | if (LangOpts.ObjCAutoRefCount && |
| 7015 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 7016 | return QualType(); |
| 7017 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7018 | // Check argument compatibility |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7019 | SmallVector<QualType, 10> types; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7020 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 7021 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 7022 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 7023 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 7024 | OfBlockPointer, |
| 7025 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7026 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7027 | |
| 7028 | if (Unqualified) |
| 7029 | argtype = argtype.getUnqualifiedType(); |
| 7030 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7031 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7032 | if (Unqualified) { |
| 7033 | largtype = largtype.getUnqualifiedType(); |
| 7034 | rargtype = rargtype.getUnqualifiedType(); |
| 7035 | } |
| 7036 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7037 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 7038 | allLTypes = false; |
| 7039 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 7040 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7041 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7042 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7043 | if (allLTypes) return lhs; |
| 7044 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7045 | |
| 7046 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 7047 | EPI.ExtInfo = einfo; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 7048 | return getFunctionType(retType, types, EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7049 | } |
| 7050 | |
| 7051 | if (lproto) allRTypes = false; |
| 7052 | if (rproto) allLTypes = false; |
| 7053 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7054 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7055 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 7056 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7057 | if (proto->isVariadic()) return QualType(); |
| 7058 | // Check that the types are compatible with the types that |
| 7059 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 7060 | // The only types actually affected are promotable integer |
| 7061 | // types and floats, which would be passed as a different |
| 7062 | // type depending on whether the prototype is visible. |
| 7063 | unsigned proto_nargs = proto->getNumArgs(); |
| 7064 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 7065 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 7066 | |
Eli Friedman | c586d5d | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 7067 | // Look at the converted type of enum types, since that is the type used |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 7068 | // to pass enum values. |
Eli Friedman | c586d5d | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 7069 | if (const EnumType *Enum = argTy->getAs<EnumType>()) { |
| 7070 | argTy = Enum->getDecl()->getIntegerType(); |
| 7071 | if (argTy.isNull()) |
| 7072 | return QualType(); |
| 7073 | } |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 7074 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7075 | if (argTy->isPromotableIntegerType() || |
| 7076 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 7077 | return QualType(); |
| 7078 | } |
| 7079 | |
| 7080 | if (allLTypes) return lhs; |
| 7081 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7082 | |
| 7083 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 7084 | EPI.ExtInfo = einfo; |
Reid Kleckner | 0567a79 | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 7085 | return getFunctionType(retType, proto->getArgTypes(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7086 | } |
| 7087 | |
| 7088 | if (allLTypes) return lhs; |
| 7089 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7090 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7091 | } |
| 7092 | |
John McCall | b9da713 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7093 | /// Given that we have an enum type and a non-enum type, try to merge them. |
| 7094 | static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, |
| 7095 | QualType other, bool isBlockReturnType) { |
| 7096 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 7097 | // a signed integer type, or an unsigned integer type. |
| 7098 | // Compatibility is based on the underlying type, not the promotion |
| 7099 | // type. |
| 7100 | QualType underlyingType = ET->getDecl()->getIntegerType(); |
| 7101 | if (underlyingType.isNull()) return QualType(); |
| 7102 | if (Context.hasSameType(underlyingType, other)) |
| 7103 | return other; |
| 7104 | |
| 7105 | // In block return types, we're more permissive and accept any |
| 7106 | // integral type of the same size. |
| 7107 | if (isBlockReturnType && other->isIntegerType() && |
| 7108 | Context.getTypeSize(underlyingType) == Context.getTypeSize(other)) |
| 7109 | return other; |
| 7110 | |
| 7111 | return QualType(); |
| 7112 | } |
| 7113 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7114 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7115 | bool OfBlockPointer, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7116 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 7117 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 7118 | // type is adjusted to "T" prior to any further analysis, the expression |
| 7119 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 7120 | // expression is an lvalue unless the reference is an rvalue reference and |
| 7121 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 7122 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 7123 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7124 | |
| 7125 | if (Unqualified) { |
| 7126 | LHS = LHS.getUnqualifiedType(); |
| 7127 | RHS = RHS.getUnqualifiedType(); |
| 7128 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 7129 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7130 | QualType LHSCan = getCanonicalType(LHS), |
| 7131 | RHSCan = getCanonicalType(RHS); |
| 7132 | |
| 7133 | // If two types are identical, they are compatible. |
| 7134 | if (LHSCan == RHSCan) |
| 7135 | return LHS; |
| 7136 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7137 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 7138 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 7139 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7140 | if (LQuals != RQuals) { |
| 7141 | // If any of these qualifiers are different, we have a type |
| 7142 | // mismatch. |
| 7143 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7144 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 7145 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7146 | return QualType(); |
| 7147 | |
| 7148 | // Exactly one GC qualifier difference is allowed: __strong is |
| 7149 | // okay if the other type has no GC qualifier but is an Objective |
| 7150 | // C object pointer (i.e. implicitly strong by default). We fix |
| 7151 | // this by pretending that the unqualified type was actually |
| 7152 | // qualified __strong. |
| 7153 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 7154 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 7155 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 7156 | |
| 7157 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 7158 | return QualType(); |
| 7159 | |
| 7160 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 7161 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 7162 | } |
| 7163 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 7164 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 7165 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7166 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7167 | } |
| 7168 | |
| 7169 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7170 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 7171 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 7172 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7173 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7174 | // We want to consider the two function types to be the same for these |
| 7175 | // comparisons, just force one to the other. |
| 7176 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 7177 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 7178 | |
| 7179 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 7180 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 7181 | LHSClass = Type::ConstantArray; |
| 7182 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 7183 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7184 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7185 | // ObjCInterfaces are just specialized ObjCObjects. |
| 7186 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 7187 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 7188 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 7189 | // Canonicalize ExtVector -> Vector. |
| 7190 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 7191 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7192 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 7193 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7194 | if (LHSClass != RHSClass) { |
John McCall | b9da713 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7195 | // Note that we only have special rules for turning block enum |
| 7196 | // returns into block int returns, not vice-versa. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7197 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
John McCall | b9da713 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7198 | return mergeEnumWithInteger(*this, ETy, RHS, false); |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 7199 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7200 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
John McCall | b9da713 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7201 | return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType); |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 7202 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 7203 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 4196363 | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 7204 | if (OfBlockPointer && !BlockReturnType) { |
| 7205 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 7206 | return LHS; |
| 7207 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 7208 | return RHS; |
| 7209 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 7210 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7211 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7212 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7213 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 7214 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7215 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7216 | #define TYPE(Class, Base) |
| 7217 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 7218 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7219 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 7220 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 7221 | #include "clang/AST/TypeNodes.def" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7222 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7223 | |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 7224 | case Type::Auto: |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 7225 | case Type::LValueReference: |
| 7226 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7227 | case Type::MemberPointer: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7228 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7229 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7230 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7231 | case Type::IncompleteArray: |
| 7232 | case Type::VariableArray: |
| 7233 | case Type::FunctionProto: |
| 7234 | case Type::ExtVector: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7235 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7236 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7237 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7238 | { |
| 7239 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7240 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 7241 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7242 | if (Unqualified) { |
| 7243 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 7244 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 7245 | } |
| 7246 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 7247 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7248 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 7249 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7250 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 7251 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7252 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7253 | return getPointerType(ResultType); |
| 7254 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 7255 | case Type::BlockPointer: |
| 7256 | { |
| 7257 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7258 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 7259 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7260 | if (Unqualified) { |
| 7261 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 7262 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 7263 | } |
| 7264 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 7265 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 7266 | if (ResultType.isNull()) return QualType(); |
| 7267 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 7268 | return LHS; |
| 7269 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 7270 | return RHS; |
| 7271 | return getBlockPointerType(ResultType); |
| 7272 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7273 | case Type::Atomic: |
| 7274 | { |
| 7275 | // Merge two pointer types, while trying to preserve typedef info |
| 7276 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 7277 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 7278 | if (Unqualified) { |
| 7279 | LHSValue = LHSValue.getUnqualifiedType(); |
| 7280 | RHSValue = RHSValue.getUnqualifiedType(); |
| 7281 | } |
| 7282 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 7283 | Unqualified); |
| 7284 | if (ResultType.isNull()) return QualType(); |
| 7285 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 7286 | return LHS; |
| 7287 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 7288 | return RHS; |
| 7289 | return getAtomicType(ResultType); |
| 7290 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7291 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7292 | { |
| 7293 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 7294 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 7295 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 7296 | return QualType(); |
| 7297 | |
| 7298 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 7299 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7300 | if (Unqualified) { |
| 7301 | LHSElem = LHSElem.getUnqualifiedType(); |
| 7302 | RHSElem = RHSElem.getUnqualifiedType(); |
| 7303 | } |
| 7304 | |
| 7305 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7306 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7307 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 7308 | return LHS; |
| 7309 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 7310 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 7311 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 7312 | ArrayType::ArraySizeModifier(), 0); |
| 7313 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 7314 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7315 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 7316 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7317 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 7318 | return LHS; |
| 7319 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 7320 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7321 | if (LVAT) { |
| 7322 | // FIXME: This isn't correct! But tricky to implement because |
| 7323 | // the array's size has to be the size of LHS, but the type |
| 7324 | // has to be different. |
| 7325 | return LHS; |
| 7326 | } |
| 7327 | if (RVAT) { |
| 7328 | // FIXME: This isn't correct! But tricky to implement because |
| 7329 | // the array's size has to be the size of RHS, but the type |
| 7330 | // has to be different. |
| 7331 | return RHS; |
| 7332 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 7333 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 7334 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 7335 | return getIncompleteArrayType(ResultType, |
| 7336 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7337 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7338 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7339 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7340 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7341 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7342 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7343 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 7344 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7345 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 7346 | case Type::Complex: |
| 7347 | // Distinct complex types are incompatible. |
| 7348 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 7349 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 7350 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 7351 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 7352 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7353 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7354 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7355 | case Type::ObjCObject: { |
| 7356 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 7357 | // FIXME: This should be type compatibility, e.g. whether |
| 7358 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7359 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 7360 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 7361 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 7362 | return LHS; |
| 7363 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7364 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 7365 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7366 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7367 | if (OfBlockPointer) { |
| 7368 | if (canAssignObjCInterfacesInBlockPointer( |
| 7369 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7370 | RHS->getAs<ObjCObjectPointerType>(), |
| 7371 | BlockReturnType)) |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7372 | return LHS; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7373 | return QualType(); |
| 7374 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7375 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 7376 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7377 | return LHS; |
| 7378 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 7379 | return QualType(); |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7380 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7381 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7382 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7383 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7384 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 7385 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7386 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 7387 | const FunctionProtoType *FromFunctionType, |
| 7388 | const FunctionProtoType *ToFunctionType) { |
| 7389 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 7390 | ToFunctionType->hasAnyConsumedArgs()) |
| 7391 | return false; |
| 7392 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 7393 | FromFunctionType->getExtProtoInfo(); |
| 7394 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 7395 | ToFunctionType->getExtProtoInfo(); |
| 7396 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 7397 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 7398 | ArgIdx != NumArgs; ++ArgIdx) { |
| 7399 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 7400 | ToEPI.ConsumedArguments[ArgIdx]) |
| 7401 | return false; |
| 7402 | } |
| 7403 | return true; |
| 7404 | } |
| 7405 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7406 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 7407 | /// 'RHS' attributes and returns the merged version; including for function |
| 7408 | /// return types. |
| 7409 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 7410 | QualType LHSCan = getCanonicalType(LHS), |
| 7411 | RHSCan = getCanonicalType(RHS); |
| 7412 | // If two types are identical, they are compatible. |
| 7413 | if (LHSCan == RHSCan) |
| 7414 | return LHS; |
| 7415 | if (RHSCan->isFunctionType()) { |
| 7416 | if (!LHSCan->isFunctionType()) |
| 7417 | return QualType(); |
| 7418 | QualType OldReturnType = |
| 7419 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 7420 | QualType NewReturnType = |
| 7421 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 7422 | QualType ResReturnType = |
| 7423 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 7424 | if (ResReturnType.isNull()) |
| 7425 | return QualType(); |
| 7426 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 7427 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 7428 | // In either case, use OldReturnType to build the new function type. |
| 7429 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 7430 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7431 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7432 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Reid Kleckner | 0567a79 | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 7433 | QualType ResultType = |
| 7434 | getFunctionType(OldReturnType, FPT->getArgTypes(), EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7435 | return ResultType; |
| 7436 | } |
| 7437 | } |
| 7438 | return QualType(); |
| 7439 | } |
| 7440 | |
| 7441 | // If the qualifiers are different, the types can still be merged. |
| 7442 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 7443 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 7444 | if (LQuals != RQuals) { |
| 7445 | // If any of these qualifiers are different, we have a type mismatch. |
| 7446 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 7447 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 7448 | return QualType(); |
| 7449 | |
| 7450 | // Exactly one GC qualifier difference is allowed: __strong is |
| 7451 | // okay if the other type has no GC qualifier but is an Objective |
| 7452 | // C object pointer (i.e. implicitly strong by default). We fix |
| 7453 | // this by pretending that the unqualified type was actually |
| 7454 | // qualified __strong. |
| 7455 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 7456 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 7457 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 7458 | |
| 7459 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 7460 | return QualType(); |
| 7461 | |
| 7462 | if (GC_L == Qualifiers::Strong) |
| 7463 | return LHS; |
| 7464 | if (GC_R == Qualifiers::Strong) |
| 7465 | return RHS; |
| 7466 | return QualType(); |
| 7467 | } |
| 7468 | |
| 7469 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 7470 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7471 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7472 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 7473 | if (ResQT == LHSBaseQT) |
| 7474 | return LHS; |
| 7475 | if (ResQT == RHSBaseQT) |
| 7476 | return RHS; |
| 7477 | } |
| 7478 | return QualType(); |
| 7479 | } |
| 7480 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 7481 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7482 | // Integer Predicates |
| 7483 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 7484 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7485 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7486 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 7487 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7488 | if (T->isBooleanType()) |
| 7489 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 7490 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7491 | return (unsigned)getTypeSize(T); |
| 7492 | } |
| 7493 | |
Abramo Bagnara | 762f159 | 2012-09-09 10:21:24 +0000 | [diff] [blame] | 7494 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7495 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7496 | |
| 7497 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 7498 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 7499 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7500 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7501 | |
| 7502 | // For enums, we return the unsigned version of the base type. |
| 7503 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7504 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7505 | |
| 7506 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 7507 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7508 | switch (BTy->getKind()) { |
| 7509 | case BuiltinType::Char_S: |
| 7510 | case BuiltinType::SChar: |
| 7511 | return UnsignedCharTy; |
| 7512 | case BuiltinType::Short: |
| 7513 | return UnsignedShortTy; |
| 7514 | case BuiltinType::Int: |
| 7515 | return UnsignedIntTy; |
| 7516 | case BuiltinType::Long: |
| 7517 | return UnsignedLongTy; |
| 7518 | case BuiltinType::LongLong: |
| 7519 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 7520 | case BuiltinType::Int128: |
| 7521 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7522 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7523 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7524 | } |
| 7525 | } |
| 7526 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 7527 | ASTMutationListener::~ASTMutationListener() { } |
| 7528 | |
Richard Smith | 9dadfab | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 7529 | void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD, |
| 7530 | QualType ReturnType) {} |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7531 | |
| 7532 | //===----------------------------------------------------------------------===// |
| 7533 | // Builtin Type Computation |
| 7534 | //===----------------------------------------------------------------------===// |
| 7535 | |
| 7536 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7537 | /// pointer over the consumed characters. This returns the resultant type. If |
| 7538 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 7539 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 7540 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7541 | /// |
| 7542 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 7543 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7544 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7545 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7546 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7547 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7548 | // Modifiers. |
| 7549 | int HowLong = 0; |
| 7550 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7551 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7552 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7553 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7554 | bool Done = false; |
| 7555 | while (!Done) { |
| 7556 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7557 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7558 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7559 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7560 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7561 | case 'S': |
| 7562 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 7563 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 7564 | Signed = true; |
| 7565 | break; |
| 7566 | case 'U': |
| 7567 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 7568 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 7569 | Unsigned = true; |
| 7570 | break; |
| 7571 | case 'L': |
| 7572 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 7573 | ++HowLong; |
| 7574 | break; |
| 7575 | } |
| 7576 | } |
| 7577 | |
| 7578 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7579 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7580 | // Read the base type. |
| 7581 | switch (*Str++) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7582 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7583 | case 'v': |
| 7584 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7585 | "Bad modifiers used with 'v'!"); |
| 7586 | Type = Context.VoidTy; |
| 7587 | break; |
| 7588 | case 'f': |
| 7589 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7590 | "Bad modifiers used with 'f'!"); |
| 7591 | Type = Context.FloatTy; |
| 7592 | break; |
| 7593 | case 'd': |
| 7594 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 7595 | "Bad modifiers used with 'd'!"); |
| 7596 | if (HowLong) |
| 7597 | Type = Context.LongDoubleTy; |
| 7598 | else |
| 7599 | Type = Context.DoubleTy; |
| 7600 | break; |
| 7601 | case 's': |
| 7602 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 7603 | if (Unsigned) |
| 7604 | Type = Context.UnsignedShortTy; |
| 7605 | else |
| 7606 | Type = Context.ShortTy; |
| 7607 | break; |
| 7608 | case 'i': |
| 7609 | if (HowLong == 3) |
| 7610 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 7611 | else if (HowLong == 2) |
| 7612 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 7613 | else if (HowLong == 1) |
| 7614 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 7615 | else |
| 7616 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 7617 | break; |
| 7618 | case 'c': |
| 7619 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 7620 | if (Signed) |
| 7621 | Type = Context.SignedCharTy; |
| 7622 | else if (Unsigned) |
| 7623 | Type = Context.UnsignedCharTy; |
| 7624 | else |
| 7625 | Type = Context.CharTy; |
| 7626 | break; |
| 7627 | case 'b': // boolean |
| 7628 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 7629 | Type = Context.BoolTy; |
| 7630 | break; |
| 7631 | case 'z': // size_t. |
| 7632 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 7633 | Type = Context.getSizeType(); |
| 7634 | break; |
| 7635 | case 'F': |
| 7636 | Type = Context.getCFConstantStringType(); |
| 7637 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 7638 | case 'G': |
| 7639 | Type = Context.getObjCIdType(); |
| 7640 | break; |
| 7641 | case 'H': |
| 7642 | Type = Context.getObjCSelType(); |
| 7643 | break; |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 7644 | case 'M': |
| 7645 | Type = Context.getObjCSuperType(); |
| 7646 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7647 | case 'a': |
| 7648 | Type = Context.getBuiltinVaListType(); |
| 7649 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 7650 | break; |
| 7651 | case 'A': |
| 7652 | // This is a "reference" to a va_list; however, what exactly |
| 7653 | // this means depends on how va_list is defined. There are two |
| 7654 | // different kinds of va_list: ones passed by value, and ones |
| 7655 | // passed by reference. An example of a by-value va_list is |
| 7656 | // x86, where va_list is a char*. An example of by-ref va_list |
| 7657 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 7658 | // we want this argument to be a char*&; for x86-64, we want |
| 7659 | // it to be a __va_list_tag*. |
| 7660 | Type = Context.getBuiltinVaListType(); |
| 7661 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7662 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7663 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7664 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7665 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7666 | break; |
| 7667 | case 'V': { |
| 7668 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7669 | unsigned NumElements = strtoul(Str, &End, 10); |
| 7670 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7671 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7672 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7673 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 7674 | RequiresICE, false); |
| 7675 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7676 | |
| 7677 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 7678 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7679 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7680 | break; |
| 7681 | } |
Douglas Gregor | b4bc99b | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 7682 | case 'E': { |
| 7683 | char *End; |
| 7684 | |
| 7685 | unsigned NumElements = strtoul(Str, &End, 10); |
| 7686 | assert(End != Str && "Missing vector size"); |
| 7687 | |
| 7688 | Str = End; |
| 7689 | |
| 7690 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 7691 | false); |
| 7692 | Type = Context.getExtVectorType(ElementType, NumElements); |
| 7693 | break; |
| 7694 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 7695 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7696 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 7697 | false); |
| 7698 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 7699 | Type = Context.getComplexType(ElementType); |
| 7700 | break; |
Fariborz Jahanian | cc075e4 | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 7701 | } |
| 7702 | case 'Y' : { |
| 7703 | Type = Context.getPointerDiffType(); |
| 7704 | break; |
| 7705 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 7706 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 7707 | Type = Context.getFILEType(); |
| 7708 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7709 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7710 | return QualType(); |
| 7711 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7712 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 7713 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7714 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 7715 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7716 | else |
| 7717 | Type = Context.getjmp_bufType(); |
| 7718 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7719 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7720 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7721 | return QualType(); |
| 7722 | } |
| 7723 | break; |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 7724 | case 'K': |
| 7725 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 7726 | Type = Context.getucontext_tType(); |
| 7727 | |
| 7728 | if (Type.isNull()) { |
| 7729 | Error = ASTContext::GE_Missing_ucontext; |
| 7730 | return QualType(); |
| 7731 | } |
| 7732 | break; |
Eli Friedman | 6902e41 | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 7733 | case 'p': |
| 7734 | Type = Context.getProcessIDType(); |
| 7735 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 7736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7737 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7738 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 7739 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7740 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 7741 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7742 | default: Done = true; --Str; break; |
| 7743 | case '*': |
| 7744 | case '&': { |
| 7745 | // Both pointers and references can have their pointee types |
| 7746 | // qualified with an address space. |
| 7747 | char *End; |
| 7748 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 7749 | if (End != Str && AddrSpace != 0) { |
| 7750 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 7751 | Str = End; |
| 7752 | } |
| 7753 | if (c == '*') |
| 7754 | Type = Context.getPointerType(Type); |
| 7755 | else |
| 7756 | Type = Context.getLValueReferenceType(Type); |
| 7757 | break; |
| 7758 | } |
| 7759 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 7760 | case 'C': |
| 7761 | Type = Type.withConst(); |
| 7762 | break; |
| 7763 | case 'D': |
| 7764 | Type = Context.getVolatileType(Type); |
| 7765 | break; |
Ted Kremenek | 18932a0 | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 7766 | case 'R': |
| 7767 | Type = Type.withRestrict(); |
| 7768 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7769 | } |
| 7770 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7771 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7772 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7773 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7774 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7775 | return Type; |
| 7776 | } |
| 7777 | |
| 7778 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7779 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7780 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7781 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7782 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7783 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7784 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7785 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7786 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7787 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7788 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 7789 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7790 | if (Error != GE_None) |
| 7791 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7792 | |
| 7793 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 7794 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7795 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7796 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7797 | if (Error != GE_None) |
| 7798 | return QualType(); |
| 7799 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7800 | // If this argument is required to be an IntegerConstantExpression and the |
| 7801 | // caller cares, fill in the bitmask we return. |
| 7802 | if (RequiresICE && IntegerConstantArgs) |
| 7803 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 7804 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7805 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 7806 | if (Ty->isArrayType()) |
| 7807 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7808 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7809 | ArgTypes.push_back(Ty); |
| 7810 | } |
| 7811 | |
| 7812 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 7813 | "'.' should only occur at end of builtin type list!"); |
| 7814 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 7815 | FunctionType::ExtInfo EI; |
| 7816 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 7817 | |
| 7818 | bool Variadic = (TypeStr[0] == '.'); |
| 7819 | |
| 7820 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 7821 | if (ArgTypes.empty() && Variadic) |
| 7822 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 7823 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7824 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 7825 | EPI.ExtInfo = EI; |
| 7826 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7827 | |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 7828 | return getFunctionType(ResType, ArgTypes, EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7829 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 7830 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7831 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7832 | if (!FD->isExternallyVisible()) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7833 | return GVA_Internal; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7834 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7835 | GVALinkage External = GVA_StrongExternal; |
| 7836 | switch (FD->getTemplateSpecializationKind()) { |
| 7837 | case TSK_Undeclared: |
| 7838 | case TSK_ExplicitSpecialization: |
| 7839 | External = GVA_StrongExternal; |
| 7840 | break; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7841 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7842 | case TSK_ExplicitInstantiationDefinition: |
| 7843 | return GVA_ExplicitTemplateInstantiation; |
| 7844 | |
| 7845 | case TSK_ExplicitInstantiationDeclaration: |
| 7846 | case TSK_ImplicitInstantiation: |
| 7847 | External = GVA_TemplateInstantiation; |
| 7848 | break; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7849 | } |
| 7850 | |
| 7851 | if (!FD->isInlined()) |
| 7852 | return External; |
| 7853 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7854 | if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7855 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 7856 | // externally visible. |
| 7857 | if (FD->isInlineDefinitionExternallyVisible()) |
| 7858 | return External; |
| 7859 | |
| 7860 | // C99 inline semantics, where the symbol is not externally visible. |
| 7861 | return GVA_C99Inline; |
| 7862 | } |
| 7863 | |
| 7864 | // C++0x [temp.explicit]p9: |
| 7865 | // [ Note: The intent is that an inline function that is the subject of |
| 7866 | // an explicit instantiation declaration will still be implicitly |
| 7867 | // instantiated when used so that the body can be considered for |
| 7868 | // inlining, but that no out-of-line copy of the inline function would be |
| 7869 | // generated in the translation unit. -- end note ] |
| 7870 | if (FD->getTemplateSpecializationKind() |
| 7871 | == TSK_ExplicitInstantiationDeclaration) |
| 7872 | return GVA_C99Inline; |
| 7873 | |
| 7874 | return GVA_CXXInline; |
| 7875 | } |
| 7876 | |
| 7877 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7878 | if (!VD->isExternallyVisible()) |
| 7879 | return GVA_Internal; |
| 7880 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7881 | // If this is a static data member, compute the kind of template |
| 7882 | // specialization. Otherwise, this variable is not part of a |
| 7883 | // template. |
| 7884 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 7885 | if (VD->isStaticDataMember()) |
| 7886 | TSK = VD->getTemplateSpecializationKind(); |
| 7887 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7888 | switch (TSK) { |
| 7889 | case TSK_Undeclared: |
| 7890 | case TSK_ExplicitSpecialization: |
| 7891 | return GVA_StrongExternal; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7892 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7893 | case TSK_ExplicitInstantiationDeclaration: |
| 7894 | llvm_unreachable("Variable should not be instantiated"); |
| 7895 | // Fall through to treat this like any other instantiation. |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7896 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7897 | case TSK_ExplicitInstantiationDefinition: |
| 7898 | return GVA_ExplicitTemplateInstantiation; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7899 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7900 | case TSK_ImplicitInstantiation: |
| 7901 | return GVA_TemplateInstantiation; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7902 | } |
Rafael Espindola | 77b5025 | 2013-05-13 14:05:53 +0000 | [diff] [blame] | 7903 | |
| 7904 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7905 | } |
| 7906 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 7907 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7908 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7909 | if (!VD->isFileVarDecl()) |
| 7910 | return false; |
Richard Smith | f396ad9 | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 7911 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7912 | // We never need to emit an uninstantiated function template. |
| 7913 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
| 7914 | return false; |
| 7915 | } else |
| 7916 | return false; |
| 7917 | |
| 7918 | // If this is a member of a class template, we do not need to emit it. |
| 7919 | if (D->getDeclContext()->isDependentContext()) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7920 | return false; |
| 7921 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7922 | // Weak references don't produce any output by themselves. |
| 7923 | if (D->hasAttr<WeakRefAttr>()) |
| 7924 | return false; |
| 7925 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7926 | // Aliases and used decls are required. |
| 7927 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 7928 | return true; |
| 7929 | |
| 7930 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7931 | // Forward declarations aren't required. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 7932 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 7933 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7934 | |
| 7935 | // Constructors and destructors are required. |
| 7936 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 7937 | return true; |
| 7938 | |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7939 | // The key function for a class is required. This rule only comes |
| 7940 | // into play when inline functions can be key functions, though. |
| 7941 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
| 7942 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 7943 | const CXXRecordDecl *RD = MD->getParent(); |
| 7944 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 7945 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
| 7946 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 7947 | return true; |
| 7948 | } |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7949 | } |
| 7950 | } |
| 7951 | |
| 7952 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 7953 | |
| 7954 | // static, static inline, always_inline, and extern inline functions can |
| 7955 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 7956 | // Implicit template instantiations can also be deferred in C++. |
| 7957 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
Anton Yartsev | 3a5aca8 | 2012-02-02 06:06:34 +0000 | [diff] [blame] | 7958 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7959 | return false; |
| 7960 | return true; |
| 7961 | } |
Douglas Gregor | 94da158 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 7962 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7963 | const VarDecl *VD = cast<VarDecl>(D); |
| 7964 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 7965 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7966 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 7967 | return false; |
| 7968 | |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7969 | // Variables that can be needed in other TUs are required. |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7970 | GVALinkage L = GetGVALinkageForVariable(VD); |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7971 | if (L != GVA_Internal && L != GVA_TemplateInstantiation) |
| 7972 | return true; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7973 | |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7974 | // Variables that have destruction with side-effects are required. |
| 7975 | if (VD->getType().isDestructedType()) |
| 7976 | return true; |
| 7977 | |
| 7978 | // Variables that have initialization with side-effects are required. |
| 7979 | if (VD->getInit() && VD->getInit()->HasSideEffects(*this)) |
| 7980 | return true; |
| 7981 | |
| 7982 | return false; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7983 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7984 | |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7985 | CallingConv ASTContext::getDefaultCXXMethodCallConv(bool isVariadic) { |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7986 | // Pass through to the C++ ABI object |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7987 | return ABI->getDefaultMethodCallConv(isVariadic); |
| 7988 | } |
| 7989 | |
| 7990 | CallingConv ASTContext::getCanonicalCallConv(CallingConv CC) const { |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7991 | if (CC == CC_C && !LangOpts.MRTD && |
| 7992 | getTargetInfo().getCXXABI().isMemberFunctionCCDefault()) |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7993 | return CC_Default; |
| 7994 | return CC; |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7995 | } |
| 7996 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7997 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 7998 | // Pass through to the C++ ABI object |
| 7999 | return ABI->isNearlyEmpty(RD); |
| 8000 | } |
| 8001 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 8002 | MangleContext *ASTContext::createMangleContext() { |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 8003 | switch (Target->getCXXABI().getKind()) { |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 8004 | case TargetCXXABI::GenericAArch64: |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 8005 | case TargetCXXABI::GenericItanium: |
| 8006 | case TargetCXXABI::GenericARM: |
| 8007 | case TargetCXXABI::iOS: |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 8008 | return createItaniumMangleContext(*this, getDiagnostics()); |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 8009 | case TargetCXXABI::Microsoft: |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 8010 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 8011 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8012 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 8013 | } |
| 8014 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 8015 | CXXABI::~CXXABI() {} |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 8016 | |
| 8017 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 0c8cd1a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 8018 | return ASTRecordLayouts.getMemorySize() |
| 8019 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 8020 | + llvm::capacity_in_bytes(KeyFunctions) |
| 8021 | + llvm::capacity_in_bytes(ObjCImpls) |
| 8022 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 8023 | + llvm::capacity_in_bytes(DeclAttrs) |
| 8024 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 8025 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 8026 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 8027 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 8028 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 8029 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 8030 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 8031 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 8032 | } |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 8033 | |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 8034 | void ASTContext::addUnnamedTag(const TagDecl *Tag) { |
| 8035 | // FIXME: This mangling should be applied to function local classes too |
| 8036 | if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl() || |
Rafael Espindola | 5bbb058 | 2013-05-28 14:09:46 +0000 | [diff] [blame] | 8037 | !isa<CXXRecordDecl>(Tag->getParent())) |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 8038 | return; |
| 8039 | |
| 8040 | std::pair<llvm::DenseMap<const DeclContext *, unsigned>::iterator, bool> P = |
| 8041 | UnnamedMangleContexts.insert(std::make_pair(Tag->getParent(), 0)); |
| 8042 | UnnamedMangleNumbers.insert(std::make_pair(Tag, P.first->second++)); |
| 8043 | } |
| 8044 | |
| 8045 | int ASTContext::getUnnamedTagManglingNumber(const TagDecl *Tag) const { |
| 8046 | llvm::DenseMap<const TagDecl *, unsigned>::const_iterator I = |
| 8047 | UnnamedMangleNumbers.find(Tag); |
| 8048 | return I != UnnamedMangleNumbers.end() ? I->second : -1; |
| 8049 | } |
| 8050 | |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 8051 | unsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) { |
| 8052 | CXXRecordDecl *Lambda = CallOperator->getParent(); |
| 8053 | return LambdaMangleContexts[Lambda->getDeclContext()] |
| 8054 | .getManglingNumber(CallOperator); |
| 8055 | } |
| 8056 | |
| 8057 | |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 8058 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 8059 | ParamIndices[D] = index; |
| 8060 | } |
| 8061 | |
| 8062 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 8063 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 8064 | assert(I != ParamIndices.end() && |
| 8065 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 8066 | return I->second; |
| 8067 | } |
Fariborz Jahanian | 538bbe5 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 8068 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 8069 | APValue * |
| 8070 | ASTContext::getMaterializedTemporaryValue(const MaterializeTemporaryExpr *E, |
| 8071 | bool MayCreate) { |
| 8072 | assert(E && E->getStorageDuration() == SD_Static && |
| 8073 | "don't need to cache the computed value for this temporary"); |
| 8074 | if (MayCreate) |
| 8075 | return &MaterializedTemporaryValues[E]; |
| 8076 | |
| 8077 | llvm::DenseMap<const MaterializeTemporaryExpr *, APValue>::iterator I = |
| 8078 | MaterializedTemporaryValues.find(E); |
| 8079 | return I == MaterializedTemporaryValues.end() ? 0 : &I->second; |
| 8080 | } |
| 8081 | |
Fariborz Jahanian | 538bbe5 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 8082 | bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const { |
| 8083 | const llvm::Triple &T = getTargetInfo().getTriple(); |
| 8084 | if (!T.isOSDarwin()) |
| 8085 | return false; |
| 8086 | |
| 8087 | QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); |
| 8088 | CharUnits sizeChars = getTypeSizeInChars(AtomicTy); |
| 8089 | uint64_t Size = sizeChars.getQuantity(); |
| 8090 | CharUnits alignChars = getTypeAlignInChars(AtomicTy); |
| 8091 | unsigned Align = alignChars.getQuantity(); |
| 8092 | unsigned MaxInlineWidthInBits = getTargetInfo().getMaxAtomicInlineWidth(); |
| 8093 | return (Size != Align || toBits(sizeChars) > MaxInlineWidthInBits); |
| 8094 | } |
Reid Kleckner | cff1512 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8095 | |
| 8096 | namespace { |
| 8097 | |
| 8098 | /// \brief A \c RecursiveASTVisitor that builds a map from nodes to their |
| 8099 | /// parents as defined by the \c RecursiveASTVisitor. |
| 8100 | /// |
| 8101 | /// Note that the relationship described here is purely in terms of AST |
| 8102 | /// traversal - there are other relationships (for example declaration context) |
| 8103 | /// in the AST that are better modeled by special matchers. |
| 8104 | /// |
| 8105 | /// FIXME: Currently only builds up the map using \c Stmt and \c Decl nodes. |
| 8106 | class ParentMapASTVisitor : public RecursiveASTVisitor<ParentMapASTVisitor> { |
| 8107 | |
| 8108 | public: |
| 8109 | /// \brief Builds and returns the translation unit's parent map. |
| 8110 | /// |
| 8111 | /// The caller takes ownership of the returned \c ParentMap. |
| 8112 | static ASTContext::ParentMap *buildMap(TranslationUnitDecl &TU) { |
| 8113 | ParentMapASTVisitor Visitor(new ASTContext::ParentMap); |
| 8114 | Visitor.TraverseDecl(&TU); |
| 8115 | return Visitor.Parents; |
| 8116 | } |
| 8117 | |
| 8118 | private: |
| 8119 | typedef RecursiveASTVisitor<ParentMapASTVisitor> VisitorBase; |
| 8120 | |
| 8121 | ParentMapASTVisitor(ASTContext::ParentMap *Parents) : Parents(Parents) { |
| 8122 | } |
| 8123 | |
| 8124 | bool shouldVisitTemplateInstantiations() const { |
| 8125 | return true; |
| 8126 | } |
| 8127 | bool shouldVisitImplicitCode() const { |
| 8128 | return true; |
| 8129 | } |
| 8130 | // Disables data recursion. We intercept Traverse* methods in the RAV, which |
| 8131 | // are not triggered during data recursion. |
| 8132 | bool shouldUseDataRecursionFor(clang::Stmt *S) const { |
| 8133 | return false; |
| 8134 | } |
| 8135 | |
| 8136 | template <typename T> |
| 8137 | bool TraverseNode(T *Node, bool(VisitorBase:: *traverse) (T *)) { |
| 8138 | if (Node == NULL) |
| 8139 | return true; |
| 8140 | if (ParentStack.size() > 0) |
| 8141 | // FIXME: Currently we add the same parent multiple times, for example |
| 8142 | // when we visit all subexpressions of template instantiations; this is |
| 8143 | // suboptimal, bug benign: the only way to visit those is with |
| 8144 | // hasAncestor / hasParent, and those do not create new matches. |
| 8145 | // The plan is to enable DynTypedNode to be storable in a map or hash |
| 8146 | // map. The main problem there is to implement hash functions / |
| 8147 | // comparison operators for all types that DynTypedNode supports that |
| 8148 | // do not have pointer identity. |
| 8149 | (*Parents)[Node].push_back(ParentStack.back()); |
| 8150 | ParentStack.push_back(ast_type_traits::DynTypedNode::create(*Node)); |
| 8151 | bool Result = (this ->* traverse) (Node); |
| 8152 | ParentStack.pop_back(); |
| 8153 | return Result; |
| 8154 | } |
| 8155 | |
| 8156 | bool TraverseDecl(Decl *DeclNode) { |
| 8157 | return TraverseNode(DeclNode, &VisitorBase::TraverseDecl); |
| 8158 | } |
| 8159 | |
| 8160 | bool TraverseStmt(Stmt *StmtNode) { |
| 8161 | return TraverseNode(StmtNode, &VisitorBase::TraverseStmt); |
| 8162 | } |
| 8163 | |
| 8164 | ASTContext::ParentMap *Parents; |
| 8165 | llvm::SmallVector<ast_type_traits::DynTypedNode, 16> ParentStack; |
| 8166 | |
| 8167 | friend class RecursiveASTVisitor<ParentMapASTVisitor>; |
| 8168 | }; |
| 8169 | |
| 8170 | } // end namespace |
| 8171 | |
| 8172 | ASTContext::ParentVector |
| 8173 | ASTContext::getParents(const ast_type_traits::DynTypedNode &Node) { |
| 8174 | assert(Node.getMemoizationData() && |
| 8175 | "Invariant broken: only nodes that support memoization may be " |
| 8176 | "used in the parent map."); |
| 8177 | if (!AllParents) { |
| 8178 | // We always need to run over the whole translation unit, as |
| 8179 | // hasAncestor can escape any subtree. |
| 8180 | AllParents.reset( |
| 8181 | ParentMapASTVisitor::buildMap(*getTranslationUnitDecl())); |
| 8182 | } |
| 8183 | ParentMap::const_iterator I = AllParents->find(Node.getMemoizationData()); |
| 8184 | if (I == AllParents->end()) { |
| 8185 | return ParentVector(); |
| 8186 | } |
| 8187 | return I->second; |
| 8188 | } |