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 | |
Aaron Ballman | f973424 | 2013-05-04 16:56:22 +0000 | [diff] [blame] | 900 | if (LangOpts.CPlusPlus && LangOpts.WChar) { // C++ 3.9.1p5 |
Eli Friedman | d3d77cd | 2011-04-30 19:24:24 +0000 | [diff] [blame] | 901 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 902 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 903 | else // -fshort-wchar makes wchar_t be unsigned. |
| 904 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
Aaron Ballman | f973424 | 2013-05-04 16:56:22 +0000 | [diff] [blame] | 905 | } else // C99 (or C++ using -fno-wchar) |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 906 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 907 | |
James Molloy | 392da48 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 908 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 909 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 910 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 911 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 912 | else // C99 |
| 913 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 914 | |
| 915 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 916 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 917 | else // C99 |
| 918 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 919 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 920 | // Placeholder type for type-dependent expressions whose type is |
| 921 | // completely unknown. No code should ever check a type against |
| 922 | // DependentTy and users should never see it; however, it is here to |
| 923 | // help diagnose failures to properly check for type-dependent |
| 924 | // expressions. |
| 925 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 926 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 927 | // Placeholder type for functions. |
| 928 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 929 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 930 | // Placeholder type for bound members. |
| 931 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 932 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 933 | // Placeholder type for pseudo-objects. |
| 934 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 935 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 936 | // "any" type; useful for debugger-like clients. |
| 937 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 938 | |
John McCall | 0ddaeb9 | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 939 | // Placeholder type for unbridged ARC casts. |
| 940 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 941 | |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 942 | // Placeholder type for builtin functions. |
| 943 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
| 944 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 945 | // C99 6.2.5p11. |
| 946 | FloatComplexTy = getComplexType(FloatTy); |
| 947 | DoubleComplexTy = getComplexType(DoubleTy); |
| 948 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 949 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 950 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 951 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 952 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 953 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 954 | |
| 955 | if (LangOpts.OpenCL) { |
| 956 | InitBuiltinType(OCLImage1dTy, BuiltinType::OCLImage1d); |
| 957 | InitBuiltinType(OCLImage1dArrayTy, BuiltinType::OCLImage1dArray); |
| 958 | InitBuiltinType(OCLImage1dBufferTy, BuiltinType::OCLImage1dBuffer); |
| 959 | InitBuiltinType(OCLImage2dTy, BuiltinType::OCLImage2d); |
| 960 | InitBuiltinType(OCLImage2dArrayTy, BuiltinType::OCLImage2dArray); |
| 961 | InitBuiltinType(OCLImage3dTy, BuiltinType::OCLImage3d); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 962 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 963 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 964 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 965 | } |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 966 | |
| 967 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 93a4994 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 968 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 969 | SignedCharTy : BoolTy); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 970 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 971 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 972 | |
| 973 | ObjCSuperType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 974 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 975 | // void * type |
| 976 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 977 | |
| 978 | // nullptr type (C++0x 2.14.7) |
| 979 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 980 | |
| 981 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 982 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 983 | |
| 984 | // Builtin type used to help define __builtin_va_list. |
| 985 | VaListTagTy = QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 986 | } |
| 987 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 988 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 989 | return SourceMgr.getDiagnostics(); |
| 990 | } |
| 991 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 992 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 993 | AttrVec *&Result = DeclAttrs[D]; |
| 994 | if (!Result) { |
| 995 | void *Mem = Allocate(sizeof(AttrVec)); |
| 996 | Result = new (Mem) AttrVec; |
| 997 | } |
| 998 | |
| 999 | return *Result; |
| 1000 | } |
| 1001 | |
| 1002 | /// \brief Erase the attributes corresponding to the given declaration. |
| 1003 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 1004 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 1005 | if (Pos != DeclAttrs.end()) { |
| 1006 | Pos->second->~AttrVec(); |
| 1007 | DeclAttrs.erase(Pos); |
| 1008 | } |
| 1009 | } |
| 1010 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1011 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1012 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1013 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1014 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1015 | = InstantiatedFromStaticDataMember.find(Var); |
| 1016 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 1017 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1018 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1019 | return Pos->second; |
| 1020 | } |
| 1021 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1023 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 1024 | TemplateSpecializationKind TSK, |
| 1025 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1026 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 1027 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 1028 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 1029 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1030 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 1031 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1034 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 1035 | const FunctionDecl *FD){ |
| 1036 | assert(FD && "Specialization is 0"); |
| 1037 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1038 | = ClassScopeSpecializationPattern.find(FD); |
| 1039 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1040 | return 0; |
| 1041 | |
| 1042 | return Pos->second; |
| 1043 | } |
| 1044 | |
| 1045 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 1046 | FunctionDecl *Pattern) { |
| 1047 | assert(FD && "Specialization is 0"); |
| 1048 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1049 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1052 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1053 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1054 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1055 | = InstantiatedFromUsingDecl.find(UUD); |
| 1056 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1057 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1059 | return Pos->second; |
| 1060 | } |
| 1061 | |
| 1062 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1063 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 1064 | assert((isa<UsingDecl>(Pattern) || |
| 1065 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 1066 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 1067 | "pattern decl is not a using decl"); |
| 1068 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 1069 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 1070 | } |
| 1071 | |
| 1072 | UsingShadowDecl * |
| 1073 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 1074 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 1075 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 1076 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 1077 | return 0; |
| 1078 | |
| 1079 | return Pos->second; |
| 1080 | } |
| 1081 | |
| 1082 | void |
| 1083 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 1084 | UsingShadowDecl *Pattern) { |
| 1085 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 1086 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1089 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 1090 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 1091 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 1092 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 1093 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1095 | return Pos->second; |
| 1096 | } |
| 1097 | |
| 1098 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 1099 | FieldDecl *Tmpl) { |
| 1100 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 1101 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 1102 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 1103 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1105 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 1106 | } |
| 1107 | |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 1108 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 1109 | const FieldDecl *LastFD) const { |
| 1110 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1111 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 1114 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 1115 | const FieldDecl *LastFD) const { |
| 1116 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1117 | FD->getBitWidthValue(*this) == 0 && |
| 1118 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1121 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 1122 | const FieldDecl *LastFD) const { |
| 1123 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1124 | FD->getBitWidthValue(*this) && |
| 1125 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1128 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1129 | const FieldDecl *LastFD) const { |
| 1130 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1131 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1134 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1135 | const FieldDecl *LastFD) const { |
| 1136 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1137 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1140 | ASTContext::overridden_cxx_method_iterator |
| 1141 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 1142 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1143 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1144 | if (Pos == OverriddenMethods.end()) |
| 1145 | return 0; |
| 1146 | |
| 1147 | return Pos->second.begin(); |
| 1148 | } |
| 1149 | |
| 1150 | ASTContext::overridden_cxx_method_iterator |
| 1151 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 1152 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1153 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1154 | if (Pos == OverriddenMethods.end()) |
| 1155 | return 0; |
| 1156 | |
| 1157 | return Pos->second.end(); |
| 1158 | } |
| 1159 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1160 | unsigned |
| 1161 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 1162 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1163 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1164 | if (Pos == OverriddenMethods.end()) |
| 1165 | return 0; |
| 1166 | |
| 1167 | return Pos->second.size(); |
| 1168 | } |
| 1169 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1170 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 1171 | const CXXMethodDecl *Overridden) { |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1172 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1173 | OverriddenMethods[Method].push_back(Overridden); |
| 1174 | } |
| 1175 | |
Dmitri Gribenko | 1e905da | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 1176 | void ASTContext::getOverriddenMethods( |
| 1177 | const NamedDecl *D, |
| 1178 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1179 | assert(D); |
| 1180 | |
| 1181 | if (const CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 685d104 | 2013-04-17 00:09:03 +0000 | [diff] [blame] | 1182 | Overridden.append(overridden_methods_begin(CXXMethod), |
| 1183 | overridden_methods_end(CXXMethod)); |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1184 | return; |
| 1185 | } |
| 1186 | |
| 1187 | const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); |
| 1188 | if (!Method) |
| 1189 | return; |
| 1190 | |
Argyrios Kyrtzidis | 740ae67 | 2012-10-09 18:19:01 +0000 | [diff] [blame] | 1191 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
| 1192 | Method->getOverriddenMethods(OverDecls); |
Argyrios Kyrtzidis | bc0a2bb | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1193 | Overridden.append(OverDecls.begin(), OverDecls.end()); |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1196 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 1197 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 1198 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 1199 | if (!FirstLocalImport) { |
| 1200 | FirstLocalImport = Import; |
| 1201 | LastLocalImport = Import; |
| 1202 | return; |
| 1203 | } |
| 1204 | |
| 1205 | LastLocalImport->NextLocalImport = Import; |
| 1206 | LastLocalImport = Import; |
| 1207 | } |
| 1208 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1209 | //===----------------------------------------------------------------------===// |
| 1210 | // Type Sizing and Analysis |
| 1211 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1212 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1213 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 1214 | /// scalar floating point type. |
| 1215 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1216 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1217 | assert(BT && "Not a floating point type!"); |
| 1218 | switch (BT->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1219 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1220 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1221 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 1222 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 1223 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1227 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1228 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 1229 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1230 | /// If @p RefAsPointee, references are treated like their underlying type |
| 1231 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1232 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1233 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1234 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1235 | bool UseAlignAttrOnly = false; |
| 1236 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 1237 | Align = AlignFromAttr; |
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 | // __attribute__((aligned)) can increase or decrease alignment |
| 1240 | // *except* on a struct or struct member, where it only increases |
| 1241 | // alignment unless 'packed' is also specified. |
| 1242 | // |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1243 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1244 | // ignore that possibility; Sema should diagnose it. |
| 1245 | if (isa<FieldDecl>(D)) { |
| 1246 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 1247 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 1248 | } else { |
| 1249 | UseAlignAttrOnly = true; |
| 1250 | } |
| 1251 | } |
Fariborz Jahanian | 78a7d7d | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1252 | else if (isa<FieldDecl>(D)) |
| 1253 | UseAlignAttrOnly = |
| 1254 | D->hasAttr<PackedAttr>() || |
| 1255 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1256 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1257 | // If we're using the align attribute only, just ignore everything |
| 1258 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1259 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1260 | // do nothing |
| 1261 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1262 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1263 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1264 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1265 | if (RefAsPointee) |
| 1266 | T = RT->getPointeeType(); |
| 1267 | else |
| 1268 | T = getPointerType(RT->getPointeeType()); |
| 1269 | } |
| 1270 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1271 | // Adjust alignments of declarations with array type by the |
| 1272 | // large-array alignment on the target. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1273 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1274 | const ArrayType *arrayType; |
| 1275 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 1276 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1277 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1278 | else if (isa<ConstantArrayType>(arrayType) && |
| 1279 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1280 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1281 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1282 | // Walk through any array types while we're at it. |
| 1283 | T = getBaseElementType(arrayType); |
| 1284 | } |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 1285 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Ulrich Weigand | 6b20351 | 2013-05-06 16:23:57 +0000 | [diff] [blame^] | 1286 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1287 | if (VD->hasGlobalStorage()) |
| 1288 | Align = std::max(Align, getTargetInfo().getMinGlobalAlign()); |
| 1289 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1290 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1291 | |
| 1292 | // Fields can be subject to extra alignment constraints, like if |
| 1293 | // the field is packed, the struct is packed, or the struct has a |
| 1294 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 1295 | // the actual alignment of the field within the struct, and then |
| 1296 | // (as we're expected to) constrain that by the alignment of the type. |
| 1297 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 1298 | // So calculate the alignment of the field. |
| 1299 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 1300 | |
| 1301 | // Start with the record's overall alignment. |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1302 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1303 | |
| 1304 | // Use the GCD of that and the offset within the record. |
| 1305 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 1306 | if (offset > 0) { |
| 1307 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 1308 | // which means we get to do this crazy thing instead of Euclid's. |
| 1309 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 1310 | if (lowBitOfOffset < fieldAlign) |
| 1311 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 1312 | } |
| 1313 | |
| 1314 | Align = std::min(Align, fieldAlign); |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 1315 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1316 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1317 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1318 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1319 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1320 | |
John McCall | 929bbfb | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1321 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
| 1322 | // chars. If the type is a record, its data size is returned. This is |
| 1323 | // the size of the memcpy that's performed when assigning this type |
| 1324 | // using a trivial copy/move assignment operator. |
| 1325 | std::pair<CharUnits, CharUnits> |
| 1326 | ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
| 1327 | std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T); |
| 1328 | |
| 1329 | // In C++, objects can sometimes be allocated into the tail padding |
| 1330 | // of a base-class subobject. We decide whether that's possible |
| 1331 | // during class layout, so here we can just trust the layout results. |
| 1332 | if (getLangOpts().CPlusPlus) { |
| 1333 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1334 | const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl()); |
| 1335 | sizeAndAlign.first = layout.getDataSize(); |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | return sizeAndAlign; |
| 1340 | } |
| 1341 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1342 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1343 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1344 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1345 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 1346 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1350 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1351 | return getTypeInfoInChars(T.getTypePtr()); |
| 1352 | } |
| 1353 | |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1354 | std::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const { |
| 1355 | TypeInfoMap::iterator it = MemoizedTypeInfo.find(T); |
| 1356 | if (it != MemoizedTypeInfo.end()) |
| 1357 | return it->second; |
| 1358 | |
| 1359 | std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T); |
| 1360 | MemoizedTypeInfo.insert(std::make_pair(T, Info)); |
| 1361 | return Info; |
| 1362 | } |
| 1363 | |
| 1364 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 1365 | /// method does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1366 | /// |
| 1367 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 1368 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1369 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1370 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1371 | ASTContext::getTypeInfoImpl(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 1372 | uint64_t Width=0; |
| 1373 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1374 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1375 | #define TYPE(Class, Base) |
| 1376 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1377 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1378 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1379 | #include "clang/AST/TypeNodes.def" |
John McCall | d3d49bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1380 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1381 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1382 | case Type::FunctionNoProto: |
| 1383 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1384 | // GCC extension: alignof(function) = 32 bits |
| 1385 | Width = 0; |
| 1386 | Align = 32; |
| 1387 | break; |
| 1388 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1389 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1390 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1391 | Width = 0; |
| 1392 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1393 | break; |
| 1394 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1395 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1396 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1398 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1399 | uint64_t Size = CAT->getSize().getZExtValue(); |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1400 | assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && |
| 1401 | "Overflow in array type bit size evaluation"); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1402 | Width = EltInfo.first*Size; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1403 | Align = EltInfo.second; |
Argyrios Kyrtzidis | cd88b41 | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 1404 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1405 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1406 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1407 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1408 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1409 | const VectorType *VT = cast<VectorType>(T); |
| 1410 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 1411 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1412 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1413 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1414 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1415 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1416 | Align = llvm::NextPowerOf2(Align); |
| 1417 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 1418 | } |
Chad Rosier | f9e9af7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1419 | // Adjust the alignment based on the target max. |
| 1420 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1421 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1422 | Align = TargetVectorAlign; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1423 | break; |
| 1424 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1425 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1426 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1427 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1428 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1429 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1430 | // GCC extension: alignof(void) = 8 bits. |
| 1431 | Width = 0; |
| 1432 | Align = 8; |
| 1433 | break; |
| 1434 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1435 | case BuiltinType::Bool: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1436 | Width = Target->getBoolWidth(); |
| 1437 | Align = Target->getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1438 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1439 | case BuiltinType::Char_S: |
| 1440 | case BuiltinType::Char_U: |
| 1441 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1442 | case BuiltinType::SChar: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1443 | Width = Target->getCharWidth(); |
| 1444 | Align = Target->getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1445 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1446 | case BuiltinType::WChar_S: |
| 1447 | case BuiltinType::WChar_U: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1448 | Width = Target->getWCharWidth(); |
| 1449 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1450 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1451 | case BuiltinType::Char16: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1452 | Width = Target->getChar16Width(); |
| 1453 | Align = Target->getChar16Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1454 | break; |
| 1455 | case BuiltinType::Char32: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1456 | Width = Target->getChar32Width(); |
| 1457 | Align = Target->getChar32Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1458 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1459 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1460 | case BuiltinType::Short: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1461 | Width = Target->getShortWidth(); |
| 1462 | Align = Target->getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1463 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1464 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1465 | case BuiltinType::Int: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1466 | Width = Target->getIntWidth(); |
| 1467 | Align = Target->getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1468 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1469 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1470 | case BuiltinType::Long: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1471 | Width = Target->getLongWidth(); |
| 1472 | Align = Target->getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1473 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1474 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1475 | case BuiltinType::LongLong: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1476 | Width = Target->getLongLongWidth(); |
| 1477 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1478 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1479 | case BuiltinType::Int128: |
| 1480 | case BuiltinType::UInt128: |
| 1481 | Width = 128; |
| 1482 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1483 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1484 | case BuiltinType::Half: |
| 1485 | Width = Target->getHalfWidth(); |
| 1486 | Align = Target->getHalfAlign(); |
| 1487 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1488 | case BuiltinType::Float: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1489 | Width = Target->getFloatWidth(); |
| 1490 | Align = Target->getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1491 | break; |
| 1492 | case BuiltinType::Double: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1493 | Width = Target->getDoubleWidth(); |
| 1494 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1495 | break; |
| 1496 | case BuiltinType::LongDouble: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1497 | Width = Target->getLongDoubleWidth(); |
| 1498 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1499 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1500 | case BuiltinType::NullPtr: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1501 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1502 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1503 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1504 | case BuiltinType::ObjCId: |
| 1505 | case BuiltinType::ObjCClass: |
| 1506 | case BuiltinType::ObjCSel: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1507 | Width = Target->getPointerWidth(0); |
| 1508 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1509 | break; |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1510 | case BuiltinType::OCLSampler: |
| 1511 | // Samplers are modeled as integers. |
| 1512 | Width = Target->getIntWidth(); |
| 1513 | Align = Target->getIntAlign(); |
| 1514 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1515 | case BuiltinType::OCLEvent: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1516 | case BuiltinType::OCLImage1d: |
| 1517 | case BuiltinType::OCLImage1dArray: |
| 1518 | case BuiltinType::OCLImage1dBuffer: |
| 1519 | case BuiltinType::OCLImage2d: |
| 1520 | case BuiltinType::OCLImage2dArray: |
| 1521 | case BuiltinType::OCLImage3d: |
| 1522 | // Currently these types are pointers to opaque types. |
| 1523 | Width = Target->getPointerWidth(0); |
| 1524 | Align = Target->getPointerAlign(0); |
| 1525 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1526 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1527 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1528 | case Type::ObjCObjectPointer: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1529 | Width = Target->getPointerWidth(0); |
| 1530 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1531 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1532 | case Type::BlockPointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1533 | unsigned AS = getTargetAddressSpace( |
| 1534 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1535 | Width = Target->getPointerWidth(AS); |
| 1536 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1537 | break; |
| 1538 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1539 | case Type::LValueReference: |
| 1540 | case Type::RValueReference: { |
| 1541 | // alignof and sizeof should never enter this code path here, so we go |
| 1542 | // the pointer route. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1543 | unsigned AS = getTargetAddressSpace( |
| 1544 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1545 | Width = Target->getPointerWidth(AS); |
| 1546 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1547 | break; |
| 1548 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1549 | case Type::Pointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1550 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1551 | Width = Target->getPointerWidth(AS); |
| 1552 | Align = Target->getPointerAlign(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1553 | break; |
| 1554 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1555 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1556 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 1557 | llvm::tie(Width, Align) = ABI->getMemberPointerWidthAndAlign(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1558 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1559 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1560 | case Type::Complex: { |
| 1561 | // Complex types have the same alignment as their elements, but twice the |
| 1562 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1564 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1565 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1566 | Align = EltInfo.second; |
| 1567 | break; |
| 1568 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1569 | case Type::ObjCObject: |
| 1570 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1571 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1572 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1573 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1574 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1575 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1576 | break; |
| 1577 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1578 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1579 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1580 | const TagType *TT = cast<TagType>(T); |
| 1581 | |
| 1582 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 22ce41d | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1583 | Width = 8; |
| 1584 | Align = 8; |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1585 | break; |
| 1586 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1588 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1589 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1590 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1591 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1592 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1593 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1594 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1595 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1596 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1597 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1598 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1599 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1600 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1601 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1602 | case Type::Auto: { |
| 1603 | const AutoType *A = cast<AutoType>(T); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 1604 | assert(!A->getDeducedType().isNull() && |
| 1605 | "cannot request the size of an undeduced or dependent auto type"); |
Matt Beaumont-Gay | dc856af | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1606 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1609 | case Type::Paren: |
| 1610 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1611 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1612 | case Type::Typedef: { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1613 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1614 | std::pair<uint64_t, unsigned> Info |
| 1615 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1616 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1617 | // alignment we have. This violates the GCC documentation (which says that |
| 1618 | // attribute(aligned) can only round up) but matches its implementation. |
| 1619 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1620 | Align = AttrAlign; |
| 1621 | else |
| 1622 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1623 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1624 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1625 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1626 | |
| 1627 | case Type::TypeOfExpr: |
| 1628 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1629 | .getTypePtr()); |
| 1630 | |
| 1631 | case Type::TypeOf: |
| 1632 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1633 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1634 | case Type::Decltype: |
| 1635 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1636 | .getTypePtr()); |
| 1637 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1638 | case Type::UnaryTransform: |
| 1639 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1640 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1641 | case Type::Elaborated: |
| 1642 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1644 | case Type::Attributed: |
| 1645 | return getTypeInfo( |
| 1646 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1647 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1648 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1650 | "Cannot request the size of a dependent type"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1651 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1652 | // A type alias template specialization may refer to a typedef with the |
| 1653 | // aligned attribute on it. |
| 1654 | if (TST->isTypeAlias()) |
| 1655 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1656 | else |
| 1657 | return getTypeInfo(getCanonicalType(T)); |
| 1658 | } |
| 1659 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1660 | case Type::Atomic: { |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1661 | // Start with the base type information. |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1662 | std::pair<uint64_t, unsigned> Info |
| 1663 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1664 | Width = Info.first; |
| 1665 | Align = Info.second; |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1666 | |
| 1667 | // If the size of the type doesn't exceed the platform's max |
| 1668 | // atomic promotion width, make the size and alignment more |
| 1669 | // favorable to atomic operations: |
| 1670 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth()) { |
| 1671 | // Round the size up to a power of 2. |
| 1672 | if (!llvm::isPowerOf2_64(Width)) |
| 1673 | Width = llvm::NextPowerOf2(Width); |
| 1674 | |
| 1675 | // Set the alignment equal to the size. |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1676 | Align = static_cast<unsigned>(Width); |
| 1677 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1680 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1682 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1683 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1686 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1687 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1688 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1689 | } |
| 1690 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1691 | /// toBits - Convert a size in characters to a size in characters. |
| 1692 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1693 | return CharSize.getQuantity() * getCharWidth(); |
| 1694 | } |
| 1695 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1696 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1697 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1698 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1699 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1700 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1701 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1702 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1705 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1706 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1707 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1708 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1709 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1710 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1711 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1714 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1715 | /// type for the current target in bits. This can be different than the ABI |
| 1716 | /// alignment in cases where it is beneficial for performance to overalign |
| 1717 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1718 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1719 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1720 | |
| 1721 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1722 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1723 | T = CT->getElementType().getTypePtr(); |
| 1724 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | cde7a1d | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1725 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1726 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1727 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1728 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1729 | return ABIAlign; |
| 1730 | } |
| 1731 | |
Ulrich Weigand | 6b20351 | 2013-05-06 16:23:57 +0000 | [diff] [blame^] | 1732 | /// getAlignOfGlobalVar - Return the alignment in bits that should be given |
| 1733 | /// to a global variable of the specified type. |
| 1734 | unsigned ASTContext::getAlignOfGlobalVar(QualType T) const { |
| 1735 | return std::max(getTypeAlign(T), getTargetInfo().getMinGlobalAlign()); |
| 1736 | } |
| 1737 | |
| 1738 | /// getAlignOfGlobalVarInChars - Return the alignment in characters that |
| 1739 | /// should be given to a global variable of the specified type. |
| 1740 | CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const { |
| 1741 | return toCharUnitsFromBits(getAlignOfGlobalVar(T)); |
| 1742 | } |
| 1743 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1744 | /// DeepCollectObjCIvars - |
| 1745 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1746 | /// super class and then collects all ivars, including those synthesized for |
| 1747 | /// current class. This routine is used for implementation of current class |
| 1748 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1749 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1750 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1751 | bool leafClass, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1752 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1753 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1754 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1755 | if (!leafClass) { |
| 1756 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1757 | E = OI->ivar_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1758 | Ivars.push_back(*I); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1759 | } else { |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1760 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1761 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1762 | Iv= Iv->getNextIvar()) |
| 1763 | Ivars.push_back(Iv); |
| 1764 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1767 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1768 | /// those inherited by it. |
| 1769 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1770 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1771 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1772 | // We can use protocol_iterator here instead of |
| 1773 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1774 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1775 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1776 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1777 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1778 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1779 | PE = Proto->protocol_end(); P != PE; ++P) { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1780 | Protocols.insert((*P)->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1781 | CollectInheritedProtocols(*P, Protocols); |
| 1782 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1783 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1784 | |
| 1785 | // Categories of this Interface. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1786 | for (ObjCInterfaceDecl::visible_categories_iterator |
| 1787 | Cat = OI->visible_categories_begin(), |
| 1788 | CatEnd = OI->visible_categories_end(); |
| 1789 | Cat != CatEnd; ++Cat) { |
| 1790 | CollectInheritedProtocols(*Cat, Protocols); |
| 1791 | } |
| 1792 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1793 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1794 | while (SD) { |
| 1795 | CollectInheritedProtocols(SD, Protocols); |
| 1796 | SD = SD->getSuperClass(); |
| 1797 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1798 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1799 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1800 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1801 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1802 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1803 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1804 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1805 | CollectInheritedProtocols(*P, Protocols); |
| 1806 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1807 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1808 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1809 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1810 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1811 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1812 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1813 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1814 | CollectInheritedProtocols(*P, Protocols); |
| 1815 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1816 | } |
| 1817 | } |
| 1818 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1819 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1820 | unsigned count = 0; |
| 1821 | // Count ivars declared in class extension. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1822 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 1823 | Ext = OI->known_extensions_begin(), |
| 1824 | ExtEnd = OI->known_extensions_end(); |
| 1825 | Ext != ExtEnd; ++Ext) { |
| 1826 | count += Ext->ivar_size(); |
| 1827 | } |
| 1828 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1829 | // Count ivar defined in this class's implementation. This |
| 1830 | // includes synthesized ivars. |
| 1831 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1832 | count += ImplDecl->ivar_size(); |
| 1833 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1834 | return count; |
| 1835 | } |
| 1836 | |
Argyrios Kyrtzidis | 8deabc1 | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 1837 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 1838 | if (!E) |
| 1839 | return false; |
| 1840 | |
| 1841 | // nullptr_t is always treated as null. |
| 1842 | if (E->getType()->isNullPtrType()) return true; |
| 1843 | |
| 1844 | if (E->getType()->isAnyPointerType() && |
| 1845 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 1846 | Expr::NPC_ValueDependentIsNull)) |
| 1847 | return true; |
| 1848 | |
| 1849 | // Unfortunately, __null has type 'int'. |
| 1850 | if (isa<GNUNullExpr>(E)) return true; |
| 1851 | |
| 1852 | return false; |
| 1853 | } |
| 1854 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1855 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1856 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1857 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1858 | I = ObjCImpls.find(D); |
| 1859 | if (I != ObjCImpls.end()) |
| 1860 | return cast<ObjCImplementationDecl>(I->second); |
| 1861 | return 0; |
| 1862 | } |
| 1863 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1864 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1865 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1866 | I = ObjCImpls.find(D); |
| 1867 | if (I != ObjCImpls.end()) |
| 1868 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1869 | return 0; |
| 1870 | } |
| 1871 | |
| 1872 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1873 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1874 | ObjCImplementationDecl *ImplD) { |
| 1875 | assert(IFaceD && ImplD && "Passed null params"); |
| 1876 | ObjCImpls[IFaceD] = ImplD; |
| 1877 | } |
| 1878 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1879 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1880 | ObjCCategoryImplDecl *ImplD) { |
| 1881 | assert(CatD && ImplD && "Passed null params"); |
| 1882 | ObjCImpls[CatD] = ImplD; |
| 1883 | } |
| 1884 | |
Dmitri Gribenko | b35cc2d | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1885 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( |
| 1886 | const NamedDecl *ND) const { |
| 1887 | if (const ObjCInterfaceDecl *ID = |
| 1888 | dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1889 | return ID; |
Dmitri Gribenko | b35cc2d | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1890 | if (const ObjCCategoryDecl *CD = |
| 1891 | dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1892 | return CD->getClassInterface(); |
Dmitri Gribenko | b35cc2d | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1893 | if (const ObjCImplDecl *IMD = |
| 1894 | dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1895 | return IMD->getClassInterface(); |
| 1896 | |
| 1897 | return 0; |
| 1898 | } |
| 1899 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1900 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1901 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1902 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1903 | assert(VD && "Passed null params"); |
| 1904 | assert(VD->hasAttr<BlocksAttr>() && |
| 1905 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1906 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1907 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1908 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1909 | } |
| 1910 | |
| 1911 | /// \brief Set the copy inialization expression of a block var decl. |
| 1912 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1913 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1914 | assert(VD->hasAttr<BlocksAttr>() && |
| 1915 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1916 | BlockVarCopyInits[VD] = Init; |
| 1917 | } |
| 1918 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1919 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1920 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1921 | if (!DataSize) |
| 1922 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1923 | else |
| 1924 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1925 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1926 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1927 | TypeSourceInfo *TInfo = |
| 1928 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1929 | new (TInfo) TypeSourceInfo(T); |
| 1930 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1933 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1934 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1935 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1936 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1937 | return DI; |
| 1938 | } |
| 1939 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1940 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1941 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1942 | return getObjCLayout(D, 0); |
| 1943 | } |
| 1944 | |
| 1945 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1946 | ASTContext::getASTObjCImplementationLayout( |
| 1947 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1948 | return getObjCLayout(D->getClassInterface(), D); |
| 1949 | } |
| 1950 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1951 | //===----------------------------------------------------------------------===// |
| 1952 | // Type creation/memoization methods |
| 1953 | //===----------------------------------------------------------------------===// |
| 1954 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1955 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1956 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1957 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1958 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1959 | |
| 1960 | // Check if we've already instantiated this type. |
| 1961 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1962 | ExtQuals::Profile(ID, baseType, quals); |
| 1963 | void *insertPos = 0; |
| 1964 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1965 | assert(eq->getQualifiers() == quals); |
| 1966 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1969 | // If the base type is not canonical, make the appropriate canonical type. |
| 1970 | QualType canon; |
| 1971 | if (!baseType->isCanonicalUnqualified()) { |
| 1972 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1973 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 1974 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1975 | |
| 1976 | // Re-find the insert position. |
| 1977 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1978 | } |
| 1979 | |
| 1980 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1981 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1982 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1985 | QualType |
| 1986 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1987 | QualType CanT = getCanonicalType(T); |
| 1988 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1989 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1990 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1991 | // If we are composing extended qualifiers together, merge together |
| 1992 | // into one ExtQuals node. |
| 1993 | QualifierCollector Quals; |
| 1994 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1996 | // If this type already has an address space specified, it cannot get |
| 1997 | // another one. |
| 1998 | assert(!Quals.hasAddressSpace() && |
| 1999 | "Type cannot be in multiple addr spaces!"); |
| 2000 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2001 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2002 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2005 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2006 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2007 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2008 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2009 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2010 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2011 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 2012 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 2013 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 2014 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 2015 | return getPointerType(ResultType); |
| 2016 | } |
| 2017 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2019 | // If we are composing extended qualifiers together, merge together |
| 2020 | // into one ExtQuals node. |
| 2021 | QualifierCollector Quals; |
| 2022 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2023 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2024 | // If this type already has an ObjCGC specified, it cannot get |
| 2025 | // another one. |
| 2026 | assert(!Quals.hasObjCGCAttr() && |
| 2027 | "Type cannot have multiple ObjCGCs!"); |
| 2028 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2029 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2030 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2031 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2032 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2033 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 2034 | FunctionType::ExtInfo Info) { |
| 2035 | if (T->getExtInfo() == Info) |
| 2036 | return T; |
| 2037 | |
| 2038 | QualType Result; |
| 2039 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 2040 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 2041 | } else { |
| 2042 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 2043 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 2044 | EPI.ExtInfo = Info; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2045 | Result = getFunctionType(FPT->getResultType(), |
| 2046 | ArrayRef<QualType>(FPT->arg_type_begin(), |
| 2047 | FPT->getNumArgs()), |
| 2048 | EPI); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | return cast<FunctionType>(Result.getTypePtr()); |
| 2052 | } |
| 2053 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2054 | void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD, |
| 2055 | QualType ResultType) { |
| 2056 | // FIXME: Need to inform serialization code about this! |
| 2057 | for (FD = FD->getMostRecentDecl(); FD; FD = FD->getPreviousDecl()) { |
| 2058 | const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>(); |
| 2059 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 2060 | FD->setType(getFunctionType(ResultType, FPT->getArgTypes(), EPI)); |
| 2061 | } |
| 2062 | } |
| 2063 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2064 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 2065 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2066 | QualType ASTContext::getComplexType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2067 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2068 | // structure. |
| 2069 | llvm::FoldingSetNodeID ID; |
| 2070 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2072 | void *InsertPos = 0; |
| 2073 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2074 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2076 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2077 | // so fill in the canonical type field. |
| 2078 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2079 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2080 | Canonical = getComplexType(getCanonicalType(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 | // Get the new insert position for the node we care about. |
| 2083 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2084 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2085 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2086 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2087 | Types.push_back(New); |
| 2088 | ComplexTypes.InsertNode(New, InsertPos); |
| 2089 | return QualType(New, 0); |
| 2090 | } |
| 2091 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2092 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 2093 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2094 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2095 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2096 | // structure. |
| 2097 | llvm::FoldingSetNodeID ID; |
| 2098 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2099 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2100 | void *InsertPos = 0; |
| 2101 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2102 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2104 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2105 | // so fill in the canonical type field. |
| 2106 | QualType Canonical; |
Bob Wilson | c90cc93 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2107 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2108 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 | |
Bob Wilson | c90cc93 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2110 | // Get the new insert position for the node we care about. |
| 2111 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2112 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 2113 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2114 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2115 | Types.push_back(New); |
| 2116 | PointerTypes.InsertNode(New, InsertPos); |
| 2117 | return QualType(New, 0); |
| 2118 | } |
| 2119 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2120 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2121 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2122 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2123 | assert(T->isFunctionType() && "block of function types only"); |
| 2124 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2125 | // structure. |
| 2126 | llvm::FoldingSetNodeID ID; |
| 2127 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2129 | void *InsertPos = 0; |
| 2130 | if (BlockPointerType *PT = |
| 2131 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2132 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2133 | |
| 2134 | // 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] | 2135 | // type either so fill in the canonical type field. |
| 2136 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2137 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2138 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2139 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2140 | // Get the new insert position for the node we care about. |
| 2141 | BlockPointerType *NewIP = |
| 2142 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2143 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2144 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2145 | BlockPointerType *New |
| 2146 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2147 | Types.push_back(New); |
| 2148 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 2149 | return QualType(New, 0); |
| 2150 | } |
| 2151 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2152 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 2153 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2154 | QualType |
| 2155 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 2156 | assert(getCanonicalType(T) != OverloadTy && |
| 2157 | "Unresolved overloaded function type"); |
| 2158 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2159 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2160 | // structure. |
| 2161 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2162 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2163 | |
| 2164 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2165 | if (LValueReferenceType *RT = |
| 2166 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2167 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2168 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2169 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2170 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2171 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2172 | // either, so fill in the canonical type field. |
| 2173 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2174 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 2175 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2176 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2177 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2178 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2179 | LValueReferenceType *NewIP = |
| 2180 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2181 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2184 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2185 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 2186 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2187 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2188 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2189 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2190 | return QualType(New, 0); |
| 2191 | } |
| 2192 | |
| 2193 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 2194 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2195 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2196 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2197 | // structure. |
| 2198 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2199 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2200 | |
| 2201 | void *InsertPos = 0; |
| 2202 | if (RValueReferenceType *RT = |
| 2203 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2204 | return QualType(RT, 0); |
| 2205 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2206 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2207 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2208 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2209 | // either, so fill in the canonical type field. |
| 2210 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2211 | if (InnerRef || !T.isCanonical()) { |
| 2212 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2213 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2214 | |
| 2215 | // Get the new insert position for the node we care about. |
| 2216 | RValueReferenceType *NewIP = |
| 2217 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2218 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2221 | RValueReferenceType *New |
| 2222 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2223 | Types.push_back(New); |
| 2224 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2225 | return QualType(New, 0); |
| 2226 | } |
| 2227 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2228 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 2229 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2230 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2231 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2232 | // structure. |
| 2233 | llvm::FoldingSetNodeID ID; |
| 2234 | MemberPointerType::Profile(ID, T, Cls); |
| 2235 | |
| 2236 | void *InsertPos = 0; |
| 2237 | if (MemberPointerType *PT = |
| 2238 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2239 | return QualType(PT, 0); |
| 2240 | |
| 2241 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 2242 | // type either, so fill in the canonical type field. |
| 2243 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2244 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2245 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 2246 | |
| 2247 | // Get the new insert position for the node we care about. |
| 2248 | MemberPointerType *NewIP = |
| 2249 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2250 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2251 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2252 | MemberPointerType *New |
| 2253 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2254 | Types.push_back(New); |
| 2255 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 2256 | return QualType(New, 0); |
| 2257 | } |
| 2258 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2259 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2260 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2261 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2262 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2263 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2264 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 2265 | assert((EltTy->isDependentType() || |
| 2266 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 2267 | "Constant array of VLAs is illegal!"); |
| 2268 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2269 | // Convert the array size into a canonical width matching the pointer size for |
| 2270 | // the target. |
| 2271 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2272 | ArySize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2273 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2274 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2275 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2276 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2277 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2278 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2279 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2280 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2281 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2282 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2283 | // If the element type isn't canonical or has qualifiers, this won't |
| 2284 | // be a canonical type either, so fill in the canonical type field. |
| 2285 | QualType Canon; |
| 2286 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2287 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2288 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2289 | ASM, IndexTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2290 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2291 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2292 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2294 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2295 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2296 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2297 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2298 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2299 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2300 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2301 | Types.push_back(New); |
| 2302 | return QualType(New, 0); |
| 2303 | } |
| 2304 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2305 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 2306 | /// variably-modified, into the corresponding type with all the known |
| 2307 | /// sizes replaced with [*]. |
| 2308 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 2309 | // Vastly most common case. |
| 2310 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2311 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2312 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2313 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2314 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2315 | const Type *ty = split.Ty; |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2316 | switch (ty->getTypeClass()) { |
| 2317 | #define TYPE(Class, Base) |
| 2318 | #define ABSTRACT_TYPE(Class, Base) |
| 2319 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2320 | #include "clang/AST/TypeNodes.def" |
| 2321 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 2322 | |
| 2323 | // These types should never be variably-modified. |
| 2324 | case Type::Builtin: |
| 2325 | case Type::Complex: |
| 2326 | case Type::Vector: |
| 2327 | case Type::ExtVector: |
| 2328 | case Type::DependentSizedExtVector: |
| 2329 | case Type::ObjCObject: |
| 2330 | case Type::ObjCInterface: |
| 2331 | case Type::ObjCObjectPointer: |
| 2332 | case Type::Record: |
| 2333 | case Type::Enum: |
| 2334 | case Type::UnresolvedUsing: |
| 2335 | case Type::TypeOfExpr: |
| 2336 | case Type::TypeOf: |
| 2337 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2338 | case Type::UnaryTransform: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2339 | case Type::DependentName: |
| 2340 | case Type::InjectedClassName: |
| 2341 | case Type::TemplateSpecialization: |
| 2342 | case Type::DependentTemplateSpecialization: |
| 2343 | case Type::TemplateTypeParm: |
| 2344 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2345 | case Type::Auto: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2346 | case Type::PackExpansion: |
| 2347 | llvm_unreachable("type should never be variably-modified"); |
| 2348 | |
| 2349 | // These types can be variably-modified but should never need to |
| 2350 | // further decay. |
| 2351 | case Type::FunctionNoProto: |
| 2352 | case Type::FunctionProto: |
| 2353 | case Type::BlockPointer: |
| 2354 | case Type::MemberPointer: |
| 2355 | return type; |
| 2356 | |
| 2357 | // These types can be variably-modified. All these modifications |
| 2358 | // preserve structure except as noted by comments. |
| 2359 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 2360 | // optimizations available here. |
| 2361 | case Type::Pointer: |
| 2362 | result = getPointerType(getVariableArrayDecayedType( |
| 2363 | cast<PointerType>(ty)->getPointeeType())); |
| 2364 | break; |
| 2365 | |
| 2366 | case Type::LValueReference: { |
| 2367 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 2368 | result = getLValueReferenceType( |
| 2369 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 2370 | lv->isSpelledAsLValue()); |
| 2371 | break; |
| 2372 | } |
| 2373 | |
| 2374 | case Type::RValueReference: { |
| 2375 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 2376 | result = getRValueReferenceType( |
| 2377 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 2378 | break; |
| 2379 | } |
| 2380 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2381 | case Type::Atomic: { |
| 2382 | const AtomicType *at = cast<AtomicType>(ty); |
| 2383 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 2384 | break; |
| 2385 | } |
| 2386 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2387 | case Type::ConstantArray: { |
| 2388 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 2389 | result = getConstantArrayType( |
| 2390 | getVariableArrayDecayedType(cat->getElementType()), |
| 2391 | cat->getSize(), |
| 2392 | cat->getSizeModifier(), |
| 2393 | cat->getIndexTypeCVRQualifiers()); |
| 2394 | break; |
| 2395 | } |
| 2396 | |
| 2397 | case Type::DependentSizedArray: { |
| 2398 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 2399 | result = getDependentSizedArrayType( |
| 2400 | getVariableArrayDecayedType(dat->getElementType()), |
| 2401 | dat->getSizeExpr(), |
| 2402 | dat->getSizeModifier(), |
| 2403 | dat->getIndexTypeCVRQualifiers(), |
| 2404 | dat->getBracketsRange()); |
| 2405 | break; |
| 2406 | } |
| 2407 | |
| 2408 | // Turn incomplete types into [*] types. |
| 2409 | case Type::IncompleteArray: { |
| 2410 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 2411 | result = getVariableArrayType( |
| 2412 | getVariableArrayDecayedType(iat->getElementType()), |
| 2413 | /*size*/ 0, |
| 2414 | ArrayType::Normal, |
| 2415 | iat->getIndexTypeCVRQualifiers(), |
| 2416 | SourceRange()); |
| 2417 | break; |
| 2418 | } |
| 2419 | |
| 2420 | // Turn VLA types into [*] types. |
| 2421 | case Type::VariableArray: { |
| 2422 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 2423 | result = getVariableArrayType( |
| 2424 | getVariableArrayDecayedType(vat->getElementType()), |
| 2425 | /*size*/ 0, |
| 2426 | ArrayType::Star, |
| 2427 | vat->getIndexTypeCVRQualifiers(), |
| 2428 | vat->getBracketsRange()); |
| 2429 | break; |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | // Apply the top-level qualifiers from the original. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2434 | return getQualifiedType(result, split.Quals); |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2435 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2436 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 2437 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 2438 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2439 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 2440 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2441 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2442 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2443 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2444 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 2445 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2446 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2447 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2448 | // Be sure to pull qualifiers off the element type. |
| 2449 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2450 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2451 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2452 | IndexTypeQuals, Brackets); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2453 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2456 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2457 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2458 | |
| 2459 | VariableArrayTypes.push_back(New); |
| 2460 | Types.push_back(New); |
| 2461 | return QualType(New, 0); |
| 2462 | } |
| 2463 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2464 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 2465 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 2466 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2467 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 2468 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2469 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2470 | unsigned elementTypeQuals, |
| 2471 | SourceRange brackets) const { |
| 2472 | assert((!numElements || numElements->isTypeDependent() || |
| 2473 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2474 | "Size must be type- or value-dependent!"); |
| 2475 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2476 | // Dependently-sized array types that do not have a specified number |
| 2477 | // of elements will have their sizes deduced from a dependent |
| 2478 | // initializer. We do no canonicalization here at all, which is okay |
| 2479 | // because they can't be used in most locations. |
| 2480 | if (!numElements) { |
| 2481 | DependentSizedArrayType *newType |
| 2482 | = new (*this, TypeAlignment) |
| 2483 | DependentSizedArrayType(*this, elementType, QualType(), |
| 2484 | numElements, ASM, elementTypeQuals, |
| 2485 | brackets); |
| 2486 | Types.push_back(newType); |
| 2487 | return QualType(newType, 0); |
| 2488 | } |
| 2489 | |
| 2490 | // Otherwise, we actually build a new type every time, but we |
| 2491 | // also build a canonical type. |
| 2492 | |
| 2493 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 2494 | |
| 2495 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2496 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2497 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2498 | QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2499 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2500 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2501 | // Look for an existing type with these properties. |
| 2502 | DependentSizedArrayType *canonTy = |
| 2503 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2504 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2505 | // If we don't have one, build one. |
| 2506 | if (!canonTy) { |
| 2507 | canonTy = new (*this, TypeAlignment) |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2508 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2509 | QualType(), numElements, ASM, elementTypeQuals, |
| 2510 | brackets); |
| 2511 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 2512 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2515 | // Apply qualifiers from the element type to the array. |
| 2516 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2517 | canonElementType.Quals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2519 | // If we didn't need extra canonicalization for the element type, |
| 2520 | // then just use that as our result. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2521 | if (QualType(canonElementType.Ty, 0) == elementType) |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2522 | return canon; |
| 2523 | |
| 2524 | // Otherwise, we need to build a type which follows the spelling |
| 2525 | // of the element type. |
| 2526 | DependentSizedArrayType *sugaredType |
| 2527 | = new (*this, TypeAlignment) |
| 2528 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 2529 | ASM, elementTypeQuals, brackets); |
| 2530 | Types.push_back(sugaredType); |
| 2531 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2534 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2535 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2536 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2537 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2538 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2539 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2540 | void *insertPos = 0; |
| 2541 | if (IncompleteArrayType *iat = |
| 2542 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 2543 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2544 | |
| 2545 | // 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] | 2546 | // either, so fill in the canonical type field. We also have to pull |
| 2547 | // qualifiers off the element type. |
| 2548 | QualType canon; |
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 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 2551 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2552 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2553 | ASM, elementTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2554 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2555 | |
| 2556 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2557 | IncompleteArrayType *existing = |
| 2558 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 2559 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 2560 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2561 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2562 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 2563 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2564 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2565 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 2566 | Types.push_back(newType); |
| 2567 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2570 | /// getVectorType - Return the unique reference to a vector type of |
| 2571 | /// 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] | 2572 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2573 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2574 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2576 | // Check if we've already instantiated a vector of this type. |
| 2577 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2578 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2579 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2580 | void *InsertPos = 0; |
| 2581 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2582 | return QualType(VTP, 0); |
| 2583 | |
| 2584 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2585 | // so fill in the canonical type field. |
| 2586 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2587 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2588 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2589 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2590 | // Get the new insert position for the node we care about. |
| 2591 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2592 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2593 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2594 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2595 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2596 | VectorTypes.InsertNode(New, InsertPos); |
| 2597 | Types.push_back(New); |
| 2598 | return QualType(New, 0); |
| 2599 | } |
| 2600 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2601 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2602 | /// 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] | 2603 | QualType |
| 2604 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2605 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2607 | // Check if we've already instantiated a vector of this type. |
| 2608 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2609 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2610 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2611 | void *InsertPos = 0; |
| 2612 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2613 | return QualType(VTP, 0); |
| 2614 | |
| 2615 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2616 | // so fill in the canonical type field. |
| 2617 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2618 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2619 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2620 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2621 | // Get the new insert position for the node we care about. |
| 2622 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2623 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2624 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2625 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2626 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2627 | VectorTypes.InsertNode(New, InsertPos); |
| 2628 | Types.push_back(New); |
| 2629 | return QualType(New, 0); |
| 2630 | } |
| 2631 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2632 | QualType |
| 2633 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2634 | Expr *SizeExpr, |
| 2635 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2636 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2638 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2639 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2640 | void *InsertPos = 0; |
| 2641 | DependentSizedExtVectorType *Canon |
| 2642 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2643 | DependentSizedExtVectorType *New; |
| 2644 | if (Canon) { |
| 2645 | // We already have a canonical version of this array type; use it as |
| 2646 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2647 | New = new (*this, TypeAlignment) |
| 2648 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2649 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2650 | } else { |
| 2651 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2652 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2653 | New = new (*this, TypeAlignment) |
| 2654 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2655 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2656 | |
| 2657 | DependentSizedExtVectorType *CanonCheck |
| 2658 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2659 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2660 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2661 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2662 | } else { |
| 2663 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2664 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2665 | New = new (*this, TypeAlignment) |
| 2666 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2667 | } |
| 2668 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2669 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2670 | Types.push_back(New); |
| 2671 | return QualType(New, 0); |
| 2672 | } |
| 2673 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2674 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2675 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2676 | QualType |
| 2677 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2678 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2679 | const CallingConv DefaultCC = Info.getCC(); |
| 2680 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2681 | CC_X86StdCall : DefaultCC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2682 | // Unique functions, to guarantee there is only one function of a particular |
| 2683 | // structure. |
| 2684 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2685 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2686 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2687 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2688 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2689 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2690 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2691 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2692 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2693 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2694 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2695 | Canonical = |
| 2696 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2697 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2698 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2699 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2700 | FunctionNoProtoType *NewIP = |
| 2701 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2702 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2703 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2705 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2706 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2707 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2708 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2709 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2710 | return QualType(New, 0); |
| 2711 | } |
| 2712 | |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2713 | /// \brief Determine whether \p T is canonical as the result type of a function. |
| 2714 | static bool isCanonicalResultType(QualType T) { |
| 2715 | return T.isCanonical() && |
| 2716 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
| 2717 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
| 2718 | } |
| 2719 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2720 | /// getFunctionType - Return a normal function type with a typed argument |
| 2721 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2722 | QualType |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2723 | ASTContext::getFunctionType(QualType ResultTy, ArrayRef<QualType> ArgArray, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2724 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2725 | size_t NumArgs = ArgArray.size(); |
| 2726 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2727 | // Unique functions, to guarantee there is only one function of a particular |
| 2728 | // structure. |
| 2729 | llvm::FoldingSetNodeID ID; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2730 | FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI, |
| 2731 | *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2732 | |
| 2733 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2734 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2735 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2736 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2737 | |
| 2738 | // Determine whether the type being created is already canonical or not. |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2739 | bool isCanonical = |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2740 | EPI.ExceptionSpecType == EST_None && isCanonicalResultType(ResultTy) && |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2741 | !EPI.HasTrailingReturn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2742 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2743 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2744 | isCanonical = false; |
| 2745 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2746 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2747 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2748 | CC_X86StdCall : DefaultCC; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2749 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2750 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2751 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2752 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2753 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2754 | SmallVector<QualType, 16> CanonicalArgs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2755 | CanonicalArgs.reserve(NumArgs); |
| 2756 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2757 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2758 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2759 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2760 | CanonicalEPI.HasTrailingReturn = false; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2761 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2762 | CanonicalEPI.NumExceptions = 0; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2763 | CanonicalEPI.ExtInfo |
| 2764 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2765 | |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2766 | // Result types do not have ARC lifetime qualifiers. |
| 2767 | QualType CanResultTy = getCanonicalType(ResultTy); |
| 2768 | if (ResultTy.getQualifiers().hasObjCLifetime()) { |
| 2769 | Qualifiers Qs = CanResultTy.getQualifiers(); |
| 2770 | Qs.removeObjCLifetime(); |
| 2771 | CanResultTy = getQualifiedType(CanResultTy.getUnqualifiedType(), Qs); |
| 2772 | } |
| 2773 | |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2774 | Canonical = getFunctionType(CanResultTy, CanonicalArgs, CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2775 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2776 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2777 | FunctionProtoType *NewIP = |
| 2778 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2779 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2780 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2781 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2782 | // FunctionProtoType objects are allocated with extra bytes after |
| 2783 | // them for three variable size arrays at the end: |
| 2784 | // - parameter types |
| 2785 | // - exception types |
| 2786 | // - consumed-arguments flags |
| 2787 | // Instead of the exception types, there could be a noexcept |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2788 | // expression, or information used to resolve the exception |
| 2789 | // specification. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2790 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2791 | NumArgs * sizeof(QualType); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2792 | if (EPI.ExceptionSpecType == EST_Dynamic) { |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2793 | Size += EPI.NumExceptions * sizeof(QualType); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2794 | } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2795 | Size += sizeof(Expr*); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2796 | } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2797 | Size += 2 * sizeof(FunctionDecl*); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2798 | } else if (EPI.ExceptionSpecType == EST_Unevaluated) { |
| 2799 | Size += sizeof(FunctionDecl*); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2800 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2801 | if (EPI.ConsumedArguments) |
| 2802 | Size += NumArgs * sizeof(bool); |
| 2803 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2804 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2805 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2806 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2807 | new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2808 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2809 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2810 | return QualType(FTP, 0); |
| 2811 | } |
| 2812 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2813 | #ifndef NDEBUG |
| 2814 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2815 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2816 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2817 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2818 | return true; |
| 2819 | if (RD->getDescribedClassTemplate() && |
| 2820 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2821 | return true; |
| 2822 | return false; |
| 2823 | } |
| 2824 | #endif |
| 2825 | |
| 2826 | /// getInjectedClassNameType - Return the unique reference to the |
| 2827 | /// injected class name type for the specified templated declaration. |
| 2828 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2829 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2830 | assert(NeedsInjectedClassNameType(Decl)); |
| 2831 | if (Decl->TypeForDecl) { |
| 2832 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2833 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2834 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2835 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2836 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2837 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2838 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2839 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2840 | Decl->TypeForDecl = newType; |
| 2841 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2842 | } |
| 2843 | return QualType(Decl->TypeForDecl, 0); |
| 2844 | } |
| 2845 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2846 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2847 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2848 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2849 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2850 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2852 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2853 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2854 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2855 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2856 | "Template type parameter types are always available."); |
| 2857 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2858 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2859 | assert(!Record->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2860 | "struct/union has previous declaration"); |
| 2861 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2862 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2863 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2864 | assert(!Enum->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2865 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2866 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2867 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2868 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2869 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2870 | Decl->TypeForDecl = newType; |
| 2871 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2872 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2873 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2874 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2875 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2878 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2879 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2880 | QualType |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2881 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2882 | QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2883 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2884 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2885 | if (Canonical.isNull()) |
| 2886 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2887 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2888 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2889 | Decl->TypeForDecl = newType; |
| 2890 | Types.push_back(newType); |
| 2891 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2894 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2895 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2896 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2897 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2898 | if (PrevDecl->TypeForDecl) |
| 2899 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2900 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2901 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2902 | Decl->TypeForDecl = newType; |
| 2903 | Types.push_back(newType); |
| 2904 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2907 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2908 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2909 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2910 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2911 | if (PrevDecl->TypeForDecl) |
| 2912 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2913 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2914 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2915 | Decl->TypeForDecl = newType; |
| 2916 | Types.push_back(newType); |
| 2917 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2920 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2921 | QualType modifiedType, |
| 2922 | QualType equivalentType) { |
| 2923 | llvm::FoldingSetNodeID id; |
| 2924 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2925 | |
| 2926 | void *insertPos = 0; |
| 2927 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2928 | if (type) return QualType(type, 0); |
| 2929 | |
| 2930 | QualType canon = getCanonicalType(equivalentType); |
| 2931 | type = new (*this, TypeAlignment) |
| 2932 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2933 | |
| 2934 | Types.push_back(type); |
| 2935 | AttributedTypes.InsertNode(type, insertPos); |
| 2936 | |
| 2937 | return QualType(type, 0); |
| 2938 | } |
| 2939 | |
| 2940 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2941 | /// \brief Retrieve a substitution-result type. |
| 2942 | QualType |
| 2943 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2944 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2945 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2946 | && "replacement types must always be canonical"); |
| 2947 | |
| 2948 | llvm::FoldingSetNodeID ID; |
| 2949 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2950 | void *InsertPos = 0; |
| 2951 | SubstTemplateTypeParmType *SubstParm |
| 2952 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2953 | |
| 2954 | if (!SubstParm) { |
| 2955 | SubstParm = new (*this, TypeAlignment) |
| 2956 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2957 | Types.push_back(SubstParm); |
| 2958 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2959 | } |
| 2960 | |
| 2961 | return QualType(SubstParm, 0); |
| 2962 | } |
| 2963 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2964 | /// \brief Retrieve a |
| 2965 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2966 | const TemplateTypeParmType *Parm, |
| 2967 | const TemplateArgument &ArgPack) { |
| 2968 | #ifndef NDEBUG |
| 2969 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2970 | PEnd = ArgPack.pack_end(); |
| 2971 | P != PEnd; ++P) { |
| 2972 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2973 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2974 | } |
| 2975 | #endif |
| 2976 | |
| 2977 | llvm::FoldingSetNodeID ID; |
| 2978 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2979 | void *InsertPos = 0; |
| 2980 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2981 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2982 | return QualType(SubstParm, 0); |
| 2983 | |
| 2984 | QualType Canon; |
| 2985 | if (!Parm->isCanonicalUnqualified()) { |
| 2986 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2987 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2988 | ArgPack); |
| 2989 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2990 | } |
| 2991 | |
| 2992 | SubstTemplateTypeParmPackType *SubstParm |
| 2993 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2994 | ArgPack); |
| 2995 | Types.push_back(SubstParm); |
| 2996 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2997 | return QualType(SubstParm, 0); |
| 2998 | } |
| 2999 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3000 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3001 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3002 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3003 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3004 | bool ParameterPack, |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3005 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3006 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3007 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3008 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3009 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3010 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3011 | |
| 3012 | if (TypeParm) |
| 3013 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3014 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3015 | if (TTPDecl) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3016 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3017 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3018 | |
| 3019 | TemplateTypeParmType *TypeCheck |
| 3020 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3021 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 3022 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3023 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3024 | TypeParm = new (*this, TypeAlignment) |
| 3025 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3026 | |
| 3027 | Types.push_back(TypeParm); |
| 3028 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 3029 | |
| 3030 | return QualType(TypeParm, 0); |
| 3031 | } |
| 3032 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3033 | TypeSourceInfo * |
| 3034 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 3035 | SourceLocation NameLoc, |
| 3036 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3037 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3038 | assert(!Name.getAsDependentTemplateName() && |
| 3039 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3040 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3041 | |
| 3042 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3043 | TemplateSpecializationTypeLoc TL = |
| 3044 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3045 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3046 | TL.setTemplateNameLoc(NameLoc); |
| 3047 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 3048 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 3049 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 3050 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 3051 | return DI; |
| 3052 | } |
| 3053 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3055 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3056 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3057 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3058 | assert(!Template.getAsDependentTemplateName() && |
| 3059 | "No dependent template names here!"); |
| 3060 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3061 | unsigned NumArgs = Args.size(); |
| 3062 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3063 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3064 | ArgVec.reserve(NumArgs); |
| 3065 | for (unsigned i = 0; i != NumArgs; ++i) |
| 3066 | ArgVec.push_back(Args[i].getArgument()); |
| 3067 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3068 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3069 | Underlying); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3072 | #ifndef NDEBUG |
| 3073 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 3074 | unsigned NumArgs) { |
| 3075 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3076 | if (Args[I].isPackExpansion()) |
| 3077 | return true; |
| 3078 | |
| 3079 | return true; |
| 3080 | } |
| 3081 | #endif |
| 3082 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3083 | QualType |
| 3084 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3085 | const TemplateArgument *Args, |
| 3086 | unsigned NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3087 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3088 | assert(!Template.getAsDependentTemplateName() && |
| 3089 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3090 | // Look through qualified template names. |
| 3091 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3092 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3093 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3094 | bool IsTypeAlias = |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3095 | Template.getAsTemplateDecl() && |
| 3096 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3097 | QualType CanonType; |
| 3098 | if (!Underlying.isNull()) |
| 3099 | CanonType = getCanonicalType(Underlying); |
| 3100 | else { |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3101 | // We can get here with an alias template when the specialization contains |
| 3102 | // a pack expansion that does not match up with a parameter pack. |
| 3103 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 3104 | "Caller must compute aliased type"); |
| 3105 | IsTypeAlias = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3106 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 3107 | NumArgs); |
| 3108 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3109 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3110 | // Allocate the (non-canonical) template specialization type, but don't |
| 3111 | // try to unique it: these types typically have location information that |
| 3112 | // we don't unique and don't want to lose. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3113 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 3114 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3115 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3116 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | TemplateSpecializationType *Spec |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3118 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 3119 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3120 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3121 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3122 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3125 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3126 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 3127 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3128 | unsigned NumArgs) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3129 | assert(!Template.getAsDependentTemplateName() && |
| 3130 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3131 | |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3132 | // Look through qualified template names. |
| 3133 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3134 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3135 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3136 | // Build the canonical template specialization type. |
| 3137 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3138 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3139 | CanonArgs.reserve(NumArgs); |
| 3140 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3141 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 3142 | |
| 3143 | // Determine whether this canonical template specialization type already |
| 3144 | // exists. |
| 3145 | llvm::FoldingSetNodeID ID; |
| 3146 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 3147 | CanonArgs.data(), NumArgs, *this); |
| 3148 | |
| 3149 | void *InsertPos = 0; |
| 3150 | TemplateSpecializationType *Spec |
| 3151 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3152 | |
| 3153 | if (!Spec) { |
| 3154 | // Allocate a new canonical template specialization type. |
| 3155 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 3156 | sizeof(TemplateArgument) * NumArgs), |
| 3157 | TypeAlignment); |
| 3158 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 3159 | CanonArgs.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3160 | QualType(), QualType()); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3161 | Types.push_back(Spec); |
| 3162 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 3163 | } |
| 3164 | |
| 3165 | assert(Spec->isDependentType() && |
| 3166 | "Non-dependent template-id type must have a canonical type"); |
| 3167 | return QualType(Spec, 0); |
| 3168 | } |
| 3169 | |
| 3170 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3171 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 3172 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3173 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3174 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3175 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3176 | |
| 3177 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3178 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3179 | if (T) |
| 3180 | return QualType(T, 0); |
| 3181 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3182 | QualType Canon = NamedType; |
| 3183 | if (!Canon.isCanonical()) { |
| 3184 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3185 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3186 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3187 | (void)CheckT; |
| 3188 | } |
| 3189 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3190 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3191 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3192 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3193 | return QualType(T, 0); |
| 3194 | } |
| 3195 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3196 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3197 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3198 | llvm::FoldingSetNodeID ID; |
| 3199 | ParenType::Profile(ID, InnerType); |
| 3200 | |
| 3201 | void *InsertPos = 0; |
| 3202 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3203 | if (T) |
| 3204 | return QualType(T, 0); |
| 3205 | |
| 3206 | QualType Canon = InnerType; |
| 3207 | if (!Canon.isCanonical()) { |
| 3208 | Canon = getCanonicalType(InnerType); |
| 3209 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3210 | assert(!CheckT && "Paren canonical type broken"); |
| 3211 | (void)CheckT; |
| 3212 | } |
| 3213 | |
| 3214 | T = new (*this) ParenType(InnerType, Canon); |
| 3215 | Types.push_back(T); |
| 3216 | ParenTypes.InsertNode(T, InsertPos); |
| 3217 | return QualType(T, 0); |
| 3218 | } |
| 3219 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3220 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 3221 | NestedNameSpecifier *NNS, |
| 3222 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3223 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3224 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 3225 | |
| 3226 | if (Canon.isNull()) { |
| 3227 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3228 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3229 | if (Keyword == ETK_None) |
| 3230 | CanonKeyword = ETK_Typename; |
| 3231 | |
| 3232 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 3233 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3237 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3238 | |
| 3239 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3240 | DependentNameType *T |
| 3241 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3242 | if (T) |
| 3243 | return QualType(T, 0); |
| 3244 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3245 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3246 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3247 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3251 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3252 | ASTContext::getDependentTemplateSpecializationType( |
| 3253 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3254 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3255 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3256 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3257 | // TODO: avoid this copy |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3258 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3259 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 3260 | ArgCopy.push_back(Args[I].getArgument()); |
| 3261 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 3262 | ArgCopy.size(), |
| 3263 | ArgCopy.data()); |
| 3264 | } |
| 3265 | |
| 3266 | QualType |
| 3267 | ASTContext::getDependentTemplateSpecializationType( |
| 3268 | ElaboratedTypeKeyword Keyword, |
| 3269 | NestedNameSpecifier *NNS, |
| 3270 | const IdentifierInfo *Name, |
| 3271 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3272 | const TemplateArgument *Args) const { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3273 | assert((!NNS || NNS->isDependent()) && |
| 3274 | "nested-name-specifier must be dependent"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3275 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3276 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3277 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 3278 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3279 | |
| 3280 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3281 | DependentTemplateSpecializationType *T |
| 3282 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3283 | if (T) |
| 3284 | return QualType(T, 0); |
| 3285 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3286 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3287 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3288 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3289 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 3290 | |
| 3291 | bool AnyNonCanonArgs = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3292 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3293 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 3294 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 3295 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 3296 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3299 | QualType Canon; |
| 3300 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 3301 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 3302 | Name, NumArgs, |
| 3303 | CanonArgs.data()); |
| 3304 | |
| 3305 | // Find the insert position again. |
| 3306 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3307 | } |
| 3308 | |
| 3309 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 3310 | sizeof(TemplateArgument) * NumArgs), |
| 3311 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 3312 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3313 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3314 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3315 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3319 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3320 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3321 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3322 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3323 | |
| 3324 | assert(Pattern->containsUnexpandedParameterPack() && |
| 3325 | "Pack expansions must expand one or more parameter packs"); |
| 3326 | void *InsertPos = 0; |
| 3327 | PackExpansionType *T |
| 3328 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3329 | if (T) |
| 3330 | return QualType(T, 0); |
| 3331 | |
| 3332 | QualType Canon; |
| 3333 | if (!Pattern.isCanonical()) { |
Richard Smith | d8672ef | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3334 | Canon = getCanonicalType(Pattern); |
| 3335 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 3336 | // contains an alias template specialization which ignores one of its |
| 3337 | // parameters. |
| 3338 | if (Canon->containsUnexpandedParameterPack()) { |
| 3339 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3340 | |
Richard Smith | d8672ef | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3341 | // Find the insert position again, in case we inserted an element into |
| 3342 | // PackExpansionTypes and invalidated our insert position. |
| 3343 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3344 | } |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3347 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3348 | Types.push_back(T); |
| 3349 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 3350 | return QualType(T, 0); |
| 3351 | } |
| 3352 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3353 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 3354 | /// alphabetically. |
| 3355 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 3356 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3357 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3360 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3361 | unsigned NumProtocols) { |
| 3362 | if (NumProtocols == 0) return true; |
| 3363 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3364 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 3365 | return false; |
| 3366 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3367 | for (unsigned i = 1; i != NumProtocols; ++i) |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3368 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) || |
| 3369 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3370 | return false; |
| 3371 | return true; |
| 3372 | } |
| 3373 | |
| 3374 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3375 | unsigned &NumProtocols) { |
| 3376 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3377 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3378 | // Sort protocols, keyed by name. |
| 3379 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 3380 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3381 | // Canonicalize. |
| 3382 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 3383 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 3384 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3385 | // Remove duplicates. |
| 3386 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 3387 | NumProtocols = ProtocolsEnd-Protocols; |
| 3388 | } |
| 3389 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3390 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 3391 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3392 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3393 | // If the base type is an interface and there aren't any protocols |
| 3394 | // to add, then the interface type will do just fine. |
| 3395 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 3396 | return BaseType; |
| 3397 | |
| 3398 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3399 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3400 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3401 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3402 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3403 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3404 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3405 | // Build the canonical type, which has the canonical base type and |
| 3406 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3407 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3408 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 3409 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 3410 | if (!ProtocolsSorted) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3411 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3412 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3413 | unsigned UniqueCount = NumProtocols; |
| 3414 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3415 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3416 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3417 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3418 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3419 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3420 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3424 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3425 | } |
| 3426 | |
| 3427 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 3428 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 3429 | void *Mem = Allocate(Size, TypeAlignment); |
| 3430 | ObjCObjectTypeImpl *T = |
| 3431 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 3432 | |
| 3433 | Types.push_back(T); |
| 3434 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 3435 | return QualType(T, 0); |
| 3436 | } |
| 3437 | |
| 3438 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 3439 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3440 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3441 | llvm::FoldingSetNodeID ID; |
| 3442 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 3443 | |
| 3444 | void *InsertPos = 0; |
| 3445 | if (ObjCObjectPointerType *QT = |
| 3446 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3447 | return QualType(QT, 0); |
| 3448 | |
| 3449 | // Find the canonical object type. |
| 3450 | QualType Canonical; |
| 3451 | if (!ObjectT.isCanonical()) { |
| 3452 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 3453 | |
| 3454 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3455 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3456 | } |
| 3457 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 3458 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3459 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 3460 | ObjCObjectPointerType *QType = |
| 3461 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3462 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3463 | Types.push_back(QType); |
| 3464 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3465 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3466 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3467 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3468 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 3469 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3470 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 3471 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3472 | if (Decl->TypeForDecl) |
| 3473 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3474 | |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3475 | if (PrevDecl) { |
| 3476 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 3477 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3478 | return QualType(PrevDecl->TypeForDecl, 0); |
| 3479 | } |
| 3480 | |
Douglas Gregor | 8d2dbbf | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 3481 | // Prefer the definition, if there is one. |
| 3482 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 3483 | Decl = Def; |
| 3484 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3485 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 3486 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 3487 | Decl->TypeForDecl = T; |
| 3488 | Types.push_back(T); |
| 3489 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3492 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 3493 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3494 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3495 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3496 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3497 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3498 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3499 | if (tofExpr->isTypeDependent()) { |
| 3500 | llvm::FoldingSetNodeID ID; |
| 3501 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3502 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3503 | void *InsertPos = 0; |
| 3504 | DependentTypeOfExprType *Canon |
| 3505 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3506 | if (Canon) { |
| 3507 | // We already have a "canonical" version of an identical, dependent |
| 3508 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3509 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3510 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3511 | } else { |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3512 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3513 | Canon |
| 3514 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3515 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 3516 | toe = Canon; |
| 3517 | } |
| 3518 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3519 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3520 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3521 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3522 | Types.push_back(toe); |
| 3523 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3526 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 3527 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 3528 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3530 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3531 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3532 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3533 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3534 | Types.push_back(tot); |
| 3535 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 3538 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3539 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 3540 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 3541 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3542 | /// an issue. This doesn't effect the type checker, since it operates |
David Blaikie | 39e0203 | 2011-11-06 22:28:03 +0000 | [diff] [blame] | 3543 | /// on canonical types (which are always unique). |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3544 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3545 | DecltypeType *dt; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3546 | |
| 3547 | // C++0x [temp.type]p2: |
| 3548 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 3549 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 3550 | // type only if their expressions are equivalent (14.5.6.1). |
| 3551 | if (e->isInstantiationDependent()) { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3552 | llvm::FoldingSetNodeID ID; |
| 3553 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3554 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3555 | void *InsertPos = 0; |
| 3556 | DependentDecltypeType *Canon |
| 3557 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3558 | if (Canon) { |
| 3559 | // We already have a "canonical" version of an equivalent, dependent |
| 3560 | // decltype type. Use that as our canonical type. |
Richard Smith | 0d72910 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 3561 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3562 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3563 | } else { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3564 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3565 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3566 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 3567 | dt = Canon; |
| 3568 | } |
| 3569 | } else { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3570 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
| 3571 | getCanonicalType(UnderlyingType)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3572 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3573 | Types.push_back(dt); |
| 3574 | return QualType(dt, 0); |
| 3575 | } |
| 3576 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3577 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 3578 | /// savings are minimal and these are rare. |
| 3579 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 3580 | QualType UnderlyingType, |
| 3581 | UnaryTransformType::UTTKind Kind) |
| 3582 | const { |
| 3583 | UnaryTransformType *Ty = |
Douglas Gregor | 69d9775 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 3584 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 3585 | Kind, |
| 3586 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 12fc4b0 | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 3587 | QualType() : getCanonicalType(UnderlyingType)); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3588 | Types.push_back(Ty); |
| 3589 | return QualType(Ty, 0); |
| 3590 | } |
| 3591 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3592 | /// getAutoType - Return the uniqued reference to the 'auto' type which has been |
| 3593 | /// deduced to the given type, or to the canonical undeduced 'auto' type, or the |
| 3594 | /// canonical deduced-but-dependent 'auto' type. |
| 3595 | QualType ASTContext::getAutoType(QualType DeducedType, bool IsDecltypeAuto, |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3596 | bool IsDependent) const { |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3597 | if (DeducedType.isNull() && !IsDecltypeAuto && !IsDependent) |
| 3598 | return getAutoDeductType(); |
| 3599 | |
| 3600 | // Look in the folding set for an existing type. |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3601 | void *InsertPos = 0; |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3602 | llvm::FoldingSetNodeID ID; |
| 3603 | AutoType::Profile(ID, DeducedType, IsDecltypeAuto, IsDependent); |
| 3604 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3605 | return QualType(AT, 0); |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3606 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3607 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType, |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3608 | IsDecltypeAuto, |
| 3609 | IsDependent); |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3610 | Types.push_back(AT); |
| 3611 | if (InsertPos) |
| 3612 | AutoTypes.InsertNode(AT, InsertPos); |
| 3613 | return QualType(AT, 0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3614 | } |
| 3615 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3616 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3617 | /// the given value type. |
| 3618 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3619 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3620 | // structure. |
| 3621 | llvm::FoldingSetNodeID ID; |
| 3622 | AtomicType::Profile(ID, T); |
| 3623 | |
| 3624 | void *InsertPos = 0; |
| 3625 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3626 | return QualType(AT, 0); |
| 3627 | |
| 3628 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3629 | // either, so fill in the canonical type field. |
| 3630 | QualType Canonical; |
| 3631 | if (!T.isCanonical()) { |
| 3632 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3633 | |
| 3634 | // Get the new insert position for the node we care about. |
| 3635 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3636 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 3637 | } |
| 3638 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3639 | Types.push_back(New); |
| 3640 | AtomicTypes.InsertNode(New, InsertPos); |
| 3641 | return QualType(New, 0); |
| 3642 | } |
| 3643 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3644 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3645 | QualType ASTContext::getAutoDeductType() const { |
| 3646 | if (AutoDeductTy.isNull()) |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3647 | AutoDeductTy = QualType( |
| 3648 | new (*this, TypeAlignment) AutoType(QualType(), /*decltype(auto)*/false, |
| 3649 | /*dependent*/false), |
| 3650 | 0); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3651 | return AutoDeductTy; |
| 3652 | } |
| 3653 | |
| 3654 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3655 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3656 | if (AutoRRefDeductTy.isNull()) |
| 3657 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3658 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3659 | return AutoRRefDeductTy; |
| 3660 | } |
| 3661 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3662 | /// getTagDeclType - Return the unique reference to the type for the |
| 3663 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3664 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3665 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3666 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3667 | // away const? mutable? |
| 3668 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3671 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3672 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3673 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3674 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3675 | return getFromTargetType(Target->getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3678 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3679 | CanQualType ASTContext::getIntMaxType() const { |
| 3680 | return getFromTargetType(Target->getIntMaxType()); |
| 3681 | } |
| 3682 | |
| 3683 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3684 | CanQualType ASTContext::getUIntMaxType() const { |
| 3685 | return getFromTargetType(Target->getUIntMaxType()); |
| 3686 | } |
| 3687 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3688 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3689 | /// Used when in C++, as a GCC extension. |
| 3690 | QualType ASTContext::getSignedWCharType() const { |
| 3691 | // FIXME: derive from "Target" ? |
| 3692 | return WCharTy; |
| 3693 | } |
| 3694 | |
| 3695 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3696 | /// Used when in C++, as a GCC extension. |
| 3697 | QualType ASTContext::getUnsignedWCharType() const { |
| 3698 | // FIXME: derive from "Target" ? |
| 3699 | return UnsignedIntTy; |
| 3700 | } |
| 3701 | |
Enea Zaffanella | 9677eb8 | 2013-01-26 17:08:37 +0000 | [diff] [blame] | 3702 | QualType ASTContext::getIntPtrType() const { |
| 3703 | return getFromTargetType(Target->getIntPtrType()); |
| 3704 | } |
| 3705 | |
| 3706 | QualType ASTContext::getUIntPtrType() const { |
| 3707 | return getCorrespondingUnsignedType(getIntPtrType()); |
| 3708 | } |
| 3709 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3710 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3711 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3712 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3713 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
Eli Friedman | 6902e41 | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 3716 | /// \brief Return the unique type for "pid_t" defined in |
| 3717 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
| 3718 | QualType ASTContext::getProcessIDType() const { |
| 3719 | return getFromTargetType(Target->getProcessIDType()); |
| 3720 | } |
| 3721 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3722 | //===----------------------------------------------------------------------===// |
| 3723 | // Type Operators |
| 3724 | //===----------------------------------------------------------------------===// |
| 3725 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3726 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3727 | // Push qualifiers into arrays, and then discard any remaining |
| 3728 | // qualifiers. |
| 3729 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3730 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3731 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3732 | QualType Result; |
| 3733 | if (isa<ArrayType>(Ty)) { |
| 3734 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3735 | } else if (isa<FunctionType>(Ty)) { |
| 3736 | Result = getPointerType(QualType(Ty, 0)); |
| 3737 | } else { |
| 3738 | Result = QualType(Ty, 0); |
| 3739 | } |
| 3740 | |
| 3741 | return CanQualType::CreateUnsafe(Result); |
| 3742 | } |
| 3743 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3744 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3745 | Qualifiers &quals) { |
| 3746 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3747 | |
| 3748 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3749 | // the unqualified desugared type and then drops it on the floor. |
| 3750 | // We then have to strip that sugar back off with |
| 3751 | // getUnqualifiedDesugaredType(), which is silly. |
| 3752 | const ArrayType *AT = |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3753 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3754 | |
| 3755 | // 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] | 3756 | if (!AT) { |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3757 | quals = splitType.Quals; |
| 3758 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3759 | } |
| 3760 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3761 | // Otherwise, recurse on the array's element type. |
| 3762 | QualType elementType = AT->getElementType(); |
| 3763 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3764 | |
| 3765 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3766 | // can just use the results in splitType. |
| 3767 | if (elementType == unqualElementType) { |
| 3768 | assert(quals.empty()); // from the recursive call |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3769 | quals = splitType.Quals; |
| 3770 | return QualType(splitType.Ty, 0); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3774 | // build the type back up. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3775 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3776 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3777 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3778 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3779 | CAT->getSizeModifier(), 0); |
| 3780 | } |
| 3781 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3782 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3783 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3786 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3787 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3788 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3789 | VAT->getSizeModifier(), |
| 3790 | VAT->getIndexTypeCVRQualifiers(), |
| 3791 | VAT->getBracketsRange()); |
| 3792 | } |
| 3793 | |
| 3794 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3795 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3796 | DSAT->getSizeModifier(), 0, |
| 3797 | SourceRange()); |
| 3798 | } |
| 3799 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3800 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3801 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3802 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3803 | /// or pointer-to-member types, or if they are not similar at this |
| 3804 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3805 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3806 | /// be called in a loop that successively "unwraps" pointer and |
| 3807 | /// pointer-to-member types to compare them at each level. |
| 3808 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3809 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3810 | *T2PtrType = T2->getAs<PointerType>(); |
| 3811 | if (T1PtrType && T2PtrType) { |
| 3812 | T1 = T1PtrType->getPointeeType(); |
| 3813 | T2 = T2PtrType->getPointeeType(); |
| 3814 | return true; |
| 3815 | } |
| 3816 | |
| 3817 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3818 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3819 | if (T1MPType && T2MPType && |
| 3820 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3821 | QualType(T2MPType->getClass(), 0))) { |
| 3822 | T1 = T1MPType->getPointeeType(); |
| 3823 | T2 = T2MPType->getPointeeType(); |
| 3824 | return true; |
| 3825 | } |
| 3826 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3827 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3828 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3829 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3830 | if (T1OPType && T2OPType) { |
| 3831 | T1 = T1OPType->getPointeeType(); |
| 3832 | T2 = T2OPType->getPointeeType(); |
| 3833 | return true; |
| 3834 | } |
| 3835 | } |
| 3836 | |
| 3837 | // FIXME: Block pointers, too? |
| 3838 | |
| 3839 | return false; |
| 3840 | } |
| 3841 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3842 | DeclarationNameInfo |
| 3843 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3844 | SourceLocation NameLoc) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3845 | switch (Name.getKind()) { |
| 3846 | case TemplateName::QualifiedTemplate: |
| 3847 | case TemplateName::Template: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3848 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3849 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3850 | NameLoc); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3851 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3852 | case TemplateName::OverloadedTemplate: { |
| 3853 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3854 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3855 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3856 | } |
| 3857 | |
| 3858 | case TemplateName::DependentTemplate: { |
| 3859 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3860 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3861 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3862 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3863 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3864 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3865 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3866 | // DNInfo work in progress: FIXME: source locations? |
| 3867 | DeclarationNameLoc DNLoc; |
| 3868 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3869 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3870 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3871 | } |
| 3872 | } |
| 3873 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3874 | case TemplateName::SubstTemplateTemplateParm: { |
| 3875 | SubstTemplateTemplateParmStorage *subst |
| 3876 | = Name.getAsSubstTemplateTemplateParm(); |
| 3877 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3878 | NameLoc); |
| 3879 | } |
| 3880 | |
| 3881 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3882 | SubstTemplateTemplateParmPackStorage *subst |
| 3883 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3884 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3885 | NameLoc); |
| 3886 | } |
| 3887 | } |
| 3888 | |
| 3889 | llvm_unreachable("bad template name kind!"); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3892 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3893 | switch (Name.getKind()) { |
| 3894 | case TemplateName::QualifiedTemplate: |
| 3895 | case TemplateName::Template: { |
| 3896 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3897 | if (TemplateTemplateParmDecl *TTP |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3898 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3899 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3900 | |
| 3901 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3902 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3903 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3904 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3905 | case TemplateName::OverloadedTemplate: |
| 3906 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3907 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3908 | case TemplateName::DependentTemplate: { |
| 3909 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3910 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3911 | return DTN->CanonicalTemplateName; |
| 3912 | } |
| 3913 | |
| 3914 | case TemplateName::SubstTemplateTemplateParm: { |
| 3915 | SubstTemplateTemplateParmStorage *subst |
| 3916 | = Name.getAsSubstTemplateTemplateParm(); |
| 3917 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3918 | } |
| 3919 | |
| 3920 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3921 | SubstTemplateTemplateParmPackStorage *subst |
| 3922 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3923 | TemplateTemplateParmDecl *canonParameter |
| 3924 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3925 | TemplateArgument canonArgPack |
| 3926 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3927 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3928 | } |
| 3929 | } |
| 3930 | |
| 3931 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3934 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3935 | X = getCanonicalTemplateName(X); |
| 3936 | Y = getCanonicalTemplateName(Y); |
| 3937 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3938 | } |
| 3939 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3940 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3941 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3942 | switch (Arg.getKind()) { |
| 3943 | case TemplateArgument::Null: |
| 3944 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3945 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3946 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3947 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3948 | |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3949 | case TemplateArgument::Declaration: { |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3950 | ValueDecl *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
| 3951 | return TemplateArgument(D, Arg.isDeclForReferenceParam()); |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3952 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3953 | |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3954 | case TemplateArgument::NullPtr: |
| 3955 | return TemplateArgument(getCanonicalType(Arg.getNullPtrType()), |
| 3956 | /*isNullPtr*/true); |
| 3957 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3958 | case TemplateArgument::Template: |
| 3959 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3960 | |
| 3961 | case TemplateArgument::TemplateExpansion: |
| 3962 | return TemplateArgument(getCanonicalTemplateName( |
| 3963 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3964 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3965 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3966 | case TemplateArgument::Integral: |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 3967 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3968 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3969 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3970 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3971 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3972 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3973 | if (Arg.pack_size() == 0) |
| 3974 | return Arg; |
| 3975 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3976 | TemplateArgument *CanonArgs |
| 3977 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3978 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3979 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3980 | AEnd = Arg.pack_end(); |
| 3981 | A != AEnd; (void)++A, ++Idx) |
| 3982 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3983 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3984 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3985 | } |
| 3986 | } |
| 3987 | |
| 3988 | // Silence GCC warning |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3989 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3990 | } |
| 3991 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3992 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3993 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3994 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3995 | return 0; |
| 3996 | |
| 3997 | switch (NNS->getKind()) { |
| 3998 | case NestedNameSpecifier::Identifier: |
| 3999 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4001 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 4002 | NNS->getAsIdentifier()); |
| 4003 | |
| 4004 | case NestedNameSpecifier::Namespace: |
| 4005 | // A namespace is canonical; build a nested-name-specifier with |
| 4006 | // this namespace and no prefix. |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4007 | return NestedNameSpecifier::Create(*this, 0, |
| 4008 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 4009 | |
| 4010 | case NestedNameSpecifier::NamespaceAlias: |
| 4011 | // A namespace is canonical; build a nested-name-specifier with |
| 4012 | // this namespace and no prefix. |
| 4013 | return NestedNameSpecifier::Create(*this, 0, |
| 4014 | NNS->getAsNamespaceAlias()->getNamespace() |
| 4015 | ->getOriginalNamespace()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4016 | |
| 4017 | case NestedNameSpecifier::TypeSpec: |
| 4018 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 4019 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4020 | |
| 4021 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 4022 | // break it apart into its prefix and identifier, then reconsititute those |
| 4023 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 4024 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 4025 | // types, e.g., |
| 4026 | // typedef typename T::type T1; |
| 4027 | // typedef typename T1::type T2; |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 4028 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 4029 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4030 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4031 | |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 4032 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 4033 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 4034 | // first place? |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4035 | return NestedNameSpecifier::Create(*this, 0, false, |
| 4036 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | case NestedNameSpecifier::Global: |
| 4040 | // The global specifier is canonical and unique. |
| 4041 | return NNS; |
| 4042 | } |
| 4043 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4044 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4047 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4048 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4049 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4050 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4051 | // Handle the common positive case fast. |
| 4052 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 4053 | return AT; |
| 4054 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4055 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4056 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4057 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4058 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4059 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4060 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4061 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 4062 | // 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] | 4063 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4064 | // If we get here, we either have type qualifiers on the type, or we have |
| 4065 | // 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] | 4066 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4067 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4068 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4069 | Qualifiers qs = split.Quals; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4070 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4071 | // If we have a simple case, just return now. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4072 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4073 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4074 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4075 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4076 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 4077 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4078 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4079 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4080 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 4081 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 4082 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4083 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4084 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 4085 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 4086 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4087 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4088 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4089 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4090 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 4091 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4092 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4093 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4094 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4095 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4096 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4097 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4098 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4099 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4100 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4101 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4102 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4103 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
Abramo Bagnara | ad9689f | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4106 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4107 | // C99 6.7.5.3p7: |
| 4108 | // A declaration of a parameter as "array of type" shall be |
| 4109 | // adjusted to "qualified pointer to type", where the type |
| 4110 | // qualifiers (if any) are those specified within the [ and ] of |
| 4111 | // the array type derivation. |
| 4112 | if (T->isArrayType()) |
| 4113 | return getArrayDecayedType(T); |
| 4114 | |
| 4115 | // C99 6.7.5.3p8: |
| 4116 | // A declaration of a parameter as "function returning type" |
| 4117 | // shall be adjusted to "pointer to function returning type", as |
| 4118 | // in 6.3.2.1. |
| 4119 | if (T->isFunctionType()) |
| 4120 | return getPointerType(T); |
| 4121 | |
| 4122 | return T; |
| 4123 | } |
| 4124 | |
Abramo Bagnara | ad9689f | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4125 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4126 | T = getVariableArrayDecayedType(T); |
| 4127 | T = getAdjustedParameterType(T); |
| 4128 | return T.getUnqualifiedType(); |
| 4129 | } |
| 4130 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4131 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 4132 | /// specified array type to a pointer. This operation is non-trivial when |
| 4133 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 4134 | /// this returns a pointer to a properly qualified element of the array. |
| 4135 | /// |
| 4136 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4137 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4138 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 4139 | // typedefs in the element type of the array. This also handles propagation |
| 4140 | // of type qualifiers from the array type into the element type if present |
| 4141 | // (C99 6.7.3p8). |
| 4142 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 4143 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4144 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4145 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4146 | |
| 4147 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4148 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4151 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 4152 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4155 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 4156 | Qualifiers qs; |
| 4157 | while (true) { |
| 4158 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4159 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4160 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4161 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4162 | type = array->getElementType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4163 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4164 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4165 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4166 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4169 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4170 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4171 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 4172 | uint64_t ElementCount = 1; |
| 4173 | do { |
| 4174 | ElementCount *= CA->getSize().getZExtValue(); |
Richard Smith | d5e8394 | 2012-12-06 03:04:50 +0000 | [diff] [blame] | 4175 | CA = dyn_cast_or_null<ConstantArrayType>( |
| 4176 | CA->getElementType()->getAsArrayTypeUnsafe()); |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4177 | } while (CA); |
| 4178 | return ElementCount; |
| 4179 | } |
| 4180 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4181 | /// getFloatingRank - Return a relative rank for floating point types. |
| 4182 | /// 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] | 4183 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4184 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4185 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4186 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4187 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 4188 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4189 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4190 | case BuiltinType::Half: return HalfRank; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4191 | case BuiltinType::Float: return FloatRank; |
| 4192 | case BuiltinType::Double: return DoubleRank; |
| 4193 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 4194 | } |
| 4195 | } |
| 4196 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4197 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 4198 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 4199 | /// 'typeDomain' is a real floating point or complex type. |
| 4200 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4201 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 4202 | QualType Domain) const { |
| 4203 | FloatingRank EltRank = getFloatingRank(Size); |
| 4204 | if (Domain->isComplexType()) { |
| 4205 | switch (EltRank) { |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4206 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4207 | case FloatRank: return FloatComplexTy; |
| 4208 | case DoubleRank: return DoubleComplexTy; |
| 4209 | case LongDoubleRank: return LongDoubleComplexTy; |
| 4210 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4211 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4212 | |
| 4213 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 4214 | switch (EltRank) { |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 4215 | case HalfRank: return HalfTy; |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4216 | case FloatRank: return FloatTy; |
| 4217 | case DoubleRank: return DoubleTy; |
| 4218 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4219 | } |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4220 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4221 | } |
| 4222 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4223 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 4224 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 4225 | /// '_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] | 4226 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4227 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4228 | FloatingRank LHSR = getFloatingRank(LHS); |
| 4229 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4230 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4231 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4232 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4233 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4234 | return 1; |
| 4235 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4238 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 4239 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 4240 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4241 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4242 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4243 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4244 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4245 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4246 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4247 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4248 | case BuiltinType::Char_S: |
| 4249 | case BuiltinType::Char_U: |
| 4250 | case BuiltinType::SChar: |
| 4251 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4252 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4253 | case BuiltinType::Short: |
| 4254 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4255 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4256 | case BuiltinType::Int: |
| 4257 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4258 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4259 | case BuiltinType::Long: |
| 4260 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4261 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4262 | case BuiltinType::LongLong: |
| 4263 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4264 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4265 | case BuiltinType::Int128: |
| 4266 | case BuiltinType::UInt128: |
| 4267 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4268 | } |
| 4269 | } |
| 4270 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4271 | /// \brief Whether this is a promotable bitfield reference according |
| 4272 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 4273 | /// |
| 4274 | /// \returns the type this bit-field will promote to, or NULL if no |
| 4275 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4276 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 4277 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4278 | return QualType(); |
| 4279 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4280 | FieldDecl *Field = E->getBitField(); |
| 4281 | if (!Field) |
| 4282 | return QualType(); |
| 4283 | |
| 4284 | QualType FT = Field->getType(); |
| 4285 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4286 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4287 | uint64_t IntSize = getTypeSize(IntTy); |
| 4288 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 4289 | // to the size of int, it gets promoted no matter what its type is. |
| 4290 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 4291 | if (BitWidth < IntSize) |
| 4292 | return IntTy; |
| 4293 | |
| 4294 | if (BitWidth == IntSize) |
| 4295 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 4296 | |
| 4297 | // Types bigger than int are not subject to promotions, and therefore act |
| 4298 | // like the base type. |
| 4299 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 4300 | // is ridiculous. |
| 4301 | return QualType(); |
| 4302 | } |
| 4303 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4304 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 4305 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 4306 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4307 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4308 | assert(!Promotable.isNull()); |
| 4309 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4310 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 4311 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 68a2dc4 | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 4312 | |
| 4313 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 4314 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 4315 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 4316 | // types that can represent all the values of its underlying type: |
| 4317 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 4318 | // unsigned long long int [...] |
| 4319 | // FIXME: Is there some better way to compute this? |
| 4320 | if (BT->getKind() == BuiltinType::WChar_S || |
| 4321 | BT->getKind() == BuiltinType::WChar_U || |
| 4322 | BT->getKind() == BuiltinType::Char16 || |
| 4323 | BT->getKind() == BuiltinType::Char32) { |
| 4324 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 4325 | uint64_t FromSize = getTypeSize(BT); |
| 4326 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 4327 | LongLongTy, UnsignedLongLongTy }; |
| 4328 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 4329 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 4330 | if (FromSize < ToSize || |
| 4331 | (FromSize == ToSize && |
| 4332 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 4333 | return PromoteTypes[Idx]; |
| 4334 | } |
| 4335 | llvm_unreachable("char type should fit into long long"); |
| 4336 | } |
| 4337 | } |
| 4338 | |
| 4339 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4340 | if (Promotable->isSignedIntegerType()) |
| 4341 | return IntTy; |
Eli Friedman | 5b64e77 | 2012-11-15 01:21:59 +0000 | [diff] [blame] | 4342 | uint64_t PromotableSize = getIntWidth(Promotable); |
| 4343 | uint64_t IntSize = getIntWidth(IntTy); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4344 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 4345 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 4346 | } |
| 4347 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4348 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 4349 | /// type and returns its ownership. |
| 4350 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 4351 | while (!T.isNull()) { |
| 4352 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 4353 | return T.getObjCLifetime(); |
| 4354 | if (T->isArrayType()) |
| 4355 | T = getBaseElementType(T); |
| 4356 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 4357 | T = PT->getPointeeType(); |
| 4358 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 28445f0 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 4359 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4360 | else |
| 4361 | break; |
| 4362 | } |
| 4363 | |
| 4364 | return Qualifiers::OCL_None; |
| 4365 | } |
| 4366 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4367 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4368 | /// 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] | 4369 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4370 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4371 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 4372 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4373 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4374 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4375 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 4376 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4377 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4378 | unsigned LHSRank = getIntegerRank(LHSC); |
| 4379 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4380 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4381 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 4382 | if (LHSRank == RHSRank) return 0; |
| 4383 | return LHSRank > RHSRank ? 1 : -1; |
| 4384 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4385 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4386 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 4387 | if (LHSUnsigned) { |
| 4388 | // If the unsigned [LHS] type is larger, return it. |
| 4389 | if (LHSRank >= RHSRank) |
| 4390 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4391 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4392 | // If the signed type can represent all values of the unsigned type, it |
| 4393 | // 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] | 4394 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4395 | return -1; |
| 4396 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4397 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4398 | // If the unsigned [RHS] type is larger, return it. |
| 4399 | if (RHSRank >= LHSRank) |
| 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4406 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4407 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4408 | static RecordDecl * |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4409 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 4410 | DeclContext *DC, IdentifierInfo *Id) { |
| 4411 | SourceLocation Loc; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4412 | if (Ctx.getLangOpts().CPlusPlus) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4413 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4414 | else |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4415 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4416 | } |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4417 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4418 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4419 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4420 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4421 | CFConstantStringTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4422 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4423 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4424 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4425 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4426 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4427 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4428 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4429 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4430 | // int flags; |
| 4431 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4432 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4433 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4434 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4435 | FieldTypes[3] = LongTy; |
| 4436 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4437 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4438 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4439 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4440 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4441 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4442 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4443 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4444 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4445 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4446 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4447 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4450 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4451 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4452 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4453 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 4454 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4455 | |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 4456 | QualType ASTContext::getObjCSuperType() const { |
| 4457 | if (ObjCSuperType.isNull()) { |
| 4458 | RecordDecl *ObjCSuperTypeDecl = |
| 4459 | CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get("objc_super")); |
| 4460 | TUDecl->addDecl(ObjCSuperTypeDecl); |
| 4461 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
| 4462 | } |
| 4463 | return ObjCSuperType; |
| 4464 | } |
| 4465 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4466 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4467 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4468 | assert(Rec && "Invalid CFConstantStringType"); |
| 4469 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 4470 | } |
| 4471 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4472 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4473 | if (BlockDescriptorType) |
| 4474 | return getTagDeclType(BlockDescriptorType); |
| 4475 | |
| 4476 | RecordDecl *T; |
| 4477 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4478 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4479 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4480 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4481 | |
| 4482 | QualType FieldTypes[] = { |
| 4483 | UnsignedLongTy, |
| 4484 | UnsignedLongTy, |
| 4485 | }; |
| 4486 | |
| 4487 | const char *FieldNames[] = { |
| 4488 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4489 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4490 | }; |
| 4491 | |
| 4492 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4493 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4494 | SourceLocation(), |
| 4495 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4496 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4497 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4498 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4499 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4500 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4501 | T->addDecl(Field); |
| 4502 | } |
| 4503 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4504 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4505 | |
| 4506 | BlockDescriptorType = T; |
| 4507 | |
| 4508 | return getTagDeclType(BlockDescriptorType); |
| 4509 | } |
| 4510 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4511 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4512 | if (BlockDescriptorExtendedType) |
| 4513 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4514 | |
| 4515 | RecordDecl *T; |
| 4516 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4517 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4518 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4519 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4520 | |
| 4521 | QualType FieldTypes[] = { |
| 4522 | UnsignedLongTy, |
| 4523 | UnsignedLongTy, |
| 4524 | getPointerType(VoidPtrTy), |
| 4525 | getPointerType(VoidPtrTy) |
| 4526 | }; |
| 4527 | |
| 4528 | const char *FieldNames[] = { |
| 4529 | "reserved", |
| 4530 | "Size", |
| 4531 | "CopyFuncPtr", |
| 4532 | "DestroyFuncPtr" |
| 4533 | }; |
| 4534 | |
| 4535 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4536 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4537 | SourceLocation(), |
| 4538 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4539 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4540 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4541 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4542 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4543 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4544 | T->addDecl(Field); |
| 4545 | } |
| 4546 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4547 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4548 | |
| 4549 | BlockDescriptorExtendedType = T; |
| 4550 | |
| 4551 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4552 | } |
| 4553 | |
Fariborz Jahanian | b15c898 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4554 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
| 4555 | /// requires copy/dispose. Note that this must match the logic |
| 4556 | /// in buildByrefHelpers. |
| 4557 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
| 4558 | const VarDecl *D) { |
| 4559 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
| 4560 | const Expr *copyExpr = getBlockVarCopyInits(D); |
| 4561 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
| 4562 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4563 | return true; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4564 | } |
Fariborz Jahanian | b15c898 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4565 | |
| 4566 | if (!Ty->isObjCRetainableType()) return false; |
| 4567 | |
| 4568 | Qualifiers qs = Ty.getQualifiers(); |
| 4569 | |
| 4570 | // If we have lifetime, that dominates. |
| 4571 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
| 4572 | assert(getLangOpts().ObjCAutoRefCount); |
| 4573 | |
| 4574 | switch (lifetime) { |
| 4575 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
| 4576 | |
| 4577 | // These are just bits as far as the runtime is concerned. |
| 4578 | case Qualifiers::OCL_ExplicitNone: |
| 4579 | case Qualifiers::OCL_Autoreleasing: |
| 4580 | return false; |
| 4581 | |
| 4582 | // Tell the runtime that this is ARC __weak, called by the |
| 4583 | // byref routines. |
| 4584 | case Qualifiers::OCL_Weak: |
| 4585 | // ARC __strong __block variables need to be retained. |
| 4586 | case Qualifiers::OCL_Strong: |
| 4587 | return true; |
| 4588 | } |
| 4589 | llvm_unreachable("fell out of lifetime switch!"); |
| 4590 | } |
| 4591 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
| 4592 | Ty->isObjCObjectPointerType()); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4595 | bool ASTContext::getByrefLifetime(QualType Ty, |
| 4596 | Qualifiers::ObjCLifetime &LifeTime, |
| 4597 | bool &HasByrefExtendedLayout) const { |
| 4598 | |
| 4599 | if (!getLangOpts().ObjC1 || |
| 4600 | getLangOpts().getGC() != LangOptions::NonGC) |
| 4601 | return false; |
| 4602 | |
| 4603 | HasByrefExtendedLayout = false; |
Fariborz Jahanian | 34db84f | 2012-12-11 19:58:01 +0000 | [diff] [blame] | 4604 | if (Ty->isRecordType()) { |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4605 | HasByrefExtendedLayout = true; |
| 4606 | LifeTime = Qualifiers::OCL_None; |
| 4607 | } |
| 4608 | else if (getLangOpts().ObjCAutoRefCount) |
| 4609 | LifeTime = Ty.getObjCLifetime(); |
| 4610 | // MRR. |
| 4611 | else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 4612 | LifeTime = Qualifiers::OCL_ExplicitNone; |
| 4613 | else |
| 4614 | LifeTime = Qualifiers::OCL_None; |
| 4615 | return true; |
| 4616 | } |
| 4617 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4618 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 4619 | if (!ObjCInstanceTypeDecl) |
| 4620 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 4621 | getTranslationUnitDecl(), |
| 4622 | SourceLocation(), |
| 4623 | SourceLocation(), |
| 4624 | &Idents.get("instancetype"), |
| 4625 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 4626 | return ObjCInstanceTypeDecl; |
| 4627 | } |
| 4628 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4629 | // This returns true if a type has been typedefed to BOOL: |
| 4630 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 4631 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4632 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 4633 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 4634 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4635 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4636 | return false; |
| 4637 | } |
| 4638 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4639 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4640 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4641 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4642 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4643 | return CharUnits::Zero(); |
| 4644 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4645 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4646 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4647 | // 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] | 4648 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4649 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4650 | // Treat arrays as pointers, since that's how they're passed in. |
| 4651 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4652 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4653 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4654 | } |
| 4655 | |
| 4656 | static inline |
| 4657 | std::string charUnitsToString(const CharUnits &CU) { |
| 4658 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4659 | } |
| 4660 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4661 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4662 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4663 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4664 | std::string S; |
| 4665 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4666 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4667 | QualType BlockTy = |
| 4668 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4669 | // Encode result type. |
Fariborz Jahanian | 3d145f6 | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 4670 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 06cffc0 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 4671 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, |
| 4672 | BlockTy->getAs<FunctionType>()->getResultType(), |
| 4673 | S, true /*Extended*/); |
| 4674 | else |
| 4675 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), |
| 4676 | S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4677 | // Compute size of all parameters. |
| 4678 | // Start with computing size of a pointer in number of bytes. |
| 4679 | // FIXME: There might(should) be a better way of doing this computation! |
| 4680 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4681 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4682 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 4683 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4684 | E = Decl->param_end(); PI != E; ++PI) { |
| 4685 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4686 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 075a543 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 4687 | if (sz.isZero()) |
| 4688 | continue; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4689 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4690 | ParmOffset += sz; |
| 4691 | } |
| 4692 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4693 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4694 | // Block pointer and offset. |
| 4695 | S += "@?0"; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4696 | |
| 4697 | // Argument types. |
| 4698 | ParmOffset = PtrSize; |
| 4699 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 4700 | Decl->param_end(); PI != E; ++PI) { |
| 4701 | ParmVarDecl *PVDecl = *PI; |
| 4702 | QualType PType = PVDecl->getOriginalType(); |
| 4703 | if (const ArrayType *AT = |
| 4704 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4705 | // Use array's original type only if it has known number of |
| 4706 | // elements. |
| 4707 | if (!isa<ConstantArrayType>(AT)) |
| 4708 | PType = PVDecl->getType(); |
| 4709 | } else if (PType->isFunctionType()) |
| 4710 | PType = PVDecl->getType(); |
Fariborz Jahanian | 3d145f6 | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 4711 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 06cffc0 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 4712 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType, |
| 4713 | S, true /*Extended*/); |
| 4714 | else |
| 4715 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4716 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4717 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4718 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4719 | |
| 4720 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4723 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4724 | std::string& S) { |
| 4725 | // Encode result type. |
| 4726 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4727 | CharUnits ParmOffset; |
| 4728 | // Compute size of all parameters. |
| 4729 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4730 | E = Decl->param_end(); PI != E; ++PI) { |
| 4731 | QualType PType = (*PI)->getType(); |
| 4732 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4733 | if (sz.isZero()) |
Fariborz Jahanian | 7e68ba5 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4734 | continue; |
| 4735 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4736 | assert (sz.isPositive() && |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4737 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4738 | ParmOffset += sz; |
| 4739 | } |
| 4740 | S += charUnitsToString(ParmOffset); |
| 4741 | ParmOffset = CharUnits::Zero(); |
| 4742 | |
| 4743 | // Argument types. |
| 4744 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4745 | E = Decl->param_end(); PI != E; ++PI) { |
| 4746 | ParmVarDecl *PVDecl = *PI; |
| 4747 | QualType PType = PVDecl->getOriginalType(); |
| 4748 | if (const ArrayType *AT = |
| 4749 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4750 | // Use array's original type only if it has known number of |
| 4751 | // elements. |
| 4752 | if (!isa<ConstantArrayType>(AT)) |
| 4753 | PType = PVDecl->getType(); |
| 4754 | } else if (PType->isFunctionType()) |
| 4755 | PType = PVDecl->getType(); |
| 4756 | getObjCEncodingForType(PType, S); |
| 4757 | S += charUnitsToString(ParmOffset); |
| 4758 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4759 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4760 | |
| 4761 | return false; |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4764 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 4765 | /// method parameter or return type. If Extended, include class names and |
| 4766 | /// block object types. |
| 4767 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 4768 | QualType T, std::string& S, |
| 4769 | bool Extended) const { |
| 4770 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 4771 | getObjCEncodingForTypeQualifier(QT, S); |
| 4772 | // Encode parameter type. |
| 4773 | getObjCEncodingForTypeImpl(T, S, true, true, 0, |
| 4774 | true /*OutermostType*/, |
| 4775 | false /*EncodingProperty*/, |
| 4776 | false /*StructField*/, |
| 4777 | Extended /*EncodeBlockParameters*/, |
| 4778 | Extended /*EncodeClassNames*/); |
| 4779 | } |
| 4780 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4781 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4782 | /// declaration. |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4783 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4784 | std::string& S, |
| 4785 | bool Extended) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4786 | // FIXME: This is not very efficient. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4787 | // Encode return type. |
| 4788 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 4789 | Decl->getResultType(), S, Extended); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4790 | // Compute size of all parameters. |
| 4791 | // Start with computing size of a pointer in number of bytes. |
| 4792 | // FIXME: There might(should) be a better way of doing this computation! |
| 4793 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4794 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4795 | // The first two arguments (self and _cmd) are pointers; account for |
| 4796 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4797 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4798 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4799 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4800 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4801 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4802 | if (sz.isZero()) |
Fariborz Jahanian | 7e68ba5 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4803 | continue; |
| 4804 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4805 | assert (sz.isPositive() && |
| 4806 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4807 | ParmOffset += sz; |
| 4808 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4809 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4810 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4811 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4812 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4813 | // Argument types. |
| 4814 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4815 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4816 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4817 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4818 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4819 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4820 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4821 | // Use array's original type only if it has known number of |
| 4822 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4823 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4824 | PType = PVDecl->getType(); |
| 4825 | } else if (PType->isFunctionType()) |
| 4826 | PType = PVDecl->getType(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4827 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 4828 | PType, S, Extended); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4829 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4830 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4831 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4832 | |
| 4833 | return false; |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4836 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4837 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4838 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4839 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4840 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4841 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4842 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4843 | /// encoded as single characters, followed by an identifier. Property types |
| 4844 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4845 | /// these attributes are defined by the following enumeration: |
| 4846 | /// @code |
| 4847 | /// enum PropertyAttributes { |
| 4848 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4849 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4850 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4851 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4852 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4853 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4854 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 0d4cb85 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 4855 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4856 | /// kPropertyWeak = 'W' // 'weak' property |
| 4857 | /// kPropertyStrong = 'P' // property GC'able |
| 4858 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4859 | /// }; |
| 4860 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4861 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4862 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4863 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4864 | // Collect information from the property implementation decl(s). |
| 4865 | bool Dynamic = false; |
| 4866 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4867 | |
| 4868 | // FIXME: Duplicated code due to poor abstraction. |
| 4869 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4870 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4871 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4872 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4873 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4874 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4875 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4876 | if (PID->getPropertyDecl() == PD) { |
| 4877 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4878 | Dynamic = true; |
| 4879 | } else { |
| 4880 | SynthesizePID = PID; |
| 4881 | } |
| 4882 | } |
| 4883 | } |
| 4884 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4885 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4886 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4887 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4888 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4889 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4890 | if (PID->getPropertyDecl() == PD) { |
| 4891 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4892 | Dynamic = true; |
| 4893 | } else { |
| 4894 | SynthesizePID = PID; |
| 4895 | } |
| 4896 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4897 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4898 | } |
| 4899 | } |
| 4900 | |
| 4901 | // FIXME: This is not very efficient. |
| 4902 | S = "T"; |
| 4903 | |
| 4904 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4905 | // GCC has some special rules regarding encoding of properties which |
| 4906 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4907 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4908 | true /* outermost type */, |
| 4909 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4910 | |
| 4911 | if (PD->isReadOnly()) { |
| 4912 | S += ",R"; |
| 4913 | } else { |
| 4914 | switch (PD->getSetterKind()) { |
| 4915 | case ObjCPropertyDecl::Assign: break; |
| 4916 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4917 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 3a02b44 | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4918 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4919 | } |
| 4920 | } |
| 4921 | |
| 4922 | // It really isn't clear at all what this means, since properties |
| 4923 | // are "dynamic by default". |
| 4924 | if (Dynamic) |
| 4925 | S += ",D"; |
| 4926 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4927 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4928 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4929 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4930 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4931 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4932 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4936 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4937 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4938 | } |
| 4939 | |
| 4940 | if (SynthesizePID) { |
| 4941 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4942 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4943 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | // FIXME: OBJCGC: weak & strong |
| 4947 | } |
| 4948 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4949 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4950 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4951 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4952 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4953 | /// |
| 4954 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4955 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4956 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4957 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4958 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4959 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4960 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4961 | PointeeTy = IntTy; |
| 4962 | } |
| 4963 | } |
| 4964 | } |
| 4965 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4966 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4967 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4968 | // We follow the behavior of gcc, expanding structures which are |
| 4969 | // directly pointed to, and expanding embedded structures. Note that |
| 4970 | // these rules are sufficient to prevent recursive encoding of the |
| 4971 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4972 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4973 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4974 | } |
| 4975 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4976 | static char getObjCEncodingForPrimitiveKind(const ASTContext *C, |
| 4977 | BuiltinType::Kind kind) { |
| 4978 | switch (kind) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4979 | case BuiltinType::Void: return 'v'; |
| 4980 | case BuiltinType::Bool: return 'B'; |
| 4981 | case BuiltinType::Char_U: |
| 4982 | case BuiltinType::UChar: return 'C'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4983 | case BuiltinType::Char16: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4984 | case BuiltinType::UShort: return 'S'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4985 | case BuiltinType::Char32: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4986 | case BuiltinType::UInt: return 'I'; |
| 4987 | case BuiltinType::ULong: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4988 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4989 | case BuiltinType::UInt128: return 'T'; |
| 4990 | case BuiltinType::ULongLong: return 'Q'; |
| 4991 | case BuiltinType::Char_S: |
| 4992 | case BuiltinType::SChar: return 'c'; |
| 4993 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4994 | case BuiltinType::WChar_S: |
| 4995 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4996 | case BuiltinType::Int: return 'i'; |
| 4997 | case BuiltinType::Long: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4998 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4999 | case BuiltinType::LongLong: return 'q'; |
| 5000 | case BuiltinType::Int128: return 't'; |
| 5001 | case BuiltinType::Float: return 'f'; |
| 5002 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 5003 | case BuiltinType::LongDouble: return 'D'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5004 | case BuiltinType::NullPtr: return '*'; // like char* |
| 5005 | |
| 5006 | case BuiltinType::Half: |
| 5007 | // FIXME: potentially need @encodes for these! |
| 5008 | return ' '; |
| 5009 | |
| 5010 | case BuiltinType::ObjCId: |
| 5011 | case BuiltinType::ObjCClass: |
| 5012 | case BuiltinType::ObjCSel: |
| 5013 | llvm_unreachable("@encoding ObjC primitive type"); |
| 5014 | |
| 5015 | // OpenCL and placeholder types don't need @encodings. |
| 5016 | case BuiltinType::OCLImage1d: |
| 5017 | case BuiltinType::OCLImage1dArray: |
| 5018 | case BuiltinType::OCLImage1dBuffer: |
| 5019 | case BuiltinType::OCLImage2d: |
| 5020 | case BuiltinType::OCLImage2dArray: |
| 5021 | case BuiltinType::OCLImage3d: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5022 | case BuiltinType::OCLEvent: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5023 | case BuiltinType::OCLSampler: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5024 | case BuiltinType::Dependent: |
| 5025 | #define BUILTIN_TYPE(KIND, ID) |
| 5026 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
| 5027 | case BuiltinType::KIND: |
| 5028 | #include "clang/AST/BuiltinTypes.def" |
| 5029 | llvm_unreachable("invalid builtin type for @encode"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5030 | } |
David Blaikie | 719e53f | 2013-01-09 17:48:41 +0000 | [diff] [blame] | 5031 | llvm_unreachable("invalid BuiltinType::Kind value"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5032 | } |
| 5033 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5034 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 5035 | EnumDecl *Enum = ET->getDecl(); |
| 5036 | |
| 5037 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 5038 | if (!Enum->isFixed()) |
| 5039 | return 'i'; |
| 5040 | |
| 5041 | // The encoding of a fixed enum type matches its fixed underlying type. |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5042 | const BuiltinType *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
| 5043 | return getObjCEncodingForPrimitiveKind(C, BT->getKind()); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5044 | } |
| 5045 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5046 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5047 | QualType T, const FieldDecl *FD) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5048 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 5049 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5050 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 5051 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 5052 | // then the offset (in bits) of the first element, then the type of the |
| 5053 | // bitfield, then the size in bits. For example, in this structure: |
| 5054 | // |
| 5055 | // struct |
| 5056 | // { |
| 5057 | // int integer; |
| 5058 | // int flags:2; |
| 5059 | // }; |
| 5060 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 5061 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 5062 | // information is not especially sensible, but we're stuck with it for |
| 5063 | // compatibility with GCC, although providing it breaks anything that |
| 5064 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 5065 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5066 | const RecordDecl *RD = FD->getParent(); |
| 5067 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | 8290574 | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 5068 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5069 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 5070 | S += ObjCEncodingForEnumType(Ctx, ET); |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5071 | else { |
| 5072 | const BuiltinType *BT = T->castAs<BuiltinType>(); |
| 5073 | S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind()); |
| 5074 | } |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5075 | } |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5076 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 5079 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5080 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 5081 | bool ExpandPointedToStructures, |
| 5082 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 5083 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 5084 | bool OutermostType, |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5085 | bool EncodingProperty, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5086 | bool StructField, |
| 5087 | bool EncodeBlockParameters, |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5088 | bool EncodeClassNames, |
| 5089 | bool EncodePointerToObjCTypedef) const { |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5090 | CanQualType CT = getCanonicalType(T); |
| 5091 | switch (CT->getTypeClass()) { |
| 5092 | case Type::Builtin: |
| 5093 | case Type::Enum: |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5094 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5095 | return EncodeBitField(this, S, T, FD); |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5096 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CT)) |
| 5097 | S += getObjCEncodingForPrimitiveKind(this, BT->getKind()); |
| 5098 | else |
| 5099 | S += ObjCEncodingForEnumType(this, cast<EnumType>(CT)); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5100 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5101 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5102 | case Type::Complex: { |
| 5103 | const ComplexType *CT = T->castAs<ComplexType>(); |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 5104 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5105 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 5106 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5107 | return; |
| 5108 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5109 | |
| 5110 | case Type::Atomic: { |
| 5111 | const AtomicType *AT = T->castAs<AtomicType>(); |
| 5112 | S += 'A'; |
| 5113 | getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, 0, |
| 5114 | false, false); |
| 5115 | return; |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 5116 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5117 | |
| 5118 | // encoding for pointer or reference types. |
| 5119 | case Type::Pointer: |
| 5120 | case Type::LValueReference: |
| 5121 | case Type::RValueReference: { |
| 5122 | QualType PointeeTy; |
| 5123 | if (isa<PointerType>(CT)) { |
| 5124 | const PointerType *PT = T->castAs<PointerType>(); |
| 5125 | if (PT->isObjCSelType()) { |
| 5126 | S += ':'; |
| 5127 | return; |
| 5128 | } |
| 5129 | PointeeTy = PT->getPointeeType(); |
| 5130 | } else { |
| 5131 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
| 5132 | } |
| 5133 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5134 | bool isReadOnly = false; |
| 5135 | // For historical/compatibility reasons, the read-only qualifier of the |
| 5136 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 5137 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5138 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 5139 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5140 | if (OutermostType && T.isConstQualified()) { |
| 5141 | isReadOnly = true; |
| 5142 | S += 'r'; |
| 5143 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 5144 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5145 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5146 | while (P->getAs<PointerType>()) |
| 5147 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5148 | if (P.isConstQualified()) { |
| 5149 | isReadOnly = true; |
| 5150 | S += 'r'; |
| 5151 | } |
| 5152 | } |
| 5153 | if (isReadOnly) { |
| 5154 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 5155 | // combinations need to be rearranged. |
| 5156 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5157 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 5158 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5159 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5160 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5161 | if (PointeeTy->isCharType()) { |
| 5162 | // char pointer types should be encoded as '*' unless it is a |
| 5163 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 5164 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5165 | S += '*'; |
| 5166 | return; |
| 5167 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5168 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 5169 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 5170 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 5171 | S += '#'; |
| 5172 | return; |
| 5173 | } |
| 5174 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 5175 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 5176 | S += '@'; |
| 5177 | return; |
| 5178 | } |
| 5179 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5180 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5181 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5182 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 5183 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5184 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5185 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5186 | return; |
| 5187 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5188 | |
| 5189 | case Type::ConstantArray: |
| 5190 | case Type::IncompleteArray: |
| 5191 | case Type::VariableArray: { |
| 5192 | const ArrayType *AT = cast<ArrayType>(CT); |
| 5193 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5194 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5195 | // Incomplete arrays are encoded as a pointer to the array element. |
| 5196 | S += '^'; |
| 5197 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5198 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5199 | false, ExpandStructures, FD); |
| 5200 | } else { |
| 5201 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5202 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5203 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 5204 | if (getTypeSize(CAT->getElementType()) == 0) |
| 5205 | S += '0'; |
| 5206 | else |
| 5207 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 5208 | } else { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5209 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5210 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 5211 | "Unknown array type!"); |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5212 | S += '0'; |
| 5213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5214 | |
| 5215 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5216 | false, ExpandStructures, FD); |
| 5217 | S += ']'; |
| 5218 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5219 | return; |
| 5220 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5221 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5222 | case Type::FunctionNoProto: |
| 5223 | case Type::FunctionProto: |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 5224 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5225 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5226 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5227 | case Type::Record: { |
| 5228 | RecordDecl *RDecl = cast<RecordType>(CT)->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5229 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5230 | // Anonymous structures print as '?' |
| 5231 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 5232 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5233 | if (ClassTemplateSpecializationDecl *Spec |
| 5234 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 5235 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Benjamin Kramer | 5eada84 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 5236 | llvm::raw_string_ostream OS(S); |
| 5237 | TemplateSpecializationType::PrintTemplateArgumentList(OS, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5238 | TemplateArgs.data(), |
| 5239 | TemplateArgs.size(), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 5240 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5241 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5242 | } else { |
| 5243 | S += '?'; |
| 5244 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 5245 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5246 | S += '='; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5247 | if (!RDecl->isUnion()) { |
| 5248 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 5249 | } else { |
| 5250 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 5251 | FieldEnd = RDecl->field_end(); |
| 5252 | Field != FieldEnd; ++Field) { |
| 5253 | if (FD) { |
| 5254 | S += '"'; |
| 5255 | S += Field->getNameAsString(); |
| 5256 | S += '"'; |
| 5257 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5258 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5259 | // Special case bit-fields. |
| 5260 | if (Field->isBitField()) { |
| 5261 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5262 | *Field); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5263 | } else { |
| 5264 | QualType qt = Field->getType(); |
| 5265 | getLegacyIntegralTypeEncoding(qt); |
| 5266 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 5267 | FD, /*OutermostType*/false, |
| 5268 | /*EncodingProperty*/false, |
| 5269 | /*StructField*/true); |
| 5270 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5271 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5272 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 5273 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5274 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5275 | return; |
| 5276 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5277 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5278 | case Type::BlockPointer: { |
| 5279 | const BlockPointerType *BT = T->castAs<BlockPointerType>(); |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 5280 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5281 | if (EncodeBlockParameters) { |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5282 | const FunctionType *FT = BT->getPointeeType()->castAs<FunctionType>(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5283 | |
| 5284 | S += '<'; |
| 5285 | // Block return type |
| 5286 | getObjCEncodingForTypeImpl(FT->getResultType(), S, |
| 5287 | ExpandPointedToStructures, ExpandStructures, |
| 5288 | FD, |
| 5289 | false /* OutermostType */, |
| 5290 | EncodingProperty, |
| 5291 | false /* StructField */, |
| 5292 | EncodeBlockParameters, |
| 5293 | EncodeClassNames); |
| 5294 | // Block self |
| 5295 | S += "@?"; |
| 5296 | // Block parameters |
| 5297 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 5298 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(), |
| 5299 | E = FPT->arg_type_end(); I && (I != E); ++I) { |
| 5300 | getObjCEncodingForTypeImpl(*I, S, |
| 5301 | ExpandPointedToStructures, |
| 5302 | ExpandStructures, |
| 5303 | FD, |
| 5304 | false /* OutermostType */, |
| 5305 | EncodingProperty, |
| 5306 | false /* StructField */, |
| 5307 | EncodeBlockParameters, |
| 5308 | EncodeClassNames); |
| 5309 | } |
| 5310 | } |
| 5311 | S += '>'; |
| 5312 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5313 | return; |
| 5314 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5315 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5316 | case Type::ObjCObject: |
| 5317 | case Type::ObjCInterface: { |
| 5318 | // Ignore protocol qualifiers when mangling at this level. |
| 5319 | T = T->castAs<ObjCObjectType>()->getBaseType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5320 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5321 | // The assumption seems to be that this assert will succeed |
| 5322 | // because nested levels will have filtered out 'id' and 'Class'. |
| 5323 | const ObjCInterfaceType *OIT = T->castAs<ObjCInterfaceType>(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5324 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5325 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5326 | S += '{'; |
| 5327 | const IdentifierInfo *II = OI->getIdentifier(); |
| 5328 | S += II->getName(); |
| 5329 | S += '='; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5330 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5331 | DeepCollectObjCIvars(OI, true, Ivars); |
| 5332 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5333 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5334 | if (Field->isBitField()) |
| 5335 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5336 | else |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5337 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD, |
| 5338 | false, false, false, false, false, |
| 5339 | EncodePointerToObjCTypedef); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5340 | } |
| 5341 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5342 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5343 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5344 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5345 | case Type::ObjCObjectPointer: { |
| 5346 | const ObjCObjectPointerType *OPT = T->castAs<ObjCObjectPointerType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5347 | if (OPT->isObjCIdType()) { |
| 5348 | S += '@'; |
| 5349 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5350 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5351 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5352 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 5353 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 5354 | // Since this is a binary compatibility issue, need to consult with runtime |
| 5355 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5356 | S += '#'; |
| 5357 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5358 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5359 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5360 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5361 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5362 | ExpandPointedToStructures, |
| 5363 | ExpandStructures, FD); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5364 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5365 | // Note that we do extended encoding of protocol qualifer list |
| 5366 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5367 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5368 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 5369 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5370 | S += '<'; |
| 5371 | S += (*I)->getNameAsString(); |
| 5372 | S += '>'; |
| 5373 | } |
| 5374 | S += '"'; |
| 5375 | } |
| 5376 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5377 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5378 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5379 | QualType PointeeTy = OPT->getPointeeType(); |
| 5380 | if (!EncodingProperty && |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5381 | isa<TypedefType>(PointeeTy.getTypePtr()) && |
| 5382 | !EncodePointerToObjCTypedef) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5383 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5384 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5385 | // {...}; |
| 5386 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5387 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 5388 | false, ExpandPointedToStructures, |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5389 | NULL, |
| 5390 | false, false, false, false, false, |
| 5391 | /*EncodePointerToObjCTypedef*/true); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5392 | return; |
| 5393 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5394 | |
| 5395 | S += '@'; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5396 | if (OPT->getInterfaceDecl() && |
| 5397 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5398 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5399 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5400 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 5401 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5402 | S += '<'; |
| 5403 | S += (*I)->getNameAsString(); |
| 5404 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5405 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5406 | S += '"'; |
| 5407 | } |
| 5408 | return; |
| 5409 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5410 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 5411 | // gcc just blithely ignores member pointers. |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5412 | // FIXME: we shoul do better than that. 'M' is available. |
| 5413 | case Type::MemberPointer: |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 5414 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5415 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5416 | case Type::Vector: |
| 5417 | case Type::ExtVector: |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5418 | // This matches gcc's encoding, even though technically it is |
| 5419 | // insufficient. |
| 5420 | // FIXME. We should do a better job than gcc. |
| 5421 | return; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5422 | |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5423 | case Type::Auto: |
| 5424 | // We could see an undeduced auto type here during error recovery. |
| 5425 | // Just ignore it. |
| 5426 | return; |
| 5427 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5428 | #define ABSTRACT_TYPE(KIND, BASE) |
| 5429 | #define TYPE(KIND, BASE) |
| 5430 | #define DEPENDENT_TYPE(KIND, BASE) \ |
| 5431 | case Type::KIND: |
| 5432 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
| 5433 | case Type::KIND: |
| 5434 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
| 5435 | case Type::KIND: |
| 5436 | #include "clang/AST/TypeNodes.def" |
| 5437 | llvm_unreachable("@encode for dependent type!"); |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5438 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5439 | llvm_unreachable("bad type kind!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5440 | } |
| 5441 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5442 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 5443 | std::string &S, |
| 5444 | const FieldDecl *FD, |
| 5445 | bool includeVBases) const { |
| 5446 | assert(RDecl && "Expected non-null RecordDecl"); |
| 5447 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 5448 | if (!RDecl->getDefinition()) |
| 5449 | return; |
| 5450 | |
| 5451 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 5452 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 5453 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 5454 | |
| 5455 | if (CXXRec) { |
| 5456 | for (CXXRecordDecl::base_class_iterator |
| 5457 | BI = CXXRec->bases_begin(), |
| 5458 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 5459 | if (!BI->isVirtual()) { |
| 5460 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5461 | if (base->isEmpty()) |
| 5462 | continue; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5463 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5464 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 5465 | std::make_pair(offs, base)); |
| 5466 | } |
| 5467 | } |
| 5468 | } |
| 5469 | |
| 5470 | unsigned i = 0; |
| 5471 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 5472 | FieldEnd = RDecl->field_end(); |
| 5473 | Field != FieldEnd; ++Field, ++i) { |
| 5474 | uint64_t offs = layout.getFieldOffset(i); |
| 5475 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5476 | std::make_pair(offs, *Field)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5477 | } |
| 5478 | |
| 5479 | if (CXXRec && includeVBases) { |
| 5480 | for (CXXRecordDecl::base_class_iterator |
| 5481 | BI = CXXRec->vbases_begin(), |
| 5482 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 5483 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5484 | if (base->isEmpty()) |
| 5485 | continue; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5486 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Argyrios Kyrtzidis | 19aa860 | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 5487 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 5488 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 5489 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5490 | } |
| 5491 | } |
| 5492 | |
| 5493 | CharUnits size; |
| 5494 | if (CXXRec) { |
| 5495 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 5496 | } else { |
| 5497 | size = layout.getSize(); |
| 5498 | } |
| 5499 | |
| 5500 | uint64_t CurOffs = 0; |
| 5501 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 5502 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 5503 | |
Douglas Gregor | 58db7a5 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 5504 | if (CXXRec && CXXRec->isDynamicClass() && |
| 5505 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5506 | if (FD) { |
| 5507 | S += "\"_vptr$"; |
| 5508 | std::string recname = CXXRec->getNameAsString(); |
| 5509 | if (recname.empty()) recname = "?"; |
| 5510 | S += recname; |
| 5511 | S += '"'; |
| 5512 | } |
| 5513 | S += "^^?"; |
| 5514 | CurOffs += getTypeSize(VoidPtrTy); |
| 5515 | } |
| 5516 | |
| 5517 | if (!RDecl->hasFlexibleArrayMember()) { |
| 5518 | // Mark the end of the structure. |
| 5519 | uint64_t offs = toBits(size); |
| 5520 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 5521 | std::make_pair(offs, (NamedDecl*)0)); |
| 5522 | } |
| 5523 | |
| 5524 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 5525 | assert(CurOffs <= CurLayObj->first); |
| 5526 | |
| 5527 | if (CurOffs < CurLayObj->first) { |
| 5528 | uint64_t padding = CurLayObj->first - CurOffs; |
| 5529 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 5530 | // packing/alignment of members is different that normal, in which case |
| 5531 | // the encoding will be out-of-sync with the real layout. |
| 5532 | // If the runtime switches to just consider the size of types without |
| 5533 | // taking into account alignment, we could make padding explicit in the |
| 5534 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 5535 | // longer then though. |
| 5536 | CurOffs += padding; |
| 5537 | } |
| 5538 | |
| 5539 | NamedDecl *dcl = CurLayObj->second; |
| 5540 | if (dcl == 0) |
| 5541 | break; // reached end of structure. |
| 5542 | |
| 5543 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 5544 | // We expand the bases without their virtual bases since those are going |
| 5545 | // in the initial structure. Note that this differs from gcc which |
| 5546 | // expands virtual bases each time one is encountered in the hierarchy, |
| 5547 | // making the encoding type bigger than it really is. |
| 5548 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5549 | assert(!base->isEmpty()); |
| 5550 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5551 | } else { |
| 5552 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 5553 | if (FD) { |
| 5554 | S += '"'; |
| 5555 | S += field->getNameAsString(); |
| 5556 | S += '"'; |
| 5557 | } |
| 5558 | |
| 5559 | if (field->isBitField()) { |
| 5560 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5561 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5562 | } else { |
| 5563 | QualType qt = field->getType(); |
| 5564 | getLegacyIntegralTypeEncoding(qt); |
| 5565 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 5566 | /*OutermostType*/false, |
| 5567 | /*EncodingProperty*/false, |
| 5568 | /*StructField*/true); |
| 5569 | CurOffs += getTypeSize(field->getType()); |
| 5570 | } |
| 5571 | } |
| 5572 | } |
| 5573 | } |
| 5574 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5575 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 5576 | std::string& S) const { |
| 5577 | if (QT & Decl::OBJC_TQ_In) |
| 5578 | S += 'n'; |
| 5579 | if (QT & Decl::OBJC_TQ_Inout) |
| 5580 | S += 'N'; |
| 5581 | if (QT & Decl::OBJC_TQ_Out) |
| 5582 | S += 'o'; |
| 5583 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 5584 | S += 'O'; |
| 5585 | if (QT & Decl::OBJC_TQ_Byref) |
| 5586 | S += 'R'; |
| 5587 | if (QT & Decl::OBJC_TQ_Oneway) |
| 5588 | S += 'V'; |
| 5589 | } |
| 5590 | |
Douglas Gregor | 4dfd02a | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 5591 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 5592 | if (!ObjCIdDecl) { |
| 5593 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 5594 | T = getObjCObjectPointerType(T); |
| 5595 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 5596 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5597 | getTranslationUnitDecl(), |
| 5598 | SourceLocation(), SourceLocation(), |
| 5599 | &Idents.get("id"), IdInfo); |
| 5600 | } |
| 5601 | |
| 5602 | return ObjCIdDecl; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
Douglas Gregor | 7a27ea5 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 5605 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 5606 | if (!ObjCSelDecl) { |
| 5607 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 5608 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 5609 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5610 | getTranslationUnitDecl(), |
| 5611 | SourceLocation(), SourceLocation(), |
| 5612 | &Idents.get("SEL"), SelInfo); |
| 5613 | } |
| 5614 | return ObjCSelDecl; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
Douglas Gregor | 79d6726 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 5617 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 5618 | if (!ObjCClassDecl) { |
| 5619 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 5620 | T = getObjCObjectPointerType(T); |
| 5621 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 5622 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5623 | getTranslationUnitDecl(), |
| 5624 | SourceLocation(), SourceLocation(), |
| 5625 | &Idents.get("Class"), ClassInfo); |
| 5626 | } |
| 5627 | |
| 5628 | return ObjCClassDecl; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 5629 | } |
| 5630 | |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 5631 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 5632 | if (!ObjCProtocolClassDecl) { |
| 5633 | ObjCProtocolClassDecl |
| 5634 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 5635 | SourceLocation(), |
| 5636 | &Idents.get("Protocol"), |
| 5637 | /*PrevDecl=*/0, |
| 5638 | SourceLocation(), true); |
| 5639 | } |
| 5640 | |
| 5641 | return ObjCProtocolClassDecl; |
| 5642 | } |
| 5643 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5644 | //===----------------------------------------------------------------------===// |
| 5645 | // __builtin_va_list Construction Functions |
| 5646 | //===----------------------------------------------------------------------===// |
| 5647 | |
| 5648 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5649 | // typedef char* __builtin_va_list; |
| 5650 | QualType CharPtrType = Context->getPointerType(Context->CharTy); |
| 5651 | TypeSourceInfo *TInfo |
| 5652 | = Context->getTrivialTypeSourceInfo(CharPtrType); |
| 5653 | |
| 5654 | TypedefDecl *VaListTypeDecl |
| 5655 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5656 | Context->getTranslationUnitDecl(), |
| 5657 | SourceLocation(), SourceLocation(), |
| 5658 | &Context->Idents.get("__builtin_va_list"), |
| 5659 | TInfo); |
| 5660 | return VaListTypeDecl; |
| 5661 | } |
| 5662 | |
| 5663 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5664 | // typedef void* __builtin_va_list; |
| 5665 | QualType VoidPtrType = Context->getPointerType(Context->VoidTy); |
| 5666 | TypeSourceInfo *TInfo |
| 5667 | = Context->getTrivialTypeSourceInfo(VoidPtrType); |
| 5668 | |
| 5669 | TypedefDecl *VaListTypeDecl |
| 5670 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5671 | Context->getTranslationUnitDecl(), |
| 5672 | SourceLocation(), SourceLocation(), |
| 5673 | &Context->Idents.get("__builtin_va_list"), |
| 5674 | TInfo); |
| 5675 | return VaListTypeDecl; |
| 5676 | } |
| 5677 | |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5678 | static TypedefDecl * |
| 5679 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5680 | RecordDecl *VaListTagDecl; |
| 5681 | if (Context->getLangOpts().CPlusPlus) { |
| 5682 | // namespace std { struct __va_list { |
| 5683 | NamespaceDecl *NS; |
| 5684 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 5685 | Context->getTranslationUnitDecl(), |
| 5686 | /*Inline*/false, SourceLocation(), |
| 5687 | SourceLocation(), &Context->Idents.get("std"), |
| 5688 | /*PrevDecl*/0); |
| 5689 | |
| 5690 | VaListTagDecl = CXXRecordDecl::Create(*Context, TTK_Struct, |
| 5691 | Context->getTranslationUnitDecl(), |
| 5692 | SourceLocation(), SourceLocation(), |
| 5693 | &Context->Idents.get("__va_list")); |
| 5694 | VaListTagDecl->setDeclContext(NS); |
| 5695 | } else { |
| 5696 | // struct __va_list |
| 5697 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5698 | Context->getTranslationUnitDecl(), |
| 5699 | &Context->Idents.get("__va_list")); |
| 5700 | } |
| 5701 | |
| 5702 | VaListTagDecl->startDefinition(); |
| 5703 | |
| 5704 | const size_t NumFields = 5; |
| 5705 | QualType FieldTypes[NumFields]; |
| 5706 | const char *FieldNames[NumFields]; |
| 5707 | |
| 5708 | // void *__stack; |
| 5709 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
| 5710 | FieldNames[0] = "__stack"; |
| 5711 | |
| 5712 | // void *__gr_top; |
| 5713 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
| 5714 | FieldNames[1] = "__gr_top"; |
| 5715 | |
| 5716 | // void *__vr_top; |
| 5717 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5718 | FieldNames[2] = "__vr_top"; |
| 5719 | |
| 5720 | // int __gr_offs; |
| 5721 | FieldTypes[3] = Context->IntTy; |
| 5722 | FieldNames[3] = "__gr_offs"; |
| 5723 | |
| 5724 | // int __vr_offs; |
| 5725 | FieldTypes[4] = Context->IntTy; |
| 5726 | FieldNames[4] = "__vr_offs"; |
| 5727 | |
| 5728 | // Create fields |
| 5729 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5730 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5731 | VaListTagDecl, |
| 5732 | SourceLocation(), |
| 5733 | SourceLocation(), |
| 5734 | &Context->Idents.get(FieldNames[i]), |
| 5735 | FieldTypes[i], /*TInfo=*/0, |
| 5736 | /*BitWidth=*/0, |
| 5737 | /*Mutable=*/false, |
| 5738 | ICIS_NoInit); |
| 5739 | Field->setAccess(AS_public); |
| 5740 | VaListTagDecl->addDecl(Field); |
| 5741 | } |
| 5742 | VaListTagDecl->completeDefinition(); |
| 5743 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 5744 | Context->VaListTagTy = VaListTagType; |
| 5745 | |
| 5746 | // } __builtin_va_list; |
| 5747 | TypedefDecl *VaListTypedefDecl |
| 5748 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5749 | Context->getTranslationUnitDecl(), |
| 5750 | SourceLocation(), SourceLocation(), |
| 5751 | &Context->Idents.get("__builtin_va_list"), |
| 5752 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5753 | |
| 5754 | return VaListTypedefDecl; |
| 5755 | } |
| 5756 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5757 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5758 | // typedef struct __va_list_tag { |
| 5759 | RecordDecl *VaListTagDecl; |
| 5760 | |
| 5761 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5762 | Context->getTranslationUnitDecl(), |
| 5763 | &Context->Idents.get("__va_list_tag")); |
| 5764 | VaListTagDecl->startDefinition(); |
| 5765 | |
| 5766 | const size_t NumFields = 5; |
| 5767 | QualType FieldTypes[NumFields]; |
| 5768 | const char *FieldNames[NumFields]; |
| 5769 | |
| 5770 | // unsigned char gpr; |
| 5771 | FieldTypes[0] = Context->UnsignedCharTy; |
| 5772 | FieldNames[0] = "gpr"; |
| 5773 | |
| 5774 | // unsigned char fpr; |
| 5775 | FieldTypes[1] = Context->UnsignedCharTy; |
| 5776 | FieldNames[1] = "fpr"; |
| 5777 | |
| 5778 | // unsigned short reserved; |
| 5779 | FieldTypes[2] = Context->UnsignedShortTy; |
| 5780 | FieldNames[2] = "reserved"; |
| 5781 | |
| 5782 | // void* overflow_arg_area; |
| 5783 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5784 | FieldNames[3] = "overflow_arg_area"; |
| 5785 | |
| 5786 | // void* reg_save_area; |
| 5787 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 5788 | FieldNames[4] = "reg_save_area"; |
| 5789 | |
| 5790 | // Create fields |
| 5791 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5792 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 5793 | SourceLocation(), |
| 5794 | SourceLocation(), |
| 5795 | &Context->Idents.get(FieldNames[i]), |
| 5796 | FieldTypes[i], /*TInfo=*/0, |
| 5797 | /*BitWidth=*/0, |
| 5798 | /*Mutable=*/false, |
| 5799 | ICIS_NoInit); |
| 5800 | Field->setAccess(AS_public); |
| 5801 | VaListTagDecl->addDecl(Field); |
| 5802 | } |
| 5803 | VaListTagDecl->completeDefinition(); |
| 5804 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5805 | Context->VaListTagTy = VaListTagType; |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5806 | |
| 5807 | // } __va_list_tag; |
| 5808 | TypedefDecl *VaListTagTypedefDecl |
| 5809 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5810 | Context->getTranslationUnitDecl(), |
| 5811 | SourceLocation(), SourceLocation(), |
| 5812 | &Context->Idents.get("__va_list_tag"), |
| 5813 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5814 | QualType VaListTagTypedefType = |
| 5815 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5816 | |
| 5817 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5818 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5819 | QualType VaListTagArrayType |
| 5820 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5821 | Size, ArrayType::Normal, 0); |
| 5822 | TypeSourceInfo *TInfo |
| 5823 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5824 | TypedefDecl *VaListTypedefDecl |
| 5825 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5826 | Context->getTranslationUnitDecl(), |
| 5827 | SourceLocation(), SourceLocation(), |
| 5828 | &Context->Idents.get("__builtin_va_list"), |
| 5829 | TInfo); |
| 5830 | |
| 5831 | return VaListTypedefDecl; |
| 5832 | } |
| 5833 | |
| 5834 | static TypedefDecl * |
| 5835 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5836 | // typedef struct __va_list_tag { |
| 5837 | RecordDecl *VaListTagDecl; |
| 5838 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5839 | Context->getTranslationUnitDecl(), |
| 5840 | &Context->Idents.get("__va_list_tag")); |
| 5841 | VaListTagDecl->startDefinition(); |
| 5842 | |
| 5843 | const size_t NumFields = 4; |
| 5844 | QualType FieldTypes[NumFields]; |
| 5845 | const char *FieldNames[NumFields]; |
| 5846 | |
| 5847 | // unsigned gp_offset; |
| 5848 | FieldTypes[0] = Context->UnsignedIntTy; |
| 5849 | FieldNames[0] = "gp_offset"; |
| 5850 | |
| 5851 | // unsigned fp_offset; |
| 5852 | FieldTypes[1] = Context->UnsignedIntTy; |
| 5853 | FieldNames[1] = "fp_offset"; |
| 5854 | |
| 5855 | // void* overflow_arg_area; |
| 5856 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5857 | FieldNames[2] = "overflow_arg_area"; |
| 5858 | |
| 5859 | // void* reg_save_area; |
| 5860 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5861 | FieldNames[3] = "reg_save_area"; |
| 5862 | |
| 5863 | // Create fields |
| 5864 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5865 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5866 | VaListTagDecl, |
| 5867 | SourceLocation(), |
| 5868 | SourceLocation(), |
| 5869 | &Context->Idents.get(FieldNames[i]), |
| 5870 | FieldTypes[i], /*TInfo=*/0, |
| 5871 | /*BitWidth=*/0, |
| 5872 | /*Mutable=*/false, |
| 5873 | ICIS_NoInit); |
| 5874 | Field->setAccess(AS_public); |
| 5875 | VaListTagDecl->addDecl(Field); |
| 5876 | } |
| 5877 | VaListTagDecl->completeDefinition(); |
| 5878 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5879 | Context->VaListTagTy = VaListTagType; |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5880 | |
| 5881 | // } __va_list_tag; |
| 5882 | TypedefDecl *VaListTagTypedefDecl |
| 5883 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5884 | Context->getTranslationUnitDecl(), |
| 5885 | SourceLocation(), SourceLocation(), |
| 5886 | &Context->Idents.get("__va_list_tag"), |
| 5887 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5888 | QualType VaListTagTypedefType = |
| 5889 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5890 | |
| 5891 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5892 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5893 | QualType VaListTagArrayType |
| 5894 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5895 | Size, ArrayType::Normal,0); |
| 5896 | TypeSourceInfo *TInfo |
| 5897 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5898 | TypedefDecl *VaListTypedefDecl |
| 5899 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5900 | Context->getTranslationUnitDecl(), |
| 5901 | SourceLocation(), SourceLocation(), |
| 5902 | &Context->Idents.get("__builtin_va_list"), |
| 5903 | TInfo); |
| 5904 | |
| 5905 | return VaListTypedefDecl; |
| 5906 | } |
| 5907 | |
| 5908 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5909 | // typedef int __builtin_va_list[4]; |
| 5910 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
| 5911 | QualType IntArrayType |
| 5912 | = Context->getConstantArrayType(Context->IntTy, |
| 5913 | Size, ArrayType::Normal, 0); |
| 5914 | TypedefDecl *VaListTypedefDecl |
| 5915 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5916 | Context->getTranslationUnitDecl(), |
| 5917 | SourceLocation(), SourceLocation(), |
| 5918 | &Context->Idents.get("__builtin_va_list"), |
| 5919 | Context->getTrivialTypeSourceInfo(IntArrayType)); |
| 5920 | |
| 5921 | return VaListTypedefDecl; |
| 5922 | } |
| 5923 | |
Logan Chien | eae5a820 | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 5924 | static TypedefDecl * |
| 5925 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5926 | RecordDecl *VaListDecl; |
| 5927 | if (Context->getLangOpts().CPlusPlus) { |
| 5928 | // namespace std { struct __va_list { |
| 5929 | NamespaceDecl *NS; |
| 5930 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 5931 | Context->getTranslationUnitDecl(), |
| 5932 | /*Inline*/false, SourceLocation(), |
| 5933 | SourceLocation(), &Context->Idents.get("std"), |
| 5934 | /*PrevDecl*/0); |
| 5935 | |
| 5936 | VaListDecl = CXXRecordDecl::Create(*Context, TTK_Struct, |
| 5937 | Context->getTranslationUnitDecl(), |
| 5938 | SourceLocation(), SourceLocation(), |
| 5939 | &Context->Idents.get("__va_list")); |
| 5940 | |
| 5941 | VaListDecl->setDeclContext(NS); |
| 5942 | |
| 5943 | } else { |
| 5944 | // struct __va_list { |
| 5945 | VaListDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5946 | Context->getTranslationUnitDecl(), |
| 5947 | &Context->Idents.get("__va_list")); |
| 5948 | } |
| 5949 | |
| 5950 | VaListDecl->startDefinition(); |
| 5951 | |
| 5952 | // void * __ap; |
| 5953 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5954 | VaListDecl, |
| 5955 | SourceLocation(), |
| 5956 | SourceLocation(), |
| 5957 | &Context->Idents.get("__ap"), |
| 5958 | Context->getPointerType(Context->VoidTy), |
| 5959 | /*TInfo=*/0, |
| 5960 | /*BitWidth=*/0, |
| 5961 | /*Mutable=*/false, |
| 5962 | ICIS_NoInit); |
| 5963 | Field->setAccess(AS_public); |
| 5964 | VaListDecl->addDecl(Field); |
| 5965 | |
| 5966 | // }; |
| 5967 | VaListDecl->completeDefinition(); |
| 5968 | |
| 5969 | // typedef struct __va_list __builtin_va_list; |
| 5970 | TypeSourceInfo *TInfo |
| 5971 | = Context->getTrivialTypeSourceInfo(Context->getRecordType(VaListDecl)); |
| 5972 | |
| 5973 | TypedefDecl *VaListTypeDecl |
| 5974 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5975 | Context->getTranslationUnitDecl(), |
| 5976 | SourceLocation(), SourceLocation(), |
| 5977 | &Context->Idents.get("__builtin_va_list"), |
| 5978 | TInfo); |
| 5979 | |
| 5980 | return VaListTypeDecl; |
| 5981 | } |
| 5982 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5983 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 5984 | TargetInfo::BuiltinVaListKind Kind) { |
| 5985 | switch (Kind) { |
| 5986 | case TargetInfo::CharPtrBuiltinVaList: |
| 5987 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 5988 | case TargetInfo::VoidPtrBuiltinVaList: |
| 5989 | return CreateVoidPtrBuiltinVaListDecl(Context); |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5990 | case TargetInfo::AArch64ABIBuiltinVaList: |
| 5991 | return CreateAArch64ABIBuiltinVaListDecl(Context); |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5992 | case TargetInfo::PowerABIBuiltinVaList: |
| 5993 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 5994 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 5995 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 5996 | case TargetInfo::PNaClABIBuiltinVaList: |
| 5997 | return CreatePNaClABIBuiltinVaListDecl(Context); |
Logan Chien | eae5a820 | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 5998 | case TargetInfo::AAPCSABIBuiltinVaList: |
| 5999 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6000 | } |
| 6001 | |
| 6002 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 6003 | } |
| 6004 | |
| 6005 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
| 6006 | if (!BuiltinVaListDecl) |
| 6007 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
| 6008 | |
| 6009 | return BuiltinVaListDecl; |
| 6010 | } |
| 6011 | |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 6012 | QualType ASTContext::getVaListTagType() const { |
| 6013 | // Force the creation of VaListTagTy by building the __builtin_va_list |
| 6014 | // declaration. |
| 6015 | if (VaListTagTy.isNull()) |
| 6016 | (void) getBuiltinVaListDecl(); |
| 6017 | |
| 6018 | return VaListTagTy; |
| 6019 | } |
| 6020 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6021 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6022 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 6023 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6024 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6025 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 6026 | } |
| 6027 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6028 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 6029 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6030 | TemplateName |
| 6031 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 6032 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6033 | unsigned size = End - Begin; |
| 6034 | assert(size > 1 && "set is not overloaded!"); |
| 6035 | |
| 6036 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 6037 | size * sizeof(FunctionTemplateDecl*)); |
| 6038 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 6039 | |
| 6040 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6041 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6042 | NamedDecl *D = *I; |
| 6043 | assert(isa<FunctionTemplateDecl>(D) || |
| 6044 | (isa<UsingShadowDecl>(D) && |
| 6045 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 6046 | *Storage++ = D; |
| 6047 | } |
| 6048 | |
| 6049 | return TemplateName(OT); |
| 6050 | } |
| 6051 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6052 | /// \brief Retrieve the template name that represents a qualified |
| 6053 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6054 | TemplateName |
| 6055 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 6056 | bool TemplateKeyword, |
| 6057 | TemplateDecl *Template) const { |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 6058 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 6059 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6060 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6061 | llvm::FoldingSetNodeID ID; |
| 6062 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 6063 | |
| 6064 | void *InsertPos = 0; |
| 6065 | QualifiedTemplateName *QTN = |
| 6066 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6067 | if (!QTN) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6068 | QTN = new (*this, llvm::alignOf<QualifiedTemplateName>()) |
| 6069 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6070 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 6071 | } |
| 6072 | |
| 6073 | return TemplateName(QTN); |
| 6074 | } |
| 6075 | |
| 6076 | /// \brief Retrieve the template name that represents a dependent |
| 6077 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6078 | TemplateName |
| 6079 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 6080 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6081 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 6082 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6083 | |
| 6084 | llvm::FoldingSetNodeID ID; |
| 6085 | DependentTemplateName::Profile(ID, NNS, Name); |
| 6086 | |
| 6087 | void *InsertPos = 0; |
| 6088 | DependentTemplateName *QTN = |
| 6089 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6090 | |
| 6091 | if (QTN) |
| 6092 | return TemplateName(QTN); |
| 6093 | |
| 6094 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 6095 | if (CanonNNS == NNS) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6096 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6097 | DependentTemplateName(NNS, Name); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6098 | } else { |
| 6099 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6100 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6101 | DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6102 | DependentTemplateName *CheckQTN = |
| 6103 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6104 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 6105 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6106 | } |
| 6107 | |
| 6108 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 6109 | return TemplateName(QTN); |
| 6110 | } |
| 6111 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6112 | /// \brief Retrieve the template name that represents a dependent |
| 6113 | /// template name such as \c MetaFun::template operator+. |
| 6114 | TemplateName |
| 6115 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6116 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6117 | assert((!NNS || NNS->isDependent()) && |
| 6118 | "Nested name specifier must be dependent"); |
| 6119 | |
| 6120 | llvm::FoldingSetNodeID ID; |
| 6121 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 6122 | |
| 6123 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6124 | DependentTemplateName *QTN |
| 6125 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6126 | |
| 6127 | if (QTN) |
| 6128 | return TemplateName(QTN); |
| 6129 | |
| 6130 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 6131 | if (CanonNNS == NNS) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6132 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6133 | DependentTemplateName(NNS, Operator); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6134 | } else { |
| 6135 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6136 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6137 | DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6138 | |
| 6139 | DependentTemplateName *CheckQTN |
| 6140 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6141 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 6142 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6143 | } |
| 6144 | |
| 6145 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 6146 | return TemplateName(QTN); |
| 6147 | } |
| 6148 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6149 | TemplateName |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6150 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 6151 | TemplateName replacement) const { |
| 6152 | llvm::FoldingSetNodeID ID; |
| 6153 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 6154 | |
| 6155 | void *insertPos = 0; |
| 6156 | SubstTemplateTemplateParmStorage *subst |
| 6157 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 6158 | |
| 6159 | if (!subst) { |
| 6160 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 6161 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 6162 | } |
| 6163 | |
| 6164 | return TemplateName(subst); |
| 6165 | } |
| 6166 | |
| 6167 | TemplateName |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6168 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 6169 | const TemplateArgument &ArgPack) const { |
| 6170 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 6171 | llvm::FoldingSetNodeID ID; |
| 6172 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 6173 | |
| 6174 | void *InsertPos = 0; |
| 6175 | SubstTemplateTemplateParmPackStorage *Subst |
| 6176 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 6177 | |
| 6178 | if (!Subst) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6179 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6180 | ArgPack.pack_size(), |
| 6181 | ArgPack.pack_begin()); |
| 6182 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 6183 | } |
| 6184 | |
| 6185 | return TemplateName(Subst); |
| 6186 | } |
| 6187 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6188 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 6189 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 6190 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6191 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6192 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6193 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6194 | case TargetInfo::SignedShort: return ShortTy; |
| 6195 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 6196 | case TargetInfo::SignedInt: return IntTy; |
| 6197 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 6198 | case TargetInfo::SignedLong: return LongTy; |
| 6199 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 6200 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 6201 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 6202 | } |
| 6203 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6204 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6205 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 6206 | |
| 6207 | //===----------------------------------------------------------------------===// |
| 6208 | // Type Predicates. |
| 6209 | //===----------------------------------------------------------------------===// |
| 6210 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6211 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 6212 | /// garbage collection attribute. |
| 6213 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6214 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6215 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6216 | return Qualifiers::GCNone; |
| 6217 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6218 | assert(getLangOpts().ObjC1); |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6219 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 6220 | |
| 6221 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 6222 | // (or pointers to them) be treated as though they were declared |
| 6223 | // as __strong. |
| 6224 | if (GCAttrs == Qualifiers::GCNone) { |
| 6225 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 6226 | return Qualifiers::Strong; |
| 6227 | else if (Ty->isPointerType()) |
| 6228 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 6229 | } else { |
| 6230 | // It's not valid to set GC attributes on anything that isn't a |
| 6231 | // pointer. |
| 6232 | #ifndef NDEBUG |
| 6233 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 6234 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 6235 | CT = AT->getElementType(); |
| 6236 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 6237 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6238 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 6239 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6240 | } |
| 6241 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6242 | //===----------------------------------------------------------------------===// |
| 6243 | // Type Compatibility Testing |
| 6244 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 6245 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6246 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6247 | /// compatible. |
| 6248 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 6249 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 6250 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6251 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6252 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6253 | } |
| 6254 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6255 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 6256 | QualType SecondVec) { |
| 6257 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 6258 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 6259 | |
| 6260 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 6261 | return true; |
| 6262 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6263 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 6264 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6265 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 6266 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6267 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6268 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6269 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 6270 | First->getVectorKind() != VectorType::AltiVecBool && |
| 6271 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 6272 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6273 | return true; |
| 6274 | |
| 6275 | return false; |
| 6276 | } |
| 6277 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6278 | //===----------------------------------------------------------------------===// |
| 6279 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 6280 | //===----------------------------------------------------------------------===// |
| 6281 | |
| 6282 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 6283 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6284 | bool |
| 6285 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 6286 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 6287 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6288 | return true; |
| 6289 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 6290 | E = rProto->protocol_end(); PI != E; ++PI) |
| 6291 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 6292 | return true; |
| 6293 | return false; |
| 6294 | } |
| 6295 | |
Dmitri Gribenko | 4c3b8a3 | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 6296 | /// QualifiedIdConformsQualifiedId - compare id<pr,...> with id<pr1,...> |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6297 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 6298 | /// otherwise. |
| 6299 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 6300 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 6301 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 6302 | return false; |
| 6303 | } |
| 6304 | |
Dmitri Gribenko | 4c3b8a3 | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 6305 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
| 6306 | /// Class<pr1, ...>. |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6307 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 6308 | QualType rhs) { |
| 6309 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 6310 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 6311 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 6312 | |
| 6313 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6314 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6315 | bool match = false; |
| 6316 | ObjCProtocolDecl *lhsProto = *I; |
| 6317 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 6318 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 6319 | ObjCProtocolDecl *rhsProto = *J; |
| 6320 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 6321 | match = true; |
| 6322 | break; |
| 6323 | } |
| 6324 | } |
| 6325 | if (!match) |
| 6326 | return false; |
| 6327 | } |
| 6328 | return true; |
| 6329 | } |
| 6330 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6331 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 6332 | /// ObjCQualifiedIDType. |
| 6333 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 6334 | bool compare) { |
| 6335 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6336 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6337 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 6338 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6339 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6340 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 6341 | return true; |
| 6342 | |
| 6343 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6344 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6345 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6346 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6347 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6348 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6349 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6350 | // make sure we check the class hierarchy. |
| 6351 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 6352 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6353 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6354 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6355 | // see if static class implements all of id's protocols, directly or |
| 6356 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 6357 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6358 | return false; |
| 6359 | } |
| 6360 | } |
| 6361 | // If there are no qualifiers and no interface, we have an 'id'. |
| 6362 | return true; |
| 6363 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6364 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6365 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6366 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6367 | ObjCProtocolDecl *lhsProto = *I; |
| 6368 | bool match = false; |
| 6369 | |
| 6370 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6371 | // see if static class implements all of id's protocols, directly or |
| 6372 | // through its super class and categories. |
| 6373 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 6374 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 6375 | ObjCProtocolDecl *rhsProto = *J; |
| 6376 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6377 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6378 | match = true; |
| 6379 | break; |
| 6380 | } |
| 6381 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6382 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6383 | // make sure we check the class hierarchy. |
| 6384 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 6385 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6386 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6387 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6388 | // see if static class implements all of id's protocols, directly or |
| 6389 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 6390 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6391 | match = true; |
| 6392 | break; |
| 6393 | } |
| 6394 | } |
| 6395 | } |
| 6396 | if (!match) |
| 6397 | return false; |
| 6398 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6399 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6400 | return true; |
| 6401 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6402 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6403 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 6404 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 6405 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6406 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6407 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6408 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6409 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 6410 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 6411 | ObjCProtocolDecl *lhsProto = *I; |
| 6412 | bool match = false; |
| 6413 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6414 | // 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] | 6415 | // see if static class implements all of id's protocols, directly or |
| 6416 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6417 | // First, lhs protocols in the qualifier list must be found, direct |
| 6418 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6419 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 6420 | E = rhsQID->qual_end(); J != E; ++J) { |
| 6421 | ObjCProtocolDecl *rhsProto = *J; |
| 6422 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6423 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6424 | match = true; |
| 6425 | break; |
| 6426 | } |
| 6427 | } |
| 6428 | if (!match) |
| 6429 | return false; |
| 6430 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6431 | |
| 6432 | // Static class's protocols, or its super class or category protocols |
| 6433 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 6434 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 6435 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 6436 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 6437 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 6438 | // no type qualifier and its class has no static protocol(s) |
| 6439 | // assume that it is mismatch. |
| 6440 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 6441 | return false; |
| 6442 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6443 | LHSInheritedProtocols.begin(), |
| 6444 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 6445 | bool match = false; |
| 6446 | ObjCProtocolDecl *lhsProto = (*I); |
| 6447 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 6448 | E = rhsQID->qual_end(); J != E; ++J) { |
| 6449 | ObjCProtocolDecl *rhsProto = *J; |
| 6450 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6451 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6452 | match = true; |
| 6453 | break; |
| 6454 | } |
| 6455 | } |
| 6456 | if (!match) |
| 6457 | return false; |
| 6458 | } |
| 6459 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6460 | return true; |
| 6461 | } |
| 6462 | return false; |
| 6463 | } |
| 6464 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6465 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6466 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 6467 | /// protocol qualifiers on the LHS or RHS. |
| 6468 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6469 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 6470 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6471 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6472 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6473 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 6474 | // 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] | 6475 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 6476 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6477 | return true; |
| 6478 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6479 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6480 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6481 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6482 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6483 | |
| 6484 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 6485 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 6486 | QualType(RHSOPT,0)); |
| 6487 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6488 | // If we have 2 user-defined types, fall into that path. |
| 6489 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6490 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6491 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6492 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6493 | } |
| 6494 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6495 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 6496 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6497 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 6498 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 6499 | /// not OK. For the return type, the opposite is not OK. |
| 6500 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 6501 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6502 | const ObjCObjectPointerType *RHSOPT, |
| 6503 | bool BlockReturnType) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6504 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6505 | return true; |
| 6506 | |
| 6507 | if (LHSOPT->isObjCBuiltinType()) { |
| 6508 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 6509 | } |
| 6510 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6511 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6512 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6513 | QualType(RHSOPT,0), |
| 6514 | false); |
| 6515 | |
| 6516 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 6517 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 6518 | if (LHS && RHS) { // We have 2 user-defined types. |
| 6519 | if (LHS != RHS) { |
| 6520 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6521 | return BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6522 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6523 | return !BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6524 | } |
| 6525 | else |
| 6526 | return true; |
| 6527 | } |
| 6528 | return false; |
| 6529 | } |
| 6530 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6531 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 6532 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 6533 | /// It is used to build composite qualifier list of the composite type of |
| 6534 | /// the conditional expression involving two objective-c pointer objects. |
| 6535 | static |
| 6536 | void getIntersectionOfProtocols(ASTContext &Context, |
| 6537 | const ObjCObjectPointerType *LHSOPT, |
| 6538 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6539 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6540 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6541 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6542 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6543 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 6544 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6545 | |
| 6546 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 6547 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 6548 | if (LHSNumProtocols > 0) |
| 6549 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 6550 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6551 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6552 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 6553 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6554 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 6555 | LHSInheritedProtocols.end()); |
| 6556 | } |
| 6557 | |
| 6558 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 6559 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 6560 | ObjCProtocolDecl **RHSProtocols = |
| 6561 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6562 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 6563 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 6564 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 6565 | } else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6566 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6567 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 6568 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6569 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6570 | RHSInheritedProtocols.begin(), |
| 6571 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 6572 | if (InheritedProtocolSet.count((*I))) |
| 6573 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6574 | } |
| 6575 | } |
| 6576 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6577 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 6578 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 6579 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 6580 | /// warning is issued. So, its invokation is extremely rare. |
| 6581 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6582 | const ObjCObjectPointerType *Lptr, |
| 6583 | const ObjCObjectPointerType *Rptr) { |
| 6584 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 6585 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 6586 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 6587 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 6588 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6589 | return QualType(); |
| 6590 | |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 6591 | do { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6592 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6593 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6594 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6595 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 6596 | |
| 6597 | QualType Result = QualType(LHS, 0); |
| 6598 | if (!Protocols.empty()) |
| 6599 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 6600 | Result = getObjCObjectPointerType(Result); |
| 6601 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6602 | } |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 6603 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6604 | |
| 6605 | return QualType(); |
| 6606 | } |
| 6607 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6608 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 6609 | const ObjCObjectType *RHS) { |
| 6610 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 6611 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 6612 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6613 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 6614 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6615 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6616 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6617 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6618 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 6619 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 6620 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6621 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6622 | |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6623 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 6624 | // more detailed analysis is required. |
| 6625 | if (RHS->getNumProtocols() == 0) { |
| 6626 | // OK, if LHS is a superclass of RHS *and* |
| 6627 | // this superclass is assignment compatible with LHS. |
| 6628 | // false otherwise. |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 6629 | bool IsSuperClass = |
| 6630 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 6631 | if (IsSuperClass) { |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6632 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 6633 | // ; i.e., SuperClass may implement at least one of the protocols |
| 6634 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 6635 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 6636 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 6637 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6638 | // If super class has no protocols, it is not a match. |
| 6639 | if (SuperClassInheritedProtocols.empty()) |
| 6640 | return false; |
| 6641 | |
| 6642 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 6643 | LHSPE = LHS->qual_end(); |
| 6644 | LHSPI != LHSPE; LHSPI++) { |
| 6645 | bool SuperImplementsProtocol = false; |
| 6646 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 6647 | |
| 6648 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6649 | SuperClassInheritedProtocols.begin(), |
| 6650 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 6651 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 6652 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 6653 | SuperImplementsProtocol = true; |
| 6654 | break; |
| 6655 | } |
| 6656 | } |
| 6657 | if (!SuperImplementsProtocol) |
| 6658 | return false; |
| 6659 | } |
| 6660 | return true; |
| 6661 | } |
| 6662 | return false; |
| 6663 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6664 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6665 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 6666 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6667 | LHSPI != LHSPE; LHSPI++) { |
| 6668 | bool RHSImplementsProtocol = false; |
| 6669 | |
| 6670 | // If the RHS doesn't implement the protocol on the left, the types |
| 6671 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6672 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 6673 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 6674 | RHSPI != RHSPE; RHSPI++) { |
| 6675 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6676 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 6677 | break; |
| 6678 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6679 | } |
| 6680 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 6681 | if (!RHSImplementsProtocol) |
| 6682 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6683 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6684 | // The RHS implements all protocols listed on the LHS. |
| 6685 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6686 | } |
| 6687 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6688 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 6689 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6690 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 6691 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6692 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6693 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6694 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6695 | |
| 6696 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 6697 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6698 | } |
| 6699 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6700 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 6701 | return canAssignObjCInterfaces( |
| 6702 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 6703 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 6704 | } |
| 6705 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6706 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6707 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6708 | /// 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] | 6709 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6710 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 6711 | bool CompareUnqualified) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6712 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6713 | return hasSameType(LHS, RHS); |
| 6714 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6715 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6716 | } |
| 6717 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6718 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | 8237839 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 6719 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6720 | } |
| 6721 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6722 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 6723 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 6724 | } |
| 6725 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6726 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 6727 | /// of T is compatible with SubType, return the merged type, else return |
| 6728 | /// QualType() |
| 6729 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 6730 | bool OfBlockPointer, |
| 6731 | bool Unqualified) { |
| 6732 | if (const RecordType *UT = T->getAsUnionType()) { |
| 6733 | RecordDecl *UD = UT->getDecl(); |
| 6734 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 6735 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6736 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 6737 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6738 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 6739 | if (!MT.isNull()) |
| 6740 | return MT; |
| 6741 | } |
| 6742 | } |
| 6743 | } |
| 6744 | |
| 6745 | return QualType(); |
| 6746 | } |
| 6747 | |
| 6748 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 6749 | /// argument types |
| 6750 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 6751 | bool OfBlockPointer, |
| 6752 | bool Unqualified) { |
| 6753 | // GNU extension: two types are compatible if they appear as a function |
| 6754 | // argument, one of the types is a transparent union type and the other |
| 6755 | // type is compatible with a union member |
| 6756 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 6757 | Unqualified); |
| 6758 | if (!lmerge.isNull()) |
| 6759 | return lmerge; |
| 6760 | |
| 6761 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 6762 | Unqualified); |
| 6763 | if (!rmerge.isNull()) |
| 6764 | return rmerge; |
| 6765 | |
| 6766 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 6767 | } |
| 6768 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6769 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6770 | bool OfBlockPointer, |
| 6771 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6772 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 6773 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6774 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 6775 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6776 | bool allLTypes = true; |
| 6777 | bool allRTypes = true; |
| 6778 | |
| 6779 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6780 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6781 | if (OfBlockPointer) { |
| 6782 | QualType RHS = rbase->getResultType(); |
| 6783 | QualType LHS = lbase->getResultType(); |
| 6784 | bool UnqualifiedResult = Unqualified; |
| 6785 | if (!UnqualifiedResult) |
| 6786 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6787 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6788 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6789 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6790 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 6791 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6792 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6793 | |
| 6794 | if (Unqualified) |
| 6795 | retType = retType.getUnqualifiedType(); |
| 6796 | |
| 6797 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 6798 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 6799 | if (Unqualified) { |
| 6800 | LRetType = LRetType.getUnqualifiedType(); |
| 6801 | RRetType = RRetType.getUnqualifiedType(); |
| 6802 | } |
| 6803 | |
| 6804 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6805 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6806 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6807 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6808 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 6809 | // FIXME: double check this |
| 6810 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 6811 | // rbase->getRegParmAttr() != 0 && |
| 6812 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 6813 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 6814 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6815 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6816 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6817 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6818 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6819 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6820 | // Regparm is part of the calling convention. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 6821 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 6822 | return QualType(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6823 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 6824 | return QualType(); |
| 6825 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6826 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 6827 | return QualType(); |
| 6828 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6829 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 6830 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6831 | |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 6832 | if (lbaseInfo.getNoReturn() != NoReturn) |
| 6833 | allLTypes = false; |
| 6834 | if (rbaseInfo.getNoReturn() != NoReturn) |
| 6835 | allRTypes = false; |
| 6836 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6837 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6838 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6839 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6840 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 6841 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6842 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 6843 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 6844 | |
| 6845 | // Compatible functions must have the same number of arguments |
| 6846 | if (lproto_nargs != rproto_nargs) |
| 6847 | return QualType(); |
| 6848 | |
| 6849 | // Variadic and non-variadic functions aren't compatible |
| 6850 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 6851 | return QualType(); |
| 6852 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 6853 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 6854 | return QualType(); |
| 6855 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6856 | if (LangOpts.ObjCAutoRefCount && |
| 6857 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 6858 | return QualType(); |
| 6859 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6860 | // Check argument compatibility |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6861 | SmallVector<QualType, 10> types; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6862 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 6863 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 6864 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6865 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 6866 | OfBlockPointer, |
| 6867 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6868 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6869 | |
| 6870 | if (Unqualified) |
| 6871 | argtype = argtype.getUnqualifiedType(); |
| 6872 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6873 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6874 | if (Unqualified) { |
| 6875 | largtype = largtype.getUnqualifiedType(); |
| 6876 | rargtype = rargtype.getUnqualifiedType(); |
| 6877 | } |
| 6878 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6879 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 6880 | allLTypes = false; |
| 6881 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 6882 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6883 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6884 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6885 | if (allLTypes) return lhs; |
| 6886 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6887 | |
| 6888 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 6889 | EPI.ExtInfo = einfo; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 6890 | return getFunctionType(retType, types, EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6891 | } |
| 6892 | |
| 6893 | if (lproto) allRTypes = false; |
| 6894 | if (rproto) allLTypes = false; |
| 6895 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6896 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6897 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6898 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6899 | if (proto->isVariadic()) return QualType(); |
| 6900 | // Check that the types are compatible with the types that |
| 6901 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 6902 | // The only types actually affected are promotable integer |
| 6903 | // types and floats, which would be passed as a different |
| 6904 | // type depending on whether the prototype is visible. |
| 6905 | unsigned proto_nargs = proto->getNumArgs(); |
| 6906 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 6907 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 6908 | |
Eli Friedman | c586d5d | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 6909 | // 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] | 6910 | // to pass enum values. |
Eli Friedman | c586d5d | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 6911 | if (const EnumType *Enum = argTy->getAs<EnumType>()) { |
| 6912 | argTy = Enum->getDecl()->getIntegerType(); |
| 6913 | if (argTy.isNull()) |
| 6914 | return QualType(); |
| 6915 | } |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 6916 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6917 | if (argTy->isPromotableIntegerType() || |
| 6918 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 6919 | return QualType(); |
| 6920 | } |
| 6921 | |
| 6922 | if (allLTypes) return lhs; |
| 6923 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6924 | |
| 6925 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 6926 | EPI.ExtInfo = einfo; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 6927 | return getFunctionType(retType, |
| 6928 | ArrayRef<QualType>(proto->arg_type_begin(), |
| 6929 | proto->getNumArgs()), |
| 6930 | EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6931 | } |
| 6932 | |
| 6933 | if (allLTypes) return lhs; |
| 6934 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6935 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6936 | } |
| 6937 | |
John McCall | b9da713 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 6938 | /// Given that we have an enum type and a non-enum type, try to merge them. |
| 6939 | static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, |
| 6940 | QualType other, bool isBlockReturnType) { |
| 6941 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 6942 | // a signed integer type, or an unsigned integer type. |
| 6943 | // Compatibility is based on the underlying type, not the promotion |
| 6944 | // type. |
| 6945 | QualType underlyingType = ET->getDecl()->getIntegerType(); |
| 6946 | if (underlyingType.isNull()) return QualType(); |
| 6947 | if (Context.hasSameType(underlyingType, other)) |
| 6948 | return other; |
| 6949 | |
| 6950 | // In block return types, we're more permissive and accept any |
| 6951 | // integral type of the same size. |
| 6952 | if (isBlockReturnType && other->isIntegerType() && |
| 6953 | Context.getTypeSize(underlyingType) == Context.getTypeSize(other)) |
| 6954 | return other; |
| 6955 | |
| 6956 | return QualType(); |
| 6957 | } |
| 6958 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6959 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6960 | bool OfBlockPointer, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6961 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 6962 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 6963 | // type is adjusted to "T" prior to any further analysis, the expression |
| 6964 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6965 | // expression is an lvalue unless the reference is an rvalue reference and |
| 6966 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6967 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 6968 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6969 | |
| 6970 | if (Unqualified) { |
| 6971 | LHS = LHS.getUnqualifiedType(); |
| 6972 | RHS = RHS.getUnqualifiedType(); |
| 6973 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6974 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6975 | QualType LHSCan = getCanonicalType(LHS), |
| 6976 | RHSCan = getCanonicalType(RHS); |
| 6977 | |
| 6978 | // If two types are identical, they are compatible. |
| 6979 | if (LHSCan == RHSCan) |
| 6980 | return LHS; |
| 6981 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6982 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6983 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6984 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6985 | if (LQuals != RQuals) { |
| 6986 | // If any of these qualifiers are different, we have a type |
| 6987 | // mismatch. |
| 6988 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6989 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 6990 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6991 | return QualType(); |
| 6992 | |
| 6993 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6994 | // okay if the other type has no GC qualifier but is an Objective |
| 6995 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6996 | // this by pretending that the unqualified type was actually |
| 6997 | // qualified __strong. |
| 6998 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6999 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 7000 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 7001 | |
| 7002 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 7003 | return QualType(); |
| 7004 | |
| 7005 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 7006 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 7007 | } |
| 7008 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 7009 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 7010 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7011 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7012 | } |
| 7013 | |
| 7014 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7015 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 7016 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 7017 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7018 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7019 | // We want to consider the two function types to be the same for these |
| 7020 | // comparisons, just force one to the other. |
| 7021 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 7022 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 7023 | |
| 7024 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 7025 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 7026 | LHSClass = Type::ConstantArray; |
| 7027 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 7028 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7029 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7030 | // ObjCInterfaces are just specialized ObjCObjects. |
| 7031 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 7032 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 7033 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 7034 | // Canonicalize ExtVector -> Vector. |
| 7035 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 7036 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7037 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 7038 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7039 | if (LHSClass != RHSClass) { |
John McCall | b9da713 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7040 | // Note that we only have special rules for turning block enum |
| 7041 | // returns into block int returns, not vice-versa. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7042 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
John McCall | b9da713 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7043 | return mergeEnumWithInteger(*this, ETy, RHS, false); |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 7044 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7045 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
John McCall | b9da713 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7046 | return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType); |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 7047 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 7048 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 4196363 | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 7049 | if (OfBlockPointer && !BlockReturnType) { |
| 7050 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 7051 | return LHS; |
| 7052 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 7053 | return RHS; |
| 7054 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 7055 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7056 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7057 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7058 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 7059 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7060 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7061 | #define TYPE(Class, Base) |
| 7062 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 7063 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7064 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 7065 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 7066 | #include "clang/AST/TypeNodes.def" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7067 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7068 | |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 7069 | case Type::Auto: |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 7070 | case Type::LValueReference: |
| 7071 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7072 | case Type::MemberPointer: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7073 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7074 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7075 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7076 | case Type::IncompleteArray: |
| 7077 | case Type::VariableArray: |
| 7078 | case Type::FunctionProto: |
| 7079 | case Type::ExtVector: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7080 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7081 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7082 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7083 | { |
| 7084 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7085 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 7086 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7087 | if (Unqualified) { |
| 7088 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 7089 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 7090 | } |
| 7091 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 7092 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7093 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 7094 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7095 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 7096 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7097 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7098 | return getPointerType(ResultType); |
| 7099 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 7100 | case Type::BlockPointer: |
| 7101 | { |
| 7102 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7103 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 7104 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7105 | if (Unqualified) { |
| 7106 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 7107 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 7108 | } |
| 7109 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 7110 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 7111 | if (ResultType.isNull()) return QualType(); |
| 7112 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 7113 | return LHS; |
| 7114 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 7115 | return RHS; |
| 7116 | return getBlockPointerType(ResultType); |
| 7117 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7118 | case Type::Atomic: |
| 7119 | { |
| 7120 | // Merge two pointer types, while trying to preserve typedef info |
| 7121 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 7122 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 7123 | if (Unqualified) { |
| 7124 | LHSValue = LHSValue.getUnqualifiedType(); |
| 7125 | RHSValue = RHSValue.getUnqualifiedType(); |
| 7126 | } |
| 7127 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 7128 | Unqualified); |
| 7129 | if (ResultType.isNull()) return QualType(); |
| 7130 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 7131 | return LHS; |
| 7132 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 7133 | return RHS; |
| 7134 | return getAtomicType(ResultType); |
| 7135 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7136 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7137 | { |
| 7138 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 7139 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 7140 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 7141 | return QualType(); |
| 7142 | |
| 7143 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 7144 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7145 | if (Unqualified) { |
| 7146 | LHSElem = LHSElem.getUnqualifiedType(); |
| 7147 | RHSElem = RHSElem.getUnqualifiedType(); |
| 7148 | } |
| 7149 | |
| 7150 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7151 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7152 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 7153 | return LHS; |
| 7154 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 7155 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 7156 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 7157 | ArrayType::ArraySizeModifier(), 0); |
| 7158 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 7159 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7160 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 7161 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7162 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 7163 | return LHS; |
| 7164 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 7165 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7166 | if (LVAT) { |
| 7167 | // FIXME: This isn't correct! But tricky to implement because |
| 7168 | // the array's size has to be the size of LHS, but the type |
| 7169 | // has to be different. |
| 7170 | return LHS; |
| 7171 | } |
| 7172 | if (RVAT) { |
| 7173 | // FIXME: This isn't correct! But tricky to implement because |
| 7174 | // the array's size has to be the size of RHS, but the type |
| 7175 | // has to be different. |
| 7176 | return RHS; |
| 7177 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 7178 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 7179 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 7180 | return getIncompleteArrayType(ResultType, |
| 7181 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7182 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7183 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7184 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7185 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7186 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7187 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7188 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 7189 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7190 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 7191 | case Type::Complex: |
| 7192 | // Distinct complex types are incompatible. |
| 7193 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 7194 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 7195 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 7196 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 7197 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7198 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7199 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7200 | case Type::ObjCObject: { |
| 7201 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 7202 | // FIXME: This should be type compatibility, e.g. whether |
| 7203 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7204 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 7205 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 7206 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 7207 | return LHS; |
| 7208 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7209 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 7210 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7211 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7212 | if (OfBlockPointer) { |
| 7213 | if (canAssignObjCInterfacesInBlockPointer( |
| 7214 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7215 | RHS->getAs<ObjCObjectPointerType>(), |
| 7216 | BlockReturnType)) |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7217 | return LHS; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7218 | return QualType(); |
| 7219 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7220 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 7221 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7222 | return LHS; |
| 7223 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 7224 | return QualType(); |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7225 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7226 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7227 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7228 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7229 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 7230 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7231 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 7232 | const FunctionProtoType *FromFunctionType, |
| 7233 | const FunctionProtoType *ToFunctionType) { |
| 7234 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 7235 | ToFunctionType->hasAnyConsumedArgs()) |
| 7236 | return false; |
| 7237 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 7238 | FromFunctionType->getExtProtoInfo(); |
| 7239 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 7240 | ToFunctionType->getExtProtoInfo(); |
| 7241 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 7242 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 7243 | ArgIdx != NumArgs; ++ArgIdx) { |
| 7244 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 7245 | ToEPI.ConsumedArguments[ArgIdx]) |
| 7246 | return false; |
| 7247 | } |
| 7248 | return true; |
| 7249 | } |
| 7250 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7251 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 7252 | /// 'RHS' attributes and returns the merged version; including for function |
| 7253 | /// return types. |
| 7254 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 7255 | QualType LHSCan = getCanonicalType(LHS), |
| 7256 | RHSCan = getCanonicalType(RHS); |
| 7257 | // If two types are identical, they are compatible. |
| 7258 | if (LHSCan == RHSCan) |
| 7259 | return LHS; |
| 7260 | if (RHSCan->isFunctionType()) { |
| 7261 | if (!LHSCan->isFunctionType()) |
| 7262 | return QualType(); |
| 7263 | QualType OldReturnType = |
| 7264 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 7265 | QualType NewReturnType = |
| 7266 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 7267 | QualType ResReturnType = |
| 7268 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 7269 | if (ResReturnType.isNull()) |
| 7270 | return QualType(); |
| 7271 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 7272 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 7273 | // In either case, use OldReturnType to build the new function type. |
| 7274 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 7275 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7276 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7277 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7278 | QualType ResultType |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 7279 | = getFunctionType(OldReturnType, |
| 7280 | ArrayRef<QualType>(FPT->arg_type_begin(), |
| 7281 | FPT->getNumArgs()), |
| 7282 | EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7283 | return ResultType; |
| 7284 | } |
| 7285 | } |
| 7286 | return QualType(); |
| 7287 | } |
| 7288 | |
| 7289 | // If the qualifiers are different, the types can still be merged. |
| 7290 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 7291 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 7292 | if (LQuals != RQuals) { |
| 7293 | // If any of these qualifiers are different, we have a type mismatch. |
| 7294 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 7295 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 7296 | return QualType(); |
| 7297 | |
| 7298 | // Exactly one GC qualifier difference is allowed: __strong is |
| 7299 | // okay if the other type has no GC qualifier but is an Objective |
| 7300 | // C object pointer (i.e. implicitly strong by default). We fix |
| 7301 | // this by pretending that the unqualified type was actually |
| 7302 | // qualified __strong. |
| 7303 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 7304 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 7305 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 7306 | |
| 7307 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 7308 | return QualType(); |
| 7309 | |
| 7310 | if (GC_L == Qualifiers::Strong) |
| 7311 | return LHS; |
| 7312 | if (GC_R == Qualifiers::Strong) |
| 7313 | return RHS; |
| 7314 | return QualType(); |
| 7315 | } |
| 7316 | |
| 7317 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 7318 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7319 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7320 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 7321 | if (ResQT == LHSBaseQT) |
| 7322 | return LHS; |
| 7323 | if (ResQT == RHSBaseQT) |
| 7324 | return RHS; |
| 7325 | } |
| 7326 | return QualType(); |
| 7327 | } |
| 7328 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 7329 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7330 | // Integer Predicates |
| 7331 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 7332 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7333 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7334 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 7335 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7336 | if (T->isBooleanType()) |
| 7337 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 7338 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7339 | return (unsigned)getTypeSize(T); |
| 7340 | } |
| 7341 | |
Abramo Bagnara | 762f159 | 2012-09-09 10:21:24 +0000 | [diff] [blame] | 7342 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7343 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7344 | |
| 7345 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 7346 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 7347 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7348 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7349 | |
| 7350 | // For enums, we return the unsigned version of the base type. |
| 7351 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7352 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7353 | |
| 7354 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 7355 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7356 | switch (BTy->getKind()) { |
| 7357 | case BuiltinType::Char_S: |
| 7358 | case BuiltinType::SChar: |
| 7359 | return UnsignedCharTy; |
| 7360 | case BuiltinType::Short: |
| 7361 | return UnsignedShortTy; |
| 7362 | case BuiltinType::Int: |
| 7363 | return UnsignedIntTy; |
| 7364 | case BuiltinType::Long: |
| 7365 | return UnsignedLongTy; |
| 7366 | case BuiltinType::LongLong: |
| 7367 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 7368 | case BuiltinType::Int128: |
| 7369 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7370 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7371 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7372 | } |
| 7373 | } |
| 7374 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 7375 | ASTMutationListener::~ASTMutationListener() { } |
| 7376 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7377 | |
| 7378 | //===----------------------------------------------------------------------===// |
| 7379 | // Builtin Type Computation |
| 7380 | //===----------------------------------------------------------------------===// |
| 7381 | |
| 7382 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7383 | /// pointer over the consumed characters. This returns the resultant type. If |
| 7384 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 7385 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 7386 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7387 | /// |
| 7388 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 7389 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7390 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7391 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7392 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7393 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7394 | // Modifiers. |
| 7395 | int HowLong = 0; |
| 7396 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7397 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7398 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7399 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7400 | bool Done = false; |
| 7401 | while (!Done) { |
| 7402 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7403 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7404 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7405 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7406 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7407 | case 'S': |
| 7408 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 7409 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 7410 | Signed = true; |
| 7411 | break; |
| 7412 | case 'U': |
| 7413 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 7414 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 7415 | Unsigned = true; |
| 7416 | break; |
| 7417 | case 'L': |
| 7418 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 7419 | ++HowLong; |
| 7420 | break; |
| 7421 | } |
| 7422 | } |
| 7423 | |
| 7424 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7425 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7426 | // Read the base type. |
| 7427 | switch (*Str++) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7428 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7429 | case 'v': |
| 7430 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7431 | "Bad modifiers used with 'v'!"); |
| 7432 | Type = Context.VoidTy; |
| 7433 | break; |
| 7434 | case 'f': |
| 7435 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7436 | "Bad modifiers used with 'f'!"); |
| 7437 | Type = Context.FloatTy; |
| 7438 | break; |
| 7439 | case 'd': |
| 7440 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 7441 | "Bad modifiers used with 'd'!"); |
| 7442 | if (HowLong) |
| 7443 | Type = Context.LongDoubleTy; |
| 7444 | else |
| 7445 | Type = Context.DoubleTy; |
| 7446 | break; |
| 7447 | case 's': |
| 7448 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 7449 | if (Unsigned) |
| 7450 | Type = Context.UnsignedShortTy; |
| 7451 | else |
| 7452 | Type = Context.ShortTy; |
| 7453 | break; |
| 7454 | case 'i': |
| 7455 | if (HowLong == 3) |
| 7456 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 7457 | else if (HowLong == 2) |
| 7458 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 7459 | else if (HowLong == 1) |
| 7460 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 7461 | else |
| 7462 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 7463 | break; |
| 7464 | case 'c': |
| 7465 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 7466 | if (Signed) |
| 7467 | Type = Context.SignedCharTy; |
| 7468 | else if (Unsigned) |
| 7469 | Type = Context.UnsignedCharTy; |
| 7470 | else |
| 7471 | Type = Context.CharTy; |
| 7472 | break; |
| 7473 | case 'b': // boolean |
| 7474 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 7475 | Type = Context.BoolTy; |
| 7476 | break; |
| 7477 | case 'z': // size_t. |
| 7478 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 7479 | Type = Context.getSizeType(); |
| 7480 | break; |
| 7481 | case 'F': |
| 7482 | Type = Context.getCFConstantStringType(); |
| 7483 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 7484 | case 'G': |
| 7485 | Type = Context.getObjCIdType(); |
| 7486 | break; |
| 7487 | case 'H': |
| 7488 | Type = Context.getObjCSelType(); |
| 7489 | break; |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 7490 | case 'M': |
| 7491 | Type = Context.getObjCSuperType(); |
| 7492 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7493 | case 'a': |
| 7494 | Type = Context.getBuiltinVaListType(); |
| 7495 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 7496 | break; |
| 7497 | case 'A': |
| 7498 | // This is a "reference" to a va_list; however, what exactly |
| 7499 | // this means depends on how va_list is defined. There are two |
| 7500 | // different kinds of va_list: ones passed by value, and ones |
| 7501 | // passed by reference. An example of a by-value va_list is |
| 7502 | // x86, where va_list is a char*. An example of by-ref va_list |
| 7503 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 7504 | // we want this argument to be a char*&; for x86-64, we want |
| 7505 | // it to be a __va_list_tag*. |
| 7506 | Type = Context.getBuiltinVaListType(); |
| 7507 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7508 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7509 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7510 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7511 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7512 | break; |
| 7513 | case 'V': { |
| 7514 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7515 | unsigned NumElements = strtoul(Str, &End, 10); |
| 7516 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7517 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7518 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7519 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 7520 | RequiresICE, false); |
| 7521 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7522 | |
| 7523 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 7524 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7525 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7526 | break; |
| 7527 | } |
Douglas Gregor | b4bc99b | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 7528 | case 'E': { |
| 7529 | char *End; |
| 7530 | |
| 7531 | unsigned NumElements = strtoul(Str, &End, 10); |
| 7532 | assert(End != Str && "Missing vector size"); |
| 7533 | |
| 7534 | Str = End; |
| 7535 | |
| 7536 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 7537 | false); |
| 7538 | Type = Context.getExtVectorType(ElementType, NumElements); |
| 7539 | break; |
| 7540 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 7541 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7542 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 7543 | false); |
| 7544 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 7545 | Type = Context.getComplexType(ElementType); |
| 7546 | break; |
Fariborz Jahanian | cc075e4 | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 7547 | } |
| 7548 | case 'Y' : { |
| 7549 | Type = Context.getPointerDiffType(); |
| 7550 | break; |
| 7551 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 7552 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 7553 | Type = Context.getFILEType(); |
| 7554 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7555 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7556 | return QualType(); |
| 7557 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7558 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 7559 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7560 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 7561 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7562 | else |
| 7563 | Type = Context.getjmp_bufType(); |
| 7564 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7565 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7566 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7567 | return QualType(); |
| 7568 | } |
| 7569 | break; |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 7570 | case 'K': |
| 7571 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 7572 | Type = Context.getucontext_tType(); |
| 7573 | |
| 7574 | if (Type.isNull()) { |
| 7575 | Error = ASTContext::GE_Missing_ucontext; |
| 7576 | return QualType(); |
| 7577 | } |
| 7578 | break; |
Eli Friedman | 6902e41 | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 7579 | case 'p': |
| 7580 | Type = Context.getProcessIDType(); |
| 7581 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 7582 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7583 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7584 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 7585 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7586 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 7587 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7588 | default: Done = true; --Str; break; |
| 7589 | case '*': |
| 7590 | case '&': { |
| 7591 | // Both pointers and references can have their pointee types |
| 7592 | // qualified with an address space. |
| 7593 | char *End; |
| 7594 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 7595 | if (End != Str && AddrSpace != 0) { |
| 7596 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 7597 | Str = End; |
| 7598 | } |
| 7599 | if (c == '*') |
| 7600 | Type = Context.getPointerType(Type); |
| 7601 | else |
| 7602 | Type = Context.getLValueReferenceType(Type); |
| 7603 | break; |
| 7604 | } |
| 7605 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 7606 | case 'C': |
| 7607 | Type = Type.withConst(); |
| 7608 | break; |
| 7609 | case 'D': |
| 7610 | Type = Context.getVolatileType(Type); |
| 7611 | break; |
Ted Kremenek | 18932a0 | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 7612 | case 'R': |
| 7613 | Type = Type.withRestrict(); |
| 7614 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7615 | } |
| 7616 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7617 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7618 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7619 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7620 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7621 | return Type; |
| 7622 | } |
| 7623 | |
| 7624 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7625 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7626 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7627 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7628 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7629 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7630 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7631 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7632 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7633 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7634 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 7635 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7636 | if (Error != GE_None) |
| 7637 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7638 | |
| 7639 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 7640 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7641 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7642 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7643 | if (Error != GE_None) |
| 7644 | return QualType(); |
| 7645 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7646 | // If this argument is required to be an IntegerConstantExpression and the |
| 7647 | // caller cares, fill in the bitmask we return. |
| 7648 | if (RequiresICE && IntegerConstantArgs) |
| 7649 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 7650 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7651 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 7652 | if (Ty->isArrayType()) |
| 7653 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7654 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7655 | ArgTypes.push_back(Ty); |
| 7656 | } |
| 7657 | |
| 7658 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 7659 | "'.' should only occur at end of builtin type list!"); |
| 7660 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 7661 | FunctionType::ExtInfo EI; |
| 7662 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 7663 | |
| 7664 | bool Variadic = (TypeStr[0] == '.'); |
| 7665 | |
| 7666 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 7667 | if (ArgTypes.empty() && Variadic) |
| 7668 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 7669 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7670 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 7671 | EPI.ExtInfo = EI; |
| 7672 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7673 | |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 7674 | return getFunctionType(ResType, ArgTypes, EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7675 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 7676 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7677 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 7678 | GVALinkage External = GVA_StrongExternal; |
| 7679 | |
| 7680 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7681 | switch (L) { |
| 7682 | case NoLinkage: |
| 7683 | case InternalLinkage: |
| 7684 | case UniqueExternalLinkage: |
| 7685 | return GVA_Internal; |
| 7686 | |
| 7687 | case ExternalLinkage: |
| 7688 | switch (FD->getTemplateSpecializationKind()) { |
| 7689 | case TSK_Undeclared: |
| 7690 | case TSK_ExplicitSpecialization: |
| 7691 | External = GVA_StrongExternal; |
| 7692 | break; |
| 7693 | |
| 7694 | case TSK_ExplicitInstantiationDefinition: |
| 7695 | return GVA_ExplicitTemplateInstantiation; |
| 7696 | |
| 7697 | case TSK_ExplicitInstantiationDeclaration: |
| 7698 | case TSK_ImplicitInstantiation: |
| 7699 | External = GVA_TemplateInstantiation; |
| 7700 | break; |
| 7701 | } |
| 7702 | } |
| 7703 | |
| 7704 | if (!FD->isInlined()) |
| 7705 | return External; |
| 7706 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7707 | if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7708 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 7709 | // externally visible. |
| 7710 | if (FD->isInlineDefinitionExternallyVisible()) |
| 7711 | return External; |
| 7712 | |
| 7713 | // C99 inline semantics, where the symbol is not externally visible. |
| 7714 | return GVA_C99Inline; |
| 7715 | } |
| 7716 | |
| 7717 | // C++0x [temp.explicit]p9: |
| 7718 | // [ Note: The intent is that an inline function that is the subject of |
| 7719 | // an explicit instantiation declaration will still be implicitly |
| 7720 | // instantiated when used so that the body can be considered for |
| 7721 | // inlining, but that no out-of-line copy of the inline function would be |
| 7722 | // generated in the translation unit. -- end note ] |
| 7723 | if (FD->getTemplateSpecializationKind() |
| 7724 | == TSK_ExplicitInstantiationDeclaration) |
| 7725 | return GVA_C99Inline; |
| 7726 | |
| 7727 | return GVA_CXXInline; |
| 7728 | } |
| 7729 | |
| 7730 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 7731 | // If this is a static data member, compute the kind of template |
| 7732 | // specialization. Otherwise, this variable is not part of a |
| 7733 | // template. |
| 7734 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 7735 | if (VD->isStaticDataMember()) |
| 7736 | TSK = VD->getTemplateSpecializationKind(); |
| 7737 | |
| 7738 | Linkage L = VD->getLinkage(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7739 | |
| 7740 | switch (L) { |
| 7741 | case NoLinkage: |
| 7742 | case InternalLinkage: |
| 7743 | case UniqueExternalLinkage: |
| 7744 | return GVA_Internal; |
| 7745 | |
| 7746 | case ExternalLinkage: |
| 7747 | switch (TSK) { |
| 7748 | case TSK_Undeclared: |
| 7749 | case TSK_ExplicitSpecialization: |
| 7750 | return GVA_StrongExternal; |
| 7751 | |
| 7752 | case TSK_ExplicitInstantiationDeclaration: |
| 7753 | llvm_unreachable("Variable should not be instantiated"); |
| 7754 | // Fall through to treat this like any other instantiation. |
| 7755 | |
| 7756 | case TSK_ExplicitInstantiationDefinition: |
| 7757 | return GVA_ExplicitTemplateInstantiation; |
| 7758 | |
| 7759 | case TSK_ImplicitInstantiation: |
| 7760 | return GVA_TemplateInstantiation; |
| 7761 | } |
| 7762 | } |
| 7763 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7764 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7765 | } |
| 7766 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 7767 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7768 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7769 | if (!VD->isFileVarDecl()) |
| 7770 | return false; |
Richard Smith | f396ad9 | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 7771 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7772 | // We never need to emit an uninstantiated function template. |
| 7773 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
| 7774 | return false; |
| 7775 | } else |
| 7776 | return false; |
| 7777 | |
| 7778 | // If this is a member of a class template, we do not need to emit it. |
| 7779 | if (D->getDeclContext()->isDependentContext()) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7780 | return false; |
| 7781 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7782 | // Weak references don't produce any output by themselves. |
| 7783 | if (D->hasAttr<WeakRefAttr>()) |
| 7784 | return false; |
| 7785 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7786 | // Aliases and used decls are required. |
| 7787 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 7788 | return true; |
| 7789 | |
| 7790 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7791 | // Forward declarations aren't required. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 7792 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 7793 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7794 | |
| 7795 | // Constructors and destructors are required. |
| 7796 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 7797 | return true; |
| 7798 | |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7799 | // The key function for a class is required. This rule only comes |
| 7800 | // into play when inline functions can be key functions, though. |
| 7801 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
| 7802 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 7803 | const CXXRecordDecl *RD = MD->getParent(); |
| 7804 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 7805 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
| 7806 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 7807 | return true; |
| 7808 | } |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7809 | } |
| 7810 | } |
| 7811 | |
| 7812 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 7813 | |
| 7814 | // static, static inline, always_inline, and extern inline functions can |
| 7815 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 7816 | // Implicit template instantiations can also be deferred in C++. |
| 7817 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
Anton Yartsev | 3a5aca8 | 2012-02-02 06:06:34 +0000 | [diff] [blame] | 7818 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7819 | return false; |
| 7820 | return true; |
| 7821 | } |
Douglas Gregor | 94da158 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 7822 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7823 | const VarDecl *VD = cast<VarDecl>(D); |
| 7824 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 7825 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7826 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 7827 | return false; |
| 7828 | |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7829 | // Variables that can be needed in other TUs are required. |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7830 | GVALinkage L = GetGVALinkageForVariable(VD); |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7831 | if (L != GVA_Internal && L != GVA_TemplateInstantiation) |
| 7832 | return true; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7833 | |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7834 | // Variables that have destruction with side-effects are required. |
| 7835 | if (VD->getType().isDestructedType()) |
| 7836 | return true; |
| 7837 | |
| 7838 | // Variables that have initialization with side-effects are required. |
| 7839 | if (VD->getInit() && VD->getInit()->HasSideEffects(*this)) |
| 7840 | return true; |
| 7841 | |
| 7842 | return false; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7843 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7844 | |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7845 | CallingConv ASTContext::getDefaultCXXMethodCallConv(bool isVariadic) { |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7846 | // Pass through to the C++ ABI object |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7847 | return ABI->getDefaultMethodCallConv(isVariadic); |
| 7848 | } |
| 7849 | |
| 7850 | CallingConv ASTContext::getCanonicalCallConv(CallingConv CC) const { |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7851 | if (CC == CC_C && !LangOpts.MRTD && |
| 7852 | getTargetInfo().getCXXABI().isMemberFunctionCCDefault()) |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7853 | return CC_Default; |
| 7854 | return CC; |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7855 | } |
| 7856 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7857 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 7858 | // Pass through to the C++ ABI object |
| 7859 | return ABI->isNearlyEmpty(RD); |
| 7860 | } |
| 7861 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7862 | MangleContext *ASTContext::createMangleContext() { |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7863 | switch (Target->getCXXABI().getKind()) { |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7864 | case TargetCXXABI::GenericAArch64: |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7865 | case TargetCXXABI::GenericItanium: |
| 7866 | case TargetCXXABI::GenericARM: |
| 7867 | case TargetCXXABI::iOS: |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7868 | return createItaniumMangleContext(*this, getDiagnostics()); |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7869 | case TargetCXXABI::Microsoft: |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7870 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 7871 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7872 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7873 | } |
| 7874 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7875 | CXXABI::~CXXABI() {} |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7876 | |
| 7877 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 0c8cd1a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 7878 | return ASTRecordLayouts.getMemorySize() |
| 7879 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 7880 | + llvm::capacity_in_bytes(KeyFunctions) |
| 7881 | + llvm::capacity_in_bytes(ObjCImpls) |
| 7882 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 7883 | + llvm::capacity_in_bytes(DeclAttrs) |
| 7884 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 7885 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 7886 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 7887 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 7888 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 7889 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7890 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 7891 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7892 | } |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7893 | |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 7894 | void ASTContext::addUnnamedTag(const TagDecl *Tag) { |
| 7895 | // FIXME: This mangling should be applied to function local classes too |
| 7896 | if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl() || |
| 7897 | !isa<CXXRecordDecl>(Tag->getParent()) || Tag->getLinkage() != ExternalLinkage) |
| 7898 | return; |
| 7899 | |
| 7900 | std::pair<llvm::DenseMap<const DeclContext *, unsigned>::iterator, bool> P = |
| 7901 | UnnamedMangleContexts.insert(std::make_pair(Tag->getParent(), 0)); |
| 7902 | UnnamedMangleNumbers.insert(std::make_pair(Tag, P.first->second++)); |
| 7903 | } |
| 7904 | |
| 7905 | int ASTContext::getUnnamedTagManglingNumber(const TagDecl *Tag) const { |
| 7906 | llvm::DenseMap<const TagDecl *, unsigned>::const_iterator I = |
| 7907 | UnnamedMangleNumbers.find(Tag); |
| 7908 | return I != UnnamedMangleNumbers.end() ? I->second : -1; |
| 7909 | } |
| 7910 | |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 7911 | unsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) { |
| 7912 | CXXRecordDecl *Lambda = CallOperator->getParent(); |
| 7913 | return LambdaMangleContexts[Lambda->getDeclContext()] |
| 7914 | .getManglingNumber(CallOperator); |
| 7915 | } |
| 7916 | |
| 7917 | |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7918 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 7919 | ParamIndices[D] = index; |
| 7920 | } |
| 7921 | |
| 7922 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 7923 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 7924 | assert(I != ParamIndices.end() && |
| 7925 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 7926 | return I->second; |
| 7927 | } |