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" |
| 29 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 30 | #include "clang/Basic/Builtins.h" |
Chris Lattner | a9376d4 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 31 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringExtras.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Capacity.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 38 | #include <map> |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 39 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | using namespace clang; |
| 41 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 42 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 43 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 44 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 45 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 46 | unsigned ASTContext::NumImplicitMoveConstructors; |
| 47 | unsigned ASTContext::NumImplicitMoveConstructorsDeclared; |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 48 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 49 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 50 | unsigned ASTContext::NumImplicitMoveAssignmentOperators; |
| 51 | unsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 52 | unsigned ASTContext::NumImplicitDestructors; |
| 53 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 54 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | enum FloatingRank { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 56 | HalfRank, FloatRank, DoubleRank, LongDoubleRank |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 59 | RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 60 | if (!CommentsLoaded && ExternalSource) { |
| 61 | ExternalSource->ReadComments(); |
| 62 | CommentsLoaded = true; |
| 63 | } |
| 64 | |
| 65 | assert(D); |
| 66 | |
Dmitri Gribenko | c3fee35 | 2012-06-28 16:19:39 +0000 | [diff] [blame] | 67 | // User can not attach documentation to implicit declarations. |
| 68 | if (D->isImplicit()) |
| 69 | return NULL; |
| 70 | |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 71 | // User can not attach documentation to implicit instantiations. |
| 72 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 73 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 74 | return NULL; |
| 75 | } |
| 76 | |
Dmitri Gribenko | dce750b | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 77 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 78 | if (VD->isStaticDataMember() && |
| 79 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) { |
| 84 | if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 85 | return NULL; |
| 86 | } |
| 87 | |
Dmitri Gribenko | d1e5c0d | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 88 | if (const ClassTemplateSpecializationDecl *CTSD = |
| 89 | dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 90 | TemplateSpecializationKind TSK = CTSD->getSpecializationKind(); |
| 91 | if (TSK == TSK_ImplicitInstantiation || |
| 92 | TSK == TSK_Undeclared) |
| 93 | return NULL; |
| 94 | } |
| 95 | |
Dmitri Gribenko | dce750b | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 96 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 97 | if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 98 | return NULL; |
| 99 | } |
Fariborz Jahanian | 099ecfb | 2013-04-17 21:05:20 +0000 | [diff] [blame] | 100 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 101 | // When tag declaration (but not definition!) is part of the |
| 102 | // decl-specifier-seq of some other declaration, it doesn't get comment |
| 103 | if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition()) |
| 104 | return NULL; |
| 105 | } |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 106 | // TODO: handle comments for function parameters properly. |
| 107 | if (isa<ParmVarDecl>(D)) |
| 108 | return NULL; |
| 109 | |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 110 | // TODO: we could look up template parameter documentation in the template |
| 111 | // documentation. |
| 112 | if (isa<TemplateTypeParmDecl>(D) || |
| 113 | isa<NonTypeTemplateParmDecl>(D) || |
| 114 | isa<TemplateTemplateParmDecl>(D)) |
| 115 | return NULL; |
| 116 | |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 117 | ArrayRef<RawComment *> RawComments = Comments.getComments(); |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 118 | |
| 119 | // If there are no comments anywhere, we won't find anything. |
| 120 | if (RawComments.empty()) |
| 121 | return NULL; |
| 122 | |
Dmitri Gribenko | abd56c8 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 123 | // Find declaration location. |
| 124 | // For Objective-C declarations we generally don't expect to have multiple |
| 125 | // declarators, thus use declaration starting location as the "declaration |
| 126 | // location". |
| 127 | // For all other declarations multiple declarators are used quite frequently, |
| 128 | // so we use the location of the identifier as the "declaration location". |
| 129 | SourceLocation DeclLoc; |
| 130 | if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) || |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 131 | isa<ObjCPropertyDecl>(D) || |
Dmitri Gribenko | c27bc80 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 132 | isa<RedeclarableTemplateDecl>(D) || |
| 133 | isa<ClassTemplateSpecializationDecl>(D)) |
Dmitri Gribenko | abd56c8 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 134 | DeclLoc = D->getLocStart(); |
| 135 | else |
| 136 | DeclLoc = D->getLocation(); |
| 137 | |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 138 | // If the declaration doesn't map directly to a location in a file, we |
| 139 | // can't find the comment. |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 140 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
| 141 | return NULL; |
| 142 | |
| 143 | // Find the comment that occurs just after this declaration. |
Dmitri Gribenko | a444f18 | 2012-07-17 22:01:09 +0000 | [diff] [blame] | 144 | ArrayRef<RawComment *>::iterator Comment; |
| 145 | { |
| 146 | // When searching for comments during parsing, the comment we are looking |
| 147 | // for is usually among the last two comments we parsed -- check them |
| 148 | // first. |
Dmitri Gribenko | 6fd7d30 | 2013-04-10 15:35:17 +0000 | [diff] [blame] | 149 | RawComment CommentAtDeclLoc( |
| 150 | SourceMgr, SourceRange(DeclLoc), false, |
| 151 | LangOpts.CommentOpts.ParseAllComments); |
Dmitri Gribenko | a444f18 | 2012-07-17 22:01:09 +0000 | [diff] [blame] | 152 | BeforeThanCompare<RawComment> Compare(SourceMgr); |
| 153 | ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1; |
| 154 | bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 155 | if (!Found && RawComments.size() >= 2) { |
| 156 | MaybeBeforeDecl--; |
| 157 | Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 158 | } |
| 159 | |
| 160 | if (Found) { |
| 161 | Comment = MaybeBeforeDecl + 1; |
| 162 | assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(), |
| 163 | &CommentAtDeclLoc, Compare)); |
| 164 | } else { |
| 165 | // Slow path. |
| 166 | Comment = std::lower_bound(RawComments.begin(), RawComments.end(), |
| 167 | &CommentAtDeclLoc, Compare); |
| 168 | } |
| 169 | } |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 170 | |
| 171 | // Decompose the location for the declaration and find the beginning of the |
| 172 | // file buffer. |
| 173 | std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc); |
| 174 | |
| 175 | // First check whether we have a trailing comment. |
| 176 | if (Comment != RawComments.end() && |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 177 | (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() && |
Dmitri Gribenko | 9c00676 | 2012-07-06 23:27:33 +0000 | [diff] [blame] | 178 | (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D))) { |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 179 | std::pair<FileID, unsigned> CommentBeginDecomp |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 180 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin()); |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 181 | // Check that Doxygen trailing comment comes after the declaration, starts |
| 182 | // on the same line and in the same file as the declaration. |
| 183 | if (DeclLocDecomp.first == CommentBeginDecomp.first && |
| 184 | SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) |
| 185 | == SourceMgr.getLineNumber(CommentBeginDecomp.first, |
| 186 | CommentBeginDecomp.second)) { |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 187 | return *Comment; |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | // The comment just after the declaration was not a trailing comment. |
| 192 | // Let's look at the previous comment. |
| 193 | if (Comment == RawComments.begin()) |
| 194 | return NULL; |
| 195 | --Comment; |
| 196 | |
| 197 | // Check that we actually have a non-member Doxygen comment. |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 198 | if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment()) |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 199 | return NULL; |
| 200 | |
| 201 | // Decompose the end of the comment. |
| 202 | std::pair<FileID, unsigned> CommentEndDecomp |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 203 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd()); |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 204 | |
| 205 | // If the comment and the declaration aren't in the same file, then they |
| 206 | // aren't related. |
| 207 | if (DeclLocDecomp.first != CommentEndDecomp.first) |
| 208 | return NULL; |
| 209 | |
| 210 | // Get the corresponding buffer. |
| 211 | bool Invalid = false; |
| 212 | const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first, |
| 213 | &Invalid).data(); |
| 214 | if (Invalid) |
| 215 | return NULL; |
| 216 | |
| 217 | // Extract text between the comment and declaration. |
| 218 | StringRef Text(Buffer + CommentEndDecomp.second, |
| 219 | DeclLocDecomp.second - CommentEndDecomp.second); |
| 220 | |
Dmitri Gribenko | 8bdb58a | 2012-06-27 23:43:37 +0000 | [diff] [blame] | 221 | // There should be no other declarations or preprocessor directives between |
| 222 | // comment and declaration. |
Dmitri Gribenko | abd56c8 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 223 | if (Text.find_first_of(",;{}#@") != StringRef::npos) |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 224 | return NULL; |
| 225 | |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 226 | return *Comment; |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 229 | namespace { |
| 230 | /// If we have a 'templated' declaration for a template, adjust 'D' to |
| 231 | /// refer to the actual template. |
Dmitri Gribenko | 2125c90 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 232 | /// If we have an implicit instantiation, adjust 'D' to refer to template. |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 233 | const Decl *adjustDeclToTemplate(const Decl *D) { |
Douglas Gregor | cd81df2 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 234 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Dmitri Gribenko | 2125c90 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 235 | // Is this function declaration part of a function template? |
Douglas Gregor | cd81df2 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 236 | if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) |
Dmitri Gribenko | 2125c90 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 237 | return FTD; |
| 238 | |
| 239 | // Nothing to do if function is not an implicit instantiation. |
| 240 | if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 241 | return D; |
| 242 | |
| 243 | // Function is an implicit instantiation of a function template? |
| 244 | if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate()) |
| 245 | return FTD; |
| 246 | |
| 247 | // Function is instantiated from a member definition of a class template? |
| 248 | if (const FunctionDecl *MemberDecl = |
| 249 | FD->getInstantiatedFromMemberFunction()) |
| 250 | return MemberDecl; |
| 251 | |
| 252 | return D; |
Douglas Gregor | cd81df2 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 253 | } |
Dmitri Gribenko | 2125c90 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 254 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 255 | // Static data member is instantiated from a member definition of a class |
| 256 | // template? |
| 257 | if (VD->isStaticDataMember()) |
| 258 | if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember()) |
| 259 | return MemberDecl; |
| 260 | |
| 261 | return D; |
| 262 | } |
| 263 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) { |
| 264 | // Is this class declaration part of a class template? |
| 265 | if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate()) |
| 266 | return CTD; |
| 267 | |
| 268 | // Class is an implicit instantiation of a class template or partial |
| 269 | // specialization? |
| 270 | if (const ClassTemplateSpecializationDecl *CTSD = |
| 271 | dyn_cast<ClassTemplateSpecializationDecl>(CRD)) { |
| 272 | if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation) |
| 273 | return D; |
| 274 | llvm::PointerUnion<ClassTemplateDecl *, |
| 275 | ClassTemplatePartialSpecializationDecl *> |
| 276 | PU = CTSD->getSpecializedTemplateOrPartial(); |
| 277 | return PU.is<ClassTemplateDecl*>() ? |
| 278 | static_cast<const Decl*>(PU.get<ClassTemplateDecl *>()) : |
| 279 | static_cast<const Decl*>( |
| 280 | PU.get<ClassTemplatePartialSpecializationDecl *>()); |
| 281 | } |
| 282 | |
| 283 | // Class is instantiated from a member definition of a class template? |
| 284 | if (const MemberSpecializationInfo *Info = |
| 285 | CRD->getMemberSpecializationInfo()) |
| 286 | return Info->getInstantiatedFrom(); |
| 287 | |
| 288 | return D; |
| 289 | } |
| 290 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 291 | // Enum is instantiated from a member definition of a class template? |
| 292 | if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum()) |
| 293 | return MemberDecl; |
| 294 | |
| 295 | return D; |
| 296 | } |
| 297 | // FIXME: Adjust alias templates? |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 298 | return D; |
| 299 | } |
| 300 | } // unnamed namespace |
| 301 | |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 302 | const RawComment *ASTContext::getRawCommentForAnyRedecl( |
| 303 | const Decl *D, |
| 304 | const Decl **OriginalDecl) const { |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 305 | D = adjustDeclToTemplate(D); |
Douglas Gregor | cd81df2 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 306 | |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 307 | // Check whether we have cached a comment for this declaration already. |
| 308 | { |
| 309 | llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos = |
| 310 | RedeclComments.find(D); |
| 311 | if (Pos != RedeclComments.end()) { |
| 312 | const RawCommentAndCacheFlags &Raw = Pos->second; |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 313 | if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) { |
| 314 | if (OriginalDecl) |
| 315 | *OriginalDecl = Raw.getOriginalDecl(); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 316 | return Raw.getRaw(); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 317 | } |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 318 | } |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 319 | } |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 320 | |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 321 | // Search for comments attached to declarations in the redeclaration chain. |
| 322 | const RawComment *RC = NULL; |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 323 | const Decl *OriginalDeclForRC = NULL; |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 324 | for (Decl::redecl_iterator I = D->redecls_begin(), |
| 325 | E = D->redecls_end(); |
| 326 | I != E; ++I) { |
| 327 | llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos = |
| 328 | RedeclComments.find(*I); |
| 329 | if (Pos != RedeclComments.end()) { |
| 330 | const RawCommentAndCacheFlags &Raw = Pos->second; |
| 331 | if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) { |
| 332 | RC = Raw.getRaw(); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 333 | OriginalDeclForRC = Raw.getOriginalDecl(); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | } else { |
| 337 | RC = getRawCommentForDeclNoCache(*I); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 338 | OriginalDeclForRC = *I; |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 339 | RawCommentAndCacheFlags Raw; |
| 340 | if (RC) { |
| 341 | Raw.setRaw(RC); |
| 342 | Raw.setKind(RawCommentAndCacheFlags::FromDecl); |
| 343 | } else |
| 344 | Raw.setKind(RawCommentAndCacheFlags::NoCommentInDecl); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 345 | Raw.setOriginalDecl(*I); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 346 | RedeclComments[*I] = Raw; |
| 347 | if (RC) |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | |
Dmitri Gribenko | 8376f59 | 2012-06-28 16:25:36 +0000 | [diff] [blame] | 352 | // If we found a comment, it should be a documentation comment. |
| 353 | assert(!RC || RC->isDocumentation()); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 354 | |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 355 | if (OriginalDecl) |
| 356 | *OriginalDecl = OriginalDeclForRC; |
| 357 | |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 358 | // Update cache for every declaration in the redeclaration chain. |
| 359 | RawCommentAndCacheFlags Raw; |
| 360 | Raw.setRaw(RC); |
| 361 | Raw.setKind(RawCommentAndCacheFlags::FromRedecl); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 362 | Raw.setOriginalDecl(OriginalDeclForRC); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 363 | |
| 364 | for (Decl::redecl_iterator I = D->redecls_begin(), |
| 365 | E = D->redecls_end(); |
| 366 | I != E; ++I) { |
| 367 | RawCommentAndCacheFlags &R = RedeclComments[*I]; |
| 368 | if (R.getKind() == RawCommentAndCacheFlags::NoCommentInDecl) |
| 369 | R = Raw; |
| 370 | } |
| 371 | |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 372 | return RC; |
| 373 | } |
| 374 | |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 375 | static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod, |
| 376 | SmallVectorImpl<const NamedDecl *> &Redeclared) { |
| 377 | const DeclContext *DC = ObjCMethod->getDeclContext(); |
| 378 | if (const ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(DC)) { |
| 379 | const ObjCInterfaceDecl *ID = IMD->getClassInterface(); |
| 380 | if (!ID) |
| 381 | return; |
| 382 | // Add redeclared method here. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 383 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 384 | Ext = ID->known_extensions_begin(), |
| 385 | ExtEnd = ID->known_extensions_end(); |
| 386 | Ext != ExtEnd; ++Ext) { |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 387 | if (ObjCMethodDecl *RedeclaredMethod = |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 388 | Ext->getMethod(ObjCMethod->getSelector(), |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 389 | ObjCMethod->isInstanceMethod())) |
| 390 | Redeclared.push_back(RedeclaredMethod); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
Fariborz Jahanian | 749ace6 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 395 | comments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC, |
| 396 | const Decl *D) const { |
| 397 | comments::DeclInfo *ThisDeclInfo = new (*this) comments::DeclInfo; |
| 398 | ThisDeclInfo->CommentDecl = D; |
| 399 | ThisDeclInfo->IsFilled = false; |
| 400 | ThisDeclInfo->fill(); |
| 401 | ThisDeclInfo->CommentDecl = FC->getDecl(); |
| 402 | comments::FullComment *CFC = |
| 403 | new (*this) comments::FullComment(FC->getBlocks(), |
| 404 | ThisDeclInfo); |
| 405 | return CFC; |
| 406 | |
| 407 | } |
| 408 | |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 409 | comments::FullComment *ASTContext::getCommentForDecl( |
| 410 | const Decl *D, |
| 411 | const Preprocessor *PP) const { |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 412 | D = adjustDeclToTemplate(D); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 413 | |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 414 | const Decl *Canonical = D->getCanonicalDecl(); |
| 415 | llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos = |
| 416 | ParsedComments.find(Canonical); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 417 | |
| 418 | if (Pos != ParsedComments.end()) { |
Fariborz Jahanian | 749ace6 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 419 | if (Canonical != D) { |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 420 | comments::FullComment *FC = Pos->second; |
Fariborz Jahanian | 749ace6 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 421 | comments::FullComment *CFC = cloneFullComment(FC, D); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 422 | return CFC; |
| 423 | } |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 424 | return Pos->second; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 427 | const Decl *OriginalDecl; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 428 | |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 429 | const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 430 | if (!RC) { |
| 431 | if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) { |
Dmitri Gribenko | 1e905da | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 432 | SmallVector<const NamedDecl*, 8> Overridden; |
Fariborz Jahanian | c328d9c | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 433 | const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 23799e3 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 434 | if (OMD && OMD->isPropertyAccessor()) |
| 435 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
| 436 | if (comments::FullComment *FC = getCommentForDecl(PDecl, PP)) |
| 437 | return cloneFullComment(FC, D); |
Fariborz Jahanian | c328d9c | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 438 | if (OMD) |
Dmitri Gribenko | 1e905da | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 439 | addRedeclaredMethods(OMD, Overridden); |
| 440 | getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden); |
Fariborz Jahanian | 23799e3 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 441 | for (unsigned i = 0, e = Overridden.size(); i < e; i++) |
| 442 | if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP)) |
| 443 | return cloneFullComment(FC, D); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 444 | } |
Fariborz Jahanian | 4857fdc | 2013-05-02 15:44:16 +0000 | [diff] [blame] | 445 | else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Dmitri Gribenko | d1e5c0d | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 446 | // Attach any tag type's documentation to its typedef if latter |
Fariborz Jahanian | 23799e3 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 447 | // does not have one of its own. |
Fariborz Jahanian | 41170b5 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 448 | QualType QT = TD->getUnderlyingType(); |
Dmitri Gribenko | d1e5c0d | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 449 | if (const TagType *TT = QT->getAs<TagType>()) |
| 450 | if (const Decl *TD = TT->getDecl()) |
| 451 | if (comments::FullComment *FC = getCommentForDecl(TD, PP)) |
Fariborz Jahanian | 23799e3 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 452 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 41170b5 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 453 | } |
Fariborz Jahanian | 622bb4a | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 454 | else if (const ObjCInterfaceDecl *IC = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 455 | while (IC->getSuperClass()) { |
| 456 | IC = IC->getSuperClass(); |
| 457 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 458 | return cloneFullComment(FC, D); |
| 459 | } |
| 460 | } |
| 461 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
| 462 | if (const ObjCInterfaceDecl *IC = CD->getClassInterface()) |
| 463 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 464 | return cloneFullComment(FC, D); |
| 465 | } |
| 466 | else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 467 | if (!(RD = RD->getDefinition())) |
| 468 | return NULL; |
| 469 | // Check non-virtual bases. |
| 470 | for (CXXRecordDecl::base_class_const_iterator I = |
| 471 | RD->bases_begin(), E = RD->bases_end(); I != E; ++I) { |
Fariborz Jahanian | 91efca0 | 2013-04-26 23:34:36 +0000 | [diff] [blame] | 472 | if (I->isVirtual() || (I->getAccessSpecifier() != AS_public)) |
Fariborz Jahanian | 622bb4a | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 473 | continue; |
| 474 | QualType Ty = I->getType(); |
| 475 | if (Ty.isNull()) |
| 476 | continue; |
| 477 | if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) { |
| 478 | if (!(NonVirtualBase= NonVirtualBase->getDefinition())) |
| 479 | continue; |
| 480 | |
| 481 | if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP)) |
| 482 | return cloneFullComment(FC, D); |
| 483 | } |
| 484 | } |
| 485 | // Check virtual bases. |
| 486 | for (CXXRecordDecl::base_class_const_iterator I = |
| 487 | RD->vbases_begin(), E = RD->vbases_end(); I != E; ++I) { |
Fariborz Jahanian | 91efca0 | 2013-04-26 23:34:36 +0000 | [diff] [blame] | 488 | if (I->getAccessSpecifier() != AS_public) |
| 489 | continue; |
Fariborz Jahanian | 622bb4a | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 490 | QualType Ty = I->getType(); |
| 491 | if (Ty.isNull()) |
| 492 | continue; |
| 493 | if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) { |
| 494 | if (!(VirtualBase= VirtualBase->getDefinition())) |
| 495 | continue; |
| 496 | if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP)) |
| 497 | return cloneFullComment(FC, D); |
| 498 | } |
| 499 | } |
| 500 | } |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 501 | return NULL; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Dmitri Gribenko | 4b41c65 | 2012-08-22 18:12:19 +0000 | [diff] [blame] | 504 | // If the RawComment was attached to other redeclaration of this Decl, we |
| 505 | // should parse the comment in context of that other Decl. This is important |
| 506 | // because comments can contain references to parameter names which can be |
| 507 | // different across redeclarations. |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 508 | if (D != OriginalDecl) |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 509 | return getCommentForDecl(OriginalDecl, PP); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 510 | |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 511 | comments::FullComment *FC = RC->parse(*this, PP, D); |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 512 | ParsedComments[Canonical] = FC; |
| 513 | return FC; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 516 | void |
| 517 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 518 | TemplateTemplateParmDecl *Parm) { |
| 519 | ID.AddInteger(Parm->getDepth()); |
| 520 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 521 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 522 | |
| 523 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 524 | ID.AddInteger(Params->size()); |
| 525 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 526 | PEnd = Params->end(); |
| 527 | P != PEnd; ++P) { |
| 528 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 529 | ID.AddInteger(0); |
| 530 | ID.AddBoolean(TTP->isParameterPack()); |
| 531 | continue; |
| 532 | } |
| 533 | |
| 534 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 535 | ID.AddInteger(1); |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 536 | ID.AddBoolean(NTTP->isParameterPack()); |
Eli Friedman | 9e9c454 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 537 | ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr()); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 538 | if (NTTP->isExpandedParameterPack()) { |
| 539 | ID.AddBoolean(true); |
| 540 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
Eli Friedman | 9e9c454 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 541 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 542 | QualType T = NTTP->getExpansionType(I); |
| 543 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); |
| 544 | } |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 545 | } else |
| 546 | ID.AddBoolean(false); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 547 | continue; |
| 548 | } |
| 549 | |
| 550 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 551 | ID.AddInteger(2); |
| 552 | Profile(ID, TTP); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | TemplateTemplateParmDecl * |
| 557 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 558 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 559 | // Check if we already have a canonical template template parameter. |
| 560 | llvm::FoldingSetNodeID ID; |
| 561 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 562 | void *InsertPos = 0; |
| 563 | CanonicalTemplateTemplateParm *Canonical |
| 564 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 565 | if (Canonical) |
| 566 | return Canonical->getParam(); |
| 567 | |
| 568 | // Build a canonical template parameter list. |
| 569 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 570 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 571 | CanonParams.reserve(Params->size()); |
| 572 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 573 | PEnd = Params->end(); |
| 574 | P != PEnd; ++P) { |
| 575 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 576 | CanonParams.push_back( |
| 577 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 578 | SourceLocation(), |
| 579 | SourceLocation(), |
| 580 | TTP->getDepth(), |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 581 | TTP->getIndex(), 0, false, |
| 582 | TTP->isParameterPack())); |
| 583 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 584 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 585 | QualType T = getCanonicalType(NTTP->getType()); |
| 586 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 587 | NonTypeTemplateParmDecl *Param; |
| 588 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 589 | SmallVector<QualType, 2> ExpandedTypes; |
| 590 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 591 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 592 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 593 | ExpandedTInfos.push_back( |
| 594 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 595 | } |
| 596 | |
| 597 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 598 | SourceLocation(), |
| 599 | SourceLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 600 | NTTP->getDepth(), |
| 601 | NTTP->getPosition(), 0, |
| 602 | T, |
| 603 | TInfo, |
| 604 | ExpandedTypes.data(), |
| 605 | ExpandedTypes.size(), |
| 606 | ExpandedTInfos.data()); |
| 607 | } else { |
| 608 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 609 | SourceLocation(), |
| 610 | SourceLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 611 | NTTP->getDepth(), |
| 612 | NTTP->getPosition(), 0, |
| 613 | T, |
| 614 | NTTP->isParameterPack(), |
| 615 | TInfo); |
| 616 | } |
| 617 | CanonParams.push_back(Param); |
| 618 | |
| 619 | } else |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 620 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 621 | cast<TemplateTemplateParmDecl>(*P))); |
| 622 | } |
| 623 | |
| 624 | TemplateTemplateParmDecl *CanonTTP |
| 625 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 626 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 627 | TTP->getPosition(), |
| 628 | TTP->isParameterPack(), |
| 629 | 0, |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 630 | TemplateParameterList::Create(*this, SourceLocation(), |
| 631 | SourceLocation(), |
| 632 | CanonParams.data(), |
| 633 | CanonParams.size(), |
| 634 | SourceLocation())); |
| 635 | |
| 636 | // Get the new insert position for the node we care about. |
| 637 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 638 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 639 | (void)Canonical; |
| 640 | |
| 641 | // Create the canonical template template parameter entry. |
| 642 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 643 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 644 | return CanonTTP; |
| 645 | } |
| 646 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 647 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 648 | if (!LangOpts.CPlusPlus) return 0; |
| 649 | |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 650 | switch (T.getCXXABI().getKind()) { |
| 651 | case TargetCXXABI::GenericARM: |
| 652 | case TargetCXXABI::iOS: |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 653 | return CreateARMCXXABI(*this); |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 654 | case TargetCXXABI::GenericAArch64: // Same as Itanium at this level |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 655 | case TargetCXXABI::GenericItanium: |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 656 | return CreateItaniumCXXABI(*this); |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 657 | case TargetCXXABI::Microsoft: |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 658 | return CreateMicrosoftCXXABI(*this); |
| 659 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 660 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 663 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 664 | const LangOptions &LOpts) { |
| 665 | if (LOpts.FakeAddressSpaceMap) { |
| 666 | // The fake address space map must have a distinct entry for each |
| 667 | // language-specific address space. |
| 668 | static const unsigned FakeAddrSpaceMap[] = { |
| 669 | 1, // opencl_global |
| 670 | 2, // opencl_local |
Peter Collingbourne | 4dc34eb | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 671 | 3, // opencl_constant |
| 672 | 4, // cuda_device |
| 673 | 5, // cuda_constant |
| 674 | 6 // cuda_shared |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 675 | }; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 676 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 677 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 678 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
Douglas Gregor | 3e3cd93 | 2011-09-01 20:23:19 +0000 | [diff] [blame] | 682 | ASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 683 | const TargetInfo *t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 684 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 685 | Builtin::Context &builtins, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 686 | unsigned size_reserve, |
| 687 | bool DelayInitialization) |
| 688 | : FunctionProtoTypes(this_()), |
| 689 | TemplateSpecializationTypes(this_()), |
| 690 | DependentTemplateSpecializationTypes(this_()), |
| 691 | SubstTemplateTemplateParmPacks(this_()), |
| 692 | GlobalNestedNameSpecifier(0), |
| 693 | Int128Decl(0), UInt128Decl(0), |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 694 | BuiltinVaListDecl(0), |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 695 | ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0), |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 696 | BOOLDecl(0), |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 697 | CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 698 | FILEDecl(0), |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 699 | jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0), |
| 700 | BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 701 | cudaConfigureCallDecl(0), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 702 | NullTypeSourceInfo(QualType()), |
| 703 | FirstLocalImport(), LastLocalImport(), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 704 | SourceMgr(SM), LangOpts(LOpts), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 705 | AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 706 | Idents(idents), Selectors(sels), |
| 707 | BuiltinInfo(builtins), |
| 708 | DeclarationNames(*this), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 709 | ExternalSource(0), Listener(0), |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 710 | Comments(SM), CommentsLoaded(false), |
Dmitri Gribenko | 6ebf091 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 711 | CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 712 | LastSDM(0, 0), |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 713 | UniqueBlockByRefTypeID(0) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 714 | { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 716 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 717 | |
| 718 | if (!DelayInitialization) { |
| 719 | assert(t && "No target supplied for ASTContext initialization"); |
| 720 | InitBuiltinTypes(*t); |
| 721 | } |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 724 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 725 | // Release the DenseMaps associated with DeclContext objects. |
| 726 | // FIXME: Is this the ideal solution? |
| 727 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 728 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 729 | // Call all of the deallocation functions. |
| 730 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 731 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 732 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 733 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 734 | // because they can contain DenseMaps. |
| 735 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 736 | const ASTRecordLayout*>::iterator |
| 737 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 738 | // Increment in loop to prevent using deallocated memory. |
| 739 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 740 | R->Destroy(*this); |
| 741 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 742 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 743 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 744 | // Increment in loop to prevent using deallocated memory. |
| 745 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 746 | R->Destroy(*this); |
| 747 | } |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 748 | |
| 749 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 750 | AEnd = DeclAttrs.end(); |
| 751 | A != AEnd; ++A) |
| 752 | A->second->~AttrVec(); |
| 753 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 754 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 755 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 756 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 757 | } |
| 758 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | void |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 760 | ASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 761 | ExternalSource.reset(Source.take()); |
| 762 | } |
| 763 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 764 | void ASTContext::PrintStats() const { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 765 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 766 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 767 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 768 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 769 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 770 | #define ABSTRACT_TYPE(Name, Parent) |
| 771 | #include "clang/AST/TypeNodes.def" |
| 772 | 0 // Extra |
| 773 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 774 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 775 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 776 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 777 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 780 | unsigned Idx = 0; |
| 781 | unsigned TotalBytes = 0; |
| 782 | #define TYPE(Name, Parent) \ |
| 783 | if (counts[Idx]) \ |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 784 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 785 | << " types\n"; \ |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 786 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 787 | ++Idx; |
| 788 | #define ABSTRACT_TYPE(Name, Parent) |
| 789 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 791 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 792 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 793 | // Implicit special member functions. |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 794 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 795 | << NumImplicitDefaultConstructors |
| 796 | << " implicit default constructors created\n"; |
| 797 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 798 | << NumImplicitCopyConstructors |
| 799 | << " implicit copy constructors created\n"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 800 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 801 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 802 | << NumImplicitMoveConstructors |
| 803 | << " implicit move constructors created\n"; |
| 804 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 805 | << NumImplicitCopyAssignmentOperators |
| 806 | << " implicit copy assignment operators created\n"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 807 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 808 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 809 | << NumImplicitMoveAssignmentOperators |
| 810 | << " implicit move assignment operators created\n"; |
| 811 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 812 | << NumImplicitDestructors |
| 813 | << " implicit destructors created\n"; |
| 814 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 815 | if (ExternalSource.get()) { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 816 | llvm::errs() << "\n"; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 817 | ExternalSource->PrintStats(); |
| 818 | } |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 819 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 820 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Douglas Gregor | 772eeae | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 823 | TypedefDecl *ASTContext::getInt128Decl() const { |
| 824 | if (!Int128Decl) { |
| 825 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty); |
| 826 | Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 827 | getTranslationUnitDecl(), |
| 828 | SourceLocation(), |
| 829 | SourceLocation(), |
| 830 | &Idents.get("__int128_t"), |
| 831 | TInfo); |
| 832 | } |
| 833 | |
| 834 | return Int128Decl; |
| 835 | } |
| 836 | |
| 837 | TypedefDecl *ASTContext::getUInt128Decl() const { |
| 838 | if (!UInt128Decl) { |
| 839 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty); |
| 840 | UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 841 | getTranslationUnitDecl(), |
| 842 | SourceLocation(), |
| 843 | SourceLocation(), |
| 844 | &Idents.get("__uint128_t"), |
| 845 | TInfo); |
| 846 | } |
| 847 | |
| 848 | return UInt128Decl; |
| 849 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 850 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 851 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 852 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 853 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 854 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 857 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target) { |
| 858 | assert((!this->Target || this->Target == &Target) && |
| 859 | "Incorrect target reinitialization"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 860 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 862 | this->Target = &Target; |
| 863 | |
| 864 | ABI.reset(createCXXABI(Target)); |
| 865 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
| 866 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 867 | // C99 6.2.5p19. |
| 868 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 869 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 870 | // C99 6.2.5p2. |
| 871 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 872 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 873 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 874 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 875 | else |
| 876 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 877 | // C99 6.2.5p4. |
| 878 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 879 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 880 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 881 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 882 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 884 | // C99 6.2.5p6. |
| 885 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 886 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 887 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 888 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 889 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 891 | // C99 6.2.5p10. |
| 892 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 893 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 894 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 895 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 896 | // GNU extension, 128-bit integers. |
| 897 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 898 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 899 | |
Hans Wennborg | 15f92ba | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 900 | // C++ 3.9.1p5 |
| 901 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
| 902 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 903 | else // -fshort-wchar makes wchar_t be unsigned. |
| 904 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 905 | if (LangOpts.CPlusPlus && LangOpts.WChar) |
| 906 | WideCharTy = WCharTy; |
| 907 | else { |
| 908 | // C99 (or C++ using -fno-wchar). |
| 909 | WideCharTy = getFromTargetType(Target.getWCharType()); |
| 910 | } |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 911 | |
James Molloy | 392da48 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 912 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 913 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 914 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 915 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 916 | else // C99 |
| 917 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 918 | |
| 919 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 920 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 921 | else // C99 |
| 922 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 923 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 924 | // Placeholder type for type-dependent expressions whose type is |
| 925 | // completely unknown. No code should ever check a type against |
| 926 | // DependentTy and users should never see it; however, it is here to |
| 927 | // help diagnose failures to properly check for type-dependent |
| 928 | // expressions. |
| 929 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 930 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 931 | // Placeholder type for functions. |
| 932 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 933 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 934 | // Placeholder type for bound members. |
| 935 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 936 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 937 | // Placeholder type for pseudo-objects. |
| 938 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 939 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 940 | // "any" type; useful for debugger-like clients. |
| 941 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 942 | |
John McCall | 0ddaeb9 | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 943 | // Placeholder type for unbridged ARC casts. |
| 944 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 945 | |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 946 | // Placeholder type for builtin functions. |
| 947 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
| 948 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 949 | // C99 6.2.5p11. |
| 950 | FloatComplexTy = getComplexType(FloatTy); |
| 951 | DoubleComplexTy = getComplexType(DoubleTy); |
| 952 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 953 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 954 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 955 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 956 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 957 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 958 | |
| 959 | if (LangOpts.OpenCL) { |
| 960 | InitBuiltinType(OCLImage1dTy, BuiltinType::OCLImage1d); |
| 961 | InitBuiltinType(OCLImage1dArrayTy, BuiltinType::OCLImage1dArray); |
| 962 | InitBuiltinType(OCLImage1dBufferTy, BuiltinType::OCLImage1dBuffer); |
| 963 | InitBuiltinType(OCLImage2dTy, BuiltinType::OCLImage2d); |
| 964 | InitBuiltinType(OCLImage2dArrayTy, BuiltinType::OCLImage2dArray); |
| 965 | InitBuiltinType(OCLImage3dTy, BuiltinType::OCLImage3d); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 966 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 967 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 968 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 969 | } |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 970 | |
| 971 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 93a4994 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 972 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 973 | SignedCharTy : BoolTy); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 974 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 975 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 976 | |
| 977 | ObjCSuperType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 979 | // void * type |
| 980 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 981 | |
| 982 | // nullptr type (C++0x 2.14.7) |
| 983 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 984 | |
| 985 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 986 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 987 | |
| 988 | // Builtin type used to help define __builtin_va_list. |
| 989 | VaListTagTy = QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 990 | } |
| 991 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 992 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 993 | return SourceMgr.getDiagnostics(); |
| 994 | } |
| 995 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 996 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 997 | AttrVec *&Result = DeclAttrs[D]; |
| 998 | if (!Result) { |
| 999 | void *Mem = Allocate(sizeof(AttrVec)); |
| 1000 | Result = new (Mem) AttrVec; |
| 1001 | } |
| 1002 | |
| 1003 | return *Result; |
| 1004 | } |
| 1005 | |
| 1006 | /// \brief Erase the attributes corresponding to the given declaration. |
| 1007 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 1008 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 1009 | if (Pos != DeclAttrs.end()) { |
| 1010 | Pos->second->~AttrVec(); |
| 1011 | DeclAttrs.erase(Pos); |
| 1012 | } |
| 1013 | } |
| 1014 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1015 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1016 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1017 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1018 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1019 | = InstantiatedFromStaticDataMember.find(Var); |
| 1020 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 1021 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1023 | return Pos->second; |
| 1024 | } |
| 1025 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1026 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1027 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 1028 | TemplateSpecializationKind TSK, |
| 1029 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1030 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 1031 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 1032 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 1033 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1034 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 1035 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1038 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 1039 | const FunctionDecl *FD){ |
| 1040 | assert(FD && "Specialization is 0"); |
| 1041 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1042 | = ClassScopeSpecializationPattern.find(FD); |
| 1043 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1044 | return 0; |
| 1045 | |
| 1046 | return Pos->second; |
| 1047 | } |
| 1048 | |
| 1049 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 1050 | FunctionDecl *Pattern) { |
| 1051 | assert(FD && "Specialization is 0"); |
| 1052 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1053 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1056 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1057 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1058 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1059 | = InstantiatedFromUsingDecl.find(UUD); |
| 1060 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1061 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1063 | return Pos->second; |
| 1064 | } |
| 1065 | |
| 1066 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1067 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 1068 | assert((isa<UsingDecl>(Pattern) || |
| 1069 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 1070 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 1071 | "pattern decl is not a using decl"); |
| 1072 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 1073 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 1074 | } |
| 1075 | |
| 1076 | UsingShadowDecl * |
| 1077 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 1078 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 1079 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 1080 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 1081 | return 0; |
| 1082 | |
| 1083 | return Pos->second; |
| 1084 | } |
| 1085 | |
| 1086 | void |
| 1087 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 1088 | UsingShadowDecl *Pattern) { |
| 1089 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 1090 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1093 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 1094 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 1095 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 1096 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 1097 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1099 | return Pos->second; |
| 1100 | } |
| 1101 | |
| 1102 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 1103 | FieldDecl *Tmpl) { |
| 1104 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 1105 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 1106 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 1107 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1109 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 1110 | } |
| 1111 | |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 1112 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 1113 | const FieldDecl *LastFD) const { |
| 1114 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1115 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 1118 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 1119 | const FieldDecl *LastFD) const { |
| 1120 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1121 | FD->getBitWidthValue(*this) == 0 && |
| 1122 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1125 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 1126 | const FieldDecl *LastFD) const { |
| 1127 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1128 | FD->getBitWidthValue(*this) && |
| 1129 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1132 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1133 | const FieldDecl *LastFD) const { |
| 1134 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1135 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1138 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1139 | const FieldDecl *LastFD) const { |
| 1140 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1141 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1144 | ASTContext::overridden_cxx_method_iterator |
| 1145 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 1146 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1147 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1148 | if (Pos == OverriddenMethods.end()) |
| 1149 | return 0; |
| 1150 | |
| 1151 | return Pos->second.begin(); |
| 1152 | } |
| 1153 | |
| 1154 | ASTContext::overridden_cxx_method_iterator |
| 1155 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 1156 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1157 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1158 | if (Pos == OverriddenMethods.end()) |
| 1159 | return 0; |
| 1160 | |
| 1161 | return Pos->second.end(); |
| 1162 | } |
| 1163 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1164 | unsigned |
| 1165 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 1166 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1167 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1168 | if (Pos == OverriddenMethods.end()) |
| 1169 | return 0; |
| 1170 | |
| 1171 | return Pos->second.size(); |
| 1172 | } |
| 1173 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1174 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 1175 | const CXXMethodDecl *Overridden) { |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1176 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1177 | OverriddenMethods[Method].push_back(Overridden); |
| 1178 | } |
| 1179 | |
Dmitri Gribenko | 1e905da | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 1180 | void ASTContext::getOverriddenMethods( |
| 1181 | const NamedDecl *D, |
| 1182 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1183 | assert(D); |
| 1184 | |
| 1185 | if (const CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 685d104 | 2013-04-17 00:09:03 +0000 | [diff] [blame] | 1186 | Overridden.append(overridden_methods_begin(CXXMethod), |
| 1187 | overridden_methods_end(CXXMethod)); |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1188 | return; |
| 1189 | } |
| 1190 | |
| 1191 | const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); |
| 1192 | if (!Method) |
| 1193 | return; |
| 1194 | |
Argyrios Kyrtzidis | 740ae67 | 2012-10-09 18:19:01 +0000 | [diff] [blame] | 1195 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
| 1196 | Method->getOverriddenMethods(OverDecls); |
Argyrios Kyrtzidis | bc0a2bb | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1197 | Overridden.append(OverDecls.begin(), OverDecls.end()); |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1200 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 1201 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 1202 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 1203 | if (!FirstLocalImport) { |
| 1204 | FirstLocalImport = Import; |
| 1205 | LastLocalImport = Import; |
| 1206 | return; |
| 1207 | } |
| 1208 | |
| 1209 | LastLocalImport->NextLocalImport = Import; |
| 1210 | LastLocalImport = Import; |
| 1211 | } |
| 1212 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1213 | //===----------------------------------------------------------------------===// |
| 1214 | // Type Sizing and Analysis |
| 1215 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1216 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1217 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 1218 | /// scalar floating point type. |
| 1219 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1220 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1221 | assert(BT && "Not a floating point type!"); |
| 1222 | switch (BT->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1223 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1224 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1225 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 1226 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 1227 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1231 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1232 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 1233 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1234 | /// If @p RefAsPointee, references are treated like their underlying type |
| 1235 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1236 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1237 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1238 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1239 | bool UseAlignAttrOnly = false; |
| 1240 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 1241 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1242 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1243 | // __attribute__((aligned)) can increase or decrease alignment |
| 1244 | // *except* on a struct or struct member, where it only increases |
| 1245 | // alignment unless 'packed' is also specified. |
| 1246 | // |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1247 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1248 | // ignore that possibility; Sema should diagnose it. |
| 1249 | if (isa<FieldDecl>(D)) { |
| 1250 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 1251 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 1252 | } else { |
| 1253 | UseAlignAttrOnly = true; |
| 1254 | } |
| 1255 | } |
Fariborz Jahanian | 78a7d7d | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1256 | else if (isa<FieldDecl>(D)) |
| 1257 | UseAlignAttrOnly = |
| 1258 | D->hasAttr<PackedAttr>() || |
| 1259 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1260 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1261 | // If we're using the align attribute only, just ignore everything |
| 1262 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1263 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1264 | // do nothing |
| 1265 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1266 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1267 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1268 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1269 | if (RefAsPointee) |
| 1270 | T = RT->getPointeeType(); |
| 1271 | else |
| 1272 | T = getPointerType(RT->getPointeeType()); |
| 1273 | } |
| 1274 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1275 | // Adjust alignments of declarations with array type by the |
| 1276 | // large-array alignment on the target. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1277 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1278 | const ArrayType *arrayType; |
| 1279 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 1280 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1281 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1282 | else if (isa<ConstantArrayType>(arrayType) && |
| 1283 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1284 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1285 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1286 | // Walk through any array types while we're at it. |
| 1287 | T = getBaseElementType(arrayType); |
| 1288 | } |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 1289 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Ulrich Weigand | 6b20351 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1290 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1291 | if (VD->hasGlobalStorage()) |
| 1292 | Align = std::max(Align, getTargetInfo().getMinGlobalAlign()); |
| 1293 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1294 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1295 | |
| 1296 | // Fields can be subject to extra alignment constraints, like if |
| 1297 | // the field is packed, the struct is packed, or the struct has a |
| 1298 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 1299 | // the actual alignment of the field within the struct, and then |
| 1300 | // (as we're expected to) constrain that by the alignment of the type. |
| 1301 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 1302 | // So calculate the alignment of the field. |
| 1303 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 1304 | |
| 1305 | // Start with the record's overall alignment. |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1306 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1307 | |
| 1308 | // Use the GCD of that and the offset within the record. |
| 1309 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 1310 | if (offset > 0) { |
| 1311 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 1312 | // which means we get to do this crazy thing instead of Euclid's. |
| 1313 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 1314 | if (lowBitOfOffset < fieldAlign) |
| 1315 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 1316 | } |
| 1317 | |
| 1318 | Align = std::min(Align, fieldAlign); |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 1319 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1320 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1321 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1322 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1323 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1324 | |
John McCall | 929bbfb | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1325 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
| 1326 | // chars. If the type is a record, its data size is returned. This is |
| 1327 | // the size of the memcpy that's performed when assigning this type |
| 1328 | // using a trivial copy/move assignment operator. |
| 1329 | std::pair<CharUnits, CharUnits> |
| 1330 | ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
| 1331 | std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T); |
| 1332 | |
| 1333 | // In C++, objects can sometimes be allocated into the tail padding |
| 1334 | // of a base-class subobject. We decide whether that's possible |
| 1335 | // during class layout, so here we can just trust the layout results. |
| 1336 | if (getLangOpts().CPlusPlus) { |
| 1337 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1338 | const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl()); |
| 1339 | sizeAndAlign.first = layout.getDataSize(); |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | return sizeAndAlign; |
| 1344 | } |
| 1345 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1346 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1347 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1348 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1349 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 1350 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1354 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1355 | return getTypeInfoInChars(T.getTypePtr()); |
| 1356 | } |
| 1357 | |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1358 | std::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const { |
| 1359 | TypeInfoMap::iterator it = MemoizedTypeInfo.find(T); |
| 1360 | if (it != MemoizedTypeInfo.end()) |
| 1361 | return it->second; |
| 1362 | |
| 1363 | std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T); |
| 1364 | MemoizedTypeInfo.insert(std::make_pair(T, Info)); |
| 1365 | return Info; |
| 1366 | } |
| 1367 | |
| 1368 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 1369 | /// method does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1370 | /// |
| 1371 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 1372 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1373 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1374 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1375 | ASTContext::getTypeInfoImpl(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 1376 | uint64_t Width=0; |
| 1377 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1378 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1379 | #define TYPE(Class, Base) |
| 1380 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1381 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1382 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1383 | #include "clang/AST/TypeNodes.def" |
John McCall | d3d49bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1384 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1385 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1386 | case Type::FunctionNoProto: |
| 1387 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1388 | // GCC extension: alignof(function) = 32 bits |
| 1389 | Width = 0; |
| 1390 | Align = 32; |
| 1391 | break; |
| 1392 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1393 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1394 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1395 | Width = 0; |
| 1396 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1397 | break; |
| 1398 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1399 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1400 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1402 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1403 | uint64_t Size = CAT->getSize().getZExtValue(); |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1404 | assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && |
| 1405 | "Overflow in array type bit size evaluation"); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1406 | Width = EltInfo.first*Size; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1407 | Align = EltInfo.second; |
Argyrios Kyrtzidis | cd88b41 | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 1408 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1409 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1410 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1411 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1412 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1413 | const VectorType *VT = cast<VectorType>(T); |
| 1414 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 1415 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1416 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1417 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1418 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1419 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1420 | Align = llvm::NextPowerOf2(Align); |
| 1421 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 1422 | } |
Chad Rosier | f9e9af7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1423 | // Adjust the alignment based on the target max. |
| 1424 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1425 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1426 | Align = TargetVectorAlign; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1427 | break; |
| 1428 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1429 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1430 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1431 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1432 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1433 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1434 | // GCC extension: alignof(void) = 8 bits. |
| 1435 | Width = 0; |
| 1436 | Align = 8; |
| 1437 | break; |
| 1438 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1439 | case BuiltinType::Bool: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1440 | Width = Target->getBoolWidth(); |
| 1441 | Align = Target->getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1442 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1443 | case BuiltinType::Char_S: |
| 1444 | case BuiltinType::Char_U: |
| 1445 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1446 | case BuiltinType::SChar: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1447 | Width = Target->getCharWidth(); |
| 1448 | Align = Target->getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1449 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1450 | case BuiltinType::WChar_S: |
| 1451 | case BuiltinType::WChar_U: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1452 | Width = Target->getWCharWidth(); |
| 1453 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1454 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1455 | case BuiltinType::Char16: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1456 | Width = Target->getChar16Width(); |
| 1457 | Align = Target->getChar16Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1458 | break; |
| 1459 | case BuiltinType::Char32: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1460 | Width = Target->getChar32Width(); |
| 1461 | Align = Target->getChar32Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1462 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1463 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1464 | case BuiltinType::Short: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1465 | Width = Target->getShortWidth(); |
| 1466 | Align = Target->getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1467 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1468 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1469 | case BuiltinType::Int: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1470 | Width = Target->getIntWidth(); |
| 1471 | Align = Target->getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1472 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1473 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1474 | case BuiltinType::Long: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1475 | Width = Target->getLongWidth(); |
| 1476 | Align = Target->getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1477 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1478 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1479 | case BuiltinType::LongLong: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1480 | Width = Target->getLongLongWidth(); |
| 1481 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1482 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1483 | case BuiltinType::Int128: |
| 1484 | case BuiltinType::UInt128: |
| 1485 | Width = 128; |
| 1486 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1487 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1488 | case BuiltinType::Half: |
| 1489 | Width = Target->getHalfWidth(); |
| 1490 | Align = Target->getHalfAlign(); |
| 1491 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1492 | case BuiltinType::Float: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1493 | Width = Target->getFloatWidth(); |
| 1494 | Align = Target->getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1495 | break; |
| 1496 | case BuiltinType::Double: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1497 | Width = Target->getDoubleWidth(); |
| 1498 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1499 | break; |
| 1500 | case BuiltinType::LongDouble: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1501 | Width = Target->getLongDoubleWidth(); |
| 1502 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1503 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1504 | case BuiltinType::NullPtr: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1505 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1506 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1507 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1508 | case BuiltinType::ObjCId: |
| 1509 | case BuiltinType::ObjCClass: |
| 1510 | case BuiltinType::ObjCSel: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1511 | Width = Target->getPointerWidth(0); |
| 1512 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1513 | break; |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1514 | case BuiltinType::OCLSampler: |
| 1515 | // Samplers are modeled as integers. |
| 1516 | Width = Target->getIntWidth(); |
| 1517 | Align = Target->getIntAlign(); |
| 1518 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1519 | case BuiltinType::OCLEvent: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1520 | case BuiltinType::OCLImage1d: |
| 1521 | case BuiltinType::OCLImage1dArray: |
| 1522 | case BuiltinType::OCLImage1dBuffer: |
| 1523 | case BuiltinType::OCLImage2d: |
| 1524 | case BuiltinType::OCLImage2dArray: |
| 1525 | case BuiltinType::OCLImage3d: |
| 1526 | // Currently these types are pointers to opaque types. |
| 1527 | Width = Target->getPointerWidth(0); |
| 1528 | Align = Target->getPointerAlign(0); |
| 1529 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1530 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1531 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1532 | case Type::ObjCObjectPointer: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1533 | Width = Target->getPointerWidth(0); |
| 1534 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1535 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1536 | case Type::BlockPointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1537 | unsigned AS = getTargetAddressSpace( |
| 1538 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1539 | Width = Target->getPointerWidth(AS); |
| 1540 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1541 | break; |
| 1542 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1543 | case Type::LValueReference: |
| 1544 | case Type::RValueReference: { |
| 1545 | // alignof and sizeof should never enter this code path here, so we go |
| 1546 | // the pointer route. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1547 | unsigned AS = getTargetAddressSpace( |
| 1548 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1549 | Width = Target->getPointerWidth(AS); |
| 1550 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1551 | break; |
| 1552 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1553 | case Type::Pointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1554 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1555 | Width = Target->getPointerWidth(AS); |
| 1556 | Align = Target->getPointerAlign(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1557 | break; |
| 1558 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1559 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1560 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 1561 | llvm::tie(Width, Align) = ABI->getMemberPointerWidthAndAlign(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1562 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1563 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1564 | case Type::Complex: { |
| 1565 | // Complex types have the same alignment as their elements, but twice the |
| 1566 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1568 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1569 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1570 | Align = EltInfo.second; |
| 1571 | break; |
| 1572 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1573 | case Type::ObjCObject: |
| 1574 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1575 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1576 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1577 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1578 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1579 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1580 | break; |
| 1581 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1582 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1583 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1584 | const TagType *TT = cast<TagType>(T); |
| 1585 | |
| 1586 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 22ce41d | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1587 | Width = 8; |
| 1588 | Align = 8; |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1589 | break; |
| 1590 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1592 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1593 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1594 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1595 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1596 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1597 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1598 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1599 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1600 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1601 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1602 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1603 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1604 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1605 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1606 | case Type::Auto: { |
| 1607 | const AutoType *A = cast<AutoType>(T); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 1608 | assert(!A->getDeducedType().isNull() && |
| 1609 | "cannot request the size of an undeduced or dependent auto type"); |
Matt Beaumont-Gay | dc856af | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1610 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1613 | case Type::Paren: |
| 1614 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1615 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1616 | case Type::Typedef: { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1617 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1618 | std::pair<uint64_t, unsigned> Info |
| 1619 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1620 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1621 | // alignment we have. This violates the GCC documentation (which says that |
| 1622 | // attribute(aligned) can only round up) but matches its implementation. |
| 1623 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1624 | Align = AttrAlign; |
| 1625 | else |
| 1626 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1627 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1628 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1629 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1630 | |
| 1631 | case Type::TypeOfExpr: |
| 1632 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1633 | .getTypePtr()); |
| 1634 | |
| 1635 | case Type::TypeOf: |
| 1636 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1637 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1638 | case Type::Decltype: |
| 1639 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1640 | .getTypePtr()); |
| 1641 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1642 | case Type::UnaryTransform: |
| 1643 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1644 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1645 | case Type::Elaborated: |
| 1646 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1647 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1648 | case Type::Attributed: |
| 1649 | return getTypeInfo( |
| 1650 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1651 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1652 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1654 | "Cannot request the size of a dependent type"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1655 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1656 | // A type alias template specialization may refer to a typedef with the |
| 1657 | // aligned attribute on it. |
| 1658 | if (TST->isTypeAlias()) |
| 1659 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1660 | else |
| 1661 | return getTypeInfo(getCanonicalType(T)); |
| 1662 | } |
| 1663 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1664 | case Type::Atomic: { |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1665 | // Start with the base type information. |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1666 | std::pair<uint64_t, unsigned> Info |
| 1667 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1668 | Width = Info.first; |
| 1669 | Align = Info.second; |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1670 | |
| 1671 | // If the size of the type doesn't exceed the platform's max |
| 1672 | // atomic promotion width, make the size and alignment more |
| 1673 | // favorable to atomic operations: |
| 1674 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth()) { |
| 1675 | // Round the size up to a power of 2. |
| 1676 | if (!llvm::isPowerOf2_64(Width)) |
| 1677 | Width = llvm::NextPowerOf2(Width); |
| 1678 | |
| 1679 | // Set the alignment equal to the size. |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1680 | Align = static_cast<unsigned>(Width); |
| 1681 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1684 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1686 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1687 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1690 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1691 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1692 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1693 | } |
| 1694 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1695 | /// toBits - Convert a size in characters to a size in characters. |
| 1696 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1697 | return CharSize.getQuantity() * getCharWidth(); |
| 1698 | } |
| 1699 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1700 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1701 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1702 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1703 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1704 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1705 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1706 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1709 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1710 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1711 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1712 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1713 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1714 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1715 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1718 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1719 | /// type for the current target in bits. This can be different than the ABI |
| 1720 | /// alignment in cases where it is beneficial for performance to overalign |
| 1721 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1722 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1723 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1724 | |
| 1725 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1726 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1727 | T = CT->getElementType().getTypePtr(); |
| 1728 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | cde7a1d | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1729 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1730 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1731 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1732 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1733 | return ABIAlign; |
| 1734 | } |
| 1735 | |
Ulrich Weigand | 6b20351 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1736 | /// getAlignOfGlobalVar - Return the alignment in bits that should be given |
| 1737 | /// to a global variable of the specified type. |
| 1738 | unsigned ASTContext::getAlignOfGlobalVar(QualType T) const { |
| 1739 | return std::max(getTypeAlign(T), getTargetInfo().getMinGlobalAlign()); |
| 1740 | } |
| 1741 | |
| 1742 | /// getAlignOfGlobalVarInChars - Return the alignment in characters that |
| 1743 | /// should be given to a global variable of the specified type. |
| 1744 | CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const { |
| 1745 | return toCharUnitsFromBits(getAlignOfGlobalVar(T)); |
| 1746 | } |
| 1747 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1748 | /// DeepCollectObjCIvars - |
| 1749 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1750 | /// super class and then collects all ivars, including those synthesized for |
| 1751 | /// current class. This routine is used for implementation of current class |
| 1752 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1753 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1754 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1755 | bool leafClass, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1756 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1757 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1758 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1759 | if (!leafClass) { |
| 1760 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1761 | E = OI->ivar_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1762 | Ivars.push_back(*I); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1763 | } else { |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1764 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1765 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1766 | Iv= Iv->getNextIvar()) |
| 1767 | Ivars.push_back(Iv); |
| 1768 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1771 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1772 | /// those inherited by it. |
| 1773 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1774 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1775 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1776 | // We can use protocol_iterator here instead of |
| 1777 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1778 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1779 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1780 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1781 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1782 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1783 | PE = Proto->protocol_end(); P != PE; ++P) { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1784 | Protocols.insert((*P)->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1785 | CollectInheritedProtocols(*P, Protocols); |
| 1786 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1787 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1788 | |
| 1789 | // Categories of this Interface. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1790 | for (ObjCInterfaceDecl::visible_categories_iterator |
| 1791 | Cat = OI->visible_categories_begin(), |
| 1792 | CatEnd = OI->visible_categories_end(); |
| 1793 | Cat != CatEnd; ++Cat) { |
| 1794 | CollectInheritedProtocols(*Cat, Protocols); |
| 1795 | } |
| 1796 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1797 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1798 | while (SD) { |
| 1799 | CollectInheritedProtocols(SD, Protocols); |
| 1800 | SD = SD->getSuperClass(); |
| 1801 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1802 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1803 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1804 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1805 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1806 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1807 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1808 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1809 | CollectInheritedProtocols(*P, Protocols); |
| 1810 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1811 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1812 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1813 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1814 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1815 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1816 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1817 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1818 | CollectInheritedProtocols(*P, Protocols); |
| 1819 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1820 | } |
| 1821 | } |
| 1822 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1823 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1824 | unsigned count = 0; |
| 1825 | // Count ivars declared in class extension. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1826 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 1827 | Ext = OI->known_extensions_begin(), |
| 1828 | ExtEnd = OI->known_extensions_end(); |
| 1829 | Ext != ExtEnd; ++Ext) { |
| 1830 | count += Ext->ivar_size(); |
| 1831 | } |
| 1832 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1833 | // Count ivar defined in this class's implementation. This |
| 1834 | // includes synthesized ivars. |
| 1835 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1836 | count += ImplDecl->ivar_size(); |
| 1837 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1838 | return count; |
| 1839 | } |
| 1840 | |
Argyrios Kyrtzidis | 8deabc1 | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 1841 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 1842 | if (!E) |
| 1843 | return false; |
| 1844 | |
| 1845 | // nullptr_t is always treated as null. |
| 1846 | if (E->getType()->isNullPtrType()) return true; |
| 1847 | |
| 1848 | if (E->getType()->isAnyPointerType() && |
| 1849 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 1850 | Expr::NPC_ValueDependentIsNull)) |
| 1851 | return true; |
| 1852 | |
| 1853 | // Unfortunately, __null has type 'int'. |
| 1854 | if (isa<GNUNullExpr>(E)) return true; |
| 1855 | |
| 1856 | return false; |
| 1857 | } |
| 1858 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1859 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1860 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1861 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1862 | I = ObjCImpls.find(D); |
| 1863 | if (I != ObjCImpls.end()) |
| 1864 | return cast<ObjCImplementationDecl>(I->second); |
| 1865 | return 0; |
| 1866 | } |
| 1867 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1868 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1869 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1870 | I = ObjCImpls.find(D); |
| 1871 | if (I != ObjCImpls.end()) |
| 1872 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1873 | return 0; |
| 1874 | } |
| 1875 | |
| 1876 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1877 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1878 | ObjCImplementationDecl *ImplD) { |
| 1879 | assert(IFaceD && ImplD && "Passed null params"); |
| 1880 | ObjCImpls[IFaceD] = ImplD; |
| 1881 | } |
| 1882 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1883 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1884 | ObjCCategoryImplDecl *ImplD) { |
| 1885 | assert(CatD && ImplD && "Passed null params"); |
| 1886 | ObjCImpls[CatD] = ImplD; |
| 1887 | } |
| 1888 | |
Dmitri Gribenko | b35cc2d | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1889 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( |
| 1890 | const NamedDecl *ND) const { |
| 1891 | if (const ObjCInterfaceDecl *ID = |
| 1892 | dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1893 | return ID; |
Dmitri Gribenko | b35cc2d | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1894 | if (const ObjCCategoryDecl *CD = |
| 1895 | dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1896 | return CD->getClassInterface(); |
Dmitri Gribenko | b35cc2d | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1897 | if (const ObjCImplDecl *IMD = |
| 1898 | dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1899 | return IMD->getClassInterface(); |
| 1900 | |
| 1901 | return 0; |
| 1902 | } |
| 1903 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1904 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1905 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1906 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1907 | assert(VD && "Passed null params"); |
| 1908 | assert(VD->hasAttr<BlocksAttr>() && |
| 1909 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1910 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1911 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1912 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1913 | } |
| 1914 | |
| 1915 | /// \brief Set the copy inialization expression of a block var decl. |
| 1916 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1917 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1918 | assert(VD->hasAttr<BlocksAttr>() && |
| 1919 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1920 | BlockVarCopyInits[VD] = Init; |
| 1921 | } |
| 1922 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1923 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1924 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1925 | if (!DataSize) |
| 1926 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1927 | else |
| 1928 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1929 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1930 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1931 | TypeSourceInfo *TInfo = |
| 1932 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1933 | new (TInfo) TypeSourceInfo(T); |
| 1934 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1937 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1938 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1939 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1940 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1941 | return DI; |
| 1942 | } |
| 1943 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1944 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1945 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1946 | return getObjCLayout(D, 0); |
| 1947 | } |
| 1948 | |
| 1949 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1950 | ASTContext::getASTObjCImplementationLayout( |
| 1951 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1952 | return getObjCLayout(D->getClassInterface(), D); |
| 1953 | } |
| 1954 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1955 | //===----------------------------------------------------------------------===// |
| 1956 | // Type creation/memoization methods |
| 1957 | //===----------------------------------------------------------------------===// |
| 1958 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1959 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1960 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1961 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1962 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1963 | |
| 1964 | // Check if we've already instantiated this type. |
| 1965 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1966 | ExtQuals::Profile(ID, baseType, quals); |
| 1967 | void *insertPos = 0; |
| 1968 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1969 | assert(eq->getQualifiers() == quals); |
| 1970 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1973 | // If the base type is not canonical, make the appropriate canonical type. |
| 1974 | QualType canon; |
| 1975 | if (!baseType->isCanonicalUnqualified()) { |
| 1976 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1977 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 1978 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1979 | |
| 1980 | // Re-find the insert position. |
| 1981 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1982 | } |
| 1983 | |
| 1984 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1985 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1986 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1989 | QualType |
| 1990 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1991 | QualType CanT = getCanonicalType(T); |
| 1992 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1993 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1994 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1995 | // If we are composing extended qualifiers together, merge together |
| 1996 | // into one ExtQuals node. |
| 1997 | QualifierCollector Quals; |
| 1998 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1999 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2000 | // If this type already has an address space specified, it cannot get |
| 2001 | // another one. |
| 2002 | assert(!Quals.hasAddressSpace() && |
| 2003 | "Type cannot be in multiple addr spaces!"); |
| 2004 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2006 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2009 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2010 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2011 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2012 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2013 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2015 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 2016 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 2017 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 2018 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 2019 | return getPointerType(ResultType); |
| 2020 | } |
| 2021 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2023 | // If we are composing extended qualifiers together, merge together |
| 2024 | // into one ExtQuals node. |
| 2025 | QualifierCollector Quals; |
| 2026 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2028 | // If this type already has an ObjCGC specified, it cannot get |
| 2029 | // another one. |
| 2030 | assert(!Quals.hasObjCGCAttr() && |
| 2031 | "Type cannot have multiple ObjCGCs!"); |
| 2032 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2034 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2035 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2036 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2037 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 2038 | FunctionType::ExtInfo Info) { |
| 2039 | if (T->getExtInfo() == Info) |
| 2040 | return T; |
| 2041 | |
| 2042 | QualType Result; |
| 2043 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 2044 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 2045 | } else { |
| 2046 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 2047 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 2048 | EPI.ExtInfo = Info; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2049 | Result = getFunctionType(FPT->getResultType(), |
| 2050 | ArrayRef<QualType>(FPT->arg_type_begin(), |
| 2051 | FPT->getNumArgs()), |
| 2052 | EPI); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | return cast<FunctionType>(Result.getTypePtr()); |
| 2056 | } |
| 2057 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2058 | void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD, |
| 2059 | QualType ResultType) { |
Richard Smith | 9dadfab | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2060 | FD = FD->getMostRecentDecl(); |
| 2061 | while (true) { |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2062 | const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>(); |
| 2063 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 2064 | FD->setType(getFunctionType(ResultType, FPT->getArgTypes(), EPI)); |
Richard Smith | 9dadfab | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2065 | if (FunctionDecl *Next = FD->getPreviousDecl()) |
| 2066 | FD = Next; |
| 2067 | else |
| 2068 | break; |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2069 | } |
Richard Smith | 9dadfab | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2070 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2071 | L->DeducedReturnType(FD, ResultType); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2074 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 2075 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2076 | QualType ASTContext::getComplexType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2077 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2078 | // structure. |
| 2079 | llvm::FoldingSetNodeID ID; |
| 2080 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2082 | void *InsertPos = 0; |
| 2083 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2084 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2086 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2087 | // so fill in the canonical type field. |
| 2088 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2089 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2090 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2092 | // Get the new insert position for the node we care about. |
| 2093 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2094 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2095 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2096 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2097 | Types.push_back(New); |
| 2098 | ComplexTypes.InsertNode(New, InsertPos); |
| 2099 | return QualType(New, 0); |
| 2100 | } |
| 2101 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2102 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 2103 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2104 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2105 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2106 | // structure. |
| 2107 | llvm::FoldingSetNodeID ID; |
| 2108 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2110 | void *InsertPos = 0; |
| 2111 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2112 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2114 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2115 | // so fill in the canonical type field. |
| 2116 | QualType Canonical; |
Bob Wilson | c90cc93 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2117 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2118 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | |
Bob Wilson | c90cc93 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2120 | // Get the new insert position for the node we care about. |
| 2121 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2122 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 2123 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2124 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2125 | Types.push_back(New); |
| 2126 | PointerTypes.InsertNode(New, InsertPos); |
| 2127 | return QualType(New, 0); |
| 2128 | } |
| 2129 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2130 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2131 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2132 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2133 | assert(T->isFunctionType() && "block of function types only"); |
| 2134 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2135 | // structure. |
| 2136 | llvm::FoldingSetNodeID ID; |
| 2137 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2138 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2139 | void *InsertPos = 0; |
| 2140 | if (BlockPointerType *PT = |
| 2141 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2142 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | |
| 2144 | // 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] | 2145 | // type either so fill in the canonical type field. |
| 2146 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2147 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2148 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2150 | // Get the new insert position for the node we care about. |
| 2151 | BlockPointerType *NewIP = |
| 2152 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2153 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2154 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2155 | BlockPointerType *New |
| 2156 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2157 | Types.push_back(New); |
| 2158 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 2159 | return QualType(New, 0); |
| 2160 | } |
| 2161 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2162 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 2163 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2164 | QualType |
| 2165 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 2166 | assert(getCanonicalType(T) != OverloadTy && |
| 2167 | "Unresolved overloaded function type"); |
| 2168 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2169 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2170 | // structure. |
| 2171 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2172 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2173 | |
| 2174 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2175 | if (LValueReferenceType *RT = |
| 2176 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2177 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2178 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2179 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2180 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2181 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2182 | // either, so fill in the canonical type field. |
| 2183 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2184 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 2185 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2186 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2187 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2188 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2189 | LValueReferenceType *NewIP = |
| 2190 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2191 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2194 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2195 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 2196 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2197 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2198 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2199 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2200 | return QualType(New, 0); |
| 2201 | } |
| 2202 | |
| 2203 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 2204 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2205 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2206 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2207 | // structure. |
| 2208 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2209 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2210 | |
| 2211 | void *InsertPos = 0; |
| 2212 | if (RValueReferenceType *RT = |
| 2213 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2214 | return QualType(RT, 0); |
| 2215 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2216 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2217 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2218 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2219 | // either, so fill in the canonical type field. |
| 2220 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2221 | if (InnerRef || !T.isCanonical()) { |
| 2222 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2223 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2224 | |
| 2225 | // Get the new insert position for the node we care about. |
| 2226 | RValueReferenceType *NewIP = |
| 2227 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2228 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2231 | RValueReferenceType *New |
| 2232 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2233 | Types.push_back(New); |
| 2234 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2235 | return QualType(New, 0); |
| 2236 | } |
| 2237 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2238 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 2239 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2240 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2241 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2242 | // structure. |
| 2243 | llvm::FoldingSetNodeID ID; |
| 2244 | MemberPointerType::Profile(ID, T, Cls); |
| 2245 | |
| 2246 | void *InsertPos = 0; |
| 2247 | if (MemberPointerType *PT = |
| 2248 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2249 | return QualType(PT, 0); |
| 2250 | |
| 2251 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 2252 | // type either, so fill in the canonical type field. |
| 2253 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2254 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2255 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 2256 | |
| 2257 | // Get the new insert position for the node we care about. |
| 2258 | MemberPointerType *NewIP = |
| 2259 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2260 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2261 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2262 | MemberPointerType *New |
| 2263 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2264 | Types.push_back(New); |
| 2265 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 2266 | return QualType(New, 0); |
| 2267 | } |
| 2268 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2269 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2270 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2271 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2272 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2273 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2274 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 2275 | assert((EltTy->isDependentType() || |
| 2276 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 2277 | "Constant array of VLAs is illegal!"); |
| 2278 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2279 | // Convert the array size into a canonical width matching the pointer size for |
| 2280 | // the target. |
| 2281 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2282 | ArySize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2283 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2285 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2286 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2287 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2288 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2289 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2290 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2291 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2293 | // If the element type isn't canonical or has qualifiers, this won't |
| 2294 | // be a canonical type either, so fill in the canonical type field. |
| 2295 | QualType Canon; |
| 2296 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2297 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2298 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2299 | ASM, IndexTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2300 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2301 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2302 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2303 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2304 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2305 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2306 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2308 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2309 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2310 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2311 | Types.push_back(New); |
| 2312 | return QualType(New, 0); |
| 2313 | } |
| 2314 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2315 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 2316 | /// variably-modified, into the corresponding type with all the known |
| 2317 | /// sizes replaced with [*]. |
| 2318 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 2319 | // Vastly most common case. |
| 2320 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2321 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2322 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2323 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2324 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2325 | const Type *ty = split.Ty; |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2326 | switch (ty->getTypeClass()) { |
| 2327 | #define TYPE(Class, Base) |
| 2328 | #define ABSTRACT_TYPE(Class, Base) |
| 2329 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2330 | #include "clang/AST/TypeNodes.def" |
| 2331 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 2332 | |
| 2333 | // These types should never be variably-modified. |
| 2334 | case Type::Builtin: |
| 2335 | case Type::Complex: |
| 2336 | case Type::Vector: |
| 2337 | case Type::ExtVector: |
| 2338 | case Type::DependentSizedExtVector: |
| 2339 | case Type::ObjCObject: |
| 2340 | case Type::ObjCInterface: |
| 2341 | case Type::ObjCObjectPointer: |
| 2342 | case Type::Record: |
| 2343 | case Type::Enum: |
| 2344 | case Type::UnresolvedUsing: |
| 2345 | case Type::TypeOfExpr: |
| 2346 | case Type::TypeOf: |
| 2347 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2348 | case Type::UnaryTransform: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2349 | case Type::DependentName: |
| 2350 | case Type::InjectedClassName: |
| 2351 | case Type::TemplateSpecialization: |
| 2352 | case Type::DependentTemplateSpecialization: |
| 2353 | case Type::TemplateTypeParm: |
| 2354 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2355 | case Type::Auto: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2356 | case Type::PackExpansion: |
| 2357 | llvm_unreachable("type should never be variably-modified"); |
| 2358 | |
| 2359 | // These types can be variably-modified but should never need to |
| 2360 | // further decay. |
| 2361 | case Type::FunctionNoProto: |
| 2362 | case Type::FunctionProto: |
| 2363 | case Type::BlockPointer: |
| 2364 | case Type::MemberPointer: |
| 2365 | return type; |
| 2366 | |
| 2367 | // These types can be variably-modified. All these modifications |
| 2368 | // preserve structure except as noted by comments. |
| 2369 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 2370 | // optimizations available here. |
| 2371 | case Type::Pointer: |
| 2372 | result = getPointerType(getVariableArrayDecayedType( |
| 2373 | cast<PointerType>(ty)->getPointeeType())); |
| 2374 | break; |
| 2375 | |
| 2376 | case Type::LValueReference: { |
| 2377 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 2378 | result = getLValueReferenceType( |
| 2379 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 2380 | lv->isSpelledAsLValue()); |
| 2381 | break; |
| 2382 | } |
| 2383 | |
| 2384 | case Type::RValueReference: { |
| 2385 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 2386 | result = getRValueReferenceType( |
| 2387 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 2388 | break; |
| 2389 | } |
| 2390 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2391 | case Type::Atomic: { |
| 2392 | const AtomicType *at = cast<AtomicType>(ty); |
| 2393 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 2394 | break; |
| 2395 | } |
| 2396 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2397 | case Type::ConstantArray: { |
| 2398 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 2399 | result = getConstantArrayType( |
| 2400 | getVariableArrayDecayedType(cat->getElementType()), |
| 2401 | cat->getSize(), |
| 2402 | cat->getSizeModifier(), |
| 2403 | cat->getIndexTypeCVRQualifiers()); |
| 2404 | break; |
| 2405 | } |
| 2406 | |
| 2407 | case Type::DependentSizedArray: { |
| 2408 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 2409 | result = getDependentSizedArrayType( |
| 2410 | getVariableArrayDecayedType(dat->getElementType()), |
| 2411 | dat->getSizeExpr(), |
| 2412 | dat->getSizeModifier(), |
| 2413 | dat->getIndexTypeCVRQualifiers(), |
| 2414 | dat->getBracketsRange()); |
| 2415 | break; |
| 2416 | } |
| 2417 | |
| 2418 | // Turn incomplete types into [*] types. |
| 2419 | case Type::IncompleteArray: { |
| 2420 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 2421 | result = getVariableArrayType( |
| 2422 | getVariableArrayDecayedType(iat->getElementType()), |
| 2423 | /*size*/ 0, |
| 2424 | ArrayType::Normal, |
| 2425 | iat->getIndexTypeCVRQualifiers(), |
| 2426 | SourceRange()); |
| 2427 | break; |
| 2428 | } |
| 2429 | |
| 2430 | // Turn VLA types into [*] types. |
| 2431 | case Type::VariableArray: { |
| 2432 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 2433 | result = getVariableArrayType( |
| 2434 | getVariableArrayDecayedType(vat->getElementType()), |
| 2435 | /*size*/ 0, |
| 2436 | ArrayType::Star, |
| 2437 | vat->getIndexTypeCVRQualifiers(), |
| 2438 | vat->getBracketsRange()); |
| 2439 | break; |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | // Apply the top-level qualifiers from the original. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2444 | return getQualifiedType(result, split.Quals); |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2445 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2446 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 2447 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 2448 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2449 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 2450 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2451 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2452 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2453 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2454 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 2455 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2456 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2457 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2458 | // Be sure to pull qualifiers off the element type. |
| 2459 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2460 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2461 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2462 | IndexTypeQuals, Brackets); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2463 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2466 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2467 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2468 | |
| 2469 | VariableArrayTypes.push_back(New); |
| 2470 | Types.push_back(New); |
| 2471 | return QualType(New, 0); |
| 2472 | } |
| 2473 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2474 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 2475 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 2476 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2477 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 2478 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2479 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2480 | unsigned elementTypeQuals, |
| 2481 | SourceRange brackets) const { |
| 2482 | assert((!numElements || numElements->isTypeDependent() || |
| 2483 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2484 | "Size must be type- or value-dependent!"); |
| 2485 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2486 | // Dependently-sized array types that do not have a specified number |
| 2487 | // of elements will have their sizes deduced from a dependent |
| 2488 | // initializer. We do no canonicalization here at all, which is okay |
| 2489 | // because they can't be used in most locations. |
| 2490 | if (!numElements) { |
| 2491 | DependentSizedArrayType *newType |
| 2492 | = new (*this, TypeAlignment) |
| 2493 | DependentSizedArrayType(*this, elementType, QualType(), |
| 2494 | numElements, ASM, elementTypeQuals, |
| 2495 | brackets); |
| 2496 | Types.push_back(newType); |
| 2497 | return QualType(newType, 0); |
| 2498 | } |
| 2499 | |
| 2500 | // Otherwise, we actually build a new type every time, but we |
| 2501 | // also build a canonical type. |
| 2502 | |
| 2503 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 2504 | |
| 2505 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2506 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2507 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2508 | QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2509 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2510 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2511 | // Look for an existing type with these properties. |
| 2512 | DependentSizedArrayType *canonTy = |
| 2513 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2514 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2515 | // If we don't have one, build one. |
| 2516 | if (!canonTy) { |
| 2517 | canonTy = new (*this, TypeAlignment) |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2518 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2519 | QualType(), numElements, ASM, elementTypeQuals, |
| 2520 | brackets); |
| 2521 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 2522 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2525 | // Apply qualifiers from the element type to the array. |
| 2526 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2527 | canonElementType.Quals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2528 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2529 | // If we didn't need extra canonicalization for the element type, |
| 2530 | // then just use that as our result. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2531 | if (QualType(canonElementType.Ty, 0) == elementType) |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2532 | return canon; |
| 2533 | |
| 2534 | // Otherwise, we need to build a type which follows the spelling |
| 2535 | // of the element type. |
| 2536 | DependentSizedArrayType *sugaredType |
| 2537 | = new (*this, TypeAlignment) |
| 2538 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 2539 | ASM, elementTypeQuals, brackets); |
| 2540 | Types.push_back(sugaredType); |
| 2541 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2544 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2545 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2546 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2547 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2548 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2549 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2550 | void *insertPos = 0; |
| 2551 | if (IncompleteArrayType *iat = |
| 2552 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 2553 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2554 | |
| 2555 | // 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] | 2556 | // either, so fill in the canonical type field. We also have to pull |
| 2557 | // qualifiers off the element type. |
| 2558 | QualType canon; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2559 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2560 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 2561 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2562 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2563 | ASM, elementTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2564 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2565 | |
| 2566 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2567 | IncompleteArrayType *existing = |
| 2568 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 2569 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 2570 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2571 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2572 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 2573 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2574 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2575 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 2576 | Types.push_back(newType); |
| 2577 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2578 | } |
| 2579 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2580 | /// getVectorType - Return the unique reference to a vector type of |
| 2581 | /// 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] | 2582 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2583 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2584 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2585 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2586 | // Check if we've already instantiated a vector of this type. |
| 2587 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2588 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2589 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2590 | void *InsertPos = 0; |
| 2591 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2592 | return QualType(VTP, 0); |
| 2593 | |
| 2594 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2595 | // so fill in the canonical type field. |
| 2596 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2597 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2598 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2599 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2600 | // Get the new insert position for the node we care about. |
| 2601 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2602 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2603 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2604 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2605 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2606 | VectorTypes.InsertNode(New, InsertPos); |
| 2607 | Types.push_back(New); |
| 2608 | return QualType(New, 0); |
| 2609 | } |
| 2610 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2611 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2612 | /// 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] | 2613 | QualType |
| 2614 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2615 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2616 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2617 | // Check if we've already instantiated a vector of this type. |
| 2618 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2619 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2620 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2621 | void *InsertPos = 0; |
| 2622 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2623 | return QualType(VTP, 0); |
| 2624 | |
| 2625 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2626 | // so fill in the canonical type field. |
| 2627 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2628 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2629 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2630 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2631 | // Get the new insert position for the node we care about. |
| 2632 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2633 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2634 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2635 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2636 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2637 | VectorTypes.InsertNode(New, InsertPos); |
| 2638 | Types.push_back(New); |
| 2639 | return QualType(New, 0); |
| 2640 | } |
| 2641 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2642 | QualType |
| 2643 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2644 | Expr *SizeExpr, |
| 2645 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2646 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2647 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2648 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2650 | void *InsertPos = 0; |
| 2651 | DependentSizedExtVectorType *Canon |
| 2652 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2653 | DependentSizedExtVectorType *New; |
| 2654 | if (Canon) { |
| 2655 | // We already have a canonical version of this array type; use it as |
| 2656 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2657 | New = new (*this, TypeAlignment) |
| 2658 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2659 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2660 | } else { |
| 2661 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2662 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2663 | New = new (*this, TypeAlignment) |
| 2664 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2665 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2666 | |
| 2667 | DependentSizedExtVectorType *CanonCheck |
| 2668 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2669 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2670 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2671 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2672 | } else { |
| 2673 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2674 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2675 | New = new (*this, TypeAlignment) |
| 2676 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2677 | } |
| 2678 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2679 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2680 | Types.push_back(New); |
| 2681 | return QualType(New, 0); |
| 2682 | } |
| 2683 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2684 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2685 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2686 | QualType |
| 2687 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2688 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2689 | const CallingConv DefaultCC = Info.getCC(); |
| 2690 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2691 | CC_X86StdCall : DefaultCC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2692 | // Unique functions, to guarantee there is only one function of a particular |
| 2693 | // structure. |
| 2694 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2695 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2696 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2697 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2698 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2699 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2700 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2701 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2702 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2703 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2704 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2705 | Canonical = |
| 2706 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2707 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2709 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2710 | FunctionNoProtoType *NewIP = |
| 2711 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2712 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2713 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2715 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2716 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2717 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2718 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2719 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2720 | return QualType(New, 0); |
| 2721 | } |
| 2722 | |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2723 | /// \brief Determine whether \p T is canonical as the result type of a function. |
| 2724 | static bool isCanonicalResultType(QualType T) { |
| 2725 | return T.isCanonical() && |
| 2726 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
| 2727 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
| 2728 | } |
| 2729 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2730 | /// getFunctionType - Return a normal function type with a typed argument |
| 2731 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2732 | QualType |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2733 | ASTContext::getFunctionType(QualType ResultTy, ArrayRef<QualType> ArgArray, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2734 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2735 | size_t NumArgs = ArgArray.size(); |
| 2736 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2737 | // Unique functions, to guarantee there is only one function of a particular |
| 2738 | // structure. |
| 2739 | llvm::FoldingSetNodeID ID; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2740 | FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI, |
| 2741 | *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2742 | |
| 2743 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2744 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2745 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2746 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2747 | |
| 2748 | // Determine whether the type being created is already canonical or not. |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2749 | bool isCanonical = |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2750 | EPI.ExceptionSpecType == EST_None && isCanonicalResultType(ResultTy) && |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2751 | !EPI.HasTrailingReturn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2752 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2753 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2754 | isCanonical = false; |
| 2755 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2756 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2757 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2758 | CC_X86StdCall : DefaultCC; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2759 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2760 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2761 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2762 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2763 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2764 | SmallVector<QualType, 16> CanonicalArgs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2765 | CanonicalArgs.reserve(NumArgs); |
| 2766 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2767 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2768 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2769 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2770 | CanonicalEPI.HasTrailingReturn = false; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2771 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2772 | CanonicalEPI.NumExceptions = 0; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2773 | CanonicalEPI.ExtInfo |
| 2774 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2775 | |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2776 | // Result types do not have ARC lifetime qualifiers. |
| 2777 | QualType CanResultTy = getCanonicalType(ResultTy); |
| 2778 | if (ResultTy.getQualifiers().hasObjCLifetime()) { |
| 2779 | Qualifiers Qs = CanResultTy.getQualifiers(); |
| 2780 | Qs.removeObjCLifetime(); |
| 2781 | CanResultTy = getQualifiedType(CanResultTy.getUnqualifiedType(), Qs); |
| 2782 | } |
| 2783 | |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2784 | Canonical = getFunctionType(CanResultTy, CanonicalArgs, CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2785 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2786 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2787 | FunctionProtoType *NewIP = |
| 2788 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2789 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2790 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2791 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2792 | // FunctionProtoType objects are allocated with extra bytes after |
| 2793 | // them for three variable size arrays at the end: |
| 2794 | // - parameter types |
| 2795 | // - exception types |
| 2796 | // - consumed-arguments flags |
| 2797 | // Instead of the exception types, there could be a noexcept |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2798 | // expression, or information used to resolve the exception |
| 2799 | // specification. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2800 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2801 | NumArgs * sizeof(QualType); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2802 | if (EPI.ExceptionSpecType == EST_Dynamic) { |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2803 | Size += EPI.NumExceptions * sizeof(QualType); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2804 | } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2805 | Size += sizeof(Expr*); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2806 | } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2807 | Size += 2 * sizeof(FunctionDecl*); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2808 | } else if (EPI.ExceptionSpecType == EST_Unevaluated) { |
| 2809 | Size += sizeof(FunctionDecl*); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2810 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2811 | if (EPI.ConsumedArguments) |
| 2812 | Size += NumArgs * sizeof(bool); |
| 2813 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2814 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2815 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2816 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2817 | new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2818 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2819 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2820 | return QualType(FTP, 0); |
| 2821 | } |
| 2822 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2823 | #ifndef NDEBUG |
| 2824 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2825 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2826 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2827 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2828 | return true; |
| 2829 | if (RD->getDescribedClassTemplate() && |
| 2830 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2831 | return true; |
| 2832 | return false; |
| 2833 | } |
| 2834 | #endif |
| 2835 | |
| 2836 | /// getInjectedClassNameType - Return the unique reference to the |
| 2837 | /// injected class name type for the specified templated declaration. |
| 2838 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2839 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2840 | assert(NeedsInjectedClassNameType(Decl)); |
| 2841 | if (Decl->TypeForDecl) { |
| 2842 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2843 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2844 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2845 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2846 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2847 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2848 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2849 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2850 | Decl->TypeForDecl = newType; |
| 2851 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2852 | } |
| 2853 | return QualType(Decl->TypeForDecl, 0); |
| 2854 | } |
| 2855 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2856 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2857 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2858 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2859 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2860 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2862 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2863 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2864 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2865 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2866 | "Template type parameter types are always available."); |
| 2867 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2868 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2869 | assert(!Record->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2870 | "struct/union has previous declaration"); |
| 2871 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2872 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2873 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2874 | assert(!Enum->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2875 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2876 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2877 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2878 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2879 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2880 | Decl->TypeForDecl = newType; |
| 2881 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2882 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2883 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2884 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2885 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2888 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2889 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2890 | QualType |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2891 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2892 | QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2893 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2894 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2895 | if (Canonical.isNull()) |
| 2896 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2897 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2898 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2899 | Decl->TypeForDecl = newType; |
| 2900 | Types.push_back(newType); |
| 2901 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2904 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2905 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2906 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2907 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2908 | if (PrevDecl->TypeForDecl) |
| 2909 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2910 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2911 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2912 | Decl->TypeForDecl = newType; |
| 2913 | Types.push_back(newType); |
| 2914 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2917 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2918 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2919 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2920 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2921 | if (PrevDecl->TypeForDecl) |
| 2922 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2923 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2924 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2925 | Decl->TypeForDecl = newType; |
| 2926 | Types.push_back(newType); |
| 2927 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2930 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2931 | QualType modifiedType, |
| 2932 | QualType equivalentType) { |
| 2933 | llvm::FoldingSetNodeID id; |
| 2934 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2935 | |
| 2936 | void *insertPos = 0; |
| 2937 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2938 | if (type) return QualType(type, 0); |
| 2939 | |
| 2940 | QualType canon = getCanonicalType(equivalentType); |
| 2941 | type = new (*this, TypeAlignment) |
| 2942 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2943 | |
| 2944 | Types.push_back(type); |
| 2945 | AttributedTypes.InsertNode(type, insertPos); |
| 2946 | |
| 2947 | return QualType(type, 0); |
| 2948 | } |
| 2949 | |
| 2950 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2951 | /// \brief Retrieve a substitution-result type. |
| 2952 | QualType |
| 2953 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2954 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2955 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2956 | && "replacement types must always be canonical"); |
| 2957 | |
| 2958 | llvm::FoldingSetNodeID ID; |
| 2959 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2960 | void *InsertPos = 0; |
| 2961 | SubstTemplateTypeParmType *SubstParm |
| 2962 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2963 | |
| 2964 | if (!SubstParm) { |
| 2965 | SubstParm = new (*this, TypeAlignment) |
| 2966 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2967 | Types.push_back(SubstParm); |
| 2968 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2969 | } |
| 2970 | |
| 2971 | return QualType(SubstParm, 0); |
| 2972 | } |
| 2973 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2974 | /// \brief Retrieve a |
| 2975 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2976 | const TemplateTypeParmType *Parm, |
| 2977 | const TemplateArgument &ArgPack) { |
| 2978 | #ifndef NDEBUG |
| 2979 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2980 | PEnd = ArgPack.pack_end(); |
| 2981 | P != PEnd; ++P) { |
| 2982 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2983 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2984 | } |
| 2985 | #endif |
| 2986 | |
| 2987 | llvm::FoldingSetNodeID ID; |
| 2988 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2989 | void *InsertPos = 0; |
| 2990 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2991 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2992 | return QualType(SubstParm, 0); |
| 2993 | |
| 2994 | QualType Canon; |
| 2995 | if (!Parm->isCanonicalUnqualified()) { |
| 2996 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2997 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2998 | ArgPack); |
| 2999 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3000 | } |
| 3001 | |
| 3002 | SubstTemplateTypeParmPackType *SubstParm |
| 3003 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 3004 | ArgPack); |
| 3005 | Types.push_back(SubstParm); |
| 3006 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 3007 | return QualType(SubstParm, 0); |
| 3008 | } |
| 3009 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3010 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3011 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3012 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3013 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3014 | bool ParameterPack, |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3015 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3016 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3017 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3018 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3019 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3020 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3021 | |
| 3022 | if (TypeParm) |
| 3023 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3024 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3025 | if (TTPDecl) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3026 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3027 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3028 | |
| 3029 | TemplateTypeParmType *TypeCheck |
| 3030 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3031 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 3032 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3033 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3034 | TypeParm = new (*this, TypeAlignment) |
| 3035 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3036 | |
| 3037 | Types.push_back(TypeParm); |
| 3038 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 3039 | |
| 3040 | return QualType(TypeParm, 0); |
| 3041 | } |
| 3042 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3043 | TypeSourceInfo * |
| 3044 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 3045 | SourceLocation NameLoc, |
| 3046 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3047 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3048 | assert(!Name.getAsDependentTemplateName() && |
| 3049 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3050 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3051 | |
| 3052 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3053 | TemplateSpecializationTypeLoc TL = |
| 3054 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3055 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3056 | TL.setTemplateNameLoc(NameLoc); |
| 3057 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 3058 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 3059 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 3060 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 3061 | return DI; |
| 3062 | } |
| 3063 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3064 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3065 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3066 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3067 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3068 | assert(!Template.getAsDependentTemplateName() && |
| 3069 | "No dependent template names here!"); |
| 3070 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3071 | unsigned NumArgs = Args.size(); |
| 3072 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3073 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3074 | ArgVec.reserve(NumArgs); |
| 3075 | for (unsigned i = 0; i != NumArgs; ++i) |
| 3076 | ArgVec.push_back(Args[i].getArgument()); |
| 3077 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3078 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3079 | Underlying); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3082 | #ifndef NDEBUG |
| 3083 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 3084 | unsigned NumArgs) { |
| 3085 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3086 | if (Args[I].isPackExpansion()) |
| 3087 | return true; |
| 3088 | |
| 3089 | return true; |
| 3090 | } |
| 3091 | #endif |
| 3092 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3093 | QualType |
| 3094 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3095 | const TemplateArgument *Args, |
| 3096 | unsigned NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3097 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3098 | assert(!Template.getAsDependentTemplateName() && |
| 3099 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3100 | // Look through qualified template names. |
| 3101 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3102 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3103 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3104 | bool IsTypeAlias = |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3105 | Template.getAsTemplateDecl() && |
| 3106 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3107 | QualType CanonType; |
| 3108 | if (!Underlying.isNull()) |
| 3109 | CanonType = getCanonicalType(Underlying); |
| 3110 | else { |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3111 | // We can get here with an alias template when the specialization contains |
| 3112 | // a pack expansion that does not match up with a parameter pack. |
| 3113 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 3114 | "Caller must compute aliased type"); |
| 3115 | IsTypeAlias = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3116 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 3117 | NumArgs); |
| 3118 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3119 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3120 | // Allocate the (non-canonical) template specialization type, but don't |
| 3121 | // try to unique it: these types typically have location information that |
| 3122 | // we don't unique and don't want to lose. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3123 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 3124 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3125 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3126 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3127 | TemplateSpecializationType *Spec |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3128 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 3129 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3130 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3131 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3132 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3135 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3136 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 3137 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3138 | unsigned NumArgs) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3139 | assert(!Template.getAsDependentTemplateName() && |
| 3140 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3141 | |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3142 | // Look through qualified template names. |
| 3143 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3144 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3145 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3146 | // Build the canonical template specialization type. |
| 3147 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3148 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3149 | CanonArgs.reserve(NumArgs); |
| 3150 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3151 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 3152 | |
| 3153 | // Determine whether this canonical template specialization type already |
| 3154 | // exists. |
| 3155 | llvm::FoldingSetNodeID ID; |
| 3156 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 3157 | CanonArgs.data(), NumArgs, *this); |
| 3158 | |
| 3159 | void *InsertPos = 0; |
| 3160 | TemplateSpecializationType *Spec |
| 3161 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3162 | |
| 3163 | if (!Spec) { |
| 3164 | // Allocate a new canonical template specialization type. |
| 3165 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 3166 | sizeof(TemplateArgument) * NumArgs), |
| 3167 | TypeAlignment); |
| 3168 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 3169 | CanonArgs.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3170 | QualType(), QualType()); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3171 | Types.push_back(Spec); |
| 3172 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 3173 | } |
| 3174 | |
| 3175 | assert(Spec->isDependentType() && |
| 3176 | "Non-dependent template-id type must have a canonical type"); |
| 3177 | return QualType(Spec, 0); |
| 3178 | } |
| 3179 | |
| 3180 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3181 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 3182 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3183 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3184 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3185 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3186 | |
| 3187 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3188 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3189 | if (T) |
| 3190 | return QualType(T, 0); |
| 3191 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3192 | QualType Canon = NamedType; |
| 3193 | if (!Canon.isCanonical()) { |
| 3194 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3195 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3196 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3197 | (void)CheckT; |
| 3198 | } |
| 3199 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3200 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3201 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3202 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3203 | return QualType(T, 0); |
| 3204 | } |
| 3205 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3206 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3207 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3208 | llvm::FoldingSetNodeID ID; |
| 3209 | ParenType::Profile(ID, InnerType); |
| 3210 | |
| 3211 | void *InsertPos = 0; |
| 3212 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3213 | if (T) |
| 3214 | return QualType(T, 0); |
| 3215 | |
| 3216 | QualType Canon = InnerType; |
| 3217 | if (!Canon.isCanonical()) { |
| 3218 | Canon = getCanonicalType(InnerType); |
| 3219 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3220 | assert(!CheckT && "Paren canonical type broken"); |
| 3221 | (void)CheckT; |
| 3222 | } |
| 3223 | |
| 3224 | T = new (*this) ParenType(InnerType, Canon); |
| 3225 | Types.push_back(T); |
| 3226 | ParenTypes.InsertNode(T, InsertPos); |
| 3227 | return QualType(T, 0); |
| 3228 | } |
| 3229 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3230 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 3231 | NestedNameSpecifier *NNS, |
| 3232 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3233 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3234 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 3235 | |
| 3236 | if (Canon.isNull()) { |
| 3237 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3238 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3239 | if (Keyword == ETK_None) |
| 3240 | CanonKeyword = ETK_Typename; |
| 3241 | |
| 3242 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 3243 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3247 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3248 | |
| 3249 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3250 | DependentNameType *T |
| 3251 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3252 | if (T) |
| 3253 | return QualType(T, 0); |
| 3254 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3255 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3256 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3257 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3258 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3261 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3262 | ASTContext::getDependentTemplateSpecializationType( |
| 3263 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3264 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3265 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3266 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3267 | // TODO: avoid this copy |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3268 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3269 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 3270 | ArgCopy.push_back(Args[I].getArgument()); |
| 3271 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 3272 | ArgCopy.size(), |
| 3273 | ArgCopy.data()); |
| 3274 | } |
| 3275 | |
| 3276 | QualType |
| 3277 | ASTContext::getDependentTemplateSpecializationType( |
| 3278 | ElaboratedTypeKeyword Keyword, |
| 3279 | NestedNameSpecifier *NNS, |
| 3280 | const IdentifierInfo *Name, |
| 3281 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3282 | const TemplateArgument *Args) const { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3283 | assert((!NNS || NNS->isDependent()) && |
| 3284 | "nested-name-specifier must be dependent"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3285 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3286 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3287 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 3288 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3289 | |
| 3290 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3291 | DependentTemplateSpecializationType *T |
| 3292 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3293 | if (T) |
| 3294 | return QualType(T, 0); |
| 3295 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3296 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3297 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3298 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3299 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 3300 | |
| 3301 | bool AnyNonCanonArgs = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3302 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3303 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 3304 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 3305 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 3306 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3309 | QualType Canon; |
| 3310 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 3311 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 3312 | Name, NumArgs, |
| 3313 | CanonArgs.data()); |
| 3314 | |
| 3315 | // Find the insert position again. |
| 3316 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3317 | } |
| 3318 | |
| 3319 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 3320 | sizeof(TemplateArgument) * NumArgs), |
| 3321 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 3322 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3323 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3324 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3325 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3326 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3329 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3330 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3331 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3332 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3333 | |
| 3334 | assert(Pattern->containsUnexpandedParameterPack() && |
| 3335 | "Pack expansions must expand one or more parameter packs"); |
| 3336 | void *InsertPos = 0; |
| 3337 | PackExpansionType *T |
| 3338 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3339 | if (T) |
| 3340 | return QualType(T, 0); |
| 3341 | |
| 3342 | QualType Canon; |
| 3343 | if (!Pattern.isCanonical()) { |
Richard Smith | d8672ef | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3344 | Canon = getCanonicalType(Pattern); |
| 3345 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 3346 | // contains an alias template specialization which ignores one of its |
| 3347 | // parameters. |
| 3348 | if (Canon->containsUnexpandedParameterPack()) { |
| 3349 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3350 | |
Richard Smith | d8672ef | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3351 | // Find the insert position again, in case we inserted an element into |
| 3352 | // PackExpansionTypes and invalidated our insert position. |
| 3353 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3354 | } |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3357 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3358 | Types.push_back(T); |
| 3359 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 3360 | return QualType(T, 0); |
| 3361 | } |
| 3362 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3363 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 3364 | /// alphabetically. |
| 3365 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 3366 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3367 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3370 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3371 | unsigned NumProtocols) { |
| 3372 | if (NumProtocols == 0) return true; |
| 3373 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3374 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 3375 | return false; |
| 3376 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3377 | for (unsigned i = 1; i != NumProtocols; ++i) |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3378 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) || |
| 3379 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3380 | return false; |
| 3381 | return true; |
| 3382 | } |
| 3383 | |
| 3384 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3385 | unsigned &NumProtocols) { |
| 3386 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3387 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3388 | // Sort protocols, keyed by name. |
| 3389 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 3390 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3391 | // Canonicalize. |
| 3392 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 3393 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 3394 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3395 | // Remove duplicates. |
| 3396 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 3397 | NumProtocols = ProtocolsEnd-Protocols; |
| 3398 | } |
| 3399 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3400 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 3401 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3402 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3403 | // If the base type is an interface and there aren't any protocols |
| 3404 | // to add, then the interface type will do just fine. |
| 3405 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 3406 | return BaseType; |
| 3407 | |
| 3408 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3409 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3410 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3411 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3412 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3413 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3414 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3415 | // Build the canonical type, which has the canonical base type and |
| 3416 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3417 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3418 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 3419 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 3420 | if (!ProtocolsSorted) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3421 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3422 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3423 | unsigned UniqueCount = NumProtocols; |
| 3424 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3425 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3426 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3427 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3428 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3429 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3430 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3434 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3435 | } |
| 3436 | |
| 3437 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 3438 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 3439 | void *Mem = Allocate(Size, TypeAlignment); |
| 3440 | ObjCObjectTypeImpl *T = |
| 3441 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 3442 | |
| 3443 | Types.push_back(T); |
| 3444 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 3445 | return QualType(T, 0); |
| 3446 | } |
| 3447 | |
| 3448 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 3449 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3450 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3451 | llvm::FoldingSetNodeID ID; |
| 3452 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 3453 | |
| 3454 | void *InsertPos = 0; |
| 3455 | if (ObjCObjectPointerType *QT = |
| 3456 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3457 | return QualType(QT, 0); |
| 3458 | |
| 3459 | // Find the canonical object type. |
| 3460 | QualType Canonical; |
| 3461 | if (!ObjectT.isCanonical()) { |
| 3462 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 3463 | |
| 3464 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3465 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3466 | } |
| 3467 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 3468 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3469 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 3470 | ObjCObjectPointerType *QType = |
| 3471 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3472 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3473 | Types.push_back(QType); |
| 3474 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3475 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3476 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3477 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3478 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 3479 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3480 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 3481 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3482 | if (Decl->TypeForDecl) |
| 3483 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3484 | |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3485 | if (PrevDecl) { |
| 3486 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 3487 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3488 | return QualType(PrevDecl->TypeForDecl, 0); |
| 3489 | } |
| 3490 | |
Douglas Gregor | 8d2dbbf | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 3491 | // Prefer the definition, if there is one. |
| 3492 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 3493 | Decl = Def; |
| 3494 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3495 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 3496 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 3497 | Decl->TypeForDecl = T; |
| 3498 | Types.push_back(T); |
| 3499 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3502 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 3503 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3504 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3505 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3506 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3507 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3508 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3509 | if (tofExpr->isTypeDependent()) { |
| 3510 | llvm::FoldingSetNodeID ID; |
| 3511 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3512 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3513 | void *InsertPos = 0; |
| 3514 | DependentTypeOfExprType *Canon |
| 3515 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3516 | if (Canon) { |
| 3517 | // We already have a "canonical" version of an identical, dependent |
| 3518 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3519 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3520 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3521 | } else { |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3522 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3523 | Canon |
| 3524 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3525 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 3526 | toe = Canon; |
| 3527 | } |
| 3528 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3529 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3530 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3531 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3532 | Types.push_back(toe); |
| 3533 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3536 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 3537 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 3538 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3539 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3540 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3541 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3542 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3543 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3544 | Types.push_back(tot); |
| 3545 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 3548 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3549 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 3550 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 3551 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3552 | /// an issue. This doesn't effect the type checker, since it operates |
David Blaikie | 39e0203 | 2011-11-06 22:28:03 +0000 | [diff] [blame] | 3553 | /// on canonical types (which are always unique). |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3554 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3555 | DecltypeType *dt; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3556 | |
| 3557 | // C++0x [temp.type]p2: |
| 3558 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 3559 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 3560 | // type only if their expressions are equivalent (14.5.6.1). |
| 3561 | if (e->isInstantiationDependent()) { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3562 | llvm::FoldingSetNodeID ID; |
| 3563 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3564 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3565 | void *InsertPos = 0; |
| 3566 | DependentDecltypeType *Canon |
| 3567 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3568 | if (Canon) { |
| 3569 | // We already have a "canonical" version of an equivalent, dependent |
| 3570 | // decltype type. Use that as our canonical type. |
Richard Smith | 0d72910 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 3571 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3572 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3573 | } else { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3574 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3575 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3576 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 3577 | dt = Canon; |
| 3578 | } |
| 3579 | } else { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3580 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
| 3581 | getCanonicalType(UnderlyingType)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3582 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3583 | Types.push_back(dt); |
| 3584 | return QualType(dt, 0); |
| 3585 | } |
| 3586 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3587 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 3588 | /// savings are minimal and these are rare. |
| 3589 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 3590 | QualType UnderlyingType, |
| 3591 | UnaryTransformType::UTTKind Kind) |
| 3592 | const { |
| 3593 | UnaryTransformType *Ty = |
Douglas Gregor | 69d9775 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 3594 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 3595 | Kind, |
| 3596 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 12fc4b0 | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 3597 | QualType() : getCanonicalType(UnderlyingType)); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3598 | Types.push_back(Ty); |
| 3599 | return QualType(Ty, 0); |
| 3600 | } |
| 3601 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3602 | /// getAutoType - Return the uniqued reference to the 'auto' type which has been |
| 3603 | /// deduced to the given type, or to the canonical undeduced 'auto' type, or the |
| 3604 | /// canonical deduced-but-dependent 'auto' type. |
| 3605 | QualType ASTContext::getAutoType(QualType DeducedType, bool IsDecltypeAuto, |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3606 | bool IsDependent) const { |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3607 | if (DeducedType.isNull() && !IsDecltypeAuto && !IsDependent) |
| 3608 | return getAutoDeductType(); |
| 3609 | |
| 3610 | // Look in the folding set for an existing type. |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3611 | void *InsertPos = 0; |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3612 | llvm::FoldingSetNodeID ID; |
| 3613 | AutoType::Profile(ID, DeducedType, IsDecltypeAuto, IsDependent); |
| 3614 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3615 | return QualType(AT, 0); |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3616 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3617 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType, |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3618 | IsDecltypeAuto, |
| 3619 | IsDependent); |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3620 | Types.push_back(AT); |
| 3621 | if (InsertPos) |
| 3622 | AutoTypes.InsertNode(AT, InsertPos); |
| 3623 | return QualType(AT, 0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3626 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3627 | /// the given value type. |
| 3628 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3629 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3630 | // structure. |
| 3631 | llvm::FoldingSetNodeID ID; |
| 3632 | AtomicType::Profile(ID, T); |
| 3633 | |
| 3634 | void *InsertPos = 0; |
| 3635 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3636 | return QualType(AT, 0); |
| 3637 | |
| 3638 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3639 | // either, so fill in the canonical type field. |
| 3640 | QualType Canonical; |
| 3641 | if (!T.isCanonical()) { |
| 3642 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3643 | |
| 3644 | // Get the new insert position for the node we care about. |
| 3645 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3646 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 3647 | } |
| 3648 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3649 | Types.push_back(New); |
| 3650 | AtomicTypes.InsertNode(New, InsertPos); |
| 3651 | return QualType(New, 0); |
| 3652 | } |
| 3653 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3654 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3655 | QualType ASTContext::getAutoDeductType() const { |
| 3656 | if (AutoDeductTy.isNull()) |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3657 | AutoDeductTy = QualType( |
| 3658 | new (*this, TypeAlignment) AutoType(QualType(), /*decltype(auto)*/false, |
| 3659 | /*dependent*/false), |
| 3660 | 0); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3661 | return AutoDeductTy; |
| 3662 | } |
| 3663 | |
| 3664 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3665 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3666 | if (AutoRRefDeductTy.isNull()) |
| 3667 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3668 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3669 | return AutoRRefDeductTy; |
| 3670 | } |
| 3671 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3672 | /// getTagDeclType - Return the unique reference to the type for the |
| 3673 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3674 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3675 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3676 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3677 | // away const? mutable? |
| 3678 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3681 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3682 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3683 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3684 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3685 | return getFromTargetType(Target->getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3686 | } |
| 3687 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3688 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3689 | CanQualType ASTContext::getIntMaxType() const { |
| 3690 | return getFromTargetType(Target->getIntMaxType()); |
| 3691 | } |
| 3692 | |
| 3693 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3694 | CanQualType ASTContext::getUIntMaxType() const { |
| 3695 | return getFromTargetType(Target->getUIntMaxType()); |
| 3696 | } |
| 3697 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3698 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3699 | /// Used when in C++, as a GCC extension. |
| 3700 | QualType ASTContext::getSignedWCharType() const { |
| 3701 | // FIXME: derive from "Target" ? |
| 3702 | return WCharTy; |
| 3703 | } |
| 3704 | |
| 3705 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3706 | /// Used when in C++, as a GCC extension. |
| 3707 | QualType ASTContext::getUnsignedWCharType() const { |
| 3708 | // FIXME: derive from "Target" ? |
| 3709 | return UnsignedIntTy; |
| 3710 | } |
| 3711 | |
Enea Zaffanella | 9677eb8 | 2013-01-26 17:08:37 +0000 | [diff] [blame] | 3712 | QualType ASTContext::getIntPtrType() const { |
| 3713 | return getFromTargetType(Target->getIntPtrType()); |
| 3714 | } |
| 3715 | |
| 3716 | QualType ASTContext::getUIntPtrType() const { |
| 3717 | return getCorrespondingUnsignedType(getIntPtrType()); |
| 3718 | } |
| 3719 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3720 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3721 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3722 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3723 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
Eli Friedman | 6902e41 | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 3726 | /// \brief Return the unique type for "pid_t" defined in |
| 3727 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
| 3728 | QualType ASTContext::getProcessIDType() const { |
| 3729 | return getFromTargetType(Target->getProcessIDType()); |
| 3730 | } |
| 3731 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3732 | //===----------------------------------------------------------------------===// |
| 3733 | // Type Operators |
| 3734 | //===----------------------------------------------------------------------===// |
| 3735 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3736 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3737 | // Push qualifiers into arrays, and then discard any remaining |
| 3738 | // qualifiers. |
| 3739 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3740 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3741 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3742 | QualType Result; |
| 3743 | if (isa<ArrayType>(Ty)) { |
| 3744 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3745 | } else if (isa<FunctionType>(Ty)) { |
| 3746 | Result = getPointerType(QualType(Ty, 0)); |
| 3747 | } else { |
| 3748 | Result = QualType(Ty, 0); |
| 3749 | } |
| 3750 | |
| 3751 | return CanQualType::CreateUnsafe(Result); |
| 3752 | } |
| 3753 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3754 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3755 | Qualifiers &quals) { |
| 3756 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3757 | |
| 3758 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3759 | // the unqualified desugared type and then drops it on the floor. |
| 3760 | // We then have to strip that sugar back off with |
| 3761 | // getUnqualifiedDesugaredType(), which is silly. |
| 3762 | const ArrayType *AT = |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3763 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3764 | |
| 3765 | // 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] | 3766 | if (!AT) { |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3767 | quals = splitType.Quals; |
| 3768 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3771 | // Otherwise, recurse on the array's element type. |
| 3772 | QualType elementType = AT->getElementType(); |
| 3773 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3774 | |
| 3775 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3776 | // can just use the results in splitType. |
| 3777 | if (elementType == unqualElementType) { |
| 3778 | assert(quals.empty()); // from the recursive call |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3779 | quals = splitType.Quals; |
| 3780 | return QualType(splitType.Ty, 0); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
| 3783 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3784 | // build the type back up. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3785 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3786 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3787 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3788 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3789 | CAT->getSizeModifier(), 0); |
| 3790 | } |
| 3791 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3792 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3793 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3796 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3797 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3798 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3799 | VAT->getSizeModifier(), |
| 3800 | VAT->getIndexTypeCVRQualifiers(), |
| 3801 | VAT->getBracketsRange()); |
| 3802 | } |
| 3803 | |
| 3804 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3805 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3806 | DSAT->getSizeModifier(), 0, |
| 3807 | SourceRange()); |
| 3808 | } |
| 3809 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3810 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3811 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3812 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3813 | /// or pointer-to-member types, or if they are not similar at this |
| 3814 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3815 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3816 | /// be called in a loop that successively "unwraps" pointer and |
| 3817 | /// pointer-to-member types to compare them at each level. |
| 3818 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3819 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3820 | *T2PtrType = T2->getAs<PointerType>(); |
| 3821 | if (T1PtrType && T2PtrType) { |
| 3822 | T1 = T1PtrType->getPointeeType(); |
| 3823 | T2 = T2PtrType->getPointeeType(); |
| 3824 | return true; |
| 3825 | } |
| 3826 | |
| 3827 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3828 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3829 | if (T1MPType && T2MPType && |
| 3830 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3831 | QualType(T2MPType->getClass(), 0))) { |
| 3832 | T1 = T1MPType->getPointeeType(); |
| 3833 | T2 = T2MPType->getPointeeType(); |
| 3834 | return true; |
| 3835 | } |
| 3836 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3837 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3838 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3839 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3840 | if (T1OPType && T2OPType) { |
| 3841 | T1 = T1OPType->getPointeeType(); |
| 3842 | T2 = T2OPType->getPointeeType(); |
| 3843 | return true; |
| 3844 | } |
| 3845 | } |
| 3846 | |
| 3847 | // FIXME: Block pointers, too? |
| 3848 | |
| 3849 | return false; |
| 3850 | } |
| 3851 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3852 | DeclarationNameInfo |
| 3853 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3854 | SourceLocation NameLoc) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3855 | switch (Name.getKind()) { |
| 3856 | case TemplateName::QualifiedTemplate: |
| 3857 | case TemplateName::Template: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3858 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3859 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3860 | NameLoc); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3861 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3862 | case TemplateName::OverloadedTemplate: { |
| 3863 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3864 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3865 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3866 | } |
| 3867 | |
| 3868 | case TemplateName::DependentTemplate: { |
| 3869 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3870 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3871 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3872 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3873 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3874 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3875 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3876 | // DNInfo work in progress: FIXME: source locations? |
| 3877 | DeclarationNameLoc DNLoc; |
| 3878 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3879 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3880 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3881 | } |
| 3882 | } |
| 3883 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3884 | case TemplateName::SubstTemplateTemplateParm: { |
| 3885 | SubstTemplateTemplateParmStorage *subst |
| 3886 | = Name.getAsSubstTemplateTemplateParm(); |
| 3887 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3888 | NameLoc); |
| 3889 | } |
| 3890 | |
| 3891 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3892 | SubstTemplateTemplateParmPackStorage *subst |
| 3893 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3894 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3895 | NameLoc); |
| 3896 | } |
| 3897 | } |
| 3898 | |
| 3899 | llvm_unreachable("bad template name kind!"); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3902 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3903 | switch (Name.getKind()) { |
| 3904 | case TemplateName::QualifiedTemplate: |
| 3905 | case TemplateName::Template: { |
| 3906 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3907 | if (TemplateTemplateParmDecl *TTP |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3908 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3909 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3910 | |
| 3911 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3912 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3913 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3914 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3915 | case TemplateName::OverloadedTemplate: |
| 3916 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3917 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3918 | case TemplateName::DependentTemplate: { |
| 3919 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3920 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3921 | return DTN->CanonicalTemplateName; |
| 3922 | } |
| 3923 | |
| 3924 | case TemplateName::SubstTemplateTemplateParm: { |
| 3925 | SubstTemplateTemplateParmStorage *subst |
| 3926 | = Name.getAsSubstTemplateTemplateParm(); |
| 3927 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3928 | } |
| 3929 | |
| 3930 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3931 | SubstTemplateTemplateParmPackStorage *subst |
| 3932 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3933 | TemplateTemplateParmDecl *canonParameter |
| 3934 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3935 | TemplateArgument canonArgPack |
| 3936 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3937 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3944 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3945 | X = getCanonicalTemplateName(X); |
| 3946 | Y = getCanonicalTemplateName(Y); |
| 3947 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3948 | } |
| 3949 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3950 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3951 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3952 | switch (Arg.getKind()) { |
| 3953 | case TemplateArgument::Null: |
| 3954 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3955 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3956 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3957 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3958 | |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3959 | case TemplateArgument::Declaration: { |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3960 | ValueDecl *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
| 3961 | return TemplateArgument(D, Arg.isDeclForReferenceParam()); |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3962 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3963 | |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3964 | case TemplateArgument::NullPtr: |
| 3965 | return TemplateArgument(getCanonicalType(Arg.getNullPtrType()), |
| 3966 | /*isNullPtr*/true); |
| 3967 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3968 | case TemplateArgument::Template: |
| 3969 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3970 | |
| 3971 | case TemplateArgument::TemplateExpansion: |
| 3972 | return TemplateArgument(getCanonicalTemplateName( |
| 3973 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3974 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3975 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3976 | case TemplateArgument::Integral: |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 3977 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3978 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3979 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3980 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3981 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3982 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3983 | if (Arg.pack_size() == 0) |
| 3984 | return Arg; |
| 3985 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3986 | TemplateArgument *CanonArgs |
| 3987 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3988 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3990 | AEnd = Arg.pack_end(); |
| 3991 | A != AEnd; (void)++A, ++Idx) |
| 3992 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3993 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3994 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3995 | } |
| 3996 | } |
| 3997 | |
| 3998 | // Silence GCC warning |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3999 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4002 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4003 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4004 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4005 | return 0; |
| 4006 | |
| 4007 | switch (NNS->getKind()) { |
| 4008 | case NestedNameSpecifier::Identifier: |
| 4009 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4010 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4011 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 4012 | NNS->getAsIdentifier()); |
| 4013 | |
| 4014 | case NestedNameSpecifier::Namespace: |
| 4015 | // A namespace is canonical; build a nested-name-specifier with |
| 4016 | // this namespace and no prefix. |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4017 | return NestedNameSpecifier::Create(*this, 0, |
| 4018 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 4019 | |
| 4020 | case NestedNameSpecifier::NamespaceAlias: |
| 4021 | // A namespace is canonical; build a nested-name-specifier with |
| 4022 | // this namespace and no prefix. |
| 4023 | return NestedNameSpecifier::Create(*this, 0, |
| 4024 | NNS->getAsNamespaceAlias()->getNamespace() |
| 4025 | ->getOriginalNamespace()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4026 | |
| 4027 | case NestedNameSpecifier::TypeSpec: |
| 4028 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 4029 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4030 | |
| 4031 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 4032 | // break it apart into its prefix and identifier, then reconsititute those |
| 4033 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 4034 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 4035 | // types, e.g., |
| 4036 | // typedef typename T::type T1; |
| 4037 | // typedef typename T1::type T2; |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 4038 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 4039 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4040 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4041 | |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 4042 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 4043 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 4044 | // first place? |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4045 | return NestedNameSpecifier::Create(*this, 0, false, |
| 4046 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
| 4049 | case NestedNameSpecifier::Global: |
| 4050 | // The global specifier is canonical and unique. |
| 4051 | return NNS; |
| 4052 | } |
| 4053 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4054 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4055 | } |
| 4056 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4057 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4058 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4059 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4060 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4061 | // Handle the common positive case fast. |
| 4062 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 4063 | return AT; |
| 4064 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4065 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4066 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4067 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4068 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4069 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4070 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4071 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 4072 | // 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] | 4073 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4074 | // If we get here, we either have type qualifiers on the type, or we have |
| 4075 | // 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] | 4076 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4077 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4078 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4079 | Qualifiers qs = split.Quals; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4080 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4081 | // If we have a simple case, just return now. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4082 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4083 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4084 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4085 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4086 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 4087 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4088 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4089 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4090 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 4091 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 4092 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4093 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4094 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 4095 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 4096 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4097 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4098 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4099 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4100 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 4101 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4102 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4103 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4104 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4105 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4106 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4108 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4109 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4110 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4111 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4112 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4113 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
Abramo Bagnara | ad9689f | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4116 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4117 | // C99 6.7.5.3p7: |
| 4118 | // A declaration of a parameter as "array of type" shall be |
| 4119 | // adjusted to "qualified pointer to type", where the type |
| 4120 | // qualifiers (if any) are those specified within the [ and ] of |
| 4121 | // the array type derivation. |
| 4122 | if (T->isArrayType()) |
| 4123 | return getArrayDecayedType(T); |
| 4124 | |
| 4125 | // C99 6.7.5.3p8: |
| 4126 | // A declaration of a parameter as "function returning type" |
| 4127 | // shall be adjusted to "pointer to function returning type", as |
| 4128 | // in 6.3.2.1. |
| 4129 | if (T->isFunctionType()) |
| 4130 | return getPointerType(T); |
| 4131 | |
| 4132 | return T; |
| 4133 | } |
| 4134 | |
Abramo Bagnara | ad9689f | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4135 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4136 | T = getVariableArrayDecayedType(T); |
| 4137 | T = getAdjustedParameterType(T); |
| 4138 | return T.getUnqualifiedType(); |
| 4139 | } |
| 4140 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4141 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 4142 | /// specified array type to a pointer. This operation is non-trivial when |
| 4143 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 4144 | /// this returns a pointer to a properly qualified element of the array. |
| 4145 | /// |
| 4146 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4147 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4148 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 4149 | // typedefs in the element type of the array. This also handles propagation |
| 4150 | // of type qualifiers from the array type into the element type if present |
| 4151 | // (C99 6.7.3p8). |
| 4152 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 4153 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4154 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4155 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4156 | |
| 4157 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4158 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4161 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 4162 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4165 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 4166 | Qualifiers qs; |
| 4167 | while (true) { |
| 4168 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4169 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4170 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4171 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4172 | type = array->getElementType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4173 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4174 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4175 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4176 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4179 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4180 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4181 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 4182 | uint64_t ElementCount = 1; |
| 4183 | do { |
| 4184 | ElementCount *= CA->getSize().getZExtValue(); |
Richard Smith | d5e8394 | 2012-12-06 03:04:50 +0000 | [diff] [blame] | 4185 | CA = dyn_cast_or_null<ConstantArrayType>( |
| 4186 | CA->getElementType()->getAsArrayTypeUnsafe()); |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4187 | } while (CA); |
| 4188 | return ElementCount; |
| 4189 | } |
| 4190 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4191 | /// getFloatingRank - Return a relative rank for floating point types. |
| 4192 | /// 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] | 4193 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4194 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4195 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4196 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4197 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 4198 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4199 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4200 | case BuiltinType::Half: return HalfRank; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4201 | case BuiltinType::Float: return FloatRank; |
| 4202 | case BuiltinType::Double: return DoubleRank; |
| 4203 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 4204 | } |
| 4205 | } |
| 4206 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4207 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 4208 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 4209 | /// 'typeDomain' is a real floating point or complex type. |
| 4210 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4211 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 4212 | QualType Domain) const { |
| 4213 | FloatingRank EltRank = getFloatingRank(Size); |
| 4214 | if (Domain->isComplexType()) { |
| 4215 | switch (EltRank) { |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4216 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4217 | case FloatRank: return FloatComplexTy; |
| 4218 | case DoubleRank: return DoubleComplexTy; |
| 4219 | case LongDoubleRank: return LongDoubleComplexTy; |
| 4220 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4221 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4222 | |
| 4223 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 4224 | switch (EltRank) { |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 4225 | case HalfRank: return HalfTy; |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4226 | case FloatRank: return FloatTy; |
| 4227 | case DoubleRank: return DoubleTy; |
| 4228 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4229 | } |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4230 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4233 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 4234 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 4235 | /// '_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] | 4236 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4237 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4238 | FloatingRank LHSR = getFloatingRank(LHS); |
| 4239 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4240 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4241 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4242 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4243 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4244 | return 1; |
| 4245 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4248 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 4249 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 4250 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4251 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4252 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4253 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4254 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4255 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4256 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4257 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4258 | case BuiltinType::Char_S: |
| 4259 | case BuiltinType::Char_U: |
| 4260 | case BuiltinType::SChar: |
| 4261 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4262 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4263 | case BuiltinType::Short: |
| 4264 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4265 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4266 | case BuiltinType::Int: |
| 4267 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4268 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4269 | case BuiltinType::Long: |
| 4270 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4271 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4272 | case BuiltinType::LongLong: |
| 4273 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4274 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4275 | case BuiltinType::Int128: |
| 4276 | case BuiltinType::UInt128: |
| 4277 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4278 | } |
| 4279 | } |
| 4280 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4281 | /// \brief Whether this is a promotable bitfield reference according |
| 4282 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 4283 | /// |
| 4284 | /// \returns the type this bit-field will promote to, or NULL if no |
| 4285 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4286 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 4287 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4288 | return QualType(); |
| 4289 | |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 4290 | FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields? |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4291 | if (!Field) |
| 4292 | return QualType(); |
| 4293 | |
| 4294 | QualType FT = Field->getType(); |
| 4295 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4296 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4297 | uint64_t IntSize = getTypeSize(IntTy); |
| 4298 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 4299 | // to the size of int, it gets promoted no matter what its type is. |
| 4300 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 4301 | if (BitWidth < IntSize) |
| 4302 | return IntTy; |
| 4303 | |
| 4304 | if (BitWidth == IntSize) |
| 4305 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 4306 | |
| 4307 | // Types bigger than int are not subject to promotions, and therefore act |
| 4308 | // like the base type. |
| 4309 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 4310 | // is ridiculous. |
| 4311 | return QualType(); |
| 4312 | } |
| 4313 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4314 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 4315 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 4316 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4317 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4318 | assert(!Promotable.isNull()); |
| 4319 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4320 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 4321 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 68a2dc4 | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 4322 | |
| 4323 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 4324 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 4325 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 4326 | // types that can represent all the values of its underlying type: |
| 4327 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 4328 | // unsigned long long int [...] |
| 4329 | // FIXME: Is there some better way to compute this? |
| 4330 | if (BT->getKind() == BuiltinType::WChar_S || |
| 4331 | BT->getKind() == BuiltinType::WChar_U || |
| 4332 | BT->getKind() == BuiltinType::Char16 || |
| 4333 | BT->getKind() == BuiltinType::Char32) { |
| 4334 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 4335 | uint64_t FromSize = getTypeSize(BT); |
| 4336 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 4337 | LongLongTy, UnsignedLongLongTy }; |
| 4338 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 4339 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 4340 | if (FromSize < ToSize || |
| 4341 | (FromSize == ToSize && |
| 4342 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 4343 | return PromoteTypes[Idx]; |
| 4344 | } |
| 4345 | llvm_unreachable("char type should fit into long long"); |
| 4346 | } |
| 4347 | } |
| 4348 | |
| 4349 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4350 | if (Promotable->isSignedIntegerType()) |
| 4351 | return IntTy; |
Eli Friedman | 5b64e77 | 2012-11-15 01:21:59 +0000 | [diff] [blame] | 4352 | uint64_t PromotableSize = getIntWidth(Promotable); |
| 4353 | uint64_t IntSize = getIntWidth(IntTy); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4354 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 4355 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 4356 | } |
| 4357 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4358 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 4359 | /// type and returns its ownership. |
| 4360 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 4361 | while (!T.isNull()) { |
| 4362 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 4363 | return T.getObjCLifetime(); |
| 4364 | if (T->isArrayType()) |
| 4365 | T = getBaseElementType(T); |
| 4366 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 4367 | T = PT->getPointeeType(); |
| 4368 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 28445f0 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 4369 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4370 | else |
| 4371 | break; |
| 4372 | } |
| 4373 | |
| 4374 | return Qualifiers::OCL_None; |
| 4375 | } |
| 4376 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4377 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4378 | /// 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] | 4379 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4380 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4381 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 4382 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4383 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4384 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4385 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 4386 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4387 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4388 | unsigned LHSRank = getIntegerRank(LHSC); |
| 4389 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4390 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4391 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 4392 | if (LHSRank == RHSRank) return 0; |
| 4393 | return LHSRank > RHSRank ? 1 : -1; |
| 4394 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4395 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4396 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 4397 | if (LHSUnsigned) { |
| 4398 | // If the unsigned [LHS] type is larger, return it. |
| 4399 | if (LHSRank >= RHSRank) |
| 4400 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4401 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4402 | // If the signed type can represent all values of the unsigned type, it |
| 4403 | // 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] | 4404 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4405 | return -1; |
| 4406 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4407 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4408 | // If the unsigned [RHS] type is larger, return it. |
| 4409 | if (RHSRank >= LHSRank) |
| 4410 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4411 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4412 | // If the signed type can represent all values of the unsigned type, it |
| 4413 | // 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] | 4414 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4415 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4416 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4417 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4418 | static RecordDecl * |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4419 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 4420 | DeclContext *DC, IdentifierInfo *Id) { |
| 4421 | SourceLocation Loc; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4422 | if (Ctx.getLangOpts().CPlusPlus) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4423 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4424 | else |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4425 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4426 | } |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4427 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4428 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4429 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4430 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4431 | CFConstantStringTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4432 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4433 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4434 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4435 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4436 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4437 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4438 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4439 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4440 | // int flags; |
| 4441 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4442 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4443 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4444 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4445 | FieldTypes[3] = LongTy; |
| 4446 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4447 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4448 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4449 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4450 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4451 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4452 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4453 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4454 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4455 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4456 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4457 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4460 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4461 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4462 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4463 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 4464 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4465 | |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 4466 | QualType ASTContext::getObjCSuperType() const { |
| 4467 | if (ObjCSuperType.isNull()) { |
| 4468 | RecordDecl *ObjCSuperTypeDecl = |
| 4469 | CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get("objc_super")); |
| 4470 | TUDecl->addDecl(ObjCSuperTypeDecl); |
| 4471 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
| 4472 | } |
| 4473 | return ObjCSuperType; |
| 4474 | } |
| 4475 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4476 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4477 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4478 | assert(Rec && "Invalid CFConstantStringType"); |
| 4479 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 4480 | } |
| 4481 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4482 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4483 | if (BlockDescriptorType) |
| 4484 | return getTagDeclType(BlockDescriptorType); |
| 4485 | |
| 4486 | RecordDecl *T; |
| 4487 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4488 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4489 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4490 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4491 | |
| 4492 | QualType FieldTypes[] = { |
| 4493 | UnsignedLongTy, |
| 4494 | UnsignedLongTy, |
| 4495 | }; |
| 4496 | |
| 4497 | const char *FieldNames[] = { |
| 4498 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4499 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4500 | }; |
| 4501 | |
| 4502 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4503 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4504 | SourceLocation(), |
| 4505 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4506 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4507 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4508 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4509 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4510 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4511 | T->addDecl(Field); |
| 4512 | } |
| 4513 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4514 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4515 | |
| 4516 | BlockDescriptorType = T; |
| 4517 | |
| 4518 | return getTagDeclType(BlockDescriptorType); |
| 4519 | } |
| 4520 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4521 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4522 | if (BlockDescriptorExtendedType) |
| 4523 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4524 | |
| 4525 | RecordDecl *T; |
| 4526 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4527 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4528 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4529 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4530 | |
| 4531 | QualType FieldTypes[] = { |
| 4532 | UnsignedLongTy, |
| 4533 | UnsignedLongTy, |
| 4534 | getPointerType(VoidPtrTy), |
| 4535 | getPointerType(VoidPtrTy) |
| 4536 | }; |
| 4537 | |
| 4538 | const char *FieldNames[] = { |
| 4539 | "reserved", |
| 4540 | "Size", |
| 4541 | "CopyFuncPtr", |
| 4542 | "DestroyFuncPtr" |
| 4543 | }; |
| 4544 | |
| 4545 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4546 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4547 | SourceLocation(), |
| 4548 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4549 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4550 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4551 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4552 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4553 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4554 | T->addDecl(Field); |
| 4555 | } |
| 4556 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4557 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4558 | |
| 4559 | BlockDescriptorExtendedType = T; |
| 4560 | |
| 4561 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4562 | } |
| 4563 | |
Fariborz Jahanian | b15c898 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4564 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
| 4565 | /// requires copy/dispose. Note that this must match the logic |
| 4566 | /// in buildByrefHelpers. |
| 4567 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
| 4568 | const VarDecl *D) { |
| 4569 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
| 4570 | const Expr *copyExpr = getBlockVarCopyInits(D); |
| 4571 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
| 4572 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4573 | return true; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4574 | } |
Fariborz Jahanian | b15c898 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4575 | |
| 4576 | if (!Ty->isObjCRetainableType()) return false; |
| 4577 | |
| 4578 | Qualifiers qs = Ty.getQualifiers(); |
| 4579 | |
| 4580 | // If we have lifetime, that dominates. |
| 4581 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
| 4582 | assert(getLangOpts().ObjCAutoRefCount); |
| 4583 | |
| 4584 | switch (lifetime) { |
| 4585 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
| 4586 | |
| 4587 | // These are just bits as far as the runtime is concerned. |
| 4588 | case Qualifiers::OCL_ExplicitNone: |
| 4589 | case Qualifiers::OCL_Autoreleasing: |
| 4590 | return false; |
| 4591 | |
| 4592 | // Tell the runtime that this is ARC __weak, called by the |
| 4593 | // byref routines. |
| 4594 | case Qualifiers::OCL_Weak: |
| 4595 | // ARC __strong __block variables need to be retained. |
| 4596 | case Qualifiers::OCL_Strong: |
| 4597 | return true; |
| 4598 | } |
| 4599 | llvm_unreachable("fell out of lifetime switch!"); |
| 4600 | } |
| 4601 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
| 4602 | Ty->isObjCObjectPointerType()); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4605 | bool ASTContext::getByrefLifetime(QualType Ty, |
| 4606 | Qualifiers::ObjCLifetime &LifeTime, |
| 4607 | bool &HasByrefExtendedLayout) const { |
| 4608 | |
| 4609 | if (!getLangOpts().ObjC1 || |
| 4610 | getLangOpts().getGC() != LangOptions::NonGC) |
| 4611 | return false; |
| 4612 | |
| 4613 | HasByrefExtendedLayout = false; |
Fariborz Jahanian | 34db84f | 2012-12-11 19:58:01 +0000 | [diff] [blame] | 4614 | if (Ty->isRecordType()) { |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4615 | HasByrefExtendedLayout = true; |
| 4616 | LifeTime = Qualifiers::OCL_None; |
| 4617 | } |
| 4618 | else if (getLangOpts().ObjCAutoRefCount) |
| 4619 | LifeTime = Ty.getObjCLifetime(); |
| 4620 | // MRR. |
| 4621 | else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 4622 | LifeTime = Qualifiers::OCL_ExplicitNone; |
| 4623 | else |
| 4624 | LifeTime = Qualifiers::OCL_None; |
| 4625 | return true; |
| 4626 | } |
| 4627 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4628 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 4629 | if (!ObjCInstanceTypeDecl) |
| 4630 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 4631 | getTranslationUnitDecl(), |
| 4632 | SourceLocation(), |
| 4633 | SourceLocation(), |
| 4634 | &Idents.get("instancetype"), |
| 4635 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 4636 | return ObjCInstanceTypeDecl; |
| 4637 | } |
| 4638 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4639 | // This returns true if a type has been typedefed to BOOL: |
| 4640 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 4641 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4642 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 4643 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 4644 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4645 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4646 | return false; |
| 4647 | } |
| 4648 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4649 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4650 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4651 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4652 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4653 | return CharUnits::Zero(); |
| 4654 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4655 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4656 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4657 | // 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] | 4658 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4659 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4660 | // Treat arrays as pointers, since that's how they're passed in. |
| 4661 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4662 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4663 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4664 | } |
| 4665 | |
| 4666 | static inline |
| 4667 | std::string charUnitsToString(const CharUnits &CU) { |
| 4668 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4671 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4672 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4673 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4674 | std::string S; |
| 4675 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4676 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4677 | QualType BlockTy = |
| 4678 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4679 | // Encode result type. |
Fariborz Jahanian | 3d145f6 | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 4680 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 06cffc0 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 4681 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, |
| 4682 | BlockTy->getAs<FunctionType>()->getResultType(), |
| 4683 | S, true /*Extended*/); |
| 4684 | else |
| 4685 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), |
| 4686 | S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4687 | // Compute size of all parameters. |
| 4688 | // Start with computing size of a pointer in number of bytes. |
| 4689 | // FIXME: There might(should) be a better way of doing this computation! |
| 4690 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4691 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4692 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 4693 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4694 | E = Decl->param_end(); PI != E; ++PI) { |
| 4695 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4696 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 075a543 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 4697 | if (sz.isZero()) |
| 4698 | continue; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4699 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4700 | ParmOffset += sz; |
| 4701 | } |
| 4702 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4703 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4704 | // Block pointer and offset. |
| 4705 | S += "@?0"; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4706 | |
| 4707 | // Argument types. |
| 4708 | ParmOffset = PtrSize; |
| 4709 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 4710 | Decl->param_end(); PI != E; ++PI) { |
| 4711 | ParmVarDecl *PVDecl = *PI; |
| 4712 | QualType PType = PVDecl->getOriginalType(); |
| 4713 | if (const ArrayType *AT = |
| 4714 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4715 | // Use array's original type only if it has known number of |
| 4716 | // elements. |
| 4717 | if (!isa<ConstantArrayType>(AT)) |
| 4718 | PType = PVDecl->getType(); |
| 4719 | } else if (PType->isFunctionType()) |
| 4720 | PType = PVDecl->getType(); |
Fariborz Jahanian | 3d145f6 | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 4721 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 06cffc0 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 4722 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType, |
| 4723 | S, true /*Extended*/); |
| 4724 | else |
| 4725 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4726 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4727 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4728 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4729 | |
| 4730 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4733 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4734 | std::string& S) { |
| 4735 | // Encode result type. |
| 4736 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4737 | CharUnits ParmOffset; |
| 4738 | // Compute size of all parameters. |
| 4739 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4740 | E = Decl->param_end(); PI != E; ++PI) { |
| 4741 | QualType PType = (*PI)->getType(); |
| 4742 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4743 | if (sz.isZero()) |
Fariborz Jahanian | 7e68ba5 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4744 | continue; |
| 4745 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4746 | assert (sz.isPositive() && |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4747 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4748 | ParmOffset += sz; |
| 4749 | } |
| 4750 | S += charUnitsToString(ParmOffset); |
| 4751 | ParmOffset = CharUnits::Zero(); |
| 4752 | |
| 4753 | // Argument types. |
| 4754 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4755 | E = Decl->param_end(); PI != E; ++PI) { |
| 4756 | ParmVarDecl *PVDecl = *PI; |
| 4757 | QualType PType = PVDecl->getOriginalType(); |
| 4758 | if (const ArrayType *AT = |
| 4759 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4760 | // Use array's original type only if it has known number of |
| 4761 | // elements. |
| 4762 | if (!isa<ConstantArrayType>(AT)) |
| 4763 | PType = PVDecl->getType(); |
| 4764 | } else if (PType->isFunctionType()) |
| 4765 | PType = PVDecl->getType(); |
| 4766 | getObjCEncodingForType(PType, S); |
| 4767 | S += charUnitsToString(ParmOffset); |
| 4768 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4769 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4770 | |
| 4771 | return false; |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4774 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 4775 | /// method parameter or return type. If Extended, include class names and |
| 4776 | /// block object types. |
| 4777 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 4778 | QualType T, std::string& S, |
| 4779 | bool Extended) const { |
| 4780 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 4781 | getObjCEncodingForTypeQualifier(QT, S); |
| 4782 | // Encode parameter type. |
| 4783 | getObjCEncodingForTypeImpl(T, S, true, true, 0, |
| 4784 | true /*OutermostType*/, |
| 4785 | false /*EncodingProperty*/, |
| 4786 | false /*StructField*/, |
| 4787 | Extended /*EncodeBlockParameters*/, |
| 4788 | Extended /*EncodeClassNames*/); |
| 4789 | } |
| 4790 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4791 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4792 | /// declaration. |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4793 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4794 | std::string& S, |
| 4795 | bool Extended) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4796 | // FIXME: This is not very efficient. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4797 | // Encode return type. |
| 4798 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 4799 | Decl->getResultType(), S, Extended); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4800 | // Compute size of all parameters. |
| 4801 | // Start with computing size of a pointer in number of bytes. |
| 4802 | // FIXME: There might(should) be a better way of doing this computation! |
| 4803 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4804 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4805 | // The first two arguments (self and _cmd) are pointers; account for |
| 4806 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4807 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4808 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4809 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4810 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4811 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4812 | if (sz.isZero()) |
Fariborz Jahanian | 7e68ba5 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4813 | continue; |
| 4814 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4815 | assert (sz.isPositive() && |
| 4816 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4817 | ParmOffset += sz; |
| 4818 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4819 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4820 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4821 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4822 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4823 | // Argument types. |
| 4824 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4825 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4826 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4827 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4828 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4829 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4830 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4831 | // Use array's original type only if it has known number of |
| 4832 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4833 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4834 | PType = PVDecl->getType(); |
| 4835 | } else if (PType->isFunctionType()) |
| 4836 | PType = PVDecl->getType(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4837 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 4838 | PType, S, Extended); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4839 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4840 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4841 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4842 | |
| 4843 | return false; |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4844 | } |
| 4845 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4846 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4847 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4848 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4849 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4850 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4851 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4852 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4853 | /// encoded as single characters, followed by an identifier. Property types |
| 4854 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4855 | /// these attributes are defined by the following enumeration: |
| 4856 | /// @code |
| 4857 | /// enum PropertyAttributes { |
| 4858 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4859 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4860 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4861 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4862 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4863 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4864 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 0d4cb85 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 4865 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4866 | /// kPropertyWeak = 'W' // 'weak' property |
| 4867 | /// kPropertyStrong = 'P' // property GC'able |
| 4868 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4869 | /// }; |
| 4870 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4871 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4872 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4873 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4874 | // Collect information from the property implementation decl(s). |
| 4875 | bool Dynamic = false; |
| 4876 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4877 | |
| 4878 | // FIXME: Duplicated code due to poor abstraction. |
| 4879 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4880 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4881 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4882 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4883 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4884 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4885 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4886 | if (PID->getPropertyDecl() == PD) { |
| 4887 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4888 | Dynamic = true; |
| 4889 | } else { |
| 4890 | SynthesizePID = PID; |
| 4891 | } |
| 4892 | } |
| 4893 | } |
| 4894 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4895 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4896 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4897 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4898 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4899 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4900 | if (PID->getPropertyDecl() == PD) { |
| 4901 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4902 | Dynamic = true; |
| 4903 | } else { |
| 4904 | SynthesizePID = PID; |
| 4905 | } |
| 4906 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4907 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4908 | } |
| 4909 | } |
| 4910 | |
| 4911 | // FIXME: This is not very efficient. |
| 4912 | S = "T"; |
| 4913 | |
| 4914 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4915 | // GCC has some special rules regarding encoding of properties which |
| 4916 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4917 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4918 | true /* outermost type */, |
| 4919 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4920 | |
| 4921 | if (PD->isReadOnly()) { |
| 4922 | S += ",R"; |
Nico Weber | d7ceab3 | 2013-05-08 23:47:40 +0000 | [diff] [blame] | 4923 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) |
| 4924 | S += ",C"; |
| 4925 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) |
| 4926 | S += ",&"; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4927 | } else { |
| 4928 | switch (PD->getSetterKind()) { |
| 4929 | case ObjCPropertyDecl::Assign: break; |
| 4930 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4931 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 3a02b44 | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4932 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4933 | } |
| 4934 | } |
| 4935 | |
| 4936 | // It really isn't clear at all what this means, since properties |
| 4937 | // are "dynamic by default". |
| 4938 | if (Dynamic) |
| 4939 | S += ",D"; |
| 4940 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4941 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4942 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4944 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4945 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4946 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
| 4949 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4950 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4951 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | if (SynthesizePID) { |
| 4955 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4956 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4957 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
| 4960 | // FIXME: OBJCGC: weak & strong |
| 4961 | } |
| 4962 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4963 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4964 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4965 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4966 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4967 | /// |
| 4968 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4969 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4970 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4971 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4972 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4973 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4974 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4975 | PointeeTy = IntTy; |
| 4976 | } |
| 4977 | } |
| 4978 | } |
| 4979 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4980 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4981 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4982 | // We follow the behavior of gcc, expanding structures which are |
| 4983 | // directly pointed to, and expanding embedded structures. Note that |
| 4984 | // these rules are sufficient to prevent recursive encoding of the |
| 4985 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4986 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4987 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4990 | static char getObjCEncodingForPrimitiveKind(const ASTContext *C, |
| 4991 | BuiltinType::Kind kind) { |
| 4992 | switch (kind) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4993 | case BuiltinType::Void: return 'v'; |
| 4994 | case BuiltinType::Bool: return 'B'; |
| 4995 | case BuiltinType::Char_U: |
| 4996 | case BuiltinType::UChar: return 'C'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4997 | case BuiltinType::Char16: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4998 | case BuiltinType::UShort: return 'S'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4999 | case BuiltinType::Char32: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5000 | case BuiltinType::UInt: return 'I'; |
| 5001 | case BuiltinType::ULong: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5002 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5003 | case BuiltinType::UInt128: return 'T'; |
| 5004 | case BuiltinType::ULongLong: return 'Q'; |
| 5005 | case BuiltinType::Char_S: |
| 5006 | case BuiltinType::SChar: return 'c'; |
| 5007 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 5008 | case BuiltinType::WChar_S: |
| 5009 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5010 | case BuiltinType::Int: return 'i'; |
| 5011 | case BuiltinType::Long: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5012 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5013 | case BuiltinType::LongLong: return 'q'; |
| 5014 | case BuiltinType::Int128: return 't'; |
| 5015 | case BuiltinType::Float: return 'f'; |
| 5016 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 5017 | case BuiltinType::LongDouble: return 'D'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5018 | case BuiltinType::NullPtr: return '*'; // like char* |
| 5019 | |
| 5020 | case BuiltinType::Half: |
| 5021 | // FIXME: potentially need @encodes for these! |
| 5022 | return ' '; |
| 5023 | |
| 5024 | case BuiltinType::ObjCId: |
| 5025 | case BuiltinType::ObjCClass: |
| 5026 | case BuiltinType::ObjCSel: |
| 5027 | llvm_unreachable("@encoding ObjC primitive type"); |
| 5028 | |
| 5029 | // OpenCL and placeholder types don't need @encodings. |
| 5030 | case BuiltinType::OCLImage1d: |
| 5031 | case BuiltinType::OCLImage1dArray: |
| 5032 | case BuiltinType::OCLImage1dBuffer: |
| 5033 | case BuiltinType::OCLImage2d: |
| 5034 | case BuiltinType::OCLImage2dArray: |
| 5035 | case BuiltinType::OCLImage3d: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5036 | case BuiltinType::OCLEvent: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5037 | case BuiltinType::OCLSampler: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5038 | case BuiltinType::Dependent: |
| 5039 | #define BUILTIN_TYPE(KIND, ID) |
| 5040 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
| 5041 | case BuiltinType::KIND: |
| 5042 | #include "clang/AST/BuiltinTypes.def" |
| 5043 | llvm_unreachable("invalid builtin type for @encode"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5044 | } |
David Blaikie | 719e53f | 2013-01-09 17:48:41 +0000 | [diff] [blame] | 5045 | llvm_unreachable("invalid BuiltinType::Kind value"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5046 | } |
| 5047 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5048 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 5049 | EnumDecl *Enum = ET->getDecl(); |
| 5050 | |
| 5051 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 5052 | if (!Enum->isFixed()) |
| 5053 | return 'i'; |
| 5054 | |
| 5055 | // The encoding of a fixed enum type matches its fixed underlying type. |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5056 | const BuiltinType *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
| 5057 | return getObjCEncodingForPrimitiveKind(C, BT->getKind()); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5058 | } |
| 5059 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5060 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5061 | QualType T, const FieldDecl *FD) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5062 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 5063 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5064 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 5065 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 5066 | // then the offset (in bits) of the first element, then the type of the |
| 5067 | // bitfield, then the size in bits. For example, in this structure: |
| 5068 | // |
| 5069 | // struct |
| 5070 | // { |
| 5071 | // int integer; |
| 5072 | // int flags:2; |
| 5073 | // }; |
| 5074 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 5075 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 5076 | // information is not especially sensible, but we're stuck with it for |
| 5077 | // compatibility with GCC, although providing it breaks anything that |
| 5078 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 5079 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5080 | const RecordDecl *RD = FD->getParent(); |
| 5081 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | 8290574 | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 5082 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5083 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 5084 | S += ObjCEncodingForEnumType(Ctx, ET); |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5085 | else { |
| 5086 | const BuiltinType *BT = T->castAs<BuiltinType>(); |
| 5087 | S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind()); |
| 5088 | } |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5089 | } |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5090 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 5091 | } |
| 5092 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 5093 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5094 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 5095 | bool ExpandPointedToStructures, |
| 5096 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 5097 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 5098 | bool OutermostType, |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5099 | bool EncodingProperty, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5100 | bool StructField, |
| 5101 | bool EncodeBlockParameters, |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5102 | bool EncodeClassNames, |
| 5103 | bool EncodePointerToObjCTypedef) const { |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5104 | CanQualType CT = getCanonicalType(T); |
| 5105 | switch (CT->getTypeClass()) { |
| 5106 | case Type::Builtin: |
| 5107 | case Type::Enum: |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5108 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5109 | return EncodeBitField(this, S, T, FD); |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5110 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CT)) |
| 5111 | S += getObjCEncodingForPrimitiveKind(this, BT->getKind()); |
| 5112 | else |
| 5113 | S += ObjCEncodingForEnumType(this, cast<EnumType>(CT)); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5114 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5115 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5116 | case Type::Complex: { |
| 5117 | const ComplexType *CT = T->castAs<ComplexType>(); |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 5118 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5119 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 5120 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5121 | return; |
| 5122 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5123 | |
| 5124 | case Type::Atomic: { |
| 5125 | const AtomicType *AT = T->castAs<AtomicType>(); |
| 5126 | S += 'A'; |
| 5127 | getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, 0, |
| 5128 | false, false); |
| 5129 | return; |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 5130 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5131 | |
| 5132 | // encoding for pointer or reference types. |
| 5133 | case Type::Pointer: |
| 5134 | case Type::LValueReference: |
| 5135 | case Type::RValueReference: { |
| 5136 | QualType PointeeTy; |
| 5137 | if (isa<PointerType>(CT)) { |
| 5138 | const PointerType *PT = T->castAs<PointerType>(); |
| 5139 | if (PT->isObjCSelType()) { |
| 5140 | S += ':'; |
| 5141 | return; |
| 5142 | } |
| 5143 | PointeeTy = PT->getPointeeType(); |
| 5144 | } else { |
| 5145 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
| 5146 | } |
| 5147 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5148 | bool isReadOnly = false; |
| 5149 | // For historical/compatibility reasons, the read-only qualifier of the |
| 5150 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 5151 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5152 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 5153 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5154 | if (OutermostType && T.isConstQualified()) { |
| 5155 | isReadOnly = true; |
| 5156 | S += 'r'; |
| 5157 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 5158 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5159 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5160 | while (P->getAs<PointerType>()) |
| 5161 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5162 | if (P.isConstQualified()) { |
| 5163 | isReadOnly = true; |
| 5164 | S += 'r'; |
| 5165 | } |
| 5166 | } |
| 5167 | if (isReadOnly) { |
| 5168 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 5169 | // combinations need to be rearranged. |
| 5170 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5171 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 5172 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5173 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5174 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5175 | if (PointeeTy->isCharType()) { |
| 5176 | // char pointer types should be encoded as '*' unless it is a |
| 5177 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 5178 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5179 | S += '*'; |
| 5180 | return; |
| 5181 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5182 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 5183 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 5184 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 5185 | S += '#'; |
| 5186 | return; |
| 5187 | } |
| 5188 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 5189 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 5190 | S += '@'; |
| 5191 | return; |
| 5192 | } |
| 5193 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5194 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5195 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5196 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 5197 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5198 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5199 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5200 | return; |
| 5201 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5202 | |
| 5203 | case Type::ConstantArray: |
| 5204 | case Type::IncompleteArray: |
| 5205 | case Type::VariableArray: { |
| 5206 | const ArrayType *AT = cast<ArrayType>(CT); |
| 5207 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5208 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5209 | // Incomplete arrays are encoded as a pointer to the array element. |
| 5210 | S += '^'; |
| 5211 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5212 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5213 | false, ExpandStructures, FD); |
| 5214 | } else { |
| 5215 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5216 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5217 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 5218 | if (getTypeSize(CAT->getElementType()) == 0) |
| 5219 | S += '0'; |
| 5220 | else |
| 5221 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 5222 | } else { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5223 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5224 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 5225 | "Unknown array type!"); |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5226 | S += '0'; |
| 5227 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5228 | |
| 5229 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5230 | false, ExpandStructures, FD); |
| 5231 | S += ']'; |
| 5232 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5233 | return; |
| 5234 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5235 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5236 | case Type::FunctionNoProto: |
| 5237 | case Type::FunctionProto: |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 5238 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5239 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5240 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5241 | case Type::Record: { |
| 5242 | RecordDecl *RDecl = cast<RecordType>(CT)->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5243 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5244 | // Anonymous structures print as '?' |
| 5245 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 5246 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5247 | if (ClassTemplateSpecializationDecl *Spec |
| 5248 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 5249 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Benjamin Kramer | 5eada84 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 5250 | llvm::raw_string_ostream OS(S); |
| 5251 | TemplateSpecializationType::PrintTemplateArgumentList(OS, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5252 | TemplateArgs.data(), |
| 5253 | TemplateArgs.size(), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 5254 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5255 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5256 | } else { |
| 5257 | S += '?'; |
| 5258 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 5259 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5260 | S += '='; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5261 | if (!RDecl->isUnion()) { |
| 5262 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 5263 | } else { |
| 5264 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 5265 | FieldEnd = RDecl->field_end(); |
| 5266 | Field != FieldEnd; ++Field) { |
| 5267 | if (FD) { |
| 5268 | S += '"'; |
| 5269 | S += Field->getNameAsString(); |
| 5270 | S += '"'; |
| 5271 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5272 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5273 | // Special case bit-fields. |
| 5274 | if (Field->isBitField()) { |
| 5275 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5276 | *Field); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5277 | } else { |
| 5278 | QualType qt = Field->getType(); |
| 5279 | getLegacyIntegralTypeEncoding(qt); |
| 5280 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 5281 | FD, /*OutermostType*/false, |
| 5282 | /*EncodingProperty*/false, |
| 5283 | /*StructField*/true); |
| 5284 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5285 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5286 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 5287 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5288 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5289 | return; |
| 5290 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5291 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5292 | case Type::BlockPointer: { |
| 5293 | const BlockPointerType *BT = T->castAs<BlockPointerType>(); |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 5294 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5295 | if (EncodeBlockParameters) { |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5296 | const FunctionType *FT = BT->getPointeeType()->castAs<FunctionType>(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5297 | |
| 5298 | S += '<'; |
| 5299 | // Block return type |
| 5300 | getObjCEncodingForTypeImpl(FT->getResultType(), S, |
| 5301 | ExpandPointedToStructures, ExpandStructures, |
| 5302 | FD, |
| 5303 | false /* OutermostType */, |
| 5304 | EncodingProperty, |
| 5305 | false /* StructField */, |
| 5306 | EncodeBlockParameters, |
| 5307 | EncodeClassNames); |
| 5308 | // Block self |
| 5309 | S += "@?"; |
| 5310 | // Block parameters |
| 5311 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 5312 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(), |
| 5313 | E = FPT->arg_type_end(); I && (I != E); ++I) { |
| 5314 | getObjCEncodingForTypeImpl(*I, S, |
| 5315 | ExpandPointedToStructures, |
| 5316 | ExpandStructures, |
| 5317 | FD, |
| 5318 | false /* OutermostType */, |
| 5319 | EncodingProperty, |
| 5320 | false /* StructField */, |
| 5321 | EncodeBlockParameters, |
| 5322 | EncodeClassNames); |
| 5323 | } |
| 5324 | } |
| 5325 | S += '>'; |
| 5326 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5327 | return; |
| 5328 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5329 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5330 | case Type::ObjCObject: |
| 5331 | case Type::ObjCInterface: { |
| 5332 | // Ignore protocol qualifiers when mangling at this level. |
| 5333 | T = T->castAs<ObjCObjectType>()->getBaseType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5334 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5335 | // The assumption seems to be that this assert will succeed |
| 5336 | // because nested levels will have filtered out 'id' and 'Class'. |
| 5337 | const ObjCInterfaceType *OIT = T->castAs<ObjCInterfaceType>(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5338 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5339 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5340 | S += '{'; |
| 5341 | const IdentifierInfo *II = OI->getIdentifier(); |
| 5342 | S += II->getName(); |
| 5343 | S += '='; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5344 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5345 | DeepCollectObjCIvars(OI, true, Ivars); |
| 5346 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5347 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5348 | if (Field->isBitField()) |
| 5349 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5350 | else |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5351 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD, |
| 5352 | false, false, false, false, false, |
| 5353 | EncodePointerToObjCTypedef); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5354 | } |
| 5355 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5356 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5357 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5358 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5359 | case Type::ObjCObjectPointer: { |
| 5360 | const ObjCObjectPointerType *OPT = T->castAs<ObjCObjectPointerType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5361 | if (OPT->isObjCIdType()) { |
| 5362 | S += '@'; |
| 5363 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5364 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5365 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5366 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 5367 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 5368 | // Since this is a binary compatibility issue, need to consult with runtime |
| 5369 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5370 | S += '#'; |
| 5371 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5372 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5373 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5374 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5375 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5376 | ExpandPointedToStructures, |
| 5377 | ExpandStructures, FD); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5378 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5379 | // Note that we do extended encoding of protocol qualifer list |
| 5380 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5381 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5382 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 5383 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5384 | S += '<'; |
| 5385 | S += (*I)->getNameAsString(); |
| 5386 | S += '>'; |
| 5387 | } |
| 5388 | S += '"'; |
| 5389 | } |
| 5390 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5391 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5392 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5393 | QualType PointeeTy = OPT->getPointeeType(); |
| 5394 | if (!EncodingProperty && |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5395 | isa<TypedefType>(PointeeTy.getTypePtr()) && |
| 5396 | !EncodePointerToObjCTypedef) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5397 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5398 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5399 | // {...}; |
| 5400 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5401 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 5402 | false, ExpandPointedToStructures, |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5403 | NULL, |
| 5404 | false, false, false, false, false, |
| 5405 | /*EncodePointerToObjCTypedef*/true); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5406 | return; |
| 5407 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5408 | |
| 5409 | S += '@'; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5410 | if (OPT->getInterfaceDecl() && |
| 5411 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5412 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5413 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5414 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 5415 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5416 | S += '<'; |
| 5417 | S += (*I)->getNameAsString(); |
| 5418 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5419 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5420 | S += '"'; |
| 5421 | } |
| 5422 | return; |
| 5423 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5424 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 5425 | // gcc just blithely ignores member pointers. |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5426 | // FIXME: we shoul do better than that. 'M' is available. |
| 5427 | case Type::MemberPointer: |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 5428 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5429 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5430 | case Type::Vector: |
| 5431 | case Type::ExtVector: |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5432 | // This matches gcc's encoding, even though technically it is |
| 5433 | // insufficient. |
| 5434 | // FIXME. We should do a better job than gcc. |
| 5435 | return; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5436 | |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5437 | case Type::Auto: |
| 5438 | // We could see an undeduced auto type here during error recovery. |
| 5439 | // Just ignore it. |
| 5440 | return; |
| 5441 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5442 | #define ABSTRACT_TYPE(KIND, BASE) |
| 5443 | #define TYPE(KIND, BASE) |
| 5444 | #define DEPENDENT_TYPE(KIND, BASE) \ |
| 5445 | case Type::KIND: |
| 5446 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
| 5447 | case Type::KIND: |
| 5448 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
| 5449 | case Type::KIND: |
| 5450 | #include "clang/AST/TypeNodes.def" |
| 5451 | llvm_unreachable("@encode for dependent type!"); |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5452 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5453 | llvm_unreachable("bad type kind!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5454 | } |
| 5455 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5456 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 5457 | std::string &S, |
| 5458 | const FieldDecl *FD, |
| 5459 | bool includeVBases) const { |
| 5460 | assert(RDecl && "Expected non-null RecordDecl"); |
| 5461 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 5462 | if (!RDecl->getDefinition()) |
| 5463 | return; |
| 5464 | |
| 5465 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 5466 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 5467 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 5468 | |
| 5469 | if (CXXRec) { |
| 5470 | for (CXXRecordDecl::base_class_iterator |
| 5471 | BI = CXXRec->bases_begin(), |
| 5472 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 5473 | if (!BI->isVirtual()) { |
| 5474 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5475 | if (base->isEmpty()) |
| 5476 | continue; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5477 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5478 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 5479 | std::make_pair(offs, base)); |
| 5480 | } |
| 5481 | } |
| 5482 | } |
| 5483 | |
| 5484 | unsigned i = 0; |
| 5485 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 5486 | FieldEnd = RDecl->field_end(); |
| 5487 | Field != FieldEnd; ++Field, ++i) { |
| 5488 | uint64_t offs = layout.getFieldOffset(i); |
| 5489 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5490 | std::make_pair(offs, *Field)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | if (CXXRec && includeVBases) { |
| 5494 | for (CXXRecordDecl::base_class_iterator |
| 5495 | BI = CXXRec->vbases_begin(), |
| 5496 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 5497 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5498 | if (base->isEmpty()) |
| 5499 | continue; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5500 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Argyrios Kyrtzidis | 19aa860 | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 5501 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 5502 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 5503 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5504 | } |
| 5505 | } |
| 5506 | |
| 5507 | CharUnits size; |
| 5508 | if (CXXRec) { |
| 5509 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 5510 | } else { |
| 5511 | size = layout.getSize(); |
| 5512 | } |
| 5513 | |
| 5514 | uint64_t CurOffs = 0; |
| 5515 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 5516 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 5517 | |
Douglas Gregor | 58db7a5 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 5518 | if (CXXRec && CXXRec->isDynamicClass() && |
| 5519 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5520 | if (FD) { |
| 5521 | S += "\"_vptr$"; |
| 5522 | std::string recname = CXXRec->getNameAsString(); |
| 5523 | if (recname.empty()) recname = "?"; |
| 5524 | S += recname; |
| 5525 | S += '"'; |
| 5526 | } |
| 5527 | S += "^^?"; |
| 5528 | CurOffs += getTypeSize(VoidPtrTy); |
| 5529 | } |
| 5530 | |
| 5531 | if (!RDecl->hasFlexibleArrayMember()) { |
| 5532 | // Mark the end of the structure. |
| 5533 | uint64_t offs = toBits(size); |
| 5534 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 5535 | std::make_pair(offs, (NamedDecl*)0)); |
| 5536 | } |
| 5537 | |
| 5538 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 5539 | assert(CurOffs <= CurLayObj->first); |
| 5540 | |
| 5541 | if (CurOffs < CurLayObj->first) { |
| 5542 | uint64_t padding = CurLayObj->first - CurOffs; |
| 5543 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 5544 | // packing/alignment of members is different that normal, in which case |
| 5545 | // the encoding will be out-of-sync with the real layout. |
| 5546 | // If the runtime switches to just consider the size of types without |
| 5547 | // taking into account alignment, we could make padding explicit in the |
| 5548 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 5549 | // longer then though. |
| 5550 | CurOffs += padding; |
| 5551 | } |
| 5552 | |
| 5553 | NamedDecl *dcl = CurLayObj->second; |
| 5554 | if (dcl == 0) |
| 5555 | break; // reached end of structure. |
| 5556 | |
| 5557 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 5558 | // We expand the bases without their virtual bases since those are going |
| 5559 | // in the initial structure. Note that this differs from gcc which |
| 5560 | // expands virtual bases each time one is encountered in the hierarchy, |
| 5561 | // making the encoding type bigger than it really is. |
| 5562 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5563 | assert(!base->isEmpty()); |
| 5564 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5565 | } else { |
| 5566 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 5567 | if (FD) { |
| 5568 | S += '"'; |
| 5569 | S += field->getNameAsString(); |
| 5570 | S += '"'; |
| 5571 | } |
| 5572 | |
| 5573 | if (field->isBitField()) { |
| 5574 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5575 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5576 | } else { |
| 5577 | QualType qt = field->getType(); |
| 5578 | getLegacyIntegralTypeEncoding(qt); |
| 5579 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 5580 | /*OutermostType*/false, |
| 5581 | /*EncodingProperty*/false, |
| 5582 | /*StructField*/true); |
| 5583 | CurOffs += getTypeSize(field->getType()); |
| 5584 | } |
| 5585 | } |
| 5586 | } |
| 5587 | } |
| 5588 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5589 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 5590 | std::string& S) const { |
| 5591 | if (QT & Decl::OBJC_TQ_In) |
| 5592 | S += 'n'; |
| 5593 | if (QT & Decl::OBJC_TQ_Inout) |
| 5594 | S += 'N'; |
| 5595 | if (QT & Decl::OBJC_TQ_Out) |
| 5596 | S += 'o'; |
| 5597 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 5598 | S += 'O'; |
| 5599 | if (QT & Decl::OBJC_TQ_Byref) |
| 5600 | S += 'R'; |
| 5601 | if (QT & Decl::OBJC_TQ_Oneway) |
| 5602 | S += 'V'; |
| 5603 | } |
| 5604 | |
Douglas Gregor | 4dfd02a | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 5605 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 5606 | if (!ObjCIdDecl) { |
| 5607 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 5608 | T = getObjCObjectPointerType(T); |
| 5609 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 5610 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5611 | getTranslationUnitDecl(), |
| 5612 | SourceLocation(), SourceLocation(), |
| 5613 | &Idents.get("id"), IdInfo); |
| 5614 | } |
| 5615 | |
| 5616 | return ObjCIdDecl; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 5617 | } |
| 5618 | |
Douglas Gregor | 7a27ea5 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 5619 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 5620 | if (!ObjCSelDecl) { |
| 5621 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 5622 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 5623 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5624 | getTranslationUnitDecl(), |
| 5625 | SourceLocation(), SourceLocation(), |
| 5626 | &Idents.get("SEL"), SelInfo); |
| 5627 | } |
| 5628 | return ObjCSelDecl; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 5629 | } |
| 5630 | |
Douglas Gregor | 79d6726 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 5631 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 5632 | if (!ObjCClassDecl) { |
| 5633 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 5634 | T = getObjCObjectPointerType(T); |
| 5635 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 5636 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5637 | getTranslationUnitDecl(), |
| 5638 | SourceLocation(), SourceLocation(), |
| 5639 | &Idents.get("Class"), ClassInfo); |
| 5640 | } |
| 5641 | |
| 5642 | return ObjCClassDecl; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 5645 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 5646 | if (!ObjCProtocolClassDecl) { |
| 5647 | ObjCProtocolClassDecl |
| 5648 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 5649 | SourceLocation(), |
| 5650 | &Idents.get("Protocol"), |
| 5651 | /*PrevDecl=*/0, |
| 5652 | SourceLocation(), true); |
| 5653 | } |
| 5654 | |
| 5655 | return ObjCProtocolClassDecl; |
| 5656 | } |
| 5657 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5658 | //===----------------------------------------------------------------------===// |
| 5659 | // __builtin_va_list Construction Functions |
| 5660 | //===----------------------------------------------------------------------===// |
| 5661 | |
| 5662 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5663 | // typedef char* __builtin_va_list; |
| 5664 | QualType CharPtrType = Context->getPointerType(Context->CharTy); |
| 5665 | TypeSourceInfo *TInfo |
| 5666 | = Context->getTrivialTypeSourceInfo(CharPtrType); |
| 5667 | |
| 5668 | TypedefDecl *VaListTypeDecl |
| 5669 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5670 | Context->getTranslationUnitDecl(), |
| 5671 | SourceLocation(), SourceLocation(), |
| 5672 | &Context->Idents.get("__builtin_va_list"), |
| 5673 | TInfo); |
| 5674 | return VaListTypeDecl; |
| 5675 | } |
| 5676 | |
| 5677 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5678 | // typedef void* __builtin_va_list; |
| 5679 | QualType VoidPtrType = Context->getPointerType(Context->VoidTy); |
| 5680 | TypeSourceInfo *TInfo |
| 5681 | = Context->getTrivialTypeSourceInfo(VoidPtrType); |
| 5682 | |
| 5683 | TypedefDecl *VaListTypeDecl |
| 5684 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5685 | Context->getTranslationUnitDecl(), |
| 5686 | SourceLocation(), SourceLocation(), |
| 5687 | &Context->Idents.get("__builtin_va_list"), |
| 5688 | TInfo); |
| 5689 | return VaListTypeDecl; |
| 5690 | } |
| 5691 | |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5692 | static TypedefDecl * |
| 5693 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5694 | RecordDecl *VaListTagDecl; |
| 5695 | if (Context->getLangOpts().CPlusPlus) { |
| 5696 | // namespace std { struct __va_list { |
| 5697 | NamespaceDecl *NS; |
| 5698 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 5699 | Context->getTranslationUnitDecl(), |
| 5700 | /*Inline*/false, SourceLocation(), |
| 5701 | SourceLocation(), &Context->Idents.get("std"), |
| 5702 | /*PrevDecl*/0); |
| 5703 | |
| 5704 | VaListTagDecl = CXXRecordDecl::Create(*Context, TTK_Struct, |
| 5705 | Context->getTranslationUnitDecl(), |
| 5706 | SourceLocation(), SourceLocation(), |
| 5707 | &Context->Idents.get("__va_list")); |
| 5708 | VaListTagDecl->setDeclContext(NS); |
| 5709 | } else { |
| 5710 | // struct __va_list |
| 5711 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5712 | Context->getTranslationUnitDecl(), |
| 5713 | &Context->Idents.get("__va_list")); |
| 5714 | } |
| 5715 | |
| 5716 | VaListTagDecl->startDefinition(); |
| 5717 | |
| 5718 | const size_t NumFields = 5; |
| 5719 | QualType FieldTypes[NumFields]; |
| 5720 | const char *FieldNames[NumFields]; |
| 5721 | |
| 5722 | // void *__stack; |
| 5723 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
| 5724 | FieldNames[0] = "__stack"; |
| 5725 | |
| 5726 | // void *__gr_top; |
| 5727 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
| 5728 | FieldNames[1] = "__gr_top"; |
| 5729 | |
| 5730 | // void *__vr_top; |
| 5731 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5732 | FieldNames[2] = "__vr_top"; |
| 5733 | |
| 5734 | // int __gr_offs; |
| 5735 | FieldTypes[3] = Context->IntTy; |
| 5736 | FieldNames[3] = "__gr_offs"; |
| 5737 | |
| 5738 | // int __vr_offs; |
| 5739 | FieldTypes[4] = Context->IntTy; |
| 5740 | FieldNames[4] = "__vr_offs"; |
| 5741 | |
| 5742 | // Create fields |
| 5743 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5744 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5745 | VaListTagDecl, |
| 5746 | SourceLocation(), |
| 5747 | SourceLocation(), |
| 5748 | &Context->Idents.get(FieldNames[i]), |
| 5749 | FieldTypes[i], /*TInfo=*/0, |
| 5750 | /*BitWidth=*/0, |
| 5751 | /*Mutable=*/false, |
| 5752 | ICIS_NoInit); |
| 5753 | Field->setAccess(AS_public); |
| 5754 | VaListTagDecl->addDecl(Field); |
| 5755 | } |
| 5756 | VaListTagDecl->completeDefinition(); |
| 5757 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 5758 | Context->VaListTagTy = VaListTagType; |
| 5759 | |
| 5760 | // } __builtin_va_list; |
| 5761 | TypedefDecl *VaListTypedefDecl |
| 5762 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5763 | Context->getTranslationUnitDecl(), |
| 5764 | SourceLocation(), SourceLocation(), |
| 5765 | &Context->Idents.get("__builtin_va_list"), |
| 5766 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5767 | |
| 5768 | return VaListTypedefDecl; |
| 5769 | } |
| 5770 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5771 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5772 | // typedef struct __va_list_tag { |
| 5773 | RecordDecl *VaListTagDecl; |
| 5774 | |
| 5775 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5776 | Context->getTranslationUnitDecl(), |
| 5777 | &Context->Idents.get("__va_list_tag")); |
| 5778 | VaListTagDecl->startDefinition(); |
| 5779 | |
| 5780 | const size_t NumFields = 5; |
| 5781 | QualType FieldTypes[NumFields]; |
| 5782 | const char *FieldNames[NumFields]; |
| 5783 | |
| 5784 | // unsigned char gpr; |
| 5785 | FieldTypes[0] = Context->UnsignedCharTy; |
| 5786 | FieldNames[0] = "gpr"; |
| 5787 | |
| 5788 | // unsigned char fpr; |
| 5789 | FieldTypes[1] = Context->UnsignedCharTy; |
| 5790 | FieldNames[1] = "fpr"; |
| 5791 | |
| 5792 | // unsigned short reserved; |
| 5793 | FieldTypes[2] = Context->UnsignedShortTy; |
| 5794 | FieldNames[2] = "reserved"; |
| 5795 | |
| 5796 | // void* overflow_arg_area; |
| 5797 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5798 | FieldNames[3] = "overflow_arg_area"; |
| 5799 | |
| 5800 | // void* reg_save_area; |
| 5801 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 5802 | FieldNames[4] = "reg_save_area"; |
| 5803 | |
| 5804 | // Create fields |
| 5805 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5806 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 5807 | SourceLocation(), |
| 5808 | SourceLocation(), |
| 5809 | &Context->Idents.get(FieldNames[i]), |
| 5810 | FieldTypes[i], /*TInfo=*/0, |
| 5811 | /*BitWidth=*/0, |
| 5812 | /*Mutable=*/false, |
| 5813 | ICIS_NoInit); |
| 5814 | Field->setAccess(AS_public); |
| 5815 | VaListTagDecl->addDecl(Field); |
| 5816 | } |
| 5817 | VaListTagDecl->completeDefinition(); |
| 5818 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5819 | Context->VaListTagTy = VaListTagType; |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5820 | |
| 5821 | // } __va_list_tag; |
| 5822 | TypedefDecl *VaListTagTypedefDecl |
| 5823 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5824 | Context->getTranslationUnitDecl(), |
| 5825 | SourceLocation(), SourceLocation(), |
| 5826 | &Context->Idents.get("__va_list_tag"), |
| 5827 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5828 | QualType VaListTagTypedefType = |
| 5829 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5830 | |
| 5831 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5832 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5833 | QualType VaListTagArrayType |
| 5834 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5835 | Size, ArrayType::Normal, 0); |
| 5836 | TypeSourceInfo *TInfo |
| 5837 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5838 | TypedefDecl *VaListTypedefDecl |
| 5839 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5840 | Context->getTranslationUnitDecl(), |
| 5841 | SourceLocation(), SourceLocation(), |
| 5842 | &Context->Idents.get("__builtin_va_list"), |
| 5843 | TInfo); |
| 5844 | |
| 5845 | return VaListTypedefDecl; |
| 5846 | } |
| 5847 | |
| 5848 | static TypedefDecl * |
| 5849 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5850 | // typedef struct __va_list_tag { |
| 5851 | RecordDecl *VaListTagDecl; |
| 5852 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5853 | Context->getTranslationUnitDecl(), |
| 5854 | &Context->Idents.get("__va_list_tag")); |
| 5855 | VaListTagDecl->startDefinition(); |
| 5856 | |
| 5857 | const size_t NumFields = 4; |
| 5858 | QualType FieldTypes[NumFields]; |
| 5859 | const char *FieldNames[NumFields]; |
| 5860 | |
| 5861 | // unsigned gp_offset; |
| 5862 | FieldTypes[0] = Context->UnsignedIntTy; |
| 5863 | FieldNames[0] = "gp_offset"; |
| 5864 | |
| 5865 | // unsigned fp_offset; |
| 5866 | FieldTypes[1] = Context->UnsignedIntTy; |
| 5867 | FieldNames[1] = "fp_offset"; |
| 5868 | |
| 5869 | // void* overflow_arg_area; |
| 5870 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5871 | FieldNames[2] = "overflow_arg_area"; |
| 5872 | |
| 5873 | // void* reg_save_area; |
| 5874 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5875 | FieldNames[3] = "reg_save_area"; |
| 5876 | |
| 5877 | // Create fields |
| 5878 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5879 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5880 | VaListTagDecl, |
| 5881 | SourceLocation(), |
| 5882 | SourceLocation(), |
| 5883 | &Context->Idents.get(FieldNames[i]), |
| 5884 | FieldTypes[i], /*TInfo=*/0, |
| 5885 | /*BitWidth=*/0, |
| 5886 | /*Mutable=*/false, |
| 5887 | ICIS_NoInit); |
| 5888 | Field->setAccess(AS_public); |
| 5889 | VaListTagDecl->addDecl(Field); |
| 5890 | } |
| 5891 | VaListTagDecl->completeDefinition(); |
| 5892 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5893 | Context->VaListTagTy = VaListTagType; |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5894 | |
| 5895 | // } __va_list_tag; |
| 5896 | TypedefDecl *VaListTagTypedefDecl |
| 5897 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5898 | Context->getTranslationUnitDecl(), |
| 5899 | SourceLocation(), SourceLocation(), |
| 5900 | &Context->Idents.get("__va_list_tag"), |
| 5901 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5902 | QualType VaListTagTypedefType = |
| 5903 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5904 | |
| 5905 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5906 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5907 | QualType VaListTagArrayType |
| 5908 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5909 | Size, ArrayType::Normal,0); |
| 5910 | TypeSourceInfo *TInfo |
| 5911 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5912 | TypedefDecl *VaListTypedefDecl |
| 5913 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5914 | Context->getTranslationUnitDecl(), |
| 5915 | SourceLocation(), SourceLocation(), |
| 5916 | &Context->Idents.get("__builtin_va_list"), |
| 5917 | TInfo); |
| 5918 | |
| 5919 | return VaListTypedefDecl; |
| 5920 | } |
| 5921 | |
| 5922 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5923 | // typedef int __builtin_va_list[4]; |
| 5924 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
| 5925 | QualType IntArrayType |
| 5926 | = Context->getConstantArrayType(Context->IntTy, |
| 5927 | Size, ArrayType::Normal, 0); |
| 5928 | TypedefDecl *VaListTypedefDecl |
| 5929 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5930 | Context->getTranslationUnitDecl(), |
| 5931 | SourceLocation(), SourceLocation(), |
| 5932 | &Context->Idents.get("__builtin_va_list"), |
| 5933 | Context->getTrivialTypeSourceInfo(IntArrayType)); |
| 5934 | |
| 5935 | return VaListTypedefDecl; |
| 5936 | } |
| 5937 | |
Logan Chien | eae5a820 | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 5938 | static TypedefDecl * |
| 5939 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5940 | RecordDecl *VaListDecl; |
| 5941 | if (Context->getLangOpts().CPlusPlus) { |
| 5942 | // namespace std { struct __va_list { |
| 5943 | NamespaceDecl *NS; |
| 5944 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 5945 | Context->getTranslationUnitDecl(), |
| 5946 | /*Inline*/false, SourceLocation(), |
| 5947 | SourceLocation(), &Context->Idents.get("std"), |
| 5948 | /*PrevDecl*/0); |
| 5949 | |
| 5950 | VaListDecl = CXXRecordDecl::Create(*Context, TTK_Struct, |
| 5951 | Context->getTranslationUnitDecl(), |
| 5952 | SourceLocation(), SourceLocation(), |
| 5953 | &Context->Idents.get("__va_list")); |
| 5954 | |
| 5955 | VaListDecl->setDeclContext(NS); |
| 5956 | |
| 5957 | } else { |
| 5958 | // struct __va_list { |
| 5959 | VaListDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5960 | Context->getTranslationUnitDecl(), |
| 5961 | &Context->Idents.get("__va_list")); |
| 5962 | } |
| 5963 | |
| 5964 | VaListDecl->startDefinition(); |
| 5965 | |
| 5966 | // void * __ap; |
| 5967 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5968 | VaListDecl, |
| 5969 | SourceLocation(), |
| 5970 | SourceLocation(), |
| 5971 | &Context->Idents.get("__ap"), |
| 5972 | Context->getPointerType(Context->VoidTy), |
| 5973 | /*TInfo=*/0, |
| 5974 | /*BitWidth=*/0, |
| 5975 | /*Mutable=*/false, |
| 5976 | ICIS_NoInit); |
| 5977 | Field->setAccess(AS_public); |
| 5978 | VaListDecl->addDecl(Field); |
| 5979 | |
| 5980 | // }; |
| 5981 | VaListDecl->completeDefinition(); |
| 5982 | |
| 5983 | // typedef struct __va_list __builtin_va_list; |
| 5984 | TypeSourceInfo *TInfo |
| 5985 | = Context->getTrivialTypeSourceInfo(Context->getRecordType(VaListDecl)); |
| 5986 | |
| 5987 | TypedefDecl *VaListTypeDecl |
| 5988 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5989 | Context->getTranslationUnitDecl(), |
| 5990 | SourceLocation(), SourceLocation(), |
| 5991 | &Context->Idents.get("__builtin_va_list"), |
| 5992 | TInfo); |
| 5993 | |
| 5994 | return VaListTypeDecl; |
| 5995 | } |
| 5996 | |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5997 | static TypedefDecl * |
| 5998 | CreateSystemZBuiltinVaListDecl(const ASTContext *Context) { |
| 5999 | // typedef struct __va_list_tag { |
| 6000 | RecordDecl *VaListTagDecl; |
| 6001 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 6002 | Context->getTranslationUnitDecl(), |
| 6003 | &Context->Idents.get("__va_list_tag")); |
| 6004 | VaListTagDecl->startDefinition(); |
| 6005 | |
| 6006 | const size_t NumFields = 4; |
| 6007 | QualType FieldTypes[NumFields]; |
| 6008 | const char *FieldNames[NumFields]; |
| 6009 | |
| 6010 | // long __gpr; |
| 6011 | FieldTypes[0] = Context->LongTy; |
| 6012 | FieldNames[0] = "__gpr"; |
| 6013 | |
| 6014 | // long __fpr; |
| 6015 | FieldTypes[1] = Context->LongTy; |
| 6016 | FieldNames[1] = "__fpr"; |
| 6017 | |
| 6018 | // void *__overflow_arg_area; |
| 6019 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 6020 | FieldNames[2] = "__overflow_arg_area"; |
| 6021 | |
| 6022 | // void *__reg_save_area; |
| 6023 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 6024 | FieldNames[3] = "__reg_save_area"; |
| 6025 | |
| 6026 | // Create fields |
| 6027 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6028 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6029 | VaListTagDecl, |
| 6030 | SourceLocation(), |
| 6031 | SourceLocation(), |
| 6032 | &Context->Idents.get(FieldNames[i]), |
| 6033 | FieldTypes[i], /*TInfo=*/0, |
| 6034 | /*BitWidth=*/0, |
| 6035 | /*Mutable=*/false, |
| 6036 | ICIS_NoInit); |
| 6037 | Field->setAccess(AS_public); |
| 6038 | VaListTagDecl->addDecl(Field); |
| 6039 | } |
| 6040 | VaListTagDecl->completeDefinition(); |
| 6041 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 6042 | Context->VaListTagTy = VaListTagType; |
| 6043 | |
| 6044 | // } __va_list_tag; |
| 6045 | TypedefDecl *VaListTagTypedefDecl |
| 6046 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 6047 | Context->getTranslationUnitDecl(), |
| 6048 | SourceLocation(), SourceLocation(), |
| 6049 | &Context->Idents.get("__va_list_tag"), |
| 6050 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 6051 | QualType VaListTagTypedefType = |
| 6052 | Context->getTypedefType(VaListTagTypedefDecl); |
| 6053 | |
| 6054 | // typedef __va_list_tag __builtin_va_list[1]; |
| 6055 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 6056 | QualType VaListTagArrayType |
| 6057 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 6058 | Size, ArrayType::Normal,0); |
| 6059 | TypeSourceInfo *TInfo |
| 6060 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 6061 | TypedefDecl *VaListTypedefDecl |
| 6062 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 6063 | Context->getTranslationUnitDecl(), |
| 6064 | SourceLocation(), SourceLocation(), |
| 6065 | &Context->Idents.get("__builtin_va_list"), |
| 6066 | TInfo); |
| 6067 | |
| 6068 | return VaListTypedefDecl; |
| 6069 | } |
| 6070 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6071 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 6072 | TargetInfo::BuiltinVaListKind Kind) { |
| 6073 | switch (Kind) { |
| 6074 | case TargetInfo::CharPtrBuiltinVaList: |
| 6075 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 6076 | case TargetInfo::VoidPtrBuiltinVaList: |
| 6077 | return CreateVoidPtrBuiltinVaListDecl(Context); |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6078 | case TargetInfo::AArch64ABIBuiltinVaList: |
| 6079 | return CreateAArch64ABIBuiltinVaListDecl(Context); |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6080 | case TargetInfo::PowerABIBuiltinVaList: |
| 6081 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 6082 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 6083 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 6084 | case TargetInfo::PNaClABIBuiltinVaList: |
| 6085 | return CreatePNaClABIBuiltinVaListDecl(Context); |
Logan Chien | eae5a820 | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6086 | case TargetInfo::AAPCSABIBuiltinVaList: |
| 6087 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6088 | case TargetInfo::SystemZBuiltinVaList: |
| 6089 | return CreateSystemZBuiltinVaListDecl(Context); |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6090 | } |
| 6091 | |
| 6092 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 6093 | } |
| 6094 | |
| 6095 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
| 6096 | if (!BuiltinVaListDecl) |
| 6097 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
| 6098 | |
| 6099 | return BuiltinVaListDecl; |
| 6100 | } |
| 6101 | |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 6102 | QualType ASTContext::getVaListTagType() const { |
| 6103 | // Force the creation of VaListTagTy by building the __builtin_va_list |
| 6104 | // declaration. |
| 6105 | if (VaListTagTy.isNull()) |
| 6106 | (void) getBuiltinVaListDecl(); |
| 6107 | |
| 6108 | return VaListTagTy; |
| 6109 | } |
| 6110 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6111 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6112 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 6113 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6114 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6115 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 6116 | } |
| 6117 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6118 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 6119 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6120 | TemplateName |
| 6121 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 6122 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6123 | unsigned size = End - Begin; |
| 6124 | assert(size > 1 && "set is not overloaded!"); |
| 6125 | |
| 6126 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 6127 | size * sizeof(FunctionTemplateDecl*)); |
| 6128 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 6129 | |
| 6130 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6131 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6132 | NamedDecl *D = *I; |
| 6133 | assert(isa<FunctionTemplateDecl>(D) || |
| 6134 | (isa<UsingShadowDecl>(D) && |
| 6135 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 6136 | *Storage++ = D; |
| 6137 | } |
| 6138 | |
| 6139 | return TemplateName(OT); |
| 6140 | } |
| 6141 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6142 | /// \brief Retrieve the template name that represents a qualified |
| 6143 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6144 | TemplateName |
| 6145 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 6146 | bool TemplateKeyword, |
| 6147 | TemplateDecl *Template) const { |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 6148 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 6149 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6150 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6151 | llvm::FoldingSetNodeID ID; |
| 6152 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 6153 | |
| 6154 | void *InsertPos = 0; |
| 6155 | QualifiedTemplateName *QTN = |
| 6156 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6157 | if (!QTN) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6158 | QTN = new (*this, llvm::alignOf<QualifiedTemplateName>()) |
| 6159 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6160 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 6161 | } |
| 6162 | |
| 6163 | return TemplateName(QTN); |
| 6164 | } |
| 6165 | |
| 6166 | /// \brief Retrieve the template name that represents a dependent |
| 6167 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6168 | TemplateName |
| 6169 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 6170 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6171 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 6172 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6173 | |
| 6174 | llvm::FoldingSetNodeID ID; |
| 6175 | DependentTemplateName::Profile(ID, NNS, Name); |
| 6176 | |
| 6177 | void *InsertPos = 0; |
| 6178 | DependentTemplateName *QTN = |
| 6179 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6180 | |
| 6181 | if (QTN) |
| 6182 | return TemplateName(QTN); |
| 6183 | |
| 6184 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 6185 | if (CanonNNS == NNS) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6186 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6187 | DependentTemplateName(NNS, Name); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6188 | } else { |
| 6189 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6190 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6191 | DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6192 | DependentTemplateName *CheckQTN = |
| 6193 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6194 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 6195 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6196 | } |
| 6197 | |
| 6198 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 6199 | return TemplateName(QTN); |
| 6200 | } |
| 6201 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6202 | /// \brief Retrieve the template name that represents a dependent |
| 6203 | /// template name such as \c MetaFun::template operator+. |
| 6204 | TemplateName |
| 6205 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6206 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6207 | assert((!NNS || NNS->isDependent()) && |
| 6208 | "Nested name specifier must be dependent"); |
| 6209 | |
| 6210 | llvm::FoldingSetNodeID ID; |
| 6211 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 6212 | |
| 6213 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6214 | DependentTemplateName *QTN |
| 6215 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6216 | |
| 6217 | if (QTN) |
| 6218 | return TemplateName(QTN); |
| 6219 | |
| 6220 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 6221 | if (CanonNNS == NNS) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6222 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6223 | DependentTemplateName(NNS, Operator); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6224 | } else { |
| 6225 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6226 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6227 | DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6228 | |
| 6229 | DependentTemplateName *CheckQTN |
| 6230 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6231 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 6232 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6233 | } |
| 6234 | |
| 6235 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 6236 | return TemplateName(QTN); |
| 6237 | } |
| 6238 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6239 | TemplateName |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6240 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 6241 | TemplateName replacement) const { |
| 6242 | llvm::FoldingSetNodeID ID; |
| 6243 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 6244 | |
| 6245 | void *insertPos = 0; |
| 6246 | SubstTemplateTemplateParmStorage *subst |
| 6247 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 6248 | |
| 6249 | if (!subst) { |
| 6250 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 6251 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 6252 | } |
| 6253 | |
| 6254 | return TemplateName(subst); |
| 6255 | } |
| 6256 | |
| 6257 | TemplateName |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6258 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 6259 | const TemplateArgument &ArgPack) const { |
| 6260 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 6261 | llvm::FoldingSetNodeID ID; |
| 6262 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 6263 | |
| 6264 | void *InsertPos = 0; |
| 6265 | SubstTemplateTemplateParmPackStorage *Subst |
| 6266 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 6267 | |
| 6268 | if (!Subst) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6269 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6270 | ArgPack.pack_size(), |
| 6271 | ArgPack.pack_begin()); |
| 6272 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 6273 | } |
| 6274 | |
| 6275 | return TemplateName(Subst); |
| 6276 | } |
| 6277 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6278 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 6279 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 6280 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6281 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6282 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6283 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6284 | case TargetInfo::SignedShort: return ShortTy; |
| 6285 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 6286 | case TargetInfo::SignedInt: return IntTy; |
| 6287 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 6288 | case TargetInfo::SignedLong: return LongTy; |
| 6289 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 6290 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 6291 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 6292 | } |
| 6293 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6294 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6295 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 6296 | |
| 6297 | //===----------------------------------------------------------------------===// |
| 6298 | // Type Predicates. |
| 6299 | //===----------------------------------------------------------------------===// |
| 6300 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6301 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 6302 | /// garbage collection attribute. |
| 6303 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6304 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6305 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6306 | return Qualifiers::GCNone; |
| 6307 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6308 | assert(getLangOpts().ObjC1); |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6309 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 6310 | |
| 6311 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 6312 | // (or pointers to them) be treated as though they were declared |
| 6313 | // as __strong. |
| 6314 | if (GCAttrs == Qualifiers::GCNone) { |
| 6315 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 6316 | return Qualifiers::Strong; |
| 6317 | else if (Ty->isPointerType()) |
| 6318 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 6319 | } else { |
| 6320 | // It's not valid to set GC attributes on anything that isn't a |
| 6321 | // pointer. |
| 6322 | #ifndef NDEBUG |
| 6323 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 6324 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 6325 | CT = AT->getElementType(); |
| 6326 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 6327 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6328 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 6329 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6330 | } |
| 6331 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6332 | //===----------------------------------------------------------------------===// |
| 6333 | // Type Compatibility Testing |
| 6334 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 6335 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6336 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6337 | /// compatible. |
| 6338 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 6339 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 6340 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6341 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6342 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6343 | } |
| 6344 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6345 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 6346 | QualType SecondVec) { |
| 6347 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 6348 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 6349 | |
| 6350 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 6351 | return true; |
| 6352 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6353 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 6354 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6355 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 6356 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6357 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6358 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6359 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 6360 | First->getVectorKind() != VectorType::AltiVecBool && |
| 6361 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 6362 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6363 | return true; |
| 6364 | |
| 6365 | return false; |
| 6366 | } |
| 6367 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6368 | //===----------------------------------------------------------------------===// |
| 6369 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 6370 | //===----------------------------------------------------------------------===// |
| 6371 | |
| 6372 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 6373 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6374 | bool |
| 6375 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 6376 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 6377 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6378 | return true; |
| 6379 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 6380 | E = rProto->protocol_end(); PI != E; ++PI) |
| 6381 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 6382 | return true; |
| 6383 | return false; |
| 6384 | } |
| 6385 | |
Dmitri Gribenko | 4c3b8a3 | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 6386 | /// QualifiedIdConformsQualifiedId - compare id<pr,...> with id<pr1,...> |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6387 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 6388 | /// otherwise. |
| 6389 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 6390 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 6391 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 6392 | return false; |
| 6393 | } |
| 6394 | |
Dmitri Gribenko | 4c3b8a3 | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 6395 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
| 6396 | /// Class<pr1, ...>. |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6397 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 6398 | QualType rhs) { |
| 6399 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 6400 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 6401 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 6402 | |
| 6403 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6404 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6405 | bool match = false; |
| 6406 | ObjCProtocolDecl *lhsProto = *I; |
| 6407 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 6408 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 6409 | ObjCProtocolDecl *rhsProto = *J; |
| 6410 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 6411 | match = true; |
| 6412 | break; |
| 6413 | } |
| 6414 | } |
| 6415 | if (!match) |
| 6416 | return false; |
| 6417 | } |
| 6418 | return true; |
| 6419 | } |
| 6420 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6421 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 6422 | /// ObjCQualifiedIDType. |
| 6423 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 6424 | bool compare) { |
| 6425 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6426 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6427 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 6428 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6429 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6430 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 6431 | return true; |
| 6432 | |
| 6433 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6434 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6435 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6436 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6437 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6438 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6439 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6440 | // make sure we check the class hierarchy. |
| 6441 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 6442 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6443 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6444 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6445 | // see if static class implements all of id's protocols, directly or |
| 6446 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 6447 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6448 | return false; |
| 6449 | } |
| 6450 | } |
| 6451 | // If there are no qualifiers and no interface, we have an 'id'. |
| 6452 | return true; |
| 6453 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6454 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6455 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6456 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6457 | ObjCProtocolDecl *lhsProto = *I; |
| 6458 | bool match = false; |
| 6459 | |
| 6460 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6461 | // see if static class implements all of id's protocols, directly or |
| 6462 | // through its super class and categories. |
| 6463 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 6464 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 6465 | ObjCProtocolDecl *rhsProto = *J; |
| 6466 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6467 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6468 | match = true; |
| 6469 | break; |
| 6470 | } |
| 6471 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6472 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6473 | // make sure we check the class hierarchy. |
| 6474 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 6475 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6476 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6477 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6478 | // see if static class implements all of id's protocols, directly or |
| 6479 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 6480 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6481 | match = true; |
| 6482 | break; |
| 6483 | } |
| 6484 | } |
| 6485 | } |
| 6486 | if (!match) |
| 6487 | return false; |
| 6488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6489 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6490 | return true; |
| 6491 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6492 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6493 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 6494 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 6495 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6496 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6497 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6498 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6499 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 6500 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 6501 | ObjCProtocolDecl *lhsProto = *I; |
| 6502 | bool match = false; |
| 6503 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6504 | // 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] | 6505 | // see if static class implements all of id's protocols, directly or |
| 6506 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6507 | // First, lhs protocols in the qualifier list must be found, direct |
| 6508 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6509 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 6510 | E = rhsQID->qual_end(); J != E; ++J) { |
| 6511 | ObjCProtocolDecl *rhsProto = *J; |
| 6512 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6513 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6514 | match = true; |
| 6515 | break; |
| 6516 | } |
| 6517 | } |
| 6518 | if (!match) |
| 6519 | return false; |
| 6520 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6521 | |
| 6522 | // Static class's protocols, or its super class or category protocols |
| 6523 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 6524 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 6525 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 6526 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 6527 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 6528 | // no type qualifier and its class has no static protocol(s) |
| 6529 | // assume that it is mismatch. |
| 6530 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 6531 | return false; |
| 6532 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6533 | LHSInheritedProtocols.begin(), |
| 6534 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 6535 | bool match = false; |
| 6536 | ObjCProtocolDecl *lhsProto = (*I); |
| 6537 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 6538 | E = rhsQID->qual_end(); J != E; ++J) { |
| 6539 | ObjCProtocolDecl *rhsProto = *J; |
| 6540 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6541 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6542 | match = true; |
| 6543 | break; |
| 6544 | } |
| 6545 | } |
| 6546 | if (!match) |
| 6547 | return false; |
| 6548 | } |
| 6549 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6550 | return true; |
| 6551 | } |
| 6552 | return false; |
| 6553 | } |
| 6554 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6555 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6556 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 6557 | /// protocol qualifiers on the LHS or RHS. |
| 6558 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6559 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 6560 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6561 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6562 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6563 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 6564 | // 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] | 6565 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 6566 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6567 | return true; |
| 6568 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6569 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6570 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6571 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6572 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6573 | |
| 6574 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 6575 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 6576 | QualType(RHSOPT,0)); |
| 6577 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6578 | // If we have 2 user-defined types, fall into that path. |
| 6579 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6580 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6581 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6582 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6583 | } |
| 6584 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6585 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 6586 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6587 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 6588 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 6589 | /// not OK. For the return type, the opposite is not OK. |
| 6590 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 6591 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6592 | const ObjCObjectPointerType *RHSOPT, |
| 6593 | bool BlockReturnType) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6594 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6595 | return true; |
| 6596 | |
| 6597 | if (LHSOPT->isObjCBuiltinType()) { |
| 6598 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 6599 | } |
| 6600 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6601 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6602 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6603 | QualType(RHSOPT,0), |
| 6604 | false); |
| 6605 | |
| 6606 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 6607 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 6608 | if (LHS && RHS) { // We have 2 user-defined types. |
| 6609 | if (LHS != RHS) { |
| 6610 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6611 | return BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6612 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6613 | return !BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6614 | } |
| 6615 | else |
| 6616 | return true; |
| 6617 | } |
| 6618 | return false; |
| 6619 | } |
| 6620 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6621 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 6622 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 6623 | /// It is used to build composite qualifier list of the composite type of |
| 6624 | /// the conditional expression involving two objective-c pointer objects. |
| 6625 | static |
| 6626 | void getIntersectionOfProtocols(ASTContext &Context, |
| 6627 | const ObjCObjectPointerType *LHSOPT, |
| 6628 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6629 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6630 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6631 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6632 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6633 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 6634 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6635 | |
| 6636 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 6637 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 6638 | if (LHSNumProtocols > 0) |
| 6639 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 6640 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6641 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6642 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 6643 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6644 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 6645 | LHSInheritedProtocols.end()); |
| 6646 | } |
| 6647 | |
| 6648 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 6649 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 6650 | ObjCProtocolDecl **RHSProtocols = |
| 6651 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6652 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 6653 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 6654 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 6655 | } else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6656 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6657 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 6658 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6659 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6660 | RHSInheritedProtocols.begin(), |
| 6661 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 6662 | if (InheritedProtocolSet.count((*I))) |
| 6663 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6664 | } |
| 6665 | } |
| 6666 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6667 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 6668 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 6669 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 6670 | /// warning is issued. So, its invokation is extremely rare. |
| 6671 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6672 | const ObjCObjectPointerType *Lptr, |
| 6673 | const ObjCObjectPointerType *Rptr) { |
| 6674 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 6675 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 6676 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 6677 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 6678 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6679 | return QualType(); |
| 6680 | |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 6681 | do { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6682 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6683 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6684 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6685 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 6686 | |
| 6687 | QualType Result = QualType(LHS, 0); |
| 6688 | if (!Protocols.empty()) |
| 6689 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 6690 | Result = getObjCObjectPointerType(Result); |
| 6691 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6692 | } |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 6693 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6694 | |
| 6695 | return QualType(); |
| 6696 | } |
| 6697 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6698 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 6699 | const ObjCObjectType *RHS) { |
| 6700 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 6701 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 6702 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6703 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 6704 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6705 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6706 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6707 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6708 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 6709 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 6710 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6711 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6712 | |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6713 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 6714 | // more detailed analysis is required. |
| 6715 | if (RHS->getNumProtocols() == 0) { |
| 6716 | // OK, if LHS is a superclass of RHS *and* |
| 6717 | // this superclass is assignment compatible with LHS. |
| 6718 | // false otherwise. |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 6719 | bool IsSuperClass = |
| 6720 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 6721 | if (IsSuperClass) { |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6722 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 6723 | // ; i.e., SuperClass may implement at least one of the protocols |
| 6724 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 6725 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 6726 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 6727 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6728 | // If super class has no protocols, it is not a match. |
| 6729 | if (SuperClassInheritedProtocols.empty()) |
| 6730 | return false; |
| 6731 | |
| 6732 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 6733 | LHSPE = LHS->qual_end(); |
| 6734 | LHSPI != LHSPE; LHSPI++) { |
| 6735 | bool SuperImplementsProtocol = false; |
| 6736 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 6737 | |
| 6738 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6739 | SuperClassInheritedProtocols.begin(), |
| 6740 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 6741 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 6742 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 6743 | SuperImplementsProtocol = true; |
| 6744 | break; |
| 6745 | } |
| 6746 | } |
| 6747 | if (!SuperImplementsProtocol) |
| 6748 | return false; |
| 6749 | } |
| 6750 | return true; |
| 6751 | } |
| 6752 | return false; |
| 6753 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6754 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6755 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 6756 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6757 | LHSPI != LHSPE; LHSPI++) { |
| 6758 | bool RHSImplementsProtocol = false; |
| 6759 | |
| 6760 | // If the RHS doesn't implement the protocol on the left, the types |
| 6761 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6762 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 6763 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 6764 | RHSPI != RHSPE; RHSPI++) { |
| 6765 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6766 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 6767 | break; |
| 6768 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6769 | } |
| 6770 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 6771 | if (!RHSImplementsProtocol) |
| 6772 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6773 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6774 | // The RHS implements all protocols listed on the LHS. |
| 6775 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6776 | } |
| 6777 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6778 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 6779 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6780 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 6781 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6782 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6783 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6784 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6785 | |
| 6786 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 6787 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6788 | } |
| 6789 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6790 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 6791 | return canAssignObjCInterfaces( |
| 6792 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 6793 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 6794 | } |
| 6795 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6796 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6797 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6798 | /// 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] | 6799 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6800 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 6801 | bool CompareUnqualified) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6802 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6803 | return hasSameType(LHS, RHS); |
| 6804 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6805 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6806 | } |
| 6807 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6808 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | 8237839 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 6809 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6810 | } |
| 6811 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6812 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 6813 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 6814 | } |
| 6815 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6816 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 6817 | /// of T is compatible with SubType, return the merged type, else return |
| 6818 | /// QualType() |
| 6819 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 6820 | bool OfBlockPointer, |
| 6821 | bool Unqualified) { |
| 6822 | if (const RecordType *UT = T->getAsUnionType()) { |
| 6823 | RecordDecl *UD = UT->getDecl(); |
| 6824 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 6825 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6826 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 6827 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6828 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 6829 | if (!MT.isNull()) |
| 6830 | return MT; |
| 6831 | } |
| 6832 | } |
| 6833 | } |
| 6834 | |
| 6835 | return QualType(); |
| 6836 | } |
| 6837 | |
| 6838 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 6839 | /// argument types |
| 6840 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 6841 | bool OfBlockPointer, |
| 6842 | bool Unqualified) { |
| 6843 | // GNU extension: two types are compatible if they appear as a function |
| 6844 | // argument, one of the types is a transparent union type and the other |
| 6845 | // type is compatible with a union member |
| 6846 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 6847 | Unqualified); |
| 6848 | if (!lmerge.isNull()) |
| 6849 | return lmerge; |
| 6850 | |
| 6851 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 6852 | Unqualified); |
| 6853 | if (!rmerge.isNull()) |
| 6854 | return rmerge; |
| 6855 | |
| 6856 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 6857 | } |
| 6858 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6859 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6860 | bool OfBlockPointer, |
| 6861 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6862 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 6863 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6864 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 6865 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6866 | bool allLTypes = true; |
| 6867 | bool allRTypes = true; |
| 6868 | |
| 6869 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6870 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6871 | if (OfBlockPointer) { |
| 6872 | QualType RHS = rbase->getResultType(); |
| 6873 | QualType LHS = lbase->getResultType(); |
| 6874 | bool UnqualifiedResult = Unqualified; |
| 6875 | if (!UnqualifiedResult) |
| 6876 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6877 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6878 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6879 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6880 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 6881 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6882 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6883 | |
| 6884 | if (Unqualified) |
| 6885 | retType = retType.getUnqualifiedType(); |
| 6886 | |
| 6887 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 6888 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 6889 | if (Unqualified) { |
| 6890 | LRetType = LRetType.getUnqualifiedType(); |
| 6891 | RRetType = RRetType.getUnqualifiedType(); |
| 6892 | } |
| 6893 | |
| 6894 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6895 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6896 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6897 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6898 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 6899 | // FIXME: double check this |
| 6900 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 6901 | // rbase->getRegParmAttr() != 0 && |
| 6902 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 6903 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 6904 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6905 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6906 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6907 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6908 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6909 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6910 | // Regparm is part of the calling convention. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 6911 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 6912 | return QualType(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6913 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 6914 | return QualType(); |
| 6915 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6916 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 6917 | return QualType(); |
| 6918 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6919 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 6920 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6921 | |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 6922 | if (lbaseInfo.getNoReturn() != NoReturn) |
| 6923 | allLTypes = false; |
| 6924 | if (rbaseInfo.getNoReturn() != NoReturn) |
| 6925 | allRTypes = false; |
| 6926 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6927 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6928 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6929 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6930 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 6931 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6932 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 6933 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 6934 | |
| 6935 | // Compatible functions must have the same number of arguments |
| 6936 | if (lproto_nargs != rproto_nargs) |
| 6937 | return QualType(); |
| 6938 | |
| 6939 | // Variadic and non-variadic functions aren't compatible |
| 6940 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 6941 | return QualType(); |
| 6942 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 6943 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 6944 | return QualType(); |
| 6945 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6946 | if (LangOpts.ObjCAutoRefCount && |
| 6947 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 6948 | return QualType(); |
| 6949 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6950 | // Check argument compatibility |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6951 | SmallVector<QualType, 10> types; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6952 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 6953 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 6954 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6955 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 6956 | OfBlockPointer, |
| 6957 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6958 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6959 | |
| 6960 | if (Unqualified) |
| 6961 | argtype = argtype.getUnqualifiedType(); |
| 6962 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6963 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6964 | if (Unqualified) { |
| 6965 | largtype = largtype.getUnqualifiedType(); |
| 6966 | rargtype = rargtype.getUnqualifiedType(); |
| 6967 | } |
| 6968 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6969 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 6970 | allLTypes = false; |
| 6971 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 6972 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6973 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6974 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6975 | if (allLTypes) return lhs; |
| 6976 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6977 | |
| 6978 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 6979 | EPI.ExtInfo = einfo; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 6980 | return getFunctionType(retType, types, EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6981 | } |
| 6982 | |
| 6983 | if (lproto) allRTypes = false; |
| 6984 | if (rproto) allLTypes = false; |
| 6985 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6986 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6987 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6988 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6989 | if (proto->isVariadic()) return QualType(); |
| 6990 | // Check that the types are compatible with the types that |
| 6991 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 6992 | // The only types actually affected are promotable integer |
| 6993 | // types and floats, which would be passed as a different |
| 6994 | // type depending on whether the prototype is visible. |
| 6995 | unsigned proto_nargs = proto->getNumArgs(); |
| 6996 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 6997 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 6998 | |
Eli Friedman | c586d5d | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 6999 | // 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] | 7000 | // to pass enum values. |
Eli Friedman | c586d5d | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 7001 | if (const EnumType *Enum = argTy->getAs<EnumType>()) { |
| 7002 | argTy = Enum->getDecl()->getIntegerType(); |
| 7003 | if (argTy.isNull()) |
| 7004 | return QualType(); |
| 7005 | } |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 7006 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7007 | if (argTy->isPromotableIntegerType() || |
| 7008 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 7009 | return QualType(); |
| 7010 | } |
| 7011 | |
| 7012 | if (allLTypes) return lhs; |
| 7013 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7014 | |
| 7015 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 7016 | EPI.ExtInfo = einfo; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 7017 | return getFunctionType(retType, |
| 7018 | ArrayRef<QualType>(proto->arg_type_begin(), |
| 7019 | proto->getNumArgs()), |
| 7020 | EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7021 | } |
| 7022 | |
| 7023 | if (allLTypes) return lhs; |
| 7024 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7025 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7026 | } |
| 7027 | |
John McCall | b9da713 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7028 | /// Given that we have an enum type and a non-enum type, try to merge them. |
| 7029 | static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, |
| 7030 | QualType other, bool isBlockReturnType) { |
| 7031 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 7032 | // a signed integer type, or an unsigned integer type. |
| 7033 | // Compatibility is based on the underlying type, not the promotion |
| 7034 | // type. |
| 7035 | QualType underlyingType = ET->getDecl()->getIntegerType(); |
| 7036 | if (underlyingType.isNull()) return QualType(); |
| 7037 | if (Context.hasSameType(underlyingType, other)) |
| 7038 | return other; |
| 7039 | |
| 7040 | // In block return types, we're more permissive and accept any |
| 7041 | // integral type of the same size. |
| 7042 | if (isBlockReturnType && other->isIntegerType() && |
| 7043 | Context.getTypeSize(underlyingType) == Context.getTypeSize(other)) |
| 7044 | return other; |
| 7045 | |
| 7046 | return QualType(); |
| 7047 | } |
| 7048 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7049 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7050 | bool OfBlockPointer, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7051 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 7052 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 7053 | // type is adjusted to "T" prior to any further analysis, the expression |
| 7054 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 7055 | // expression is an lvalue unless the reference is an rvalue reference and |
| 7056 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 7057 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 7058 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7059 | |
| 7060 | if (Unqualified) { |
| 7061 | LHS = LHS.getUnqualifiedType(); |
| 7062 | RHS = RHS.getUnqualifiedType(); |
| 7063 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 7064 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7065 | QualType LHSCan = getCanonicalType(LHS), |
| 7066 | RHSCan = getCanonicalType(RHS); |
| 7067 | |
| 7068 | // If two types are identical, they are compatible. |
| 7069 | if (LHSCan == RHSCan) |
| 7070 | return LHS; |
| 7071 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7072 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 7073 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 7074 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7075 | if (LQuals != RQuals) { |
| 7076 | // If any of these qualifiers are different, we have a type |
| 7077 | // mismatch. |
| 7078 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7079 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 7080 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7081 | return QualType(); |
| 7082 | |
| 7083 | // Exactly one GC qualifier difference is allowed: __strong is |
| 7084 | // okay if the other type has no GC qualifier but is an Objective |
| 7085 | // C object pointer (i.e. implicitly strong by default). We fix |
| 7086 | // this by pretending that the unqualified type was actually |
| 7087 | // qualified __strong. |
| 7088 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 7089 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 7090 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 7091 | |
| 7092 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 7093 | return QualType(); |
| 7094 | |
| 7095 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 7096 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 7097 | } |
| 7098 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 7099 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 7100 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7101 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7102 | } |
| 7103 | |
| 7104 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7105 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 7106 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 7107 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7108 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7109 | // We want to consider the two function types to be the same for these |
| 7110 | // comparisons, just force one to the other. |
| 7111 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 7112 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 7113 | |
| 7114 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 7115 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 7116 | LHSClass = Type::ConstantArray; |
| 7117 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 7118 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7119 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7120 | // ObjCInterfaces are just specialized ObjCObjects. |
| 7121 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 7122 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 7123 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 7124 | // Canonicalize ExtVector -> Vector. |
| 7125 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 7126 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7127 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 7128 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7129 | if (LHSClass != RHSClass) { |
John McCall | b9da713 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7130 | // Note that we only have special rules for turning block enum |
| 7131 | // returns into block int returns, not vice-versa. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7132 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
John McCall | b9da713 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7133 | return mergeEnumWithInteger(*this, ETy, RHS, false); |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 7134 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7135 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
John McCall | b9da713 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7136 | return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType); |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 7137 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 7138 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 4196363 | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 7139 | if (OfBlockPointer && !BlockReturnType) { |
| 7140 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 7141 | return LHS; |
| 7142 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 7143 | return RHS; |
| 7144 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 7145 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7146 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7147 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7148 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 7149 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7150 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7151 | #define TYPE(Class, Base) |
| 7152 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 7153 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7154 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 7155 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 7156 | #include "clang/AST/TypeNodes.def" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7157 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7158 | |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 7159 | case Type::Auto: |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 7160 | case Type::LValueReference: |
| 7161 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7162 | case Type::MemberPointer: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7163 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7164 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7165 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7166 | case Type::IncompleteArray: |
| 7167 | case Type::VariableArray: |
| 7168 | case Type::FunctionProto: |
| 7169 | case Type::ExtVector: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7170 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7171 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7172 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7173 | { |
| 7174 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7175 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 7176 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7177 | if (Unqualified) { |
| 7178 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 7179 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 7180 | } |
| 7181 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 7182 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7183 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 7184 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7185 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 7186 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7187 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7188 | return getPointerType(ResultType); |
| 7189 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 7190 | case Type::BlockPointer: |
| 7191 | { |
| 7192 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7193 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 7194 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7195 | if (Unqualified) { |
| 7196 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 7197 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 7198 | } |
| 7199 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 7200 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 7201 | if (ResultType.isNull()) return QualType(); |
| 7202 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 7203 | return LHS; |
| 7204 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 7205 | return RHS; |
| 7206 | return getBlockPointerType(ResultType); |
| 7207 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7208 | case Type::Atomic: |
| 7209 | { |
| 7210 | // Merge two pointer types, while trying to preserve typedef info |
| 7211 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 7212 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 7213 | if (Unqualified) { |
| 7214 | LHSValue = LHSValue.getUnqualifiedType(); |
| 7215 | RHSValue = RHSValue.getUnqualifiedType(); |
| 7216 | } |
| 7217 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 7218 | Unqualified); |
| 7219 | if (ResultType.isNull()) return QualType(); |
| 7220 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 7221 | return LHS; |
| 7222 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 7223 | return RHS; |
| 7224 | return getAtomicType(ResultType); |
| 7225 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7226 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7227 | { |
| 7228 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 7229 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 7230 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 7231 | return QualType(); |
| 7232 | |
| 7233 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 7234 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7235 | if (Unqualified) { |
| 7236 | LHSElem = LHSElem.getUnqualifiedType(); |
| 7237 | RHSElem = RHSElem.getUnqualifiedType(); |
| 7238 | } |
| 7239 | |
| 7240 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7241 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7242 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 7243 | return LHS; |
| 7244 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 7245 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 7246 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 7247 | ArrayType::ArraySizeModifier(), 0); |
| 7248 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 7249 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7250 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 7251 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7252 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 7253 | return LHS; |
| 7254 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 7255 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7256 | if (LVAT) { |
| 7257 | // FIXME: This isn't correct! But tricky to implement because |
| 7258 | // the array's size has to be the size of LHS, but the type |
| 7259 | // has to be different. |
| 7260 | return LHS; |
| 7261 | } |
| 7262 | if (RVAT) { |
| 7263 | // FIXME: This isn't correct! But tricky to implement because |
| 7264 | // the array's size has to be the size of RHS, but the type |
| 7265 | // has to be different. |
| 7266 | return RHS; |
| 7267 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 7268 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 7269 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 7270 | return getIncompleteArrayType(ResultType, |
| 7271 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7272 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7273 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7274 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7275 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7276 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7277 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7278 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 7279 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7280 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 7281 | case Type::Complex: |
| 7282 | // Distinct complex types are incompatible. |
| 7283 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 7284 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 7285 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 7286 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 7287 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7288 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7289 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7290 | case Type::ObjCObject: { |
| 7291 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 7292 | // FIXME: This should be type compatibility, e.g. whether |
| 7293 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7294 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 7295 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 7296 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 7297 | return LHS; |
| 7298 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7299 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 7300 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7301 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7302 | if (OfBlockPointer) { |
| 7303 | if (canAssignObjCInterfacesInBlockPointer( |
| 7304 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7305 | RHS->getAs<ObjCObjectPointerType>(), |
| 7306 | BlockReturnType)) |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7307 | return LHS; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7308 | return QualType(); |
| 7309 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7310 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 7311 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7312 | return LHS; |
| 7313 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 7314 | return QualType(); |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7315 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7316 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7317 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7318 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7319 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 7320 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7321 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 7322 | const FunctionProtoType *FromFunctionType, |
| 7323 | const FunctionProtoType *ToFunctionType) { |
| 7324 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 7325 | ToFunctionType->hasAnyConsumedArgs()) |
| 7326 | return false; |
| 7327 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 7328 | FromFunctionType->getExtProtoInfo(); |
| 7329 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 7330 | ToFunctionType->getExtProtoInfo(); |
| 7331 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 7332 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 7333 | ArgIdx != NumArgs; ++ArgIdx) { |
| 7334 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 7335 | ToEPI.ConsumedArguments[ArgIdx]) |
| 7336 | return false; |
| 7337 | } |
| 7338 | return true; |
| 7339 | } |
| 7340 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7341 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 7342 | /// 'RHS' attributes and returns the merged version; including for function |
| 7343 | /// return types. |
| 7344 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 7345 | QualType LHSCan = getCanonicalType(LHS), |
| 7346 | RHSCan = getCanonicalType(RHS); |
| 7347 | // If two types are identical, they are compatible. |
| 7348 | if (LHSCan == RHSCan) |
| 7349 | return LHS; |
| 7350 | if (RHSCan->isFunctionType()) { |
| 7351 | if (!LHSCan->isFunctionType()) |
| 7352 | return QualType(); |
| 7353 | QualType OldReturnType = |
| 7354 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 7355 | QualType NewReturnType = |
| 7356 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 7357 | QualType ResReturnType = |
| 7358 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 7359 | if (ResReturnType.isNull()) |
| 7360 | return QualType(); |
| 7361 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 7362 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 7363 | // In either case, use OldReturnType to build the new function type. |
| 7364 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 7365 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7366 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7367 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7368 | QualType ResultType |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 7369 | = getFunctionType(OldReturnType, |
| 7370 | ArrayRef<QualType>(FPT->arg_type_begin(), |
| 7371 | FPT->getNumArgs()), |
| 7372 | EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7373 | return ResultType; |
| 7374 | } |
| 7375 | } |
| 7376 | return QualType(); |
| 7377 | } |
| 7378 | |
| 7379 | // If the qualifiers are different, the types can still be merged. |
| 7380 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 7381 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 7382 | if (LQuals != RQuals) { |
| 7383 | // If any of these qualifiers are different, we have a type mismatch. |
| 7384 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 7385 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 7386 | return QualType(); |
| 7387 | |
| 7388 | // Exactly one GC qualifier difference is allowed: __strong is |
| 7389 | // okay if the other type has no GC qualifier but is an Objective |
| 7390 | // C object pointer (i.e. implicitly strong by default). We fix |
| 7391 | // this by pretending that the unqualified type was actually |
| 7392 | // qualified __strong. |
| 7393 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 7394 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 7395 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 7396 | |
| 7397 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 7398 | return QualType(); |
| 7399 | |
| 7400 | if (GC_L == Qualifiers::Strong) |
| 7401 | return LHS; |
| 7402 | if (GC_R == Qualifiers::Strong) |
| 7403 | return RHS; |
| 7404 | return QualType(); |
| 7405 | } |
| 7406 | |
| 7407 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 7408 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7409 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7410 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 7411 | if (ResQT == LHSBaseQT) |
| 7412 | return LHS; |
| 7413 | if (ResQT == RHSBaseQT) |
| 7414 | return RHS; |
| 7415 | } |
| 7416 | return QualType(); |
| 7417 | } |
| 7418 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 7419 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7420 | // Integer Predicates |
| 7421 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 7422 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7423 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7424 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 7425 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7426 | if (T->isBooleanType()) |
| 7427 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 7428 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7429 | return (unsigned)getTypeSize(T); |
| 7430 | } |
| 7431 | |
Abramo Bagnara | 762f159 | 2012-09-09 10:21:24 +0000 | [diff] [blame] | 7432 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7433 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7434 | |
| 7435 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 7436 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 7437 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7438 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7439 | |
| 7440 | // For enums, we return the unsigned version of the base type. |
| 7441 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7442 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7443 | |
| 7444 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 7445 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7446 | switch (BTy->getKind()) { |
| 7447 | case BuiltinType::Char_S: |
| 7448 | case BuiltinType::SChar: |
| 7449 | return UnsignedCharTy; |
| 7450 | case BuiltinType::Short: |
| 7451 | return UnsignedShortTy; |
| 7452 | case BuiltinType::Int: |
| 7453 | return UnsignedIntTy; |
| 7454 | case BuiltinType::Long: |
| 7455 | return UnsignedLongTy; |
| 7456 | case BuiltinType::LongLong: |
| 7457 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 7458 | case BuiltinType::Int128: |
| 7459 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7460 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7461 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7462 | } |
| 7463 | } |
| 7464 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 7465 | ASTMutationListener::~ASTMutationListener() { } |
| 7466 | |
Richard Smith | 9dadfab | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 7467 | void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD, |
| 7468 | QualType ReturnType) {} |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7469 | |
| 7470 | //===----------------------------------------------------------------------===// |
| 7471 | // Builtin Type Computation |
| 7472 | //===----------------------------------------------------------------------===// |
| 7473 | |
| 7474 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7475 | /// pointer over the consumed characters. This returns the resultant type. If |
| 7476 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 7477 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 7478 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7479 | /// |
| 7480 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 7481 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7482 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7483 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7484 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7485 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7486 | // Modifiers. |
| 7487 | int HowLong = 0; |
| 7488 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7489 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7490 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7491 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7492 | bool Done = false; |
| 7493 | while (!Done) { |
| 7494 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7495 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7496 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7497 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7498 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7499 | case 'S': |
| 7500 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 7501 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 7502 | Signed = true; |
| 7503 | break; |
| 7504 | case 'U': |
| 7505 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 7506 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 7507 | Unsigned = true; |
| 7508 | break; |
| 7509 | case 'L': |
| 7510 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 7511 | ++HowLong; |
| 7512 | break; |
| 7513 | } |
| 7514 | } |
| 7515 | |
| 7516 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7517 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7518 | // Read the base type. |
| 7519 | switch (*Str++) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7520 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7521 | case 'v': |
| 7522 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7523 | "Bad modifiers used with 'v'!"); |
| 7524 | Type = Context.VoidTy; |
| 7525 | break; |
| 7526 | case 'f': |
| 7527 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7528 | "Bad modifiers used with 'f'!"); |
| 7529 | Type = Context.FloatTy; |
| 7530 | break; |
| 7531 | case 'd': |
| 7532 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 7533 | "Bad modifiers used with 'd'!"); |
| 7534 | if (HowLong) |
| 7535 | Type = Context.LongDoubleTy; |
| 7536 | else |
| 7537 | Type = Context.DoubleTy; |
| 7538 | break; |
| 7539 | case 's': |
| 7540 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 7541 | if (Unsigned) |
| 7542 | Type = Context.UnsignedShortTy; |
| 7543 | else |
| 7544 | Type = Context.ShortTy; |
| 7545 | break; |
| 7546 | case 'i': |
| 7547 | if (HowLong == 3) |
| 7548 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 7549 | else if (HowLong == 2) |
| 7550 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 7551 | else if (HowLong == 1) |
| 7552 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 7553 | else |
| 7554 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 7555 | break; |
| 7556 | case 'c': |
| 7557 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 7558 | if (Signed) |
| 7559 | Type = Context.SignedCharTy; |
| 7560 | else if (Unsigned) |
| 7561 | Type = Context.UnsignedCharTy; |
| 7562 | else |
| 7563 | Type = Context.CharTy; |
| 7564 | break; |
| 7565 | case 'b': // boolean |
| 7566 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 7567 | Type = Context.BoolTy; |
| 7568 | break; |
| 7569 | case 'z': // size_t. |
| 7570 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 7571 | Type = Context.getSizeType(); |
| 7572 | break; |
| 7573 | case 'F': |
| 7574 | Type = Context.getCFConstantStringType(); |
| 7575 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 7576 | case 'G': |
| 7577 | Type = Context.getObjCIdType(); |
| 7578 | break; |
| 7579 | case 'H': |
| 7580 | Type = Context.getObjCSelType(); |
| 7581 | break; |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 7582 | case 'M': |
| 7583 | Type = Context.getObjCSuperType(); |
| 7584 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7585 | case 'a': |
| 7586 | Type = Context.getBuiltinVaListType(); |
| 7587 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 7588 | break; |
| 7589 | case 'A': |
| 7590 | // This is a "reference" to a va_list; however, what exactly |
| 7591 | // this means depends on how va_list is defined. There are two |
| 7592 | // different kinds of va_list: ones passed by value, and ones |
| 7593 | // passed by reference. An example of a by-value va_list is |
| 7594 | // x86, where va_list is a char*. An example of by-ref va_list |
| 7595 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 7596 | // we want this argument to be a char*&; for x86-64, we want |
| 7597 | // it to be a __va_list_tag*. |
| 7598 | Type = Context.getBuiltinVaListType(); |
| 7599 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7600 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7601 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7602 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7603 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7604 | break; |
| 7605 | case 'V': { |
| 7606 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7607 | unsigned NumElements = strtoul(Str, &End, 10); |
| 7608 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7609 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7610 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7611 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 7612 | RequiresICE, false); |
| 7613 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7614 | |
| 7615 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 7616 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7617 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7618 | break; |
| 7619 | } |
Douglas Gregor | b4bc99b | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 7620 | case 'E': { |
| 7621 | char *End; |
| 7622 | |
| 7623 | unsigned NumElements = strtoul(Str, &End, 10); |
| 7624 | assert(End != Str && "Missing vector size"); |
| 7625 | |
| 7626 | Str = End; |
| 7627 | |
| 7628 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 7629 | false); |
| 7630 | Type = Context.getExtVectorType(ElementType, NumElements); |
| 7631 | break; |
| 7632 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 7633 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7634 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 7635 | false); |
| 7636 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 7637 | Type = Context.getComplexType(ElementType); |
| 7638 | break; |
Fariborz Jahanian | cc075e4 | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 7639 | } |
| 7640 | case 'Y' : { |
| 7641 | Type = Context.getPointerDiffType(); |
| 7642 | break; |
| 7643 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 7644 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 7645 | Type = Context.getFILEType(); |
| 7646 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7647 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7648 | return QualType(); |
| 7649 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7650 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 7651 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7652 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 7653 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7654 | else |
| 7655 | Type = Context.getjmp_bufType(); |
| 7656 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7657 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7658 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7659 | return QualType(); |
| 7660 | } |
| 7661 | break; |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 7662 | case 'K': |
| 7663 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 7664 | Type = Context.getucontext_tType(); |
| 7665 | |
| 7666 | if (Type.isNull()) { |
| 7667 | Error = ASTContext::GE_Missing_ucontext; |
| 7668 | return QualType(); |
| 7669 | } |
| 7670 | break; |
Eli Friedman | 6902e41 | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 7671 | case 'p': |
| 7672 | Type = Context.getProcessIDType(); |
| 7673 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 7674 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7675 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7676 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 7677 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7678 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 7679 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7680 | default: Done = true; --Str; break; |
| 7681 | case '*': |
| 7682 | case '&': { |
| 7683 | // Both pointers and references can have their pointee types |
| 7684 | // qualified with an address space. |
| 7685 | char *End; |
| 7686 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 7687 | if (End != Str && AddrSpace != 0) { |
| 7688 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 7689 | Str = End; |
| 7690 | } |
| 7691 | if (c == '*') |
| 7692 | Type = Context.getPointerType(Type); |
| 7693 | else |
| 7694 | Type = Context.getLValueReferenceType(Type); |
| 7695 | break; |
| 7696 | } |
| 7697 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 7698 | case 'C': |
| 7699 | Type = Type.withConst(); |
| 7700 | break; |
| 7701 | case 'D': |
| 7702 | Type = Context.getVolatileType(Type); |
| 7703 | break; |
Ted Kremenek | 18932a0 | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 7704 | case 'R': |
| 7705 | Type = Type.withRestrict(); |
| 7706 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7707 | } |
| 7708 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7709 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7710 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7711 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7712 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7713 | return Type; |
| 7714 | } |
| 7715 | |
| 7716 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7717 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7718 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7719 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7720 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7721 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7722 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7723 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7724 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7725 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7726 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 7727 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7728 | if (Error != GE_None) |
| 7729 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7730 | |
| 7731 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 7732 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7733 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7734 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7735 | if (Error != GE_None) |
| 7736 | return QualType(); |
| 7737 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7738 | // If this argument is required to be an IntegerConstantExpression and the |
| 7739 | // caller cares, fill in the bitmask we return. |
| 7740 | if (RequiresICE && IntegerConstantArgs) |
| 7741 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 7742 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7743 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 7744 | if (Ty->isArrayType()) |
| 7745 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7746 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7747 | ArgTypes.push_back(Ty); |
| 7748 | } |
| 7749 | |
| 7750 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 7751 | "'.' should only occur at end of builtin type list!"); |
| 7752 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 7753 | FunctionType::ExtInfo EI; |
| 7754 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 7755 | |
| 7756 | bool Variadic = (TypeStr[0] == '.'); |
| 7757 | |
| 7758 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 7759 | if (ArgTypes.empty() && Variadic) |
| 7760 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 7761 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7762 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 7763 | EPI.ExtInfo = EI; |
| 7764 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7765 | |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 7766 | return getFunctionType(ResType, ArgTypes, EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7767 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 7768 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7769 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame^] | 7770 | if (!FD->isExternallyVisible()) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7771 | return GVA_Internal; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7772 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame^] | 7773 | GVALinkage External = GVA_StrongExternal; |
| 7774 | switch (FD->getTemplateSpecializationKind()) { |
| 7775 | case TSK_Undeclared: |
| 7776 | case TSK_ExplicitSpecialization: |
| 7777 | External = GVA_StrongExternal; |
| 7778 | break; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7779 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame^] | 7780 | case TSK_ExplicitInstantiationDefinition: |
| 7781 | return GVA_ExplicitTemplateInstantiation; |
| 7782 | |
| 7783 | case TSK_ExplicitInstantiationDeclaration: |
| 7784 | case TSK_ImplicitInstantiation: |
| 7785 | External = GVA_TemplateInstantiation; |
| 7786 | break; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7787 | } |
| 7788 | |
| 7789 | if (!FD->isInlined()) |
| 7790 | return External; |
| 7791 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7792 | if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7793 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 7794 | // externally visible. |
| 7795 | if (FD->isInlineDefinitionExternallyVisible()) |
| 7796 | return External; |
| 7797 | |
| 7798 | // C99 inline semantics, where the symbol is not externally visible. |
| 7799 | return GVA_C99Inline; |
| 7800 | } |
| 7801 | |
| 7802 | // C++0x [temp.explicit]p9: |
| 7803 | // [ Note: The intent is that an inline function that is the subject of |
| 7804 | // an explicit instantiation declaration will still be implicitly |
| 7805 | // instantiated when used so that the body can be considered for |
| 7806 | // inlining, but that no out-of-line copy of the inline function would be |
| 7807 | // generated in the translation unit. -- end note ] |
| 7808 | if (FD->getTemplateSpecializationKind() |
| 7809 | == TSK_ExplicitInstantiationDeclaration) |
| 7810 | return GVA_C99Inline; |
| 7811 | |
| 7812 | return GVA_CXXInline; |
| 7813 | } |
| 7814 | |
| 7815 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame^] | 7816 | if (!VD->isExternallyVisible()) |
| 7817 | return GVA_Internal; |
| 7818 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7819 | // If this is a static data member, compute the kind of template |
| 7820 | // specialization. Otherwise, this variable is not part of a |
| 7821 | // template. |
| 7822 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 7823 | if (VD->isStaticDataMember()) |
| 7824 | TSK = VD->getTemplateSpecializationKind(); |
| 7825 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame^] | 7826 | switch (TSK) { |
| 7827 | case TSK_Undeclared: |
| 7828 | case TSK_ExplicitSpecialization: |
| 7829 | return GVA_StrongExternal; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7830 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame^] | 7831 | case TSK_ExplicitInstantiationDeclaration: |
| 7832 | llvm_unreachable("Variable should not be instantiated"); |
| 7833 | // Fall through to treat this like any other instantiation. |
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 | case TSK_ExplicitInstantiationDefinition: |
| 7836 | return GVA_ExplicitTemplateInstantiation; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7837 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame^] | 7838 | case TSK_ImplicitInstantiation: |
| 7839 | return GVA_TemplateInstantiation; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7840 | } |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7841 | } |
| 7842 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 7843 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7844 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7845 | if (!VD->isFileVarDecl()) |
| 7846 | return false; |
Richard Smith | f396ad9 | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 7847 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7848 | // We never need to emit an uninstantiated function template. |
| 7849 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
| 7850 | return false; |
| 7851 | } else |
| 7852 | return false; |
| 7853 | |
| 7854 | // If this is a member of a class template, we do not need to emit it. |
| 7855 | if (D->getDeclContext()->isDependentContext()) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7856 | return false; |
| 7857 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7858 | // Weak references don't produce any output by themselves. |
| 7859 | if (D->hasAttr<WeakRefAttr>()) |
| 7860 | return false; |
| 7861 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7862 | // Aliases and used decls are required. |
| 7863 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 7864 | return true; |
| 7865 | |
| 7866 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7867 | // Forward declarations aren't required. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 7868 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 7869 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7870 | |
| 7871 | // Constructors and destructors are required. |
| 7872 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 7873 | return true; |
| 7874 | |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7875 | // The key function for a class is required. This rule only comes |
| 7876 | // into play when inline functions can be key functions, though. |
| 7877 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
| 7878 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 7879 | const CXXRecordDecl *RD = MD->getParent(); |
| 7880 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 7881 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
| 7882 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 7883 | return true; |
| 7884 | } |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7885 | } |
| 7886 | } |
| 7887 | |
| 7888 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 7889 | |
| 7890 | // static, static inline, always_inline, and extern inline functions can |
| 7891 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 7892 | // Implicit template instantiations can also be deferred in C++. |
| 7893 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
Anton Yartsev | 3a5aca8 | 2012-02-02 06:06:34 +0000 | [diff] [blame] | 7894 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7895 | return false; |
| 7896 | return true; |
| 7897 | } |
Douglas Gregor | 94da158 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 7898 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7899 | const VarDecl *VD = cast<VarDecl>(D); |
| 7900 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 7901 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7902 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 7903 | return false; |
| 7904 | |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7905 | // Variables that can be needed in other TUs are required. |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7906 | GVALinkage L = GetGVALinkageForVariable(VD); |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7907 | if (L != GVA_Internal && L != GVA_TemplateInstantiation) |
| 7908 | return true; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7909 | |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7910 | // Variables that have destruction with side-effects are required. |
| 7911 | if (VD->getType().isDestructedType()) |
| 7912 | return true; |
| 7913 | |
| 7914 | // Variables that have initialization with side-effects are required. |
| 7915 | if (VD->getInit() && VD->getInit()->HasSideEffects(*this)) |
| 7916 | return true; |
| 7917 | |
| 7918 | return false; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7919 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7920 | |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7921 | CallingConv ASTContext::getDefaultCXXMethodCallConv(bool isVariadic) { |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7922 | // Pass through to the C++ ABI object |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7923 | return ABI->getDefaultMethodCallConv(isVariadic); |
| 7924 | } |
| 7925 | |
| 7926 | CallingConv ASTContext::getCanonicalCallConv(CallingConv CC) const { |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7927 | if (CC == CC_C && !LangOpts.MRTD && |
| 7928 | getTargetInfo().getCXXABI().isMemberFunctionCCDefault()) |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7929 | return CC_Default; |
| 7930 | return CC; |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7931 | } |
| 7932 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7933 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 7934 | // Pass through to the C++ ABI object |
| 7935 | return ABI->isNearlyEmpty(RD); |
| 7936 | } |
| 7937 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7938 | MangleContext *ASTContext::createMangleContext() { |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7939 | switch (Target->getCXXABI().getKind()) { |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7940 | case TargetCXXABI::GenericAArch64: |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7941 | case TargetCXXABI::GenericItanium: |
| 7942 | case TargetCXXABI::GenericARM: |
| 7943 | case TargetCXXABI::iOS: |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7944 | return createItaniumMangleContext(*this, getDiagnostics()); |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7945 | case TargetCXXABI::Microsoft: |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7946 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 7947 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7948 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7949 | } |
| 7950 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7951 | CXXABI::~CXXABI() {} |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7952 | |
| 7953 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 0c8cd1a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 7954 | return ASTRecordLayouts.getMemorySize() |
| 7955 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 7956 | + llvm::capacity_in_bytes(KeyFunctions) |
| 7957 | + llvm::capacity_in_bytes(ObjCImpls) |
| 7958 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 7959 | + llvm::capacity_in_bytes(DeclAttrs) |
| 7960 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 7961 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 7962 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 7963 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 7964 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 7965 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7966 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 7967 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7968 | } |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7969 | |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 7970 | void ASTContext::addUnnamedTag(const TagDecl *Tag) { |
| 7971 | // FIXME: This mangling should be applied to function local classes too |
| 7972 | if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl() || |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame^] | 7973 | !isa<CXXRecordDecl>(Tag->getParent()) || |
| 7974 | !Tag->isExternallyVisible()) |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 7975 | return; |
| 7976 | |
| 7977 | std::pair<llvm::DenseMap<const DeclContext *, unsigned>::iterator, bool> P = |
| 7978 | UnnamedMangleContexts.insert(std::make_pair(Tag->getParent(), 0)); |
| 7979 | UnnamedMangleNumbers.insert(std::make_pair(Tag, P.first->second++)); |
| 7980 | } |
| 7981 | |
| 7982 | int ASTContext::getUnnamedTagManglingNumber(const TagDecl *Tag) const { |
| 7983 | llvm::DenseMap<const TagDecl *, unsigned>::const_iterator I = |
| 7984 | UnnamedMangleNumbers.find(Tag); |
| 7985 | return I != UnnamedMangleNumbers.end() ? I->second : -1; |
| 7986 | } |
| 7987 | |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 7988 | unsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) { |
| 7989 | CXXRecordDecl *Lambda = CallOperator->getParent(); |
| 7990 | return LambdaMangleContexts[Lambda->getDeclContext()] |
| 7991 | .getManglingNumber(CallOperator); |
| 7992 | } |
| 7993 | |
| 7994 | |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7995 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 7996 | ParamIndices[D] = index; |
| 7997 | } |
| 7998 | |
| 7999 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 8000 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 8001 | assert(I != ParamIndices.end() && |
| 8002 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 8003 | return I->second; |
| 8004 | } |