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 | } |
| 100 | |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 101 | // TODO: handle comments for function parameters properly. |
| 102 | if (isa<ParmVarDecl>(D)) |
| 103 | return NULL; |
| 104 | |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 105 | // TODO: we could look up template parameter documentation in the template |
| 106 | // documentation. |
| 107 | if (isa<TemplateTypeParmDecl>(D) || |
| 108 | isa<NonTypeTemplateParmDecl>(D) || |
| 109 | isa<TemplateTemplateParmDecl>(D)) |
| 110 | return NULL; |
| 111 | |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 112 | ArrayRef<RawComment *> RawComments = Comments.getComments(); |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 113 | |
| 114 | // If there are no comments anywhere, we won't find anything. |
| 115 | if (RawComments.empty()) |
| 116 | return NULL; |
| 117 | |
Dmitri Gribenko | abd56c8 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 118 | // Find declaration location. |
| 119 | // For Objective-C declarations we generally don't expect to have multiple |
| 120 | // declarators, thus use declaration starting location as the "declaration |
| 121 | // location". |
| 122 | // For all other declarations multiple declarators are used quite frequently, |
| 123 | // so we use the location of the identifier as the "declaration location". |
| 124 | SourceLocation DeclLoc; |
| 125 | if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) || |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 126 | isa<ObjCPropertyDecl>(D) || |
Dmitri Gribenko | c27bc80 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 127 | isa<RedeclarableTemplateDecl>(D) || |
| 128 | isa<ClassTemplateSpecializationDecl>(D)) |
Dmitri Gribenko | abd56c8 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 129 | DeclLoc = D->getLocStart(); |
| 130 | else |
| 131 | DeclLoc = D->getLocation(); |
| 132 | |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 133 | // If the declaration doesn't map directly to a location in a file, we |
| 134 | // can't find the comment. |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 135 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
| 136 | return NULL; |
| 137 | |
| 138 | // Find the comment that occurs just after this declaration. |
Dmitri Gribenko | a444f18 | 2012-07-17 22:01:09 +0000 | [diff] [blame] | 139 | ArrayRef<RawComment *>::iterator Comment; |
| 140 | { |
| 141 | // When searching for comments during parsing, the comment we are looking |
| 142 | // for is usually among the last two comments we parsed -- check them |
| 143 | // first. |
| 144 | RawComment CommentAtDeclLoc(SourceMgr, SourceRange(DeclLoc)); |
| 145 | BeforeThanCompare<RawComment> Compare(SourceMgr); |
| 146 | ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1; |
| 147 | bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 148 | if (!Found && RawComments.size() >= 2) { |
| 149 | MaybeBeforeDecl--; |
| 150 | Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 151 | } |
| 152 | |
| 153 | if (Found) { |
| 154 | Comment = MaybeBeforeDecl + 1; |
| 155 | assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(), |
| 156 | &CommentAtDeclLoc, Compare)); |
| 157 | } else { |
| 158 | // Slow path. |
| 159 | Comment = std::lower_bound(RawComments.begin(), RawComments.end(), |
| 160 | &CommentAtDeclLoc, Compare); |
| 161 | } |
| 162 | } |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 163 | |
| 164 | // Decompose the location for the declaration and find the beginning of the |
| 165 | // file buffer. |
| 166 | std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc); |
| 167 | |
| 168 | // First check whether we have a trailing comment. |
| 169 | if (Comment != RawComments.end() && |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 170 | (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() && |
Dmitri Gribenko | 9c00676 | 2012-07-06 23:27:33 +0000 | [diff] [blame] | 171 | (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D))) { |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 172 | std::pair<FileID, unsigned> CommentBeginDecomp |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 173 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin()); |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 174 | // Check that Doxygen trailing comment comes after the declaration, starts |
| 175 | // on the same line and in the same file as the declaration. |
| 176 | if (DeclLocDecomp.first == CommentBeginDecomp.first && |
| 177 | SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) |
| 178 | == SourceMgr.getLineNumber(CommentBeginDecomp.first, |
| 179 | CommentBeginDecomp.second)) { |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 180 | return *Comment; |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | // The comment just after the declaration was not a trailing comment. |
| 185 | // Let's look at the previous comment. |
| 186 | if (Comment == RawComments.begin()) |
| 187 | return NULL; |
| 188 | --Comment; |
| 189 | |
| 190 | // Check that we actually have a non-member Doxygen comment. |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 191 | if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment()) |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 192 | return NULL; |
| 193 | |
| 194 | // Decompose the end of the comment. |
| 195 | std::pair<FileID, unsigned> CommentEndDecomp |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 196 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd()); |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 197 | |
| 198 | // If the comment and the declaration aren't in the same file, then they |
| 199 | // aren't related. |
| 200 | if (DeclLocDecomp.first != CommentEndDecomp.first) |
| 201 | return NULL; |
| 202 | |
| 203 | // Get the corresponding buffer. |
| 204 | bool Invalid = false; |
| 205 | const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first, |
| 206 | &Invalid).data(); |
| 207 | if (Invalid) |
| 208 | return NULL; |
| 209 | |
| 210 | // Extract text between the comment and declaration. |
| 211 | StringRef Text(Buffer + CommentEndDecomp.second, |
| 212 | DeclLocDecomp.second - CommentEndDecomp.second); |
| 213 | |
Dmitri Gribenko | 8bdb58a | 2012-06-27 23:43:37 +0000 | [diff] [blame] | 214 | // There should be no other declarations or preprocessor directives between |
| 215 | // comment and declaration. |
Dmitri Gribenko | abd56c8 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 216 | if (Text.find_first_of(",;{}#@") != StringRef::npos) |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 217 | return NULL; |
| 218 | |
Dmitri Gribenko | 811c820 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 219 | return *Comment; |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 222 | namespace { |
| 223 | /// If we have a 'templated' declaration for a template, adjust 'D' to |
| 224 | /// refer to the actual template. |
Dmitri Gribenko | 2125c90 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 225 | /// If we have an implicit instantiation, adjust 'D' to refer to template. |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 226 | const Decl *adjustDeclToTemplate(const Decl *D) { |
Douglas Gregor | cd81df2 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 227 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Dmitri Gribenko | 2125c90 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 228 | // Is this function declaration part of a function template? |
Douglas Gregor | cd81df2 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 229 | if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) |
Dmitri Gribenko | 2125c90 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 230 | return FTD; |
| 231 | |
| 232 | // Nothing to do if function is not an implicit instantiation. |
| 233 | if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 234 | return D; |
| 235 | |
| 236 | // Function is an implicit instantiation of a function template? |
| 237 | if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate()) |
| 238 | return FTD; |
| 239 | |
| 240 | // Function is instantiated from a member definition of a class template? |
| 241 | if (const FunctionDecl *MemberDecl = |
| 242 | FD->getInstantiatedFromMemberFunction()) |
| 243 | return MemberDecl; |
| 244 | |
| 245 | return D; |
Douglas Gregor | cd81df2 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 246 | } |
Dmitri Gribenko | 2125c90 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 247 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 248 | // Static data member is instantiated from a member definition of a class |
| 249 | // template? |
| 250 | if (VD->isStaticDataMember()) |
| 251 | if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember()) |
| 252 | return MemberDecl; |
| 253 | |
| 254 | return D; |
| 255 | } |
| 256 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) { |
| 257 | // Is this class declaration part of a class template? |
| 258 | if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate()) |
| 259 | return CTD; |
| 260 | |
| 261 | // Class is an implicit instantiation of a class template or partial |
| 262 | // specialization? |
| 263 | if (const ClassTemplateSpecializationDecl *CTSD = |
| 264 | dyn_cast<ClassTemplateSpecializationDecl>(CRD)) { |
| 265 | if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation) |
| 266 | return D; |
| 267 | llvm::PointerUnion<ClassTemplateDecl *, |
| 268 | ClassTemplatePartialSpecializationDecl *> |
| 269 | PU = CTSD->getSpecializedTemplateOrPartial(); |
| 270 | return PU.is<ClassTemplateDecl*>() ? |
| 271 | static_cast<const Decl*>(PU.get<ClassTemplateDecl *>()) : |
| 272 | static_cast<const Decl*>( |
| 273 | PU.get<ClassTemplatePartialSpecializationDecl *>()); |
| 274 | } |
| 275 | |
| 276 | // Class is instantiated from a member definition of a class template? |
| 277 | if (const MemberSpecializationInfo *Info = |
| 278 | CRD->getMemberSpecializationInfo()) |
| 279 | return Info->getInstantiatedFrom(); |
| 280 | |
| 281 | return D; |
| 282 | } |
| 283 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 284 | // Enum is instantiated from a member definition of a class template? |
| 285 | if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum()) |
| 286 | return MemberDecl; |
| 287 | |
| 288 | return D; |
| 289 | } |
| 290 | // FIXME: Adjust alias templates? |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 291 | return D; |
| 292 | } |
| 293 | } // unnamed namespace |
| 294 | |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 295 | const RawComment *ASTContext::getRawCommentForAnyRedecl( |
| 296 | const Decl *D, |
| 297 | const Decl **OriginalDecl) const { |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 298 | D = adjustDeclToTemplate(D); |
Douglas Gregor | cd81df2 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 299 | |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 300 | // Check whether we have cached a comment for this declaration already. |
| 301 | { |
| 302 | llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos = |
| 303 | RedeclComments.find(D); |
| 304 | if (Pos != RedeclComments.end()) { |
| 305 | const RawCommentAndCacheFlags &Raw = Pos->second; |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 306 | if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) { |
| 307 | if (OriginalDecl) |
| 308 | *OriginalDecl = Raw.getOriginalDecl(); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 309 | return Raw.getRaw(); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 310 | } |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 311 | } |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 312 | } |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 313 | |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 314 | // Search for comments attached to declarations in the redeclaration chain. |
| 315 | const RawComment *RC = NULL; |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 316 | const Decl *OriginalDeclForRC = NULL; |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 317 | for (Decl::redecl_iterator I = D->redecls_begin(), |
| 318 | E = D->redecls_end(); |
| 319 | I != E; ++I) { |
| 320 | llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos = |
| 321 | RedeclComments.find(*I); |
| 322 | if (Pos != RedeclComments.end()) { |
| 323 | const RawCommentAndCacheFlags &Raw = Pos->second; |
| 324 | if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) { |
| 325 | RC = Raw.getRaw(); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 326 | OriginalDeclForRC = Raw.getOriginalDecl(); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 327 | break; |
| 328 | } |
| 329 | } else { |
| 330 | RC = getRawCommentForDeclNoCache(*I); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 331 | OriginalDeclForRC = *I; |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 332 | RawCommentAndCacheFlags Raw; |
| 333 | if (RC) { |
| 334 | Raw.setRaw(RC); |
| 335 | Raw.setKind(RawCommentAndCacheFlags::FromDecl); |
| 336 | } else |
| 337 | Raw.setKind(RawCommentAndCacheFlags::NoCommentInDecl); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 338 | Raw.setOriginalDecl(*I); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 339 | RedeclComments[*I] = Raw; |
| 340 | if (RC) |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
Dmitri Gribenko | 8376f59 | 2012-06-28 16:25:36 +0000 | [diff] [blame] | 345 | // If we found a comment, it should be a documentation comment. |
| 346 | assert(!RC || RC->isDocumentation()); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 347 | |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 348 | if (OriginalDecl) |
| 349 | *OriginalDecl = OriginalDeclForRC; |
| 350 | |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 351 | // Update cache for every declaration in the redeclaration chain. |
| 352 | RawCommentAndCacheFlags Raw; |
| 353 | Raw.setRaw(RC); |
| 354 | Raw.setKind(RawCommentAndCacheFlags::FromRedecl); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 355 | Raw.setOriginalDecl(OriginalDeclForRC); |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 356 | |
| 357 | for (Decl::redecl_iterator I = D->redecls_begin(), |
| 358 | E = D->redecls_end(); |
| 359 | I != E; ++I) { |
| 360 | RawCommentAndCacheFlags &R = RedeclComments[*I]; |
| 361 | if (R.getKind() == RawCommentAndCacheFlags::NoCommentInDecl) |
| 362 | R = Raw; |
| 363 | } |
| 364 | |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 365 | return RC; |
| 366 | } |
| 367 | |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 368 | static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod, |
| 369 | SmallVectorImpl<const NamedDecl *> &Redeclared) { |
| 370 | const DeclContext *DC = ObjCMethod->getDeclContext(); |
| 371 | if (const ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(DC)) { |
| 372 | const ObjCInterfaceDecl *ID = IMD->getClassInterface(); |
| 373 | if (!ID) |
| 374 | return; |
| 375 | // Add redeclared method here. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 376 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 377 | Ext = ID->known_extensions_begin(), |
| 378 | ExtEnd = ID->known_extensions_end(); |
| 379 | Ext != ExtEnd; ++Ext) { |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 380 | if (ObjCMethodDecl *RedeclaredMethod = |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 381 | Ext->getMethod(ObjCMethod->getSelector(), |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 382 | ObjCMethod->isInstanceMethod())) |
| 383 | Redeclared.push_back(RedeclaredMethod); |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
Fariborz Jahanian | 749ace6 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 388 | comments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC, |
| 389 | const Decl *D) const { |
| 390 | comments::DeclInfo *ThisDeclInfo = new (*this) comments::DeclInfo; |
| 391 | ThisDeclInfo->CommentDecl = D; |
| 392 | ThisDeclInfo->IsFilled = false; |
| 393 | ThisDeclInfo->fill(); |
| 394 | ThisDeclInfo->CommentDecl = FC->getDecl(); |
| 395 | comments::FullComment *CFC = |
| 396 | new (*this) comments::FullComment(FC->getBlocks(), |
| 397 | ThisDeclInfo); |
| 398 | return CFC; |
| 399 | |
| 400 | } |
| 401 | |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 402 | comments::FullComment *ASTContext::getCommentForDecl( |
| 403 | const Decl *D, |
| 404 | const Preprocessor *PP) const { |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 405 | D = adjustDeclToTemplate(D); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 406 | |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 407 | const Decl *Canonical = D->getCanonicalDecl(); |
| 408 | llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos = |
| 409 | ParsedComments.find(Canonical); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 410 | |
| 411 | if (Pos != ParsedComments.end()) { |
Fariborz Jahanian | 749ace6 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 412 | if (Canonical != D) { |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 413 | comments::FullComment *FC = Pos->second; |
Fariborz Jahanian | 749ace6 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 414 | comments::FullComment *CFC = cloneFullComment(FC, D); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 415 | return CFC; |
| 416 | } |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 417 | return Pos->second; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 420 | const Decl *OriginalDecl; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 421 | |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 422 | const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 423 | if (!RC) { |
| 424 | if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) { |
Dmitri Gribenko | 1e905da | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 425 | SmallVector<const NamedDecl*, 8> Overridden; |
Fariborz Jahanian | c328d9c | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 426 | const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 23799e3 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 427 | if (OMD && OMD->isPropertyAccessor()) |
| 428 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
| 429 | if (comments::FullComment *FC = getCommentForDecl(PDecl, PP)) |
| 430 | return cloneFullComment(FC, D); |
Fariborz Jahanian | c328d9c | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 431 | if (OMD) |
Dmitri Gribenko | 1e905da | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 432 | addRedeclaredMethods(OMD, Overridden); |
| 433 | getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden); |
Fariborz Jahanian | 23799e3 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 434 | for (unsigned i = 0, e = Overridden.size(); i < e; i++) |
| 435 | if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP)) |
| 436 | return cloneFullComment(FC, D); |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 437 | } |
Fariborz Jahanian | 41170b5 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 438 | else if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Dmitri Gribenko | d1e5c0d | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 439 | // Attach any tag type's documentation to its typedef if latter |
Fariborz Jahanian | 23799e3 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 440 | // does not have one of its own. |
Fariborz Jahanian | 41170b5 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 441 | QualType QT = TD->getUnderlyingType(); |
Dmitri Gribenko | d1e5c0d | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 442 | if (const TagType *TT = QT->getAs<TagType>()) |
| 443 | if (const Decl *TD = TT->getDecl()) |
| 444 | if (comments::FullComment *FC = getCommentForDecl(TD, PP)) |
Fariborz Jahanian | 23799e3 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 445 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 41170b5 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 446 | } |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 447 | return NULL; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Dmitri Gribenko | 4b41c65 | 2012-08-22 18:12:19 +0000 | [diff] [blame] | 450 | // If the RawComment was attached to other redeclaration of this Decl, we |
| 451 | // should parse the comment in context of that other Decl. This is important |
| 452 | // because comments can contain references to parameter names which can be |
| 453 | // different across redeclarations. |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 454 | if (D != OriginalDecl) |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 455 | return getCommentForDecl(OriginalDecl, PP); |
Dmitri Gribenko | 1599eac | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 456 | |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 457 | comments::FullComment *FC = RC->parse(*this, PP, D); |
Dmitri Gribenko | c41ace9 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 458 | ParsedComments[Canonical] = FC; |
| 459 | return FC; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 462 | void |
| 463 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 464 | TemplateTemplateParmDecl *Parm) { |
| 465 | ID.AddInteger(Parm->getDepth()); |
| 466 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 467 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 468 | |
| 469 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 470 | ID.AddInteger(Params->size()); |
| 471 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 472 | PEnd = Params->end(); |
| 473 | P != PEnd; ++P) { |
| 474 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 475 | ID.AddInteger(0); |
| 476 | ID.AddBoolean(TTP->isParameterPack()); |
| 477 | continue; |
| 478 | } |
| 479 | |
| 480 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 481 | ID.AddInteger(1); |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 482 | ID.AddBoolean(NTTP->isParameterPack()); |
Eli Friedman | 9e9c454 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 483 | ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr()); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 484 | if (NTTP->isExpandedParameterPack()) { |
| 485 | ID.AddBoolean(true); |
| 486 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
Eli Friedman | 9e9c454 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 487 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 488 | QualType T = NTTP->getExpansionType(I); |
| 489 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); |
| 490 | } |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 491 | } else |
| 492 | ID.AddBoolean(false); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 493 | continue; |
| 494 | } |
| 495 | |
| 496 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 497 | ID.AddInteger(2); |
| 498 | Profile(ID, TTP); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | TemplateTemplateParmDecl * |
| 503 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 504 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 505 | // Check if we already have a canonical template template parameter. |
| 506 | llvm::FoldingSetNodeID ID; |
| 507 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 508 | void *InsertPos = 0; |
| 509 | CanonicalTemplateTemplateParm *Canonical |
| 510 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 511 | if (Canonical) |
| 512 | return Canonical->getParam(); |
| 513 | |
| 514 | // Build a canonical template parameter list. |
| 515 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 516 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 517 | CanonParams.reserve(Params->size()); |
| 518 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 519 | PEnd = Params->end(); |
| 520 | P != PEnd; ++P) { |
| 521 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 522 | CanonParams.push_back( |
| 523 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 524 | SourceLocation(), |
| 525 | SourceLocation(), |
| 526 | TTP->getDepth(), |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 527 | TTP->getIndex(), 0, false, |
| 528 | TTP->isParameterPack())); |
| 529 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 530 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 531 | QualType T = getCanonicalType(NTTP->getType()); |
| 532 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 533 | NonTypeTemplateParmDecl *Param; |
| 534 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 535 | SmallVector<QualType, 2> ExpandedTypes; |
| 536 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 537 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 538 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 539 | ExpandedTInfos.push_back( |
| 540 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 541 | } |
| 542 | |
| 543 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 544 | SourceLocation(), |
| 545 | SourceLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 546 | NTTP->getDepth(), |
| 547 | NTTP->getPosition(), 0, |
| 548 | T, |
| 549 | TInfo, |
| 550 | ExpandedTypes.data(), |
| 551 | ExpandedTypes.size(), |
| 552 | ExpandedTInfos.data()); |
| 553 | } else { |
| 554 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 555 | SourceLocation(), |
| 556 | SourceLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 557 | NTTP->getDepth(), |
| 558 | NTTP->getPosition(), 0, |
| 559 | T, |
| 560 | NTTP->isParameterPack(), |
| 561 | TInfo); |
| 562 | } |
| 563 | CanonParams.push_back(Param); |
| 564 | |
| 565 | } else |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 566 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 567 | cast<TemplateTemplateParmDecl>(*P))); |
| 568 | } |
| 569 | |
| 570 | TemplateTemplateParmDecl *CanonTTP |
| 571 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 572 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 573 | TTP->getPosition(), |
| 574 | TTP->isParameterPack(), |
| 575 | 0, |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 576 | TemplateParameterList::Create(*this, SourceLocation(), |
| 577 | SourceLocation(), |
| 578 | CanonParams.data(), |
| 579 | CanonParams.size(), |
| 580 | SourceLocation())); |
| 581 | |
| 582 | // Get the new insert position for the node we care about. |
| 583 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 584 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 585 | (void)Canonical; |
| 586 | |
| 587 | // Create the canonical template template parameter entry. |
| 588 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 589 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 590 | return CanonTTP; |
| 591 | } |
| 592 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 593 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 594 | if (!LangOpts.CPlusPlus) return 0; |
| 595 | |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 596 | switch (T.getCXXABI().getKind()) { |
| 597 | case TargetCXXABI::GenericARM: |
| 598 | case TargetCXXABI::iOS: |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 599 | return CreateARMCXXABI(*this); |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 600 | case TargetCXXABI::GenericAArch64: // Same as Itanium at this level |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 601 | case TargetCXXABI::GenericItanium: |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 602 | return CreateItaniumCXXABI(*this); |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 603 | case TargetCXXABI::Microsoft: |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 604 | return CreateMicrosoftCXXABI(*this); |
| 605 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 606 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 609 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 610 | const LangOptions &LOpts) { |
| 611 | if (LOpts.FakeAddressSpaceMap) { |
| 612 | // The fake address space map must have a distinct entry for each |
| 613 | // language-specific address space. |
| 614 | static const unsigned FakeAddrSpaceMap[] = { |
| 615 | 1, // opencl_global |
| 616 | 2, // opencl_local |
Peter Collingbourne | 4dc34eb | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 617 | 3, // opencl_constant |
| 618 | 4, // cuda_device |
| 619 | 5, // cuda_constant |
| 620 | 6 // cuda_shared |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 621 | }; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 622 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 623 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 624 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Douglas Gregor | 3e3cd93 | 2011-09-01 20:23:19 +0000 | [diff] [blame] | 628 | ASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 629 | const TargetInfo *t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 630 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 631 | Builtin::Context &builtins, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 632 | unsigned size_reserve, |
| 633 | bool DelayInitialization) |
| 634 | : FunctionProtoTypes(this_()), |
| 635 | TemplateSpecializationTypes(this_()), |
| 636 | DependentTemplateSpecializationTypes(this_()), |
| 637 | SubstTemplateTemplateParmPacks(this_()), |
| 638 | GlobalNestedNameSpecifier(0), |
| 639 | Int128Decl(0), UInt128Decl(0), |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 640 | BuiltinVaListDecl(0), |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 641 | ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0), |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 642 | BOOLDecl(0), |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 643 | CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 644 | FILEDecl(0), |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 645 | jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0), |
| 646 | BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 647 | cudaConfigureCallDecl(0), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 648 | NullTypeSourceInfo(QualType()), |
| 649 | FirstLocalImport(), LastLocalImport(), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 650 | SourceMgr(SM), LangOpts(LOpts), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 651 | AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 652 | Idents(idents), Selectors(sels), |
| 653 | BuiltinInfo(builtins), |
| 654 | DeclarationNames(*this), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 655 | ExternalSource(0), Listener(0), |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 656 | Comments(SM), CommentsLoaded(false), |
Dmitri Gribenko | 6ebf091 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 657 | CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 658 | LastSDM(0, 0), |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 659 | UniqueBlockByRefTypeID(0) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 660 | { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 662 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 663 | |
| 664 | if (!DelayInitialization) { |
| 665 | assert(t && "No target supplied for ASTContext initialization"); |
| 666 | InitBuiltinTypes(*t); |
| 667 | } |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 670 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 671 | // Release the DenseMaps associated with DeclContext objects. |
| 672 | // FIXME: Is this the ideal solution? |
| 673 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 674 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 675 | // Call all of the deallocation functions. |
| 676 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 677 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 678 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 679 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 680 | // because they can contain DenseMaps. |
| 681 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 682 | const ASTRecordLayout*>::iterator |
| 683 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 684 | // Increment in loop to prevent using deallocated memory. |
| 685 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 686 | R->Destroy(*this); |
| 687 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 688 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 689 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 690 | // Increment in loop to prevent using deallocated memory. |
| 691 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 692 | R->Destroy(*this); |
| 693 | } |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 694 | |
| 695 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 696 | AEnd = DeclAttrs.end(); |
| 697 | A != AEnd; ++A) |
| 698 | A->second->~AttrVec(); |
| 699 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 700 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 701 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 702 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 703 | } |
| 704 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 705 | void |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 706 | ASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 707 | ExternalSource.reset(Source.take()); |
| 708 | } |
| 709 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 710 | void ASTContext::PrintStats() const { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 711 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 712 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 713 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 714 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 716 | #define ABSTRACT_TYPE(Name, Parent) |
| 717 | #include "clang/AST/TypeNodes.def" |
| 718 | 0 // Extra |
| 719 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 720 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 721 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 722 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 723 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 726 | unsigned Idx = 0; |
| 727 | unsigned TotalBytes = 0; |
| 728 | #define TYPE(Name, Parent) \ |
| 729 | if (counts[Idx]) \ |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 730 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 731 | << " types\n"; \ |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 732 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 733 | ++Idx; |
| 734 | #define ABSTRACT_TYPE(Name, Parent) |
| 735 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 736 | |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 737 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 738 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 739 | // Implicit special member functions. |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 740 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 741 | << NumImplicitDefaultConstructors |
| 742 | << " implicit default constructors created\n"; |
| 743 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 744 | << NumImplicitCopyConstructors |
| 745 | << " implicit copy constructors created\n"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 746 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 747 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 748 | << NumImplicitMoveConstructors |
| 749 | << " implicit move constructors created\n"; |
| 750 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 751 | << NumImplicitCopyAssignmentOperators |
| 752 | << " implicit copy assignment operators created\n"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 753 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 754 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 755 | << NumImplicitMoveAssignmentOperators |
| 756 | << " implicit move assignment operators created\n"; |
| 757 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 758 | << NumImplicitDestructors |
| 759 | << " implicit destructors created\n"; |
| 760 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 761 | if (ExternalSource.get()) { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 762 | llvm::errs() << "\n"; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 763 | ExternalSource->PrintStats(); |
| 764 | } |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 765 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 766 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Douglas Gregor | 772eeae | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 769 | TypedefDecl *ASTContext::getInt128Decl() const { |
| 770 | if (!Int128Decl) { |
| 771 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty); |
| 772 | Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 773 | getTranslationUnitDecl(), |
| 774 | SourceLocation(), |
| 775 | SourceLocation(), |
| 776 | &Idents.get("__int128_t"), |
| 777 | TInfo); |
| 778 | } |
| 779 | |
| 780 | return Int128Decl; |
| 781 | } |
| 782 | |
| 783 | TypedefDecl *ASTContext::getUInt128Decl() const { |
| 784 | if (!UInt128Decl) { |
| 785 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty); |
| 786 | UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 787 | getTranslationUnitDecl(), |
| 788 | SourceLocation(), |
| 789 | SourceLocation(), |
| 790 | &Idents.get("__uint128_t"), |
| 791 | TInfo); |
| 792 | } |
| 793 | |
| 794 | return UInt128Decl; |
| 795 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 796 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 797 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 798 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 799 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 800 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 803 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target) { |
| 804 | assert((!this->Target || this->Target == &Target) && |
| 805 | "Incorrect target reinitialization"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 806 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 808 | this->Target = &Target; |
| 809 | |
| 810 | ABI.reset(createCXXABI(Target)); |
| 811 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
| 812 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 813 | // C99 6.2.5p19. |
| 814 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 815 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 816 | // C99 6.2.5p2. |
| 817 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 818 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 819 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 820 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 821 | else |
| 822 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 823 | // C99 6.2.5p4. |
| 824 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 825 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 826 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 827 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 828 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 830 | // C99 6.2.5p6. |
| 831 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 832 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 833 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 834 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 835 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 837 | // C99 6.2.5p10. |
| 838 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 839 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 840 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 841 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 842 | // GNU extension, 128-bit integers. |
| 843 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 844 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 845 | |
Abramo Bagnara | e75bb61 | 2012-09-09 10:13:32 +0000 | [diff] [blame] | 846 | if (LangOpts.CPlusPlus && LangOpts.WChar) { // C++ 3.9.1p5 |
Eli Friedman | d3d77cd | 2011-04-30 19:24:24 +0000 | [diff] [blame] | 847 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 848 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 849 | else // -fshort-wchar makes wchar_t be unsigned. |
| 850 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
Abramo Bagnara | e75bb61 | 2012-09-09 10:13:32 +0000 | [diff] [blame] | 851 | } else // C99 (or C++ using -fno-wchar) |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 852 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 853 | |
James Molloy | 392da48 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 854 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 855 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 856 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 857 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 858 | else // C99 |
| 859 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 860 | |
| 861 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 862 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 863 | else // C99 |
| 864 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 865 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 866 | // Placeholder type for type-dependent expressions whose type is |
| 867 | // completely unknown. No code should ever check a type against |
| 868 | // DependentTy and users should never see it; however, it is here to |
| 869 | // help diagnose failures to properly check for type-dependent |
| 870 | // expressions. |
| 871 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 872 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 873 | // Placeholder type for functions. |
| 874 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 875 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 876 | // Placeholder type for bound members. |
| 877 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 878 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 879 | // Placeholder type for pseudo-objects. |
| 880 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 881 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 882 | // "any" type; useful for debugger-like clients. |
| 883 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 884 | |
John McCall | 0ddaeb9 | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 885 | // Placeholder type for unbridged ARC casts. |
| 886 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 887 | |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 888 | // Placeholder type for builtin functions. |
| 889 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
| 890 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 891 | // C99 6.2.5p11. |
| 892 | FloatComplexTy = getComplexType(FloatTy); |
| 893 | DoubleComplexTy = getComplexType(DoubleTy); |
| 894 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 895 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 896 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 897 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 898 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 899 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 900 | |
| 901 | if (LangOpts.OpenCL) { |
| 902 | InitBuiltinType(OCLImage1dTy, BuiltinType::OCLImage1d); |
| 903 | InitBuiltinType(OCLImage1dArrayTy, BuiltinType::OCLImage1dArray); |
| 904 | InitBuiltinType(OCLImage1dBufferTy, BuiltinType::OCLImage1dBuffer); |
| 905 | InitBuiltinType(OCLImage2dTy, BuiltinType::OCLImage2d); |
| 906 | InitBuiltinType(OCLImage2dArrayTy, BuiltinType::OCLImage2dArray); |
| 907 | InitBuiltinType(OCLImage3dTy, BuiltinType::OCLImage3d); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 908 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 909 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 910 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 911 | } |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 912 | |
| 913 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 93a4994 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 914 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 915 | SignedCharTy : BoolTy); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 916 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 917 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 918 | |
| 919 | ObjCSuperType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 921 | // void * type |
| 922 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 923 | |
| 924 | // nullptr type (C++0x 2.14.7) |
| 925 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 926 | |
| 927 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 928 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 929 | |
| 930 | // Builtin type used to help define __builtin_va_list. |
| 931 | VaListTagTy = QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 932 | } |
| 933 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 934 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 935 | return SourceMgr.getDiagnostics(); |
| 936 | } |
| 937 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 938 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 939 | AttrVec *&Result = DeclAttrs[D]; |
| 940 | if (!Result) { |
| 941 | void *Mem = Allocate(sizeof(AttrVec)); |
| 942 | Result = new (Mem) AttrVec; |
| 943 | } |
| 944 | |
| 945 | return *Result; |
| 946 | } |
| 947 | |
| 948 | /// \brief Erase the attributes corresponding to the given declaration. |
| 949 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 950 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 951 | if (Pos != DeclAttrs.end()) { |
| 952 | Pos->second->~AttrVec(); |
| 953 | DeclAttrs.erase(Pos); |
| 954 | } |
| 955 | } |
| 956 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 957 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 958 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 959 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 960 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 961 | = InstantiatedFromStaticDataMember.find(Var); |
| 962 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 963 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 965 | return Pos->second; |
| 966 | } |
| 967 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 968 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 969 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 970 | TemplateSpecializationKind TSK, |
| 971 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 972 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 973 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 974 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 975 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 976 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 977 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 980 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 981 | const FunctionDecl *FD){ |
| 982 | assert(FD && "Specialization is 0"); |
| 983 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 984 | = ClassScopeSpecializationPattern.find(FD); |
| 985 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 986 | return 0; |
| 987 | |
| 988 | return Pos->second; |
| 989 | } |
| 990 | |
| 991 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 992 | FunctionDecl *Pattern) { |
| 993 | assert(FD && "Specialization is 0"); |
| 994 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 995 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 996 | } |
| 997 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 998 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 999 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1000 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1001 | = InstantiatedFromUsingDecl.find(UUD); |
| 1002 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1003 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1005 | return Pos->second; |
| 1006 | } |
| 1007 | |
| 1008 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1009 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 1010 | assert((isa<UsingDecl>(Pattern) || |
| 1011 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 1012 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 1013 | "pattern decl is not a using decl"); |
| 1014 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 1015 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 1016 | } |
| 1017 | |
| 1018 | UsingShadowDecl * |
| 1019 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 1020 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 1021 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 1022 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 1023 | return 0; |
| 1024 | |
| 1025 | return Pos->second; |
| 1026 | } |
| 1027 | |
| 1028 | void |
| 1029 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 1030 | UsingShadowDecl *Pattern) { |
| 1031 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 1032 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1035 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 1036 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 1037 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 1038 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 1039 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1041 | return Pos->second; |
| 1042 | } |
| 1043 | |
| 1044 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 1045 | FieldDecl *Tmpl) { |
| 1046 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 1047 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 1048 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 1049 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1051 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 1052 | } |
| 1053 | |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 1054 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 1055 | const FieldDecl *LastFD) const { |
| 1056 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1057 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 1060 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 1061 | const FieldDecl *LastFD) const { |
| 1062 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1063 | FD->getBitWidthValue(*this) == 0 && |
| 1064 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1067 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 1068 | const FieldDecl *LastFD) const { |
| 1069 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1070 | FD->getBitWidthValue(*this) && |
| 1071 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1074 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1075 | const FieldDecl *LastFD) const { |
| 1076 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1077 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1080 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1081 | const FieldDecl *LastFD) const { |
| 1082 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1083 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1086 | ASTContext::overridden_cxx_method_iterator |
| 1087 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 1088 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1089 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1090 | if (Pos == OverriddenMethods.end()) |
| 1091 | return 0; |
| 1092 | |
| 1093 | return Pos->second.begin(); |
| 1094 | } |
| 1095 | |
| 1096 | ASTContext::overridden_cxx_method_iterator |
| 1097 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 1098 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1099 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1100 | if (Pos == OverriddenMethods.end()) |
| 1101 | return 0; |
| 1102 | |
| 1103 | return Pos->second.end(); |
| 1104 | } |
| 1105 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1106 | unsigned |
| 1107 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 1108 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1109 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1110 | if (Pos == OverriddenMethods.end()) |
| 1111 | return 0; |
| 1112 | |
| 1113 | return Pos->second.size(); |
| 1114 | } |
| 1115 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1116 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 1117 | const CXXMethodDecl *Overridden) { |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1118 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1119 | OverriddenMethods[Method].push_back(Overridden); |
| 1120 | } |
| 1121 | |
Dmitri Gribenko | 1e905da | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 1122 | void ASTContext::getOverriddenMethods( |
| 1123 | const NamedDecl *D, |
| 1124 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1125 | assert(D); |
| 1126 | |
| 1127 | if (const CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | bc0a2bb | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1128 | Overridden.append(CXXMethod->begin_overridden_methods(), |
| 1129 | CXXMethod->end_overridden_methods()); |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); |
| 1134 | if (!Method) |
| 1135 | return; |
| 1136 | |
Argyrios Kyrtzidis | 740ae67 | 2012-10-09 18:19:01 +0000 | [diff] [blame] | 1137 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
| 1138 | Method->getOverriddenMethods(OverDecls); |
Argyrios Kyrtzidis | bc0a2bb | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1139 | Overridden.append(OverDecls.begin(), OverDecls.end()); |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1142 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 1143 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 1144 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 1145 | if (!FirstLocalImport) { |
| 1146 | FirstLocalImport = Import; |
| 1147 | LastLocalImport = Import; |
| 1148 | return; |
| 1149 | } |
| 1150 | |
| 1151 | LastLocalImport->NextLocalImport = Import; |
| 1152 | LastLocalImport = Import; |
| 1153 | } |
| 1154 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1155 | //===----------------------------------------------------------------------===// |
| 1156 | // Type Sizing and Analysis |
| 1157 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1158 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1159 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 1160 | /// scalar floating point type. |
| 1161 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1162 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1163 | assert(BT && "Not a floating point type!"); |
| 1164 | switch (BT->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1165 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1166 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1167 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 1168 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 1169 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1170 | } |
| 1171 | } |
| 1172 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1173 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1174 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 1175 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1176 | /// If @p RefAsPointee, references are treated like their underlying type |
| 1177 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1178 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1179 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1180 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1181 | bool UseAlignAttrOnly = false; |
| 1182 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 1183 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1184 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1185 | // __attribute__((aligned)) can increase or decrease alignment |
| 1186 | // *except* on a struct or struct member, where it only increases |
| 1187 | // alignment unless 'packed' is also specified. |
| 1188 | // |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1189 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1190 | // ignore that possibility; Sema should diagnose it. |
| 1191 | if (isa<FieldDecl>(D)) { |
| 1192 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 1193 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 1194 | } else { |
| 1195 | UseAlignAttrOnly = true; |
| 1196 | } |
| 1197 | } |
Fariborz Jahanian | 78a7d7d | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1198 | else if (isa<FieldDecl>(D)) |
| 1199 | UseAlignAttrOnly = |
| 1200 | D->hasAttr<PackedAttr>() || |
| 1201 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1202 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1203 | // If we're using the align attribute only, just ignore everything |
| 1204 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1205 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1206 | // do nothing |
| 1207 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1208 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1209 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1210 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1211 | if (RefAsPointee) |
| 1212 | T = RT->getPointeeType(); |
| 1213 | else |
| 1214 | T = getPointerType(RT->getPointeeType()); |
| 1215 | } |
| 1216 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1217 | // Adjust alignments of declarations with array type by the |
| 1218 | // large-array alignment on the target. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1219 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1220 | const ArrayType *arrayType; |
| 1221 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 1222 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1223 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1224 | else if (isa<ConstantArrayType>(arrayType) && |
| 1225 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1226 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1227 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1228 | // Walk through any array types while we're at it. |
| 1229 | T = getBaseElementType(arrayType); |
| 1230 | } |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 1231 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1232 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1233 | |
| 1234 | // Fields can be subject to extra alignment constraints, like if |
| 1235 | // the field is packed, the struct is packed, or the struct has a |
| 1236 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 1237 | // the actual alignment of the field within the struct, and then |
| 1238 | // (as we're expected to) constrain that by the alignment of the type. |
| 1239 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 1240 | // So calculate the alignment of the field. |
| 1241 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 1242 | |
| 1243 | // Start with the record's overall alignment. |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1244 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1245 | |
| 1246 | // Use the GCD of that and the offset within the record. |
| 1247 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 1248 | if (offset > 0) { |
| 1249 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 1250 | // which means we get to do this crazy thing instead of Euclid's. |
| 1251 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 1252 | if (lowBitOfOffset < fieldAlign) |
| 1253 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 1254 | } |
| 1255 | |
| 1256 | Align = std::min(Align, fieldAlign); |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 1257 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1258 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1259 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1260 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1261 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1262 | |
John McCall | 929bbfb | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1263 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
| 1264 | // chars. If the type is a record, its data size is returned. This is |
| 1265 | // the size of the memcpy that's performed when assigning this type |
| 1266 | // using a trivial copy/move assignment operator. |
| 1267 | std::pair<CharUnits, CharUnits> |
| 1268 | ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
| 1269 | std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T); |
| 1270 | |
| 1271 | // In C++, objects can sometimes be allocated into the tail padding |
| 1272 | // of a base-class subobject. We decide whether that's possible |
| 1273 | // during class layout, so here we can just trust the layout results. |
| 1274 | if (getLangOpts().CPlusPlus) { |
| 1275 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1276 | const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl()); |
| 1277 | sizeAndAlign.first = layout.getDataSize(); |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | return sizeAndAlign; |
| 1282 | } |
| 1283 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1284 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1285 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1286 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1287 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 1288 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1292 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1293 | return getTypeInfoInChars(T.getTypePtr()); |
| 1294 | } |
| 1295 | |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1296 | std::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const { |
| 1297 | TypeInfoMap::iterator it = MemoizedTypeInfo.find(T); |
| 1298 | if (it != MemoizedTypeInfo.end()) |
| 1299 | return it->second; |
| 1300 | |
| 1301 | std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T); |
| 1302 | MemoizedTypeInfo.insert(std::make_pair(T, Info)); |
| 1303 | return Info; |
| 1304 | } |
| 1305 | |
| 1306 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 1307 | /// method does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1308 | /// |
| 1309 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 1310 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1311 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1312 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1313 | ASTContext::getTypeInfoImpl(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 1314 | uint64_t Width=0; |
| 1315 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1316 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1317 | #define TYPE(Class, Base) |
| 1318 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1319 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1320 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1321 | #include "clang/AST/TypeNodes.def" |
John McCall | d3d49bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1322 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1323 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1324 | case Type::FunctionNoProto: |
| 1325 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1326 | // GCC extension: alignof(function) = 32 bits |
| 1327 | Width = 0; |
| 1328 | Align = 32; |
| 1329 | break; |
| 1330 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1331 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1332 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1333 | Width = 0; |
| 1334 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1335 | break; |
| 1336 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1337 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1338 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1339 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1340 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1341 | uint64_t Size = CAT->getSize().getZExtValue(); |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1342 | assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && |
| 1343 | "Overflow in array type bit size evaluation"); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1344 | Width = EltInfo.first*Size; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1345 | Align = EltInfo.second; |
Argyrios Kyrtzidis | cd88b41 | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 1346 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1347 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1348 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1349 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1350 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1351 | const VectorType *VT = cast<VectorType>(T); |
| 1352 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 1353 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1354 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1355 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1356 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1357 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1358 | Align = llvm::NextPowerOf2(Align); |
| 1359 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 1360 | } |
Chad Rosier | f9e9af7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1361 | // Adjust the alignment based on the target max. |
| 1362 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1363 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1364 | Align = TargetVectorAlign; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1365 | break; |
| 1366 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1367 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1368 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1369 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1370 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1371 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1372 | // GCC extension: alignof(void) = 8 bits. |
| 1373 | Width = 0; |
| 1374 | Align = 8; |
| 1375 | break; |
| 1376 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1377 | case BuiltinType::Bool: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1378 | Width = Target->getBoolWidth(); |
| 1379 | Align = Target->getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1380 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1381 | case BuiltinType::Char_S: |
| 1382 | case BuiltinType::Char_U: |
| 1383 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1384 | case BuiltinType::SChar: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1385 | Width = Target->getCharWidth(); |
| 1386 | Align = Target->getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1387 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1388 | case BuiltinType::WChar_S: |
| 1389 | case BuiltinType::WChar_U: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1390 | Width = Target->getWCharWidth(); |
| 1391 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1392 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1393 | case BuiltinType::Char16: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1394 | Width = Target->getChar16Width(); |
| 1395 | Align = Target->getChar16Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1396 | break; |
| 1397 | case BuiltinType::Char32: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1398 | Width = Target->getChar32Width(); |
| 1399 | Align = Target->getChar32Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1400 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1401 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1402 | case BuiltinType::Short: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1403 | Width = Target->getShortWidth(); |
| 1404 | Align = Target->getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1405 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1406 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1407 | case BuiltinType::Int: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1408 | Width = Target->getIntWidth(); |
| 1409 | Align = Target->getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1410 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1411 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1412 | case BuiltinType::Long: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1413 | Width = Target->getLongWidth(); |
| 1414 | Align = Target->getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1415 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1416 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1417 | case BuiltinType::LongLong: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1418 | Width = Target->getLongLongWidth(); |
| 1419 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1420 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1421 | case BuiltinType::Int128: |
| 1422 | case BuiltinType::UInt128: |
| 1423 | Width = 128; |
| 1424 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1425 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1426 | case BuiltinType::Half: |
| 1427 | Width = Target->getHalfWidth(); |
| 1428 | Align = Target->getHalfAlign(); |
| 1429 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1430 | case BuiltinType::Float: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1431 | Width = Target->getFloatWidth(); |
| 1432 | Align = Target->getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1433 | break; |
| 1434 | case BuiltinType::Double: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1435 | Width = Target->getDoubleWidth(); |
| 1436 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1437 | break; |
| 1438 | case BuiltinType::LongDouble: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1439 | Width = Target->getLongDoubleWidth(); |
| 1440 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1441 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1442 | case BuiltinType::NullPtr: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1443 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1444 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1445 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1446 | case BuiltinType::ObjCId: |
| 1447 | case BuiltinType::ObjCClass: |
| 1448 | case BuiltinType::ObjCSel: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1449 | Width = Target->getPointerWidth(0); |
| 1450 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1451 | break; |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1452 | case BuiltinType::OCLSampler: |
| 1453 | // Samplers are modeled as integers. |
| 1454 | Width = Target->getIntWidth(); |
| 1455 | Align = Target->getIntAlign(); |
| 1456 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1457 | case BuiltinType::OCLEvent: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1458 | case BuiltinType::OCLImage1d: |
| 1459 | case BuiltinType::OCLImage1dArray: |
| 1460 | case BuiltinType::OCLImage1dBuffer: |
| 1461 | case BuiltinType::OCLImage2d: |
| 1462 | case BuiltinType::OCLImage2dArray: |
| 1463 | case BuiltinType::OCLImage3d: |
| 1464 | // Currently these types are pointers to opaque types. |
| 1465 | Width = Target->getPointerWidth(0); |
| 1466 | Align = Target->getPointerAlign(0); |
| 1467 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1468 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1469 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1470 | case Type::ObjCObjectPointer: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1471 | Width = Target->getPointerWidth(0); |
| 1472 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1473 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1474 | case Type::BlockPointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1475 | unsigned AS = getTargetAddressSpace( |
| 1476 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1477 | Width = Target->getPointerWidth(AS); |
| 1478 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1479 | break; |
| 1480 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1481 | case Type::LValueReference: |
| 1482 | case Type::RValueReference: { |
| 1483 | // alignof and sizeof should never enter this code path here, so we go |
| 1484 | // the pointer route. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1485 | unsigned AS = getTargetAddressSpace( |
| 1486 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1487 | Width = Target->getPointerWidth(AS); |
| 1488 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1489 | break; |
| 1490 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1491 | case Type::Pointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1492 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1493 | Width = Target->getPointerWidth(AS); |
| 1494 | Align = Target->getPointerAlign(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1495 | break; |
| 1496 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1497 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1498 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1500 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1501 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1502 | Align = PtrDiffInfo.second; |
| 1503 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1504 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1505 | case Type::Complex: { |
| 1506 | // Complex types have the same alignment as their elements, but twice the |
| 1507 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1508 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1509 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1510 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1511 | Align = EltInfo.second; |
| 1512 | break; |
| 1513 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1514 | case Type::ObjCObject: |
| 1515 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1516 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1517 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1518 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1519 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1520 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1521 | break; |
| 1522 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1523 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1524 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1525 | const TagType *TT = cast<TagType>(T); |
| 1526 | |
| 1527 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 22ce41d | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1528 | Width = 8; |
| 1529 | Align = 8; |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1530 | break; |
| 1531 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1533 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1534 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1535 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1536 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1537 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1538 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1539 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1540 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1541 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1542 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1543 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1544 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1545 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1546 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1547 | case Type::Auto: { |
| 1548 | const AutoType *A = cast<AutoType>(T); |
| 1549 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | dc856af | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1550 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1553 | case Type::Paren: |
| 1554 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1555 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1556 | case Type::Typedef: { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1557 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1558 | std::pair<uint64_t, unsigned> Info |
| 1559 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1560 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1561 | // alignment we have. This violates the GCC documentation (which says that |
| 1562 | // attribute(aligned) can only round up) but matches its implementation. |
| 1563 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1564 | Align = AttrAlign; |
| 1565 | else |
| 1566 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1567 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1568 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1569 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1570 | |
| 1571 | case Type::TypeOfExpr: |
| 1572 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1573 | .getTypePtr()); |
| 1574 | |
| 1575 | case Type::TypeOf: |
| 1576 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1577 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1578 | case Type::Decltype: |
| 1579 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1580 | .getTypePtr()); |
| 1581 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1582 | case Type::UnaryTransform: |
| 1583 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1584 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1585 | case Type::Elaborated: |
| 1586 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1588 | case Type::Attributed: |
| 1589 | return getTypeInfo( |
| 1590 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1591 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1592 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1593 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1594 | "Cannot request the size of a dependent type"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1595 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1596 | // A type alias template specialization may refer to a typedef with the |
| 1597 | // aligned attribute on it. |
| 1598 | if (TST->isTypeAlias()) |
| 1599 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1600 | else |
| 1601 | return getTypeInfo(getCanonicalType(T)); |
| 1602 | } |
| 1603 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1604 | case Type::Atomic: { |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1605 | std::pair<uint64_t, unsigned> Info |
| 1606 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1607 | Width = Info.first; |
| 1608 | Align = Info.second; |
| 1609 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() && |
| 1610 | llvm::isPowerOf2_64(Width)) { |
| 1611 | // We can potentially perform lock-free atomic operations for this |
| 1612 | // type; promote the alignment appropriately. |
| 1613 | // FIXME: We could potentially promote the width here as well... |
| 1614 | // is that worthwhile? (Non-struct atomic types generally have |
| 1615 | // power-of-two size anyway, but structs might not. Requires a bit |
| 1616 | // of implementation work to make sure we zero out the extra bits.) |
| 1617 | Align = static_cast<unsigned>(Width); |
| 1618 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1621 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1622 | |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1623 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1624 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1627 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1628 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1629 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1630 | } |
| 1631 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1632 | /// toBits - Convert a size in characters to a size in characters. |
| 1633 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1634 | return CharSize.getQuantity() * getCharWidth(); |
| 1635 | } |
| 1636 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1637 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1638 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1639 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1640 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1641 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1642 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1643 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1646 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1647 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1648 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1649 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1650 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1651 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1652 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1655 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1656 | /// type for the current target in bits. This can be different than the ABI |
| 1657 | /// alignment in cases where it is beneficial for performance to overalign |
| 1658 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1659 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1660 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1661 | |
| 1662 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1663 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1664 | T = CT->getElementType().getTypePtr(); |
| 1665 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | cde7a1d | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1666 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1667 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1668 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1669 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1670 | return ABIAlign; |
| 1671 | } |
| 1672 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1673 | /// DeepCollectObjCIvars - |
| 1674 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1675 | /// super class and then collects all ivars, including those synthesized for |
| 1676 | /// current class. This routine is used for implementation of current class |
| 1677 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1678 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1679 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1680 | bool leafClass, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1681 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1682 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1683 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1684 | if (!leafClass) { |
| 1685 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1686 | E = OI->ivar_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1687 | Ivars.push_back(*I); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1688 | } else { |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1689 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1690 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1691 | Iv= Iv->getNextIvar()) |
| 1692 | Ivars.push_back(Iv); |
| 1693 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1696 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1697 | /// those inherited by it. |
| 1698 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1699 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1700 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1701 | // We can use protocol_iterator here instead of |
| 1702 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1703 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1704 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1705 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1706 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1707 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1708 | PE = Proto->protocol_end(); P != PE; ++P) { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1709 | Protocols.insert((*P)->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1710 | CollectInheritedProtocols(*P, Protocols); |
| 1711 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1712 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1713 | |
| 1714 | // Categories of this Interface. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1715 | for (ObjCInterfaceDecl::visible_categories_iterator |
| 1716 | Cat = OI->visible_categories_begin(), |
| 1717 | CatEnd = OI->visible_categories_end(); |
| 1718 | Cat != CatEnd; ++Cat) { |
| 1719 | CollectInheritedProtocols(*Cat, Protocols); |
| 1720 | } |
| 1721 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1722 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1723 | while (SD) { |
| 1724 | CollectInheritedProtocols(SD, Protocols); |
| 1725 | SD = SD->getSuperClass(); |
| 1726 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1727 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1728 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1729 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1730 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1731 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1732 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1733 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1734 | CollectInheritedProtocols(*P, Protocols); |
| 1735 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1736 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1737 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1738 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1739 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1740 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1741 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1742 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1743 | CollectInheritedProtocols(*P, Protocols); |
| 1744 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1748 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1749 | unsigned count = 0; |
| 1750 | // Count ivars declared in class extension. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1751 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 1752 | Ext = OI->known_extensions_begin(), |
| 1753 | ExtEnd = OI->known_extensions_end(); |
| 1754 | Ext != ExtEnd; ++Ext) { |
| 1755 | count += Ext->ivar_size(); |
| 1756 | } |
| 1757 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1758 | // Count ivar defined in this class's implementation. This |
| 1759 | // includes synthesized ivars. |
| 1760 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1761 | count += ImplDecl->ivar_size(); |
| 1762 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1763 | return count; |
| 1764 | } |
| 1765 | |
Argyrios Kyrtzidis | 8deabc1 | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 1766 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 1767 | if (!E) |
| 1768 | return false; |
| 1769 | |
| 1770 | // nullptr_t is always treated as null. |
| 1771 | if (E->getType()->isNullPtrType()) return true; |
| 1772 | |
| 1773 | if (E->getType()->isAnyPointerType() && |
| 1774 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 1775 | Expr::NPC_ValueDependentIsNull)) |
| 1776 | return true; |
| 1777 | |
| 1778 | // Unfortunately, __null has type 'int'. |
| 1779 | if (isa<GNUNullExpr>(E)) return true; |
| 1780 | |
| 1781 | return false; |
| 1782 | } |
| 1783 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1784 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1785 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1786 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1787 | I = ObjCImpls.find(D); |
| 1788 | if (I != ObjCImpls.end()) |
| 1789 | return cast<ObjCImplementationDecl>(I->second); |
| 1790 | return 0; |
| 1791 | } |
| 1792 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1793 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1794 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1795 | I = ObjCImpls.find(D); |
| 1796 | if (I != ObjCImpls.end()) |
| 1797 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1798 | return 0; |
| 1799 | } |
| 1800 | |
| 1801 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1802 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1803 | ObjCImplementationDecl *ImplD) { |
| 1804 | assert(IFaceD && ImplD && "Passed null params"); |
| 1805 | ObjCImpls[IFaceD] = ImplD; |
| 1806 | } |
| 1807 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1808 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1809 | ObjCCategoryImplDecl *ImplD) { |
| 1810 | assert(CatD && ImplD && "Passed null params"); |
| 1811 | ObjCImpls[CatD] = ImplD; |
| 1812 | } |
| 1813 | |
Dmitri Gribenko | b35cc2d | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1814 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( |
| 1815 | const NamedDecl *ND) const { |
| 1816 | if (const ObjCInterfaceDecl *ID = |
| 1817 | dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1818 | return ID; |
Dmitri Gribenko | b35cc2d | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1819 | if (const ObjCCategoryDecl *CD = |
| 1820 | dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1821 | return CD->getClassInterface(); |
Dmitri Gribenko | b35cc2d | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1822 | if (const ObjCImplDecl *IMD = |
| 1823 | dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1824 | return IMD->getClassInterface(); |
| 1825 | |
| 1826 | return 0; |
| 1827 | } |
| 1828 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1829 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1830 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1831 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1832 | assert(VD && "Passed null params"); |
| 1833 | assert(VD->hasAttr<BlocksAttr>() && |
| 1834 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1835 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1836 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1837 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1838 | } |
| 1839 | |
| 1840 | /// \brief Set the copy inialization expression of a block var decl. |
| 1841 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1842 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1843 | assert(VD->hasAttr<BlocksAttr>() && |
| 1844 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1845 | BlockVarCopyInits[VD] = Init; |
| 1846 | } |
| 1847 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1848 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1849 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1850 | if (!DataSize) |
| 1851 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1852 | else |
| 1853 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1854 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1855 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1856 | TypeSourceInfo *TInfo = |
| 1857 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1858 | new (TInfo) TypeSourceInfo(T); |
| 1859 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1862 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1863 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1864 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1865 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1866 | return DI; |
| 1867 | } |
| 1868 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1869 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1870 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1871 | return getObjCLayout(D, 0); |
| 1872 | } |
| 1873 | |
| 1874 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1875 | ASTContext::getASTObjCImplementationLayout( |
| 1876 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1877 | return getObjCLayout(D->getClassInterface(), D); |
| 1878 | } |
| 1879 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1880 | //===----------------------------------------------------------------------===// |
| 1881 | // Type creation/memoization methods |
| 1882 | //===----------------------------------------------------------------------===// |
| 1883 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1884 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1885 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1886 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1887 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1888 | |
| 1889 | // Check if we've already instantiated this type. |
| 1890 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1891 | ExtQuals::Profile(ID, baseType, quals); |
| 1892 | void *insertPos = 0; |
| 1893 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1894 | assert(eq->getQualifiers() == quals); |
| 1895 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1898 | // If the base type is not canonical, make the appropriate canonical type. |
| 1899 | QualType canon; |
| 1900 | if (!baseType->isCanonicalUnqualified()) { |
| 1901 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1902 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 1903 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1904 | |
| 1905 | // Re-find the insert position. |
| 1906 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1907 | } |
| 1908 | |
| 1909 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1910 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1911 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1914 | QualType |
| 1915 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1916 | QualType CanT = getCanonicalType(T); |
| 1917 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1918 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1919 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1920 | // If we are composing extended qualifiers together, merge together |
| 1921 | // into one ExtQuals node. |
| 1922 | QualifierCollector Quals; |
| 1923 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1924 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1925 | // If this type already has an address space specified, it cannot get |
| 1926 | // another one. |
| 1927 | assert(!Quals.hasAddressSpace() && |
| 1928 | "Type cannot be in multiple addr spaces!"); |
| 1929 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1931 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1934 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1935 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1936 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1937 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1938 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1940 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1941 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1942 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1943 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1944 | return getPointerType(ResultType); |
| 1945 | } |
| 1946 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1948 | // If we are composing extended qualifiers together, merge together |
| 1949 | // into one ExtQuals node. |
| 1950 | QualifierCollector Quals; |
| 1951 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1953 | // If this type already has an ObjCGC specified, it cannot get |
| 1954 | // another one. |
| 1955 | assert(!Quals.hasObjCGCAttr() && |
| 1956 | "Type cannot have multiple ObjCGCs!"); |
| 1957 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1959 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1960 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1961 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1962 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1963 | FunctionType::ExtInfo Info) { |
| 1964 | if (T->getExtInfo() == Info) |
| 1965 | return T; |
| 1966 | |
| 1967 | QualType Result; |
| 1968 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1969 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1970 | } else { |
| 1971 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1972 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1973 | EPI.ExtInfo = Info; |
| 1974 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1975 | FPT->getNumArgs(), EPI); |
| 1976 | } |
| 1977 | |
| 1978 | return cast<FunctionType>(Result.getTypePtr()); |
| 1979 | } |
| 1980 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1981 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1982 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1983 | QualType ASTContext::getComplexType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1984 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1985 | // structure. |
| 1986 | llvm::FoldingSetNodeID ID; |
| 1987 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1989 | void *InsertPos = 0; |
| 1990 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1991 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1992 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1993 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1994 | // so fill in the canonical type field. |
| 1995 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1996 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1997 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1999 | // Get the new insert position for the node we care about. |
| 2000 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2001 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2002 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2003 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2004 | Types.push_back(New); |
| 2005 | ComplexTypes.InsertNode(New, InsertPos); |
| 2006 | return QualType(New, 0); |
| 2007 | } |
| 2008 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2009 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 2010 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2011 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2012 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2013 | // structure. |
| 2014 | llvm::FoldingSetNodeID ID; |
| 2015 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2016 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2017 | void *InsertPos = 0; |
| 2018 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2019 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2020 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2021 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2022 | // so fill in the canonical type field. |
| 2023 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2024 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2025 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2026 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2027 | // Get the new insert position for the node we care about. |
| 2028 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2029 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2030 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2031 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2032 | Types.push_back(New); |
| 2033 | PointerTypes.InsertNode(New, InsertPos); |
| 2034 | return QualType(New, 0); |
| 2035 | } |
| 2036 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2037 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2038 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2039 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2040 | assert(T->isFunctionType() && "block of function types only"); |
| 2041 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2042 | // structure. |
| 2043 | llvm::FoldingSetNodeID ID; |
| 2044 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2045 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2046 | void *InsertPos = 0; |
| 2047 | if (BlockPointerType *PT = |
| 2048 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2049 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | |
| 2051 | // 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] | 2052 | // type either so fill in the canonical type field. |
| 2053 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2054 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2055 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2057 | // Get the new insert position for the node we care about. |
| 2058 | BlockPointerType *NewIP = |
| 2059 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2060 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2061 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2062 | BlockPointerType *New |
| 2063 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2064 | Types.push_back(New); |
| 2065 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 2066 | return QualType(New, 0); |
| 2067 | } |
| 2068 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2069 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 2070 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2071 | QualType |
| 2072 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 2073 | assert(getCanonicalType(T) != OverloadTy && |
| 2074 | "Unresolved overloaded function type"); |
| 2075 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2076 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2077 | // structure. |
| 2078 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2079 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2080 | |
| 2081 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2082 | if (LValueReferenceType *RT = |
| 2083 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2084 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2085 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2086 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2087 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2088 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2089 | // either, so fill in the canonical type field. |
| 2090 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2091 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 2092 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2093 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2094 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2095 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2096 | LValueReferenceType *NewIP = |
| 2097 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2098 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2101 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2102 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 2103 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2104 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2105 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2106 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2107 | return QualType(New, 0); |
| 2108 | } |
| 2109 | |
| 2110 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 2111 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2112 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2113 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2114 | // structure. |
| 2115 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2116 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2117 | |
| 2118 | void *InsertPos = 0; |
| 2119 | if (RValueReferenceType *RT = |
| 2120 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2121 | return QualType(RT, 0); |
| 2122 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2123 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2124 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2125 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2126 | // either, so fill in the canonical type field. |
| 2127 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2128 | if (InnerRef || !T.isCanonical()) { |
| 2129 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2130 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2131 | |
| 2132 | // Get the new insert position for the node we care about. |
| 2133 | RValueReferenceType *NewIP = |
| 2134 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2135 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2138 | RValueReferenceType *New |
| 2139 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2140 | Types.push_back(New); |
| 2141 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2142 | return QualType(New, 0); |
| 2143 | } |
| 2144 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2145 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 2146 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2147 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2148 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2149 | // structure. |
| 2150 | llvm::FoldingSetNodeID ID; |
| 2151 | MemberPointerType::Profile(ID, T, Cls); |
| 2152 | |
| 2153 | void *InsertPos = 0; |
| 2154 | if (MemberPointerType *PT = |
| 2155 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2156 | return QualType(PT, 0); |
| 2157 | |
| 2158 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 2159 | // type either, so fill in the canonical type field. |
| 2160 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2161 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2162 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 2163 | |
| 2164 | // Get the new insert position for the node we care about. |
| 2165 | MemberPointerType *NewIP = |
| 2166 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2167 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2168 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2169 | MemberPointerType *New |
| 2170 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2171 | Types.push_back(New); |
| 2172 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 2173 | return QualType(New, 0); |
| 2174 | } |
| 2175 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2177 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2178 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2179 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2180 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2181 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 2182 | assert((EltTy->isDependentType() || |
| 2183 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 2184 | "Constant array of VLAs is illegal!"); |
| 2185 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2186 | // Convert the array size into a canonical width matching the pointer size for |
| 2187 | // the target. |
| 2188 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2189 | ArySize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2190 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2192 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2193 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2195 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2196 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2197 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2198 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2200 | // If the element type isn't canonical or has qualifiers, this won't |
| 2201 | // be a canonical type either, so fill in the canonical type field. |
| 2202 | QualType Canon; |
| 2203 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2204 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2205 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2206 | ASM, IndexTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2207 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2208 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2209 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2210 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2211 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2212 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2215 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2216 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2217 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2218 | Types.push_back(New); |
| 2219 | return QualType(New, 0); |
| 2220 | } |
| 2221 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2222 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 2223 | /// variably-modified, into the corresponding type with all the known |
| 2224 | /// sizes replaced with [*]. |
| 2225 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 2226 | // Vastly most common case. |
| 2227 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2228 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2229 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2230 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2231 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2232 | const Type *ty = split.Ty; |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2233 | switch (ty->getTypeClass()) { |
| 2234 | #define TYPE(Class, Base) |
| 2235 | #define ABSTRACT_TYPE(Class, Base) |
| 2236 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2237 | #include "clang/AST/TypeNodes.def" |
| 2238 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 2239 | |
| 2240 | // These types should never be variably-modified. |
| 2241 | case Type::Builtin: |
| 2242 | case Type::Complex: |
| 2243 | case Type::Vector: |
| 2244 | case Type::ExtVector: |
| 2245 | case Type::DependentSizedExtVector: |
| 2246 | case Type::ObjCObject: |
| 2247 | case Type::ObjCInterface: |
| 2248 | case Type::ObjCObjectPointer: |
| 2249 | case Type::Record: |
| 2250 | case Type::Enum: |
| 2251 | case Type::UnresolvedUsing: |
| 2252 | case Type::TypeOfExpr: |
| 2253 | case Type::TypeOf: |
| 2254 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2255 | case Type::UnaryTransform: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2256 | case Type::DependentName: |
| 2257 | case Type::InjectedClassName: |
| 2258 | case Type::TemplateSpecialization: |
| 2259 | case Type::DependentTemplateSpecialization: |
| 2260 | case Type::TemplateTypeParm: |
| 2261 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2262 | case Type::Auto: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2263 | case Type::PackExpansion: |
| 2264 | llvm_unreachable("type should never be variably-modified"); |
| 2265 | |
| 2266 | // These types can be variably-modified but should never need to |
| 2267 | // further decay. |
| 2268 | case Type::FunctionNoProto: |
| 2269 | case Type::FunctionProto: |
| 2270 | case Type::BlockPointer: |
| 2271 | case Type::MemberPointer: |
| 2272 | return type; |
| 2273 | |
| 2274 | // These types can be variably-modified. All these modifications |
| 2275 | // preserve structure except as noted by comments. |
| 2276 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 2277 | // optimizations available here. |
| 2278 | case Type::Pointer: |
| 2279 | result = getPointerType(getVariableArrayDecayedType( |
| 2280 | cast<PointerType>(ty)->getPointeeType())); |
| 2281 | break; |
| 2282 | |
| 2283 | case Type::LValueReference: { |
| 2284 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 2285 | result = getLValueReferenceType( |
| 2286 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 2287 | lv->isSpelledAsLValue()); |
| 2288 | break; |
| 2289 | } |
| 2290 | |
| 2291 | case Type::RValueReference: { |
| 2292 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 2293 | result = getRValueReferenceType( |
| 2294 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 2295 | break; |
| 2296 | } |
| 2297 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2298 | case Type::Atomic: { |
| 2299 | const AtomicType *at = cast<AtomicType>(ty); |
| 2300 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 2301 | break; |
| 2302 | } |
| 2303 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2304 | case Type::ConstantArray: { |
| 2305 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 2306 | result = getConstantArrayType( |
| 2307 | getVariableArrayDecayedType(cat->getElementType()), |
| 2308 | cat->getSize(), |
| 2309 | cat->getSizeModifier(), |
| 2310 | cat->getIndexTypeCVRQualifiers()); |
| 2311 | break; |
| 2312 | } |
| 2313 | |
| 2314 | case Type::DependentSizedArray: { |
| 2315 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 2316 | result = getDependentSizedArrayType( |
| 2317 | getVariableArrayDecayedType(dat->getElementType()), |
| 2318 | dat->getSizeExpr(), |
| 2319 | dat->getSizeModifier(), |
| 2320 | dat->getIndexTypeCVRQualifiers(), |
| 2321 | dat->getBracketsRange()); |
| 2322 | break; |
| 2323 | } |
| 2324 | |
| 2325 | // Turn incomplete types into [*] types. |
| 2326 | case Type::IncompleteArray: { |
| 2327 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 2328 | result = getVariableArrayType( |
| 2329 | getVariableArrayDecayedType(iat->getElementType()), |
| 2330 | /*size*/ 0, |
| 2331 | ArrayType::Normal, |
| 2332 | iat->getIndexTypeCVRQualifiers(), |
| 2333 | SourceRange()); |
| 2334 | break; |
| 2335 | } |
| 2336 | |
| 2337 | // Turn VLA types into [*] types. |
| 2338 | case Type::VariableArray: { |
| 2339 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 2340 | result = getVariableArrayType( |
| 2341 | getVariableArrayDecayedType(vat->getElementType()), |
| 2342 | /*size*/ 0, |
| 2343 | ArrayType::Star, |
| 2344 | vat->getIndexTypeCVRQualifiers(), |
| 2345 | vat->getBracketsRange()); |
| 2346 | break; |
| 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | // Apply the top-level qualifiers from the original. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2351 | return getQualifiedType(result, split.Quals); |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2352 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2353 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 2354 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 2355 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2356 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 2357 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2358 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2359 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2360 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2361 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 2362 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2363 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2364 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2365 | // Be sure to pull qualifiers off the element type. |
| 2366 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2367 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2368 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2369 | IndexTypeQuals, Brackets); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2370 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2373 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2374 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2375 | |
| 2376 | VariableArrayTypes.push_back(New); |
| 2377 | Types.push_back(New); |
| 2378 | return QualType(New, 0); |
| 2379 | } |
| 2380 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2381 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 2382 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 2383 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2384 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 2385 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2386 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2387 | unsigned elementTypeQuals, |
| 2388 | SourceRange brackets) const { |
| 2389 | assert((!numElements || numElements->isTypeDependent() || |
| 2390 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2391 | "Size must be type- or value-dependent!"); |
| 2392 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2393 | // Dependently-sized array types that do not have a specified number |
| 2394 | // of elements will have their sizes deduced from a dependent |
| 2395 | // initializer. We do no canonicalization here at all, which is okay |
| 2396 | // because they can't be used in most locations. |
| 2397 | if (!numElements) { |
| 2398 | DependentSizedArrayType *newType |
| 2399 | = new (*this, TypeAlignment) |
| 2400 | DependentSizedArrayType(*this, elementType, QualType(), |
| 2401 | numElements, ASM, elementTypeQuals, |
| 2402 | brackets); |
| 2403 | Types.push_back(newType); |
| 2404 | return QualType(newType, 0); |
| 2405 | } |
| 2406 | |
| 2407 | // Otherwise, we actually build a new type every time, but we |
| 2408 | // also build a canonical type. |
| 2409 | |
| 2410 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 2411 | |
| 2412 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2413 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2414 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2415 | QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2416 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2417 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2418 | // Look for an existing type with these properties. |
| 2419 | DependentSizedArrayType *canonTy = |
| 2420 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2421 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2422 | // If we don't have one, build one. |
| 2423 | if (!canonTy) { |
| 2424 | canonTy = new (*this, TypeAlignment) |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2425 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2426 | QualType(), numElements, ASM, elementTypeQuals, |
| 2427 | brackets); |
| 2428 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 2429 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2432 | // Apply qualifiers from the element type to the array. |
| 2433 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2434 | canonElementType.Quals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2435 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2436 | // If we didn't need extra canonicalization for the element type, |
| 2437 | // then just use that as our result. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2438 | if (QualType(canonElementType.Ty, 0) == elementType) |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2439 | return canon; |
| 2440 | |
| 2441 | // Otherwise, we need to build a type which follows the spelling |
| 2442 | // of the element type. |
| 2443 | DependentSizedArrayType *sugaredType |
| 2444 | = new (*this, TypeAlignment) |
| 2445 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 2446 | ASM, elementTypeQuals, brackets); |
| 2447 | Types.push_back(sugaredType); |
| 2448 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2451 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2452 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2453 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2454 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2455 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2456 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2457 | void *insertPos = 0; |
| 2458 | if (IncompleteArrayType *iat = |
| 2459 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 2460 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2461 | |
| 2462 | // 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] | 2463 | // either, so fill in the canonical type field. We also have to pull |
| 2464 | // qualifiers off the element type. |
| 2465 | QualType canon; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2466 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2467 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 2468 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2469 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2470 | ASM, elementTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2471 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2472 | |
| 2473 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2474 | IncompleteArrayType *existing = |
| 2475 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 2476 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 2477 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2478 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2479 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 2480 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2481 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2482 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 2483 | Types.push_back(newType); |
| 2484 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2487 | /// getVectorType - Return the unique reference to a vector type of |
| 2488 | /// 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] | 2489 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2490 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2491 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2492 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2493 | // Check if we've already instantiated a vector of this type. |
| 2494 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2495 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2496 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2497 | void *InsertPos = 0; |
| 2498 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2499 | return QualType(VTP, 0); |
| 2500 | |
| 2501 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2502 | // so fill in the canonical type field. |
| 2503 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2504 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2505 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2506 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2507 | // Get the new insert position for the node we care about. |
| 2508 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2509 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2510 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2511 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2512 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2513 | VectorTypes.InsertNode(New, InsertPos); |
| 2514 | Types.push_back(New); |
| 2515 | return QualType(New, 0); |
| 2516 | } |
| 2517 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2518 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2519 | /// 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] | 2520 | QualType |
| 2521 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2522 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2523 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2524 | // Check if we've already instantiated a vector of this type. |
| 2525 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2526 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2527 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2528 | void *InsertPos = 0; |
| 2529 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2530 | return QualType(VTP, 0); |
| 2531 | |
| 2532 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2533 | // so fill in the canonical type field. |
| 2534 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2535 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2536 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2537 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2538 | // Get the new insert position for the node we care about. |
| 2539 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2540 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2541 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2542 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2543 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2544 | VectorTypes.InsertNode(New, InsertPos); |
| 2545 | Types.push_back(New); |
| 2546 | return QualType(New, 0); |
| 2547 | } |
| 2548 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2549 | QualType |
| 2550 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2551 | Expr *SizeExpr, |
| 2552 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2553 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2555 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2556 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2557 | void *InsertPos = 0; |
| 2558 | DependentSizedExtVectorType *Canon |
| 2559 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2560 | DependentSizedExtVectorType *New; |
| 2561 | if (Canon) { |
| 2562 | // We already have a canonical version of this array type; use it as |
| 2563 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2564 | New = new (*this, TypeAlignment) |
| 2565 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2566 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2567 | } else { |
| 2568 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2569 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2570 | New = new (*this, TypeAlignment) |
| 2571 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2572 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2573 | |
| 2574 | DependentSizedExtVectorType *CanonCheck |
| 2575 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2576 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2577 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2578 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2579 | } else { |
| 2580 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2581 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2582 | New = new (*this, TypeAlignment) |
| 2583 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2584 | } |
| 2585 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2586 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2587 | Types.push_back(New); |
| 2588 | return QualType(New, 0); |
| 2589 | } |
| 2590 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2591 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2592 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2593 | QualType |
| 2594 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2595 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2596 | const CallingConv DefaultCC = Info.getCC(); |
| 2597 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2598 | CC_X86StdCall : DefaultCC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2599 | // Unique functions, to guarantee there is only one function of a particular |
| 2600 | // structure. |
| 2601 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2602 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2603 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2604 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2606 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2607 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2609 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2610 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2611 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2612 | Canonical = |
| 2613 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2614 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2615 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2616 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2617 | FunctionNoProtoType *NewIP = |
| 2618 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2619 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2620 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2621 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2622 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2623 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2624 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2625 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2626 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2627 | return QualType(New, 0); |
| 2628 | } |
| 2629 | |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2630 | /// \brief Determine whether \p T is canonical as the result type of a function. |
| 2631 | static bool isCanonicalResultType(QualType T) { |
| 2632 | return T.isCanonical() && |
| 2633 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
| 2634 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
| 2635 | } |
| 2636 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2637 | /// getFunctionType - Return a normal function type with a typed argument |
| 2638 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2639 | QualType |
| 2640 | ASTContext::getFunctionType(QualType ResultTy, |
| 2641 | const QualType *ArgArray, unsigned NumArgs, |
| 2642 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2643 | // Unique functions, to guarantee there is only one function of a particular |
| 2644 | // structure. |
| 2645 | llvm::FoldingSetNodeID ID; |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2646 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2647 | |
| 2648 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2650 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2651 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2652 | |
| 2653 | // Determine whether the type being created is already canonical or not. |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2654 | bool isCanonical = |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2655 | EPI.ExceptionSpecType == EST_None && isCanonicalResultType(ResultTy) && |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2656 | !EPI.HasTrailingReturn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2657 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2658 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2659 | isCanonical = false; |
| 2660 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2661 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2662 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2663 | CC_X86StdCall : DefaultCC; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2664 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2665 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2666 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2667 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2668 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2669 | SmallVector<QualType, 16> CanonicalArgs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2670 | CanonicalArgs.reserve(NumArgs); |
| 2671 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2672 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2673 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2674 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2675 | CanonicalEPI.HasTrailingReturn = false; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2676 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2677 | CanonicalEPI.NumExceptions = 0; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2678 | CanonicalEPI.ExtInfo |
| 2679 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2680 | |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2681 | // Result types do not have ARC lifetime qualifiers. |
| 2682 | QualType CanResultTy = getCanonicalType(ResultTy); |
| 2683 | if (ResultTy.getQualifiers().hasObjCLifetime()) { |
| 2684 | Qualifiers Qs = CanResultTy.getQualifiers(); |
| 2685 | Qs.removeObjCLifetime(); |
| 2686 | CanResultTy = getQualifiedType(CanResultTy.getUnqualifiedType(), Qs); |
| 2687 | } |
| 2688 | |
| 2689 | Canonical = getFunctionType(CanResultTy, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2690 | CanonicalArgs.data(), NumArgs, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2691 | CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2692 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2693 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2694 | FunctionProtoType *NewIP = |
| 2695 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2696 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2697 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2698 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2699 | // FunctionProtoType objects are allocated with extra bytes after |
| 2700 | // them for three variable size arrays at the end: |
| 2701 | // - parameter types |
| 2702 | // - exception types |
| 2703 | // - consumed-arguments flags |
| 2704 | // Instead of the exception types, there could be a noexcept |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2705 | // expression, or information used to resolve the exception |
| 2706 | // specification. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2707 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2708 | NumArgs * sizeof(QualType); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2709 | if (EPI.ExceptionSpecType == EST_Dynamic) { |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2710 | Size += EPI.NumExceptions * sizeof(QualType); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2711 | } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2712 | Size += sizeof(Expr*); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2713 | } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2714 | Size += 2 * sizeof(FunctionDecl*); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2715 | } else if (EPI.ExceptionSpecType == EST_Unevaluated) { |
| 2716 | Size += sizeof(FunctionDecl*); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2717 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2718 | if (EPI.ConsumedArguments) |
| 2719 | Size += NumArgs * sizeof(bool); |
| 2720 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2721 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2722 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2723 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2724 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2725 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2726 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2727 | return QualType(FTP, 0); |
| 2728 | } |
| 2729 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2730 | #ifndef NDEBUG |
| 2731 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2732 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2733 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2734 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2735 | return true; |
| 2736 | if (RD->getDescribedClassTemplate() && |
| 2737 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2738 | return true; |
| 2739 | return false; |
| 2740 | } |
| 2741 | #endif |
| 2742 | |
| 2743 | /// getInjectedClassNameType - Return the unique reference to the |
| 2744 | /// injected class name type for the specified templated declaration. |
| 2745 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2746 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2747 | assert(NeedsInjectedClassNameType(Decl)); |
| 2748 | if (Decl->TypeForDecl) { |
| 2749 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2750 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2751 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2752 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2753 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2754 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2755 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2756 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2757 | Decl->TypeForDecl = newType; |
| 2758 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2759 | } |
| 2760 | return QualType(Decl->TypeForDecl, 0); |
| 2761 | } |
| 2762 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2763 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2764 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2765 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2766 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2767 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2768 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2769 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2770 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2771 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2772 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2773 | "Template type parameter types are always available."); |
| 2774 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2775 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2776 | assert(!Record->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2777 | "struct/union has previous declaration"); |
| 2778 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2779 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2780 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2781 | assert(!Enum->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2782 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2783 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2784 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2785 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2786 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2787 | Decl->TypeForDecl = newType; |
| 2788 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2789 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2790 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2791 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2792 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2795 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2796 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2797 | QualType |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2798 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2799 | QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2800 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2801 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2802 | if (Canonical.isNull()) |
| 2803 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2804 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2805 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2806 | Decl->TypeForDecl = newType; |
| 2807 | Types.push_back(newType); |
| 2808 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2811 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2812 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2813 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2814 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2815 | if (PrevDecl->TypeForDecl) |
| 2816 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2817 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2818 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2819 | Decl->TypeForDecl = newType; |
| 2820 | Types.push_back(newType); |
| 2821 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2824 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2825 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2826 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2827 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2828 | if (PrevDecl->TypeForDecl) |
| 2829 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2830 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2831 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2832 | Decl->TypeForDecl = newType; |
| 2833 | Types.push_back(newType); |
| 2834 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2837 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2838 | QualType modifiedType, |
| 2839 | QualType equivalentType) { |
| 2840 | llvm::FoldingSetNodeID id; |
| 2841 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2842 | |
| 2843 | void *insertPos = 0; |
| 2844 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2845 | if (type) return QualType(type, 0); |
| 2846 | |
| 2847 | QualType canon = getCanonicalType(equivalentType); |
| 2848 | type = new (*this, TypeAlignment) |
| 2849 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2850 | |
| 2851 | Types.push_back(type); |
| 2852 | AttributedTypes.InsertNode(type, insertPos); |
| 2853 | |
| 2854 | return QualType(type, 0); |
| 2855 | } |
| 2856 | |
| 2857 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2858 | /// \brief Retrieve a substitution-result type. |
| 2859 | QualType |
| 2860 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2861 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2862 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2863 | && "replacement types must always be canonical"); |
| 2864 | |
| 2865 | llvm::FoldingSetNodeID ID; |
| 2866 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2867 | void *InsertPos = 0; |
| 2868 | SubstTemplateTypeParmType *SubstParm |
| 2869 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2870 | |
| 2871 | if (!SubstParm) { |
| 2872 | SubstParm = new (*this, TypeAlignment) |
| 2873 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2874 | Types.push_back(SubstParm); |
| 2875 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2876 | } |
| 2877 | |
| 2878 | return QualType(SubstParm, 0); |
| 2879 | } |
| 2880 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2881 | /// \brief Retrieve a |
| 2882 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2883 | const TemplateTypeParmType *Parm, |
| 2884 | const TemplateArgument &ArgPack) { |
| 2885 | #ifndef NDEBUG |
| 2886 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2887 | PEnd = ArgPack.pack_end(); |
| 2888 | P != PEnd; ++P) { |
| 2889 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2890 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2891 | } |
| 2892 | #endif |
| 2893 | |
| 2894 | llvm::FoldingSetNodeID ID; |
| 2895 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2896 | void *InsertPos = 0; |
| 2897 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2898 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2899 | return QualType(SubstParm, 0); |
| 2900 | |
| 2901 | QualType Canon; |
| 2902 | if (!Parm->isCanonicalUnqualified()) { |
| 2903 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2904 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2905 | ArgPack); |
| 2906 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2907 | } |
| 2908 | |
| 2909 | SubstTemplateTypeParmPackType *SubstParm |
| 2910 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2911 | ArgPack); |
| 2912 | Types.push_back(SubstParm); |
| 2913 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2914 | return QualType(SubstParm, 0); |
| 2915 | } |
| 2916 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2917 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2918 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2919 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2920 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2921 | bool ParameterPack, |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2922 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2923 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2924 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2925 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2926 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2927 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2928 | |
| 2929 | if (TypeParm) |
| 2930 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2932 | if (TTPDecl) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2933 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2934 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2935 | |
| 2936 | TemplateTypeParmType *TypeCheck |
| 2937 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2938 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2939 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2940 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2941 | TypeParm = new (*this, TypeAlignment) |
| 2942 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2943 | |
| 2944 | Types.push_back(TypeParm); |
| 2945 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2946 | |
| 2947 | return QualType(TypeParm, 0); |
| 2948 | } |
| 2949 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2950 | TypeSourceInfo * |
| 2951 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2952 | SourceLocation NameLoc, |
| 2953 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2954 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2955 | assert(!Name.getAsDependentTemplateName() && |
| 2956 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2957 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2958 | |
| 2959 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2960 | TemplateSpecializationTypeLoc TL = |
| 2961 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2962 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2963 | TL.setTemplateNameLoc(NameLoc); |
| 2964 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2965 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2966 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2967 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2968 | return DI; |
| 2969 | } |
| 2970 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2971 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2972 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2973 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2974 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2975 | assert(!Template.getAsDependentTemplateName() && |
| 2976 | "No dependent template names here!"); |
| 2977 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2978 | unsigned NumArgs = Args.size(); |
| 2979 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2980 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2981 | ArgVec.reserve(NumArgs); |
| 2982 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2983 | ArgVec.push_back(Args[i].getArgument()); |
| 2984 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2985 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2986 | Underlying); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2989 | #ifndef NDEBUG |
| 2990 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 2991 | unsigned NumArgs) { |
| 2992 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2993 | if (Args[I].isPackExpansion()) |
| 2994 | return true; |
| 2995 | |
| 2996 | return true; |
| 2997 | } |
| 2998 | #endif |
| 2999 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3000 | QualType |
| 3001 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3002 | const TemplateArgument *Args, |
| 3003 | unsigned NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3004 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3005 | assert(!Template.getAsDependentTemplateName() && |
| 3006 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3007 | // Look through qualified template names. |
| 3008 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3009 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3010 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3011 | bool IsTypeAlias = |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3012 | Template.getAsTemplateDecl() && |
| 3013 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3014 | QualType CanonType; |
| 3015 | if (!Underlying.isNull()) |
| 3016 | CanonType = getCanonicalType(Underlying); |
| 3017 | else { |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3018 | // We can get here with an alias template when the specialization contains |
| 3019 | // a pack expansion that does not match up with a parameter pack. |
| 3020 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 3021 | "Caller must compute aliased type"); |
| 3022 | IsTypeAlias = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3023 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 3024 | NumArgs); |
| 3025 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3026 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3027 | // Allocate the (non-canonical) template specialization type, but don't |
| 3028 | // try to unique it: these types typically have location information that |
| 3029 | // we don't unique and don't want to lose. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3030 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 3031 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3032 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3033 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3034 | TemplateSpecializationType *Spec |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3035 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 3036 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3037 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3038 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3039 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3042 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3043 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 3044 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3045 | unsigned NumArgs) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3046 | assert(!Template.getAsDependentTemplateName() && |
| 3047 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3048 | |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3049 | // Look through qualified template names. |
| 3050 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3051 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3052 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3053 | // Build the canonical template specialization type. |
| 3054 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3055 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3056 | CanonArgs.reserve(NumArgs); |
| 3057 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3058 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 3059 | |
| 3060 | // Determine whether this canonical template specialization type already |
| 3061 | // exists. |
| 3062 | llvm::FoldingSetNodeID ID; |
| 3063 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 3064 | CanonArgs.data(), NumArgs, *this); |
| 3065 | |
| 3066 | void *InsertPos = 0; |
| 3067 | TemplateSpecializationType *Spec |
| 3068 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3069 | |
| 3070 | if (!Spec) { |
| 3071 | // Allocate a new canonical template specialization type. |
| 3072 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 3073 | sizeof(TemplateArgument) * NumArgs), |
| 3074 | TypeAlignment); |
| 3075 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 3076 | CanonArgs.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3077 | QualType(), QualType()); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3078 | Types.push_back(Spec); |
| 3079 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 3080 | } |
| 3081 | |
| 3082 | assert(Spec->isDependentType() && |
| 3083 | "Non-dependent template-id type must have a canonical type"); |
| 3084 | return QualType(Spec, 0); |
| 3085 | } |
| 3086 | |
| 3087 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3088 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 3089 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3090 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3091 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3092 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3093 | |
| 3094 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3095 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3096 | if (T) |
| 3097 | return QualType(T, 0); |
| 3098 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3099 | QualType Canon = NamedType; |
| 3100 | if (!Canon.isCanonical()) { |
| 3101 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3102 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3103 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3104 | (void)CheckT; |
| 3105 | } |
| 3106 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3107 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3108 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3109 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3110 | return QualType(T, 0); |
| 3111 | } |
| 3112 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3113 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3114 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3115 | llvm::FoldingSetNodeID ID; |
| 3116 | ParenType::Profile(ID, InnerType); |
| 3117 | |
| 3118 | void *InsertPos = 0; |
| 3119 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3120 | if (T) |
| 3121 | return QualType(T, 0); |
| 3122 | |
| 3123 | QualType Canon = InnerType; |
| 3124 | if (!Canon.isCanonical()) { |
| 3125 | Canon = getCanonicalType(InnerType); |
| 3126 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3127 | assert(!CheckT && "Paren canonical type broken"); |
| 3128 | (void)CheckT; |
| 3129 | } |
| 3130 | |
| 3131 | T = new (*this) ParenType(InnerType, Canon); |
| 3132 | Types.push_back(T); |
| 3133 | ParenTypes.InsertNode(T, InsertPos); |
| 3134 | return QualType(T, 0); |
| 3135 | } |
| 3136 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3137 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 3138 | NestedNameSpecifier *NNS, |
| 3139 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3140 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3141 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 3142 | |
| 3143 | if (Canon.isNull()) { |
| 3144 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3145 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3146 | if (Keyword == ETK_None) |
| 3147 | CanonKeyword = ETK_Typename; |
| 3148 | |
| 3149 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 3150 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3154 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3155 | |
| 3156 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3157 | DependentNameType *T |
| 3158 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3159 | if (T) |
| 3160 | return QualType(T, 0); |
| 3161 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3162 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3163 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3164 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3165 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3169 | ASTContext::getDependentTemplateSpecializationType( |
| 3170 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3171 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3172 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3173 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3174 | // TODO: avoid this copy |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3175 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3176 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 3177 | ArgCopy.push_back(Args[I].getArgument()); |
| 3178 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 3179 | ArgCopy.size(), |
| 3180 | ArgCopy.data()); |
| 3181 | } |
| 3182 | |
| 3183 | QualType |
| 3184 | ASTContext::getDependentTemplateSpecializationType( |
| 3185 | ElaboratedTypeKeyword Keyword, |
| 3186 | NestedNameSpecifier *NNS, |
| 3187 | const IdentifierInfo *Name, |
| 3188 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3189 | const TemplateArgument *Args) const { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3190 | assert((!NNS || NNS->isDependent()) && |
| 3191 | "nested-name-specifier must be dependent"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3192 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3193 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3194 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 3195 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3196 | |
| 3197 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3198 | DependentTemplateSpecializationType *T |
| 3199 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3200 | if (T) |
| 3201 | return QualType(T, 0); |
| 3202 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3203 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3204 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3205 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3206 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 3207 | |
| 3208 | bool AnyNonCanonArgs = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3209 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3210 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 3211 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 3212 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 3213 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3216 | QualType Canon; |
| 3217 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 3218 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 3219 | Name, NumArgs, |
| 3220 | CanonArgs.data()); |
| 3221 | |
| 3222 | // Find the insert position again. |
| 3223 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3224 | } |
| 3225 | |
| 3226 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 3227 | sizeof(TemplateArgument) * NumArgs), |
| 3228 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 3229 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3230 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3231 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3232 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3233 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3234 | } |
| 3235 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3236 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3237 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3238 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3239 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3240 | |
| 3241 | assert(Pattern->containsUnexpandedParameterPack() && |
| 3242 | "Pack expansions must expand one or more parameter packs"); |
| 3243 | void *InsertPos = 0; |
| 3244 | PackExpansionType *T |
| 3245 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3246 | if (T) |
| 3247 | return QualType(T, 0); |
| 3248 | |
| 3249 | QualType Canon; |
| 3250 | if (!Pattern.isCanonical()) { |
Richard Smith | d8672ef | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3251 | Canon = getCanonicalType(Pattern); |
| 3252 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 3253 | // contains an alias template specialization which ignores one of its |
| 3254 | // parameters. |
| 3255 | if (Canon->containsUnexpandedParameterPack()) { |
| 3256 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3257 | |
Richard Smith | d8672ef | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3258 | // Find the insert position again, in case we inserted an element into |
| 3259 | // PackExpansionTypes and invalidated our insert position. |
| 3260 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3261 | } |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3264 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3265 | Types.push_back(T); |
| 3266 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 3267 | return QualType(T, 0); |
| 3268 | } |
| 3269 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3270 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 3271 | /// alphabetically. |
| 3272 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 3273 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3274 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3277 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3278 | unsigned NumProtocols) { |
| 3279 | if (NumProtocols == 0) return true; |
| 3280 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3281 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 3282 | return false; |
| 3283 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3284 | for (unsigned i = 1; i != NumProtocols; ++i) |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3285 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) || |
| 3286 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3287 | return false; |
| 3288 | return true; |
| 3289 | } |
| 3290 | |
| 3291 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3292 | unsigned &NumProtocols) { |
| 3293 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3294 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3295 | // Sort protocols, keyed by name. |
| 3296 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 3297 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3298 | // Canonicalize. |
| 3299 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 3300 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 3301 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3302 | // Remove duplicates. |
| 3303 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 3304 | NumProtocols = ProtocolsEnd-Protocols; |
| 3305 | } |
| 3306 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3307 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 3308 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3309 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3310 | // If the base type is an interface and there aren't any protocols |
| 3311 | // to add, then the interface type will do just fine. |
| 3312 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 3313 | return BaseType; |
| 3314 | |
| 3315 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3316 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3317 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3318 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3319 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3320 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3321 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3322 | // Build the canonical type, which has the canonical base type and |
| 3323 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3324 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3325 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 3326 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 3327 | if (!ProtocolsSorted) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3328 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3329 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3330 | unsigned UniqueCount = NumProtocols; |
| 3331 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3332 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3333 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3334 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3335 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3336 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3337 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3341 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3342 | } |
| 3343 | |
| 3344 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 3345 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 3346 | void *Mem = Allocate(Size, TypeAlignment); |
| 3347 | ObjCObjectTypeImpl *T = |
| 3348 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 3349 | |
| 3350 | Types.push_back(T); |
| 3351 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 3352 | return QualType(T, 0); |
| 3353 | } |
| 3354 | |
| 3355 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 3356 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3357 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3358 | llvm::FoldingSetNodeID ID; |
| 3359 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 3360 | |
| 3361 | void *InsertPos = 0; |
| 3362 | if (ObjCObjectPointerType *QT = |
| 3363 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3364 | return QualType(QT, 0); |
| 3365 | |
| 3366 | // Find the canonical object type. |
| 3367 | QualType Canonical; |
| 3368 | if (!ObjectT.isCanonical()) { |
| 3369 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 3370 | |
| 3371 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3372 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3373 | } |
| 3374 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 3375 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3376 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 3377 | ObjCObjectPointerType *QType = |
| 3378 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3379 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3380 | Types.push_back(QType); |
| 3381 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3382 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3383 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3385 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 3386 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3387 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 3388 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3389 | if (Decl->TypeForDecl) |
| 3390 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3391 | |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3392 | if (PrevDecl) { |
| 3393 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 3394 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3395 | return QualType(PrevDecl->TypeForDecl, 0); |
| 3396 | } |
| 3397 | |
Douglas Gregor | 8d2dbbf | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 3398 | // Prefer the definition, if there is one. |
| 3399 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 3400 | Decl = Def; |
| 3401 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3402 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 3403 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 3404 | Decl->TypeForDecl = T; |
| 3405 | Types.push_back(T); |
| 3406 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3409 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 3410 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3411 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3412 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3413 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3414 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3415 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3416 | if (tofExpr->isTypeDependent()) { |
| 3417 | llvm::FoldingSetNodeID ID; |
| 3418 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3419 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3420 | void *InsertPos = 0; |
| 3421 | DependentTypeOfExprType *Canon |
| 3422 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3423 | if (Canon) { |
| 3424 | // We already have a "canonical" version of an identical, dependent |
| 3425 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3426 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3427 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3428 | } else { |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3429 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3430 | Canon |
| 3431 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3432 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 3433 | toe = Canon; |
| 3434 | } |
| 3435 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3436 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3437 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3438 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3439 | Types.push_back(toe); |
| 3440 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3443 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 3444 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 3445 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3446 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3447 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3448 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3449 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3450 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3451 | Types.push_back(tot); |
| 3452 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 3455 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3456 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 3457 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 3458 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3459 | /// an issue. This doesn't effect the type checker, since it operates |
David Blaikie | 39e0203 | 2011-11-06 22:28:03 +0000 | [diff] [blame] | 3460 | /// on canonical types (which are always unique). |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3461 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3462 | DecltypeType *dt; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3463 | |
| 3464 | // C++0x [temp.type]p2: |
| 3465 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 3466 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 3467 | // type only if their expressions are equivalent (14.5.6.1). |
| 3468 | if (e->isInstantiationDependent()) { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3469 | llvm::FoldingSetNodeID ID; |
| 3470 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3472 | void *InsertPos = 0; |
| 3473 | DependentDecltypeType *Canon |
| 3474 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3475 | if (Canon) { |
| 3476 | // We already have a "canonical" version of an equivalent, dependent |
| 3477 | // decltype type. Use that as our canonical type. |
Richard Smith | 0d72910 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 3478 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3479 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3480 | } else { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3481 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3482 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3483 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 3484 | dt = Canon; |
| 3485 | } |
| 3486 | } else { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3487 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
| 3488 | getCanonicalType(UnderlyingType)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3489 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3490 | Types.push_back(dt); |
| 3491 | return QualType(dt, 0); |
| 3492 | } |
| 3493 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3494 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 3495 | /// savings are minimal and these are rare. |
| 3496 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 3497 | QualType UnderlyingType, |
| 3498 | UnaryTransformType::UTTKind Kind) |
| 3499 | const { |
| 3500 | UnaryTransformType *Ty = |
Douglas Gregor | 69d9775 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 3501 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 3502 | Kind, |
| 3503 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 12fc4b0 | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 3504 | QualType() : getCanonicalType(UnderlyingType)); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3505 | Types.push_back(Ty); |
| 3506 | return QualType(Ty, 0); |
| 3507 | } |
| 3508 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3509 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3510 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3511 | void *InsertPos = 0; |
| 3512 | if (!DeducedType.isNull()) { |
| 3513 | // Look in the folding set for an existing type. |
| 3514 | llvm::FoldingSetNodeID ID; |
| 3515 | AutoType::Profile(ID, DeducedType); |
| 3516 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3517 | return QualType(AT, 0); |
| 3518 | } |
| 3519 | |
| 3520 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 3521 | Types.push_back(AT); |
| 3522 | if (InsertPos) |
| 3523 | AutoTypes.InsertNode(AT, InsertPos); |
| 3524 | return QualType(AT, 0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3527 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3528 | /// the given value type. |
| 3529 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3530 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3531 | // structure. |
| 3532 | llvm::FoldingSetNodeID ID; |
| 3533 | AtomicType::Profile(ID, T); |
| 3534 | |
| 3535 | void *InsertPos = 0; |
| 3536 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3537 | return QualType(AT, 0); |
| 3538 | |
| 3539 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3540 | // either, so fill in the canonical type field. |
| 3541 | QualType Canonical; |
| 3542 | if (!T.isCanonical()) { |
| 3543 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3544 | |
| 3545 | // Get the new insert position for the node we care about. |
| 3546 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3547 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 3548 | } |
| 3549 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3550 | Types.push_back(New); |
| 3551 | AtomicTypes.InsertNode(New, InsertPos); |
| 3552 | return QualType(New, 0); |
| 3553 | } |
| 3554 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3555 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3556 | QualType ASTContext::getAutoDeductType() const { |
| 3557 | if (AutoDeductTy.isNull()) |
| 3558 | AutoDeductTy = getAutoType(QualType()); |
| 3559 | assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern"); |
| 3560 | return AutoDeductTy; |
| 3561 | } |
| 3562 | |
| 3563 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3564 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3565 | if (AutoRRefDeductTy.isNull()) |
| 3566 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3567 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3568 | return AutoRRefDeductTy; |
| 3569 | } |
| 3570 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3571 | /// getTagDeclType - Return the unique reference to the type for the |
| 3572 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3573 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3574 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3575 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3576 | // away const? mutable? |
| 3577 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3580 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3581 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3582 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3583 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3584 | return getFromTargetType(Target->getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3585 | } |
| 3586 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3587 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3588 | CanQualType ASTContext::getIntMaxType() const { |
| 3589 | return getFromTargetType(Target->getIntMaxType()); |
| 3590 | } |
| 3591 | |
| 3592 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3593 | CanQualType ASTContext::getUIntMaxType() const { |
| 3594 | return getFromTargetType(Target->getUIntMaxType()); |
| 3595 | } |
| 3596 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3597 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3598 | /// Used when in C++, as a GCC extension. |
| 3599 | QualType ASTContext::getSignedWCharType() const { |
| 3600 | // FIXME: derive from "Target" ? |
| 3601 | return WCharTy; |
| 3602 | } |
| 3603 | |
| 3604 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3605 | /// Used when in C++, as a GCC extension. |
| 3606 | QualType ASTContext::getUnsignedWCharType() const { |
| 3607 | // FIXME: derive from "Target" ? |
| 3608 | return UnsignedIntTy; |
| 3609 | } |
| 3610 | |
Enea Zaffanella | 9677eb8 | 2013-01-26 17:08:37 +0000 | [diff] [blame] | 3611 | QualType ASTContext::getIntPtrType() const { |
| 3612 | return getFromTargetType(Target->getIntPtrType()); |
| 3613 | } |
| 3614 | |
| 3615 | QualType ASTContext::getUIntPtrType() const { |
| 3616 | return getCorrespondingUnsignedType(getIntPtrType()); |
| 3617 | } |
| 3618 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3619 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3620 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3621 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3622 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
Eli Friedman | 6902e41 | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 3625 | /// \brief Return the unique type for "pid_t" defined in |
| 3626 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
| 3627 | QualType ASTContext::getProcessIDType() const { |
| 3628 | return getFromTargetType(Target->getProcessIDType()); |
| 3629 | } |
| 3630 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3631 | //===----------------------------------------------------------------------===// |
| 3632 | // Type Operators |
| 3633 | //===----------------------------------------------------------------------===// |
| 3634 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3635 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3636 | // Push qualifiers into arrays, and then discard any remaining |
| 3637 | // qualifiers. |
| 3638 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3639 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3640 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3641 | QualType Result; |
| 3642 | if (isa<ArrayType>(Ty)) { |
| 3643 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3644 | } else if (isa<FunctionType>(Ty)) { |
| 3645 | Result = getPointerType(QualType(Ty, 0)); |
| 3646 | } else { |
| 3647 | Result = QualType(Ty, 0); |
| 3648 | } |
| 3649 | |
| 3650 | return CanQualType::CreateUnsafe(Result); |
| 3651 | } |
| 3652 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3653 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3654 | Qualifiers &quals) { |
| 3655 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3656 | |
| 3657 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3658 | // the unqualified desugared type and then drops it on the floor. |
| 3659 | // We then have to strip that sugar back off with |
| 3660 | // getUnqualifiedDesugaredType(), which is silly. |
| 3661 | const ArrayType *AT = |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3662 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3663 | |
| 3664 | // 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] | 3665 | if (!AT) { |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3666 | quals = splitType.Quals; |
| 3667 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3670 | // Otherwise, recurse on the array's element type. |
| 3671 | QualType elementType = AT->getElementType(); |
| 3672 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3673 | |
| 3674 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3675 | // can just use the results in splitType. |
| 3676 | if (elementType == unqualElementType) { |
| 3677 | assert(quals.empty()); // from the recursive call |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3678 | quals = splitType.Quals; |
| 3679 | return QualType(splitType.Ty, 0); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3683 | // build the type back up. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3684 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3685 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3686 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3687 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3688 | CAT->getSizeModifier(), 0); |
| 3689 | } |
| 3690 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3691 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3692 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3695 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3696 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3697 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3698 | VAT->getSizeModifier(), |
| 3699 | VAT->getIndexTypeCVRQualifiers(), |
| 3700 | VAT->getBracketsRange()); |
| 3701 | } |
| 3702 | |
| 3703 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3704 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3705 | DSAT->getSizeModifier(), 0, |
| 3706 | SourceRange()); |
| 3707 | } |
| 3708 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3709 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3710 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3711 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3712 | /// or pointer-to-member types, or if they are not similar at this |
| 3713 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3714 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3715 | /// be called in a loop that successively "unwraps" pointer and |
| 3716 | /// pointer-to-member types to compare them at each level. |
| 3717 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3718 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3719 | *T2PtrType = T2->getAs<PointerType>(); |
| 3720 | if (T1PtrType && T2PtrType) { |
| 3721 | T1 = T1PtrType->getPointeeType(); |
| 3722 | T2 = T2PtrType->getPointeeType(); |
| 3723 | return true; |
| 3724 | } |
| 3725 | |
| 3726 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3727 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3728 | if (T1MPType && T2MPType && |
| 3729 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3730 | QualType(T2MPType->getClass(), 0))) { |
| 3731 | T1 = T1MPType->getPointeeType(); |
| 3732 | T2 = T2MPType->getPointeeType(); |
| 3733 | return true; |
| 3734 | } |
| 3735 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3736 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3737 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3738 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3739 | if (T1OPType && T2OPType) { |
| 3740 | T1 = T1OPType->getPointeeType(); |
| 3741 | T2 = T2OPType->getPointeeType(); |
| 3742 | return true; |
| 3743 | } |
| 3744 | } |
| 3745 | |
| 3746 | // FIXME: Block pointers, too? |
| 3747 | |
| 3748 | return false; |
| 3749 | } |
| 3750 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3751 | DeclarationNameInfo |
| 3752 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3753 | SourceLocation NameLoc) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3754 | switch (Name.getKind()) { |
| 3755 | case TemplateName::QualifiedTemplate: |
| 3756 | case TemplateName::Template: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3757 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3758 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3759 | NameLoc); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3760 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3761 | case TemplateName::OverloadedTemplate: { |
| 3762 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3763 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3764 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3765 | } |
| 3766 | |
| 3767 | case TemplateName::DependentTemplate: { |
| 3768 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3769 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3770 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3771 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3772 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3773 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3774 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3775 | // DNInfo work in progress: FIXME: source locations? |
| 3776 | DeclarationNameLoc DNLoc; |
| 3777 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3778 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3779 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3780 | } |
| 3781 | } |
| 3782 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3783 | case TemplateName::SubstTemplateTemplateParm: { |
| 3784 | SubstTemplateTemplateParmStorage *subst |
| 3785 | = Name.getAsSubstTemplateTemplateParm(); |
| 3786 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3787 | NameLoc); |
| 3788 | } |
| 3789 | |
| 3790 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3791 | SubstTemplateTemplateParmPackStorage *subst |
| 3792 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3793 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3794 | NameLoc); |
| 3795 | } |
| 3796 | } |
| 3797 | |
| 3798 | llvm_unreachable("bad template name kind!"); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3801 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3802 | switch (Name.getKind()) { |
| 3803 | case TemplateName::QualifiedTemplate: |
| 3804 | case TemplateName::Template: { |
| 3805 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3806 | if (TemplateTemplateParmDecl *TTP |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3807 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3808 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3809 | |
| 3810 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3811 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3812 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3813 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3814 | case TemplateName::OverloadedTemplate: |
| 3815 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3817 | case TemplateName::DependentTemplate: { |
| 3818 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3819 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3820 | return DTN->CanonicalTemplateName; |
| 3821 | } |
| 3822 | |
| 3823 | case TemplateName::SubstTemplateTemplateParm: { |
| 3824 | SubstTemplateTemplateParmStorage *subst |
| 3825 | = Name.getAsSubstTemplateTemplateParm(); |
| 3826 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3827 | } |
| 3828 | |
| 3829 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3830 | SubstTemplateTemplateParmPackStorage *subst |
| 3831 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3832 | TemplateTemplateParmDecl *canonParameter |
| 3833 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3834 | TemplateArgument canonArgPack |
| 3835 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3836 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3843 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3844 | X = getCanonicalTemplateName(X); |
| 3845 | Y = getCanonicalTemplateName(Y); |
| 3846 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3847 | } |
| 3848 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3849 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3850 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3851 | switch (Arg.getKind()) { |
| 3852 | case TemplateArgument::Null: |
| 3853 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3854 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3855 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3856 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3857 | |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3858 | case TemplateArgument::Declaration: { |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3859 | ValueDecl *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
| 3860 | return TemplateArgument(D, Arg.isDeclForReferenceParam()); |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3861 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3862 | |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3863 | case TemplateArgument::NullPtr: |
| 3864 | return TemplateArgument(getCanonicalType(Arg.getNullPtrType()), |
| 3865 | /*isNullPtr*/true); |
| 3866 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3867 | case TemplateArgument::Template: |
| 3868 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3869 | |
| 3870 | case TemplateArgument::TemplateExpansion: |
| 3871 | return TemplateArgument(getCanonicalTemplateName( |
| 3872 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3873 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3874 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3875 | case TemplateArgument::Integral: |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 3876 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3878 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3879 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3880 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3881 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3882 | if (Arg.pack_size() == 0) |
| 3883 | return Arg; |
| 3884 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3885 | TemplateArgument *CanonArgs |
| 3886 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3887 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3888 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3889 | AEnd = Arg.pack_end(); |
| 3890 | A != AEnd; (void)++A, ++Idx) |
| 3891 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3893 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3894 | } |
| 3895 | } |
| 3896 | |
| 3897 | // Silence GCC warning |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3898 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3901 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3902 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3903 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3904 | return 0; |
| 3905 | |
| 3906 | switch (NNS->getKind()) { |
| 3907 | case NestedNameSpecifier::Identifier: |
| 3908 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3909 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3910 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3911 | NNS->getAsIdentifier()); |
| 3912 | |
| 3913 | case NestedNameSpecifier::Namespace: |
| 3914 | // A namespace is canonical; build a nested-name-specifier with |
| 3915 | // this namespace and no prefix. |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3916 | return NestedNameSpecifier::Create(*this, 0, |
| 3917 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3918 | |
| 3919 | case NestedNameSpecifier::NamespaceAlias: |
| 3920 | // A namespace is canonical; build a nested-name-specifier with |
| 3921 | // this namespace and no prefix. |
| 3922 | return NestedNameSpecifier::Create(*this, 0, |
| 3923 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3924 | ->getOriginalNamespace()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3925 | |
| 3926 | case NestedNameSpecifier::TypeSpec: |
| 3927 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3928 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3929 | |
| 3930 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3931 | // break it apart into its prefix and identifier, then reconsititute those |
| 3932 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3933 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3934 | // types, e.g., |
| 3935 | // typedef typename T::type T1; |
| 3936 | // typedef typename T1::type T2; |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3937 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 3938 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3939 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3940 | |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3941 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 3942 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 3943 | // first place? |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3944 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3945 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | case NestedNameSpecifier::Global: |
| 3949 | // The global specifier is canonical and unique. |
| 3950 | return NNS; |
| 3951 | } |
| 3952 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3953 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3956 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3957 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3958 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3959 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3960 | // Handle the common positive case fast. |
| 3961 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3962 | return AT; |
| 3963 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3964 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3965 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3966 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3967 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3968 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3969 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3970 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3971 | // 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] | 3972 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3973 | // If we get here, we either have type qualifiers on the type, or we have |
| 3974 | // 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] | 3975 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3976 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3977 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3978 | Qualifiers qs = split.Quals; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3979 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3980 | // If we have a simple case, just return now. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3981 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3982 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3983 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3984 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3985 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3986 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3987 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3988 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3989 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3990 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3991 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3992 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3993 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3994 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3995 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3996 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3997 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3998 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3999 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 4000 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4001 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4002 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4003 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4004 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4005 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4006 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4007 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4008 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4009 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4010 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4011 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4012 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Abramo Bagnara | ad9689f | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4015 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4016 | // C99 6.7.5.3p7: |
| 4017 | // A declaration of a parameter as "array of type" shall be |
| 4018 | // adjusted to "qualified pointer to type", where the type |
| 4019 | // qualifiers (if any) are those specified within the [ and ] of |
| 4020 | // the array type derivation. |
| 4021 | if (T->isArrayType()) |
| 4022 | return getArrayDecayedType(T); |
| 4023 | |
| 4024 | // C99 6.7.5.3p8: |
| 4025 | // A declaration of a parameter as "function returning type" |
| 4026 | // shall be adjusted to "pointer to function returning type", as |
| 4027 | // in 6.3.2.1. |
| 4028 | if (T->isFunctionType()) |
| 4029 | return getPointerType(T); |
| 4030 | |
| 4031 | return T; |
| 4032 | } |
| 4033 | |
Abramo Bagnara | ad9689f | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4034 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4035 | T = getVariableArrayDecayedType(T); |
| 4036 | T = getAdjustedParameterType(T); |
| 4037 | return T.getUnqualifiedType(); |
| 4038 | } |
| 4039 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4040 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 4041 | /// specified array type to a pointer. This operation is non-trivial when |
| 4042 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 4043 | /// this returns a pointer to a properly qualified element of the array. |
| 4044 | /// |
| 4045 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4046 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4047 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 4048 | // typedefs in the element type of the array. This also handles propagation |
| 4049 | // of type qualifiers from the array type into the element type if present |
| 4050 | // (C99 6.7.3p8). |
| 4051 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 4052 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4053 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4054 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4055 | |
| 4056 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4057 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4060 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 4061 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 4062 | } |
| 4063 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4064 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 4065 | Qualifiers qs; |
| 4066 | while (true) { |
| 4067 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4068 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4069 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4070 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4071 | type = array->getElementType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4072 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4073 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4074 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4075 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4078 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4079 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4080 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 4081 | uint64_t ElementCount = 1; |
| 4082 | do { |
| 4083 | ElementCount *= CA->getSize().getZExtValue(); |
Richard Smith | d5e8394 | 2012-12-06 03:04:50 +0000 | [diff] [blame] | 4084 | CA = dyn_cast_or_null<ConstantArrayType>( |
| 4085 | CA->getElementType()->getAsArrayTypeUnsafe()); |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4086 | } while (CA); |
| 4087 | return ElementCount; |
| 4088 | } |
| 4089 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4090 | /// getFloatingRank - Return a relative rank for floating point types. |
| 4091 | /// 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] | 4092 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4093 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4094 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4095 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4096 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 4097 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4098 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4099 | case BuiltinType::Half: return HalfRank; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4100 | case BuiltinType::Float: return FloatRank; |
| 4101 | case BuiltinType::Double: return DoubleRank; |
| 4102 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 4103 | } |
| 4104 | } |
| 4105 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4106 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 4107 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 4108 | /// 'typeDomain' is a real floating point or complex type. |
| 4109 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4110 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 4111 | QualType Domain) const { |
| 4112 | FloatingRank EltRank = getFloatingRank(Size); |
| 4113 | if (Domain->isComplexType()) { |
| 4114 | switch (EltRank) { |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4115 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4116 | case FloatRank: return FloatComplexTy; |
| 4117 | case DoubleRank: return DoubleComplexTy; |
| 4118 | case LongDoubleRank: return LongDoubleComplexTy; |
| 4119 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4120 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4121 | |
| 4122 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 4123 | switch (EltRank) { |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 4124 | case HalfRank: return HalfTy; |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4125 | case FloatRank: return FloatTy; |
| 4126 | case DoubleRank: return DoubleTy; |
| 4127 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4128 | } |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4129 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4132 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 4133 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 4134 | /// '_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] | 4135 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4136 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4137 | FloatingRank LHSR = getFloatingRank(LHS); |
| 4138 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4139 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4140 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4141 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4142 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4143 | return 1; |
| 4144 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4147 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 4148 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 4149 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4150 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4151 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4152 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4153 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4154 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4155 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4156 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4157 | case BuiltinType::Char_S: |
| 4158 | case BuiltinType::Char_U: |
| 4159 | case BuiltinType::SChar: |
| 4160 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4161 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4162 | case BuiltinType::Short: |
| 4163 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4164 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4165 | case BuiltinType::Int: |
| 4166 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4167 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4168 | case BuiltinType::Long: |
| 4169 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4170 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4171 | case BuiltinType::LongLong: |
| 4172 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4173 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4174 | case BuiltinType::Int128: |
| 4175 | case BuiltinType::UInt128: |
| 4176 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4177 | } |
| 4178 | } |
| 4179 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4180 | /// \brief Whether this is a promotable bitfield reference according |
| 4181 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 4182 | /// |
| 4183 | /// \returns the type this bit-field will promote to, or NULL if no |
| 4184 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4185 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 4186 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4187 | return QualType(); |
| 4188 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4189 | FieldDecl *Field = E->getBitField(); |
| 4190 | if (!Field) |
| 4191 | return QualType(); |
| 4192 | |
| 4193 | QualType FT = Field->getType(); |
| 4194 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4195 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4196 | uint64_t IntSize = getTypeSize(IntTy); |
| 4197 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 4198 | // to the size of int, it gets promoted no matter what its type is. |
| 4199 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 4200 | if (BitWidth < IntSize) |
| 4201 | return IntTy; |
| 4202 | |
| 4203 | if (BitWidth == IntSize) |
| 4204 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 4205 | |
| 4206 | // Types bigger than int are not subject to promotions, and therefore act |
| 4207 | // like the base type. |
| 4208 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 4209 | // is ridiculous. |
| 4210 | return QualType(); |
| 4211 | } |
| 4212 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4213 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 4214 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 4215 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4216 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4217 | assert(!Promotable.isNull()); |
| 4218 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4219 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 4220 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 68a2dc4 | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 4221 | |
| 4222 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 4223 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 4224 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 4225 | // types that can represent all the values of its underlying type: |
| 4226 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 4227 | // unsigned long long int [...] |
| 4228 | // FIXME: Is there some better way to compute this? |
| 4229 | if (BT->getKind() == BuiltinType::WChar_S || |
| 4230 | BT->getKind() == BuiltinType::WChar_U || |
| 4231 | BT->getKind() == BuiltinType::Char16 || |
| 4232 | BT->getKind() == BuiltinType::Char32) { |
| 4233 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 4234 | uint64_t FromSize = getTypeSize(BT); |
| 4235 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 4236 | LongLongTy, UnsignedLongLongTy }; |
| 4237 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 4238 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 4239 | if (FromSize < ToSize || |
| 4240 | (FromSize == ToSize && |
| 4241 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 4242 | return PromoteTypes[Idx]; |
| 4243 | } |
| 4244 | llvm_unreachable("char type should fit into long long"); |
| 4245 | } |
| 4246 | } |
| 4247 | |
| 4248 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4249 | if (Promotable->isSignedIntegerType()) |
| 4250 | return IntTy; |
Eli Friedman | 5b64e77 | 2012-11-15 01:21:59 +0000 | [diff] [blame] | 4251 | uint64_t PromotableSize = getIntWidth(Promotable); |
| 4252 | uint64_t IntSize = getIntWidth(IntTy); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4253 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 4254 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 4255 | } |
| 4256 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4257 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 4258 | /// type and returns its ownership. |
| 4259 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 4260 | while (!T.isNull()) { |
| 4261 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 4262 | return T.getObjCLifetime(); |
| 4263 | if (T->isArrayType()) |
| 4264 | T = getBaseElementType(T); |
| 4265 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 4266 | T = PT->getPointeeType(); |
| 4267 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 28445f0 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 4268 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4269 | else |
| 4270 | break; |
| 4271 | } |
| 4272 | |
| 4273 | return Qualifiers::OCL_None; |
| 4274 | } |
| 4275 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4276 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4277 | /// 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] | 4278 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4279 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4280 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 4281 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4282 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4283 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4284 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 4285 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4286 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4287 | unsigned LHSRank = getIntegerRank(LHSC); |
| 4288 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4289 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4290 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 4291 | if (LHSRank == RHSRank) return 0; |
| 4292 | return LHSRank > RHSRank ? 1 : -1; |
| 4293 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4295 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 4296 | if (LHSUnsigned) { |
| 4297 | // If the unsigned [LHS] type is larger, return it. |
| 4298 | if (LHSRank >= RHSRank) |
| 4299 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4300 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4301 | // If the signed type can represent all values of the unsigned type, it |
| 4302 | // 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] | 4303 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4304 | return -1; |
| 4305 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4306 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4307 | // If the unsigned [RHS] type is larger, return it. |
| 4308 | if (RHSRank >= LHSRank) |
| 4309 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4310 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4311 | // If the signed type can represent all values of the unsigned type, it |
| 4312 | // 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] | 4313 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4314 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4315 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4316 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4317 | static RecordDecl * |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4318 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 4319 | DeclContext *DC, IdentifierInfo *Id) { |
| 4320 | SourceLocation Loc; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4321 | if (Ctx.getLangOpts().CPlusPlus) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4322 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4323 | else |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4324 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4325 | } |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4326 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4327 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4328 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4329 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4330 | CFConstantStringTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4331 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4332 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4333 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4334 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4335 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4336 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4337 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4338 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4339 | // int flags; |
| 4340 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4341 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4342 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4343 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4344 | FieldTypes[3] = LongTy; |
| 4345 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4346 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4347 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4348 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4349 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4350 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4351 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4352 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4353 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4354 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4355 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4356 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4359 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4360 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4361 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4362 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 4363 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4364 | |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 4365 | QualType ASTContext::getObjCSuperType() const { |
| 4366 | if (ObjCSuperType.isNull()) { |
| 4367 | RecordDecl *ObjCSuperTypeDecl = |
| 4368 | CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get("objc_super")); |
| 4369 | TUDecl->addDecl(ObjCSuperTypeDecl); |
| 4370 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
| 4371 | } |
| 4372 | return ObjCSuperType; |
| 4373 | } |
| 4374 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4375 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4376 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4377 | assert(Rec && "Invalid CFConstantStringType"); |
| 4378 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 4379 | } |
| 4380 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4381 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4382 | if (BlockDescriptorType) |
| 4383 | return getTagDeclType(BlockDescriptorType); |
| 4384 | |
| 4385 | RecordDecl *T; |
| 4386 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4387 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4388 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4389 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4390 | |
| 4391 | QualType FieldTypes[] = { |
| 4392 | UnsignedLongTy, |
| 4393 | UnsignedLongTy, |
| 4394 | }; |
| 4395 | |
| 4396 | const char *FieldNames[] = { |
| 4397 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4398 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4399 | }; |
| 4400 | |
| 4401 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4402 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4403 | SourceLocation(), |
| 4404 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4405 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4406 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4407 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4408 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4409 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4410 | T->addDecl(Field); |
| 4411 | } |
| 4412 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4413 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4414 | |
| 4415 | BlockDescriptorType = T; |
| 4416 | |
| 4417 | return getTagDeclType(BlockDescriptorType); |
| 4418 | } |
| 4419 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4420 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4421 | if (BlockDescriptorExtendedType) |
| 4422 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4423 | |
| 4424 | RecordDecl *T; |
| 4425 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4426 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4427 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4428 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4429 | |
| 4430 | QualType FieldTypes[] = { |
| 4431 | UnsignedLongTy, |
| 4432 | UnsignedLongTy, |
| 4433 | getPointerType(VoidPtrTy), |
| 4434 | getPointerType(VoidPtrTy) |
| 4435 | }; |
| 4436 | |
| 4437 | const char *FieldNames[] = { |
| 4438 | "reserved", |
| 4439 | "Size", |
| 4440 | "CopyFuncPtr", |
| 4441 | "DestroyFuncPtr" |
| 4442 | }; |
| 4443 | |
| 4444 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4445 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4446 | SourceLocation(), |
| 4447 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4448 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4449 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4450 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4451 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4452 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4453 | T->addDecl(Field); |
| 4454 | } |
| 4455 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4456 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4457 | |
| 4458 | BlockDescriptorExtendedType = T; |
| 4459 | |
| 4460 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4461 | } |
| 4462 | |
Fariborz Jahanian | b15c898 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4463 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
| 4464 | /// requires copy/dispose. Note that this must match the logic |
| 4465 | /// in buildByrefHelpers. |
| 4466 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
| 4467 | const VarDecl *D) { |
| 4468 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
| 4469 | const Expr *copyExpr = getBlockVarCopyInits(D); |
| 4470 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
| 4471 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4472 | return true; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4473 | } |
Fariborz Jahanian | b15c898 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4474 | |
| 4475 | if (!Ty->isObjCRetainableType()) return false; |
| 4476 | |
| 4477 | Qualifiers qs = Ty.getQualifiers(); |
| 4478 | |
| 4479 | // If we have lifetime, that dominates. |
| 4480 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
| 4481 | assert(getLangOpts().ObjCAutoRefCount); |
| 4482 | |
| 4483 | switch (lifetime) { |
| 4484 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
| 4485 | |
| 4486 | // These are just bits as far as the runtime is concerned. |
| 4487 | case Qualifiers::OCL_ExplicitNone: |
| 4488 | case Qualifiers::OCL_Autoreleasing: |
| 4489 | return false; |
| 4490 | |
| 4491 | // Tell the runtime that this is ARC __weak, called by the |
| 4492 | // byref routines. |
| 4493 | case Qualifiers::OCL_Weak: |
| 4494 | // ARC __strong __block variables need to be retained. |
| 4495 | case Qualifiers::OCL_Strong: |
| 4496 | return true; |
| 4497 | } |
| 4498 | llvm_unreachable("fell out of lifetime switch!"); |
| 4499 | } |
| 4500 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
| 4501 | Ty->isObjCObjectPointerType()); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4502 | } |
| 4503 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4504 | bool ASTContext::getByrefLifetime(QualType Ty, |
| 4505 | Qualifiers::ObjCLifetime &LifeTime, |
| 4506 | bool &HasByrefExtendedLayout) const { |
| 4507 | |
| 4508 | if (!getLangOpts().ObjC1 || |
| 4509 | getLangOpts().getGC() != LangOptions::NonGC) |
| 4510 | return false; |
| 4511 | |
| 4512 | HasByrefExtendedLayout = false; |
Fariborz Jahanian | 34db84f | 2012-12-11 19:58:01 +0000 | [diff] [blame] | 4513 | if (Ty->isRecordType()) { |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4514 | HasByrefExtendedLayout = true; |
| 4515 | LifeTime = Qualifiers::OCL_None; |
| 4516 | } |
| 4517 | else if (getLangOpts().ObjCAutoRefCount) |
| 4518 | LifeTime = Ty.getObjCLifetime(); |
| 4519 | // MRR. |
| 4520 | else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 4521 | LifeTime = Qualifiers::OCL_ExplicitNone; |
| 4522 | else |
| 4523 | LifeTime = Qualifiers::OCL_None; |
| 4524 | return true; |
| 4525 | } |
| 4526 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4527 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 4528 | if (!ObjCInstanceTypeDecl) |
| 4529 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 4530 | getTranslationUnitDecl(), |
| 4531 | SourceLocation(), |
| 4532 | SourceLocation(), |
| 4533 | &Idents.get("instancetype"), |
| 4534 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 4535 | return ObjCInstanceTypeDecl; |
| 4536 | } |
| 4537 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4538 | // This returns true if a type has been typedefed to BOOL: |
| 4539 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 4540 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4541 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 4542 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 4543 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4544 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4545 | return false; |
| 4546 | } |
| 4547 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4548 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4549 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4550 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4551 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4552 | return CharUnits::Zero(); |
| 4553 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4554 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4555 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4556 | // 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] | 4557 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4558 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4559 | // Treat arrays as pointers, since that's how they're passed in. |
| 4560 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4561 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4562 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | static inline |
| 4566 | std::string charUnitsToString(const CharUnits &CU) { |
| 4567 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4570 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4571 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4572 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4573 | std::string S; |
| 4574 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4575 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4576 | QualType BlockTy = |
| 4577 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4578 | // Encode result type. |
Fariborz Jahanian | 3d145f6 | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 4579 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 06cffc0 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 4580 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, |
| 4581 | BlockTy->getAs<FunctionType>()->getResultType(), |
| 4582 | S, true /*Extended*/); |
| 4583 | else |
| 4584 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), |
| 4585 | S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4586 | // Compute size of all parameters. |
| 4587 | // Start with computing size of a pointer in number of bytes. |
| 4588 | // FIXME: There might(should) be a better way of doing this computation! |
| 4589 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4590 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4591 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 4592 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4593 | E = Decl->param_end(); PI != E; ++PI) { |
| 4594 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4595 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 075a543 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 4596 | if (sz.isZero()) |
| 4597 | continue; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4598 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4599 | ParmOffset += sz; |
| 4600 | } |
| 4601 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4602 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4603 | // Block pointer and offset. |
| 4604 | S += "@?0"; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4605 | |
| 4606 | // Argument types. |
| 4607 | ParmOffset = PtrSize; |
| 4608 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 4609 | Decl->param_end(); PI != E; ++PI) { |
| 4610 | ParmVarDecl *PVDecl = *PI; |
| 4611 | QualType PType = PVDecl->getOriginalType(); |
| 4612 | if (const ArrayType *AT = |
| 4613 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4614 | // Use array's original type only if it has known number of |
| 4615 | // elements. |
| 4616 | if (!isa<ConstantArrayType>(AT)) |
| 4617 | PType = PVDecl->getType(); |
| 4618 | } else if (PType->isFunctionType()) |
| 4619 | PType = PVDecl->getType(); |
Fariborz Jahanian | 3d145f6 | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 4620 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 06cffc0 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 4621 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType, |
| 4622 | S, true /*Extended*/); |
| 4623 | else |
| 4624 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4625 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4626 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4627 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4628 | |
| 4629 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4632 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4633 | std::string& S) { |
| 4634 | // Encode result type. |
| 4635 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4636 | CharUnits ParmOffset; |
| 4637 | // Compute size of all parameters. |
| 4638 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4639 | E = Decl->param_end(); PI != E; ++PI) { |
| 4640 | QualType PType = (*PI)->getType(); |
| 4641 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4642 | if (sz.isZero()) |
Fariborz Jahanian | 7e68ba5 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4643 | continue; |
| 4644 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4645 | assert (sz.isPositive() && |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4646 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4647 | ParmOffset += sz; |
| 4648 | } |
| 4649 | S += charUnitsToString(ParmOffset); |
| 4650 | ParmOffset = CharUnits::Zero(); |
| 4651 | |
| 4652 | // Argument types. |
| 4653 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4654 | E = Decl->param_end(); PI != E; ++PI) { |
| 4655 | ParmVarDecl *PVDecl = *PI; |
| 4656 | QualType PType = PVDecl->getOriginalType(); |
| 4657 | if (const ArrayType *AT = |
| 4658 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4659 | // Use array's original type only if it has known number of |
| 4660 | // elements. |
| 4661 | if (!isa<ConstantArrayType>(AT)) |
| 4662 | PType = PVDecl->getType(); |
| 4663 | } else if (PType->isFunctionType()) |
| 4664 | PType = PVDecl->getType(); |
| 4665 | getObjCEncodingForType(PType, S); |
| 4666 | S += charUnitsToString(ParmOffset); |
| 4667 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4668 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4669 | |
| 4670 | return false; |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4673 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 4674 | /// method parameter or return type. If Extended, include class names and |
| 4675 | /// block object types. |
| 4676 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 4677 | QualType T, std::string& S, |
| 4678 | bool Extended) const { |
| 4679 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 4680 | getObjCEncodingForTypeQualifier(QT, S); |
| 4681 | // Encode parameter type. |
| 4682 | getObjCEncodingForTypeImpl(T, S, true, true, 0, |
| 4683 | true /*OutermostType*/, |
| 4684 | false /*EncodingProperty*/, |
| 4685 | false /*StructField*/, |
| 4686 | Extended /*EncodeBlockParameters*/, |
| 4687 | Extended /*EncodeClassNames*/); |
| 4688 | } |
| 4689 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4690 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4691 | /// declaration. |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4692 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4693 | std::string& S, |
| 4694 | bool Extended) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4695 | // FIXME: This is not very efficient. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4696 | // Encode return type. |
| 4697 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 4698 | Decl->getResultType(), S, Extended); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4699 | // Compute size of all parameters. |
| 4700 | // Start with computing size of a pointer in number of bytes. |
| 4701 | // FIXME: There might(should) be a better way of doing this computation! |
| 4702 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4703 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4704 | // The first two arguments (self and _cmd) are pointers; account for |
| 4705 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4706 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4707 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4708 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4709 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4710 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4711 | if (sz.isZero()) |
Fariborz Jahanian | 7e68ba5 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4712 | continue; |
| 4713 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4714 | assert (sz.isPositive() && |
| 4715 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4716 | ParmOffset += sz; |
| 4717 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4718 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4719 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4720 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4721 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4722 | // Argument types. |
| 4723 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4724 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4725 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4726 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4728 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4729 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4730 | // Use array's original type only if it has known number of |
| 4731 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4732 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4733 | PType = PVDecl->getType(); |
| 4734 | } else if (PType->isFunctionType()) |
| 4735 | PType = PVDecl->getType(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4736 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 4737 | PType, S, Extended); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4738 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4739 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4740 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4741 | |
| 4742 | return false; |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4745 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4746 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4747 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4748 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4749 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4750 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4751 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4752 | /// encoded as single characters, followed by an identifier. Property types |
| 4753 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4754 | /// these attributes are defined by the following enumeration: |
| 4755 | /// @code |
| 4756 | /// enum PropertyAttributes { |
| 4757 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4758 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4759 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4760 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4761 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4762 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4763 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 0d4cb85 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 4764 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4765 | /// kPropertyWeak = 'W' // 'weak' property |
| 4766 | /// kPropertyStrong = 'P' // property GC'able |
| 4767 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4768 | /// }; |
| 4769 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4770 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4771 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4772 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4773 | // Collect information from the property implementation decl(s). |
| 4774 | bool Dynamic = false; |
| 4775 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4776 | |
| 4777 | // FIXME: Duplicated code due to poor abstraction. |
| 4778 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4779 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4780 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4781 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4782 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4783 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4784 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4785 | if (PID->getPropertyDecl() == PD) { |
| 4786 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4787 | Dynamic = true; |
| 4788 | } else { |
| 4789 | SynthesizePID = PID; |
| 4790 | } |
| 4791 | } |
| 4792 | } |
| 4793 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4794 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4795 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4796 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4797 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4798 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4799 | if (PID->getPropertyDecl() == PD) { |
| 4800 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4801 | Dynamic = true; |
| 4802 | } else { |
| 4803 | SynthesizePID = PID; |
| 4804 | } |
| 4805 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4806 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4807 | } |
| 4808 | } |
| 4809 | |
| 4810 | // FIXME: This is not very efficient. |
| 4811 | S = "T"; |
| 4812 | |
| 4813 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4814 | // GCC has some special rules regarding encoding of properties which |
| 4815 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4816 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4817 | true /* outermost type */, |
| 4818 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4819 | |
| 4820 | if (PD->isReadOnly()) { |
| 4821 | S += ",R"; |
| 4822 | } else { |
| 4823 | switch (PD->getSetterKind()) { |
| 4824 | case ObjCPropertyDecl::Assign: break; |
| 4825 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4826 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 3a02b44 | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4827 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4828 | } |
| 4829 | } |
| 4830 | |
| 4831 | // It really isn't clear at all what this means, since properties |
| 4832 | // are "dynamic by default". |
| 4833 | if (Dynamic) |
| 4834 | S += ",D"; |
| 4835 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4836 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4837 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4838 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4839 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4840 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4841 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4842 | } |
| 4843 | |
| 4844 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4845 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4846 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | if (SynthesizePID) { |
| 4850 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4851 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4852 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | // FIXME: OBJCGC: weak & strong |
| 4856 | } |
| 4857 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4858 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4859 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4860 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4861 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4862 | /// |
| 4863 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4864 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4865 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4866 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4867 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4868 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4869 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4870 | PointeeTy = IntTy; |
| 4871 | } |
| 4872 | } |
| 4873 | } |
| 4874 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4875 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4876 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4877 | // We follow the behavior of gcc, expanding structures which are |
| 4878 | // directly pointed to, and expanding embedded structures. Note that |
| 4879 | // these rules are sufficient to prevent recursive encoding of the |
| 4880 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4881 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4882 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4883 | } |
| 4884 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4885 | static char getObjCEncodingForPrimitiveKind(const ASTContext *C, |
| 4886 | BuiltinType::Kind kind) { |
| 4887 | switch (kind) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4888 | case BuiltinType::Void: return 'v'; |
| 4889 | case BuiltinType::Bool: return 'B'; |
| 4890 | case BuiltinType::Char_U: |
| 4891 | case BuiltinType::UChar: return 'C'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4892 | case BuiltinType::Char16: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4893 | case BuiltinType::UShort: return 'S'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4894 | case BuiltinType::Char32: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4895 | case BuiltinType::UInt: return 'I'; |
| 4896 | case BuiltinType::ULong: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4897 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4898 | case BuiltinType::UInt128: return 'T'; |
| 4899 | case BuiltinType::ULongLong: return 'Q'; |
| 4900 | case BuiltinType::Char_S: |
| 4901 | case BuiltinType::SChar: return 'c'; |
| 4902 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4903 | case BuiltinType::WChar_S: |
| 4904 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4905 | case BuiltinType::Int: return 'i'; |
| 4906 | case BuiltinType::Long: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4907 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4908 | case BuiltinType::LongLong: return 'q'; |
| 4909 | case BuiltinType::Int128: return 't'; |
| 4910 | case BuiltinType::Float: return 'f'; |
| 4911 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4912 | case BuiltinType::LongDouble: return 'D'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4913 | case BuiltinType::NullPtr: return '*'; // like char* |
| 4914 | |
| 4915 | case BuiltinType::Half: |
| 4916 | // FIXME: potentially need @encodes for these! |
| 4917 | return ' '; |
| 4918 | |
| 4919 | case BuiltinType::ObjCId: |
| 4920 | case BuiltinType::ObjCClass: |
| 4921 | case BuiltinType::ObjCSel: |
| 4922 | llvm_unreachable("@encoding ObjC primitive type"); |
| 4923 | |
| 4924 | // OpenCL and placeholder types don't need @encodings. |
| 4925 | case BuiltinType::OCLImage1d: |
| 4926 | case BuiltinType::OCLImage1dArray: |
| 4927 | case BuiltinType::OCLImage1dBuffer: |
| 4928 | case BuiltinType::OCLImage2d: |
| 4929 | case BuiltinType::OCLImage2dArray: |
| 4930 | case BuiltinType::OCLImage3d: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4931 | case BuiltinType::OCLEvent: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4932 | case BuiltinType::OCLSampler: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4933 | case BuiltinType::Dependent: |
| 4934 | #define BUILTIN_TYPE(KIND, ID) |
| 4935 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
| 4936 | case BuiltinType::KIND: |
| 4937 | #include "clang/AST/BuiltinTypes.def" |
| 4938 | llvm_unreachable("invalid builtin type for @encode"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4939 | } |
David Blaikie | 719e53f | 2013-01-09 17:48:41 +0000 | [diff] [blame] | 4940 | llvm_unreachable("invalid BuiltinType::Kind value"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4943 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 4944 | EnumDecl *Enum = ET->getDecl(); |
| 4945 | |
| 4946 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 4947 | if (!Enum->isFixed()) |
| 4948 | return 'i'; |
| 4949 | |
| 4950 | // The encoding of a fixed enum type matches its fixed underlying type. |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4951 | const BuiltinType *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
| 4952 | return getObjCEncodingForPrimitiveKind(C, BT->getKind()); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4955 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4956 | QualType T, const FieldDecl *FD) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4957 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4958 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4959 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4960 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4961 | // then the offset (in bits) of the first element, then the type of the |
| 4962 | // bitfield, then the size in bits. For example, in this structure: |
| 4963 | // |
| 4964 | // struct |
| 4965 | // { |
| 4966 | // int integer; |
| 4967 | // int flags:2; |
| 4968 | // }; |
| 4969 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4970 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4971 | // information is not especially sensible, but we're stuck with it for |
| 4972 | // compatibility with GCC, although providing it breaks anything that |
| 4973 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4974 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4975 | const RecordDecl *RD = FD->getParent(); |
| 4976 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | 8290574 | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 4977 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4978 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 4979 | S += ObjCEncodingForEnumType(Ctx, ET); |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4980 | else { |
| 4981 | const BuiltinType *BT = T->castAs<BuiltinType>(); |
| 4982 | S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind()); |
| 4983 | } |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4984 | } |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4985 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4988 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4989 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4990 | bool ExpandPointedToStructures, |
| 4991 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4992 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4993 | bool OutermostType, |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4994 | bool EncodingProperty, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4995 | bool StructField, |
| 4996 | bool EncodeBlockParameters, |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 4997 | bool EncodeClassNames, |
| 4998 | bool EncodePointerToObjCTypedef) const { |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4999 | CanQualType CT = getCanonicalType(T); |
| 5000 | switch (CT->getTypeClass()) { |
| 5001 | case Type::Builtin: |
| 5002 | case Type::Enum: |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5003 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5004 | return EncodeBitField(this, S, T, FD); |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5005 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CT)) |
| 5006 | S += getObjCEncodingForPrimitiveKind(this, BT->getKind()); |
| 5007 | else |
| 5008 | S += ObjCEncodingForEnumType(this, cast<EnumType>(CT)); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5009 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5010 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5011 | case Type::Complex: { |
| 5012 | const ComplexType *CT = T->castAs<ComplexType>(); |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 5013 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5014 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 5015 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5016 | return; |
| 5017 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5018 | |
| 5019 | case Type::Atomic: { |
| 5020 | const AtomicType *AT = T->castAs<AtomicType>(); |
| 5021 | S += 'A'; |
| 5022 | getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, 0, |
| 5023 | false, false); |
| 5024 | return; |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 5025 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5026 | |
| 5027 | // encoding for pointer or reference types. |
| 5028 | case Type::Pointer: |
| 5029 | case Type::LValueReference: |
| 5030 | case Type::RValueReference: { |
| 5031 | QualType PointeeTy; |
| 5032 | if (isa<PointerType>(CT)) { |
| 5033 | const PointerType *PT = T->castAs<PointerType>(); |
| 5034 | if (PT->isObjCSelType()) { |
| 5035 | S += ':'; |
| 5036 | return; |
| 5037 | } |
| 5038 | PointeeTy = PT->getPointeeType(); |
| 5039 | } else { |
| 5040 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
| 5041 | } |
| 5042 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5043 | bool isReadOnly = false; |
| 5044 | // For historical/compatibility reasons, the read-only qualifier of the |
| 5045 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 5046 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5047 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 5048 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5049 | if (OutermostType && T.isConstQualified()) { |
| 5050 | isReadOnly = true; |
| 5051 | S += 'r'; |
| 5052 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 5053 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5054 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5055 | while (P->getAs<PointerType>()) |
| 5056 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5057 | if (P.isConstQualified()) { |
| 5058 | isReadOnly = true; |
| 5059 | S += 'r'; |
| 5060 | } |
| 5061 | } |
| 5062 | if (isReadOnly) { |
| 5063 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 5064 | // combinations need to be rearranged. |
| 5065 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5066 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 5067 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5068 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5069 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5070 | if (PointeeTy->isCharType()) { |
| 5071 | // char pointer types should be encoded as '*' unless it is a |
| 5072 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 5073 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5074 | S += '*'; |
| 5075 | return; |
| 5076 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5077 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 5078 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 5079 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 5080 | S += '#'; |
| 5081 | return; |
| 5082 | } |
| 5083 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 5084 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 5085 | S += '@'; |
| 5086 | return; |
| 5087 | } |
| 5088 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5089 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5090 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5091 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 5092 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5093 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5094 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5095 | return; |
| 5096 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5097 | |
| 5098 | case Type::ConstantArray: |
| 5099 | case Type::IncompleteArray: |
| 5100 | case Type::VariableArray: { |
| 5101 | const ArrayType *AT = cast<ArrayType>(CT); |
| 5102 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5103 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5104 | // Incomplete arrays are encoded as a pointer to the array element. |
| 5105 | S += '^'; |
| 5106 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5107 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5108 | false, ExpandStructures, FD); |
| 5109 | } else { |
| 5110 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5111 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5112 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 5113 | if (getTypeSize(CAT->getElementType()) == 0) |
| 5114 | S += '0'; |
| 5115 | else |
| 5116 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 5117 | } else { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5118 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5119 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 5120 | "Unknown array type!"); |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5121 | S += '0'; |
| 5122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5123 | |
| 5124 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5125 | false, ExpandStructures, FD); |
| 5126 | S += ']'; |
| 5127 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5128 | return; |
| 5129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5130 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5131 | case Type::FunctionNoProto: |
| 5132 | case Type::FunctionProto: |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 5133 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5134 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5135 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5136 | case Type::Record: { |
| 5137 | RecordDecl *RDecl = cast<RecordType>(CT)->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5138 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5139 | // Anonymous structures print as '?' |
| 5140 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 5141 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5142 | if (ClassTemplateSpecializationDecl *Spec |
| 5143 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 5144 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Benjamin Kramer | 5eada84 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 5145 | llvm::raw_string_ostream OS(S); |
| 5146 | TemplateSpecializationType::PrintTemplateArgumentList(OS, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5147 | TemplateArgs.data(), |
| 5148 | TemplateArgs.size(), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 5149 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5150 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5151 | } else { |
| 5152 | S += '?'; |
| 5153 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 5154 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5155 | S += '='; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5156 | if (!RDecl->isUnion()) { |
| 5157 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 5158 | } else { |
| 5159 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 5160 | FieldEnd = RDecl->field_end(); |
| 5161 | Field != FieldEnd; ++Field) { |
| 5162 | if (FD) { |
| 5163 | S += '"'; |
| 5164 | S += Field->getNameAsString(); |
| 5165 | S += '"'; |
| 5166 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5167 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5168 | // Special case bit-fields. |
| 5169 | if (Field->isBitField()) { |
| 5170 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5171 | *Field); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5172 | } else { |
| 5173 | QualType qt = Field->getType(); |
| 5174 | getLegacyIntegralTypeEncoding(qt); |
| 5175 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 5176 | FD, /*OutermostType*/false, |
| 5177 | /*EncodingProperty*/false, |
| 5178 | /*StructField*/true); |
| 5179 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5180 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5181 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 5182 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5183 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5184 | return; |
| 5185 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5186 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5187 | case Type::BlockPointer: { |
| 5188 | const BlockPointerType *BT = T->castAs<BlockPointerType>(); |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 5189 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5190 | if (EncodeBlockParameters) { |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5191 | const FunctionType *FT = BT->getPointeeType()->castAs<FunctionType>(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5192 | |
| 5193 | S += '<'; |
| 5194 | // Block return type |
| 5195 | getObjCEncodingForTypeImpl(FT->getResultType(), S, |
| 5196 | ExpandPointedToStructures, ExpandStructures, |
| 5197 | FD, |
| 5198 | false /* OutermostType */, |
| 5199 | EncodingProperty, |
| 5200 | false /* StructField */, |
| 5201 | EncodeBlockParameters, |
| 5202 | EncodeClassNames); |
| 5203 | // Block self |
| 5204 | S += "@?"; |
| 5205 | // Block parameters |
| 5206 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 5207 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(), |
| 5208 | E = FPT->arg_type_end(); I && (I != E); ++I) { |
| 5209 | getObjCEncodingForTypeImpl(*I, S, |
| 5210 | ExpandPointedToStructures, |
| 5211 | ExpandStructures, |
| 5212 | FD, |
| 5213 | false /* OutermostType */, |
| 5214 | EncodingProperty, |
| 5215 | false /* StructField */, |
| 5216 | EncodeBlockParameters, |
| 5217 | EncodeClassNames); |
| 5218 | } |
| 5219 | } |
| 5220 | S += '>'; |
| 5221 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5222 | return; |
| 5223 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5224 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5225 | case Type::ObjCObject: |
| 5226 | case Type::ObjCInterface: { |
| 5227 | // Ignore protocol qualifiers when mangling at this level. |
| 5228 | T = T->castAs<ObjCObjectType>()->getBaseType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5229 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5230 | // The assumption seems to be that this assert will succeed |
| 5231 | // because nested levels will have filtered out 'id' and 'Class'. |
| 5232 | const ObjCInterfaceType *OIT = T->castAs<ObjCInterfaceType>(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5233 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5234 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5235 | S += '{'; |
| 5236 | const IdentifierInfo *II = OI->getIdentifier(); |
| 5237 | S += II->getName(); |
| 5238 | S += '='; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5239 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5240 | DeepCollectObjCIvars(OI, true, Ivars); |
| 5241 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5242 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5243 | if (Field->isBitField()) |
| 5244 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5245 | else |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5246 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD, |
| 5247 | false, false, false, false, false, |
| 5248 | EncodePointerToObjCTypedef); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5249 | } |
| 5250 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5251 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5252 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5253 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5254 | case Type::ObjCObjectPointer: { |
| 5255 | const ObjCObjectPointerType *OPT = T->castAs<ObjCObjectPointerType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5256 | if (OPT->isObjCIdType()) { |
| 5257 | S += '@'; |
| 5258 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5259 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5260 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5261 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 5262 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 5263 | // Since this is a binary compatibility issue, need to consult with runtime |
| 5264 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5265 | S += '#'; |
| 5266 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5267 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5268 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5269 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5270 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5271 | ExpandPointedToStructures, |
| 5272 | ExpandStructures, FD); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5273 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5274 | // Note that we do extended encoding of protocol qualifer list |
| 5275 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5276 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5277 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 5278 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5279 | S += '<'; |
| 5280 | S += (*I)->getNameAsString(); |
| 5281 | S += '>'; |
| 5282 | } |
| 5283 | S += '"'; |
| 5284 | } |
| 5285 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5286 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5287 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5288 | QualType PointeeTy = OPT->getPointeeType(); |
| 5289 | if (!EncodingProperty && |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5290 | isa<TypedefType>(PointeeTy.getTypePtr()) && |
| 5291 | !EncodePointerToObjCTypedef) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5292 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5293 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5294 | // {...}; |
| 5295 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5296 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 5297 | false, ExpandPointedToStructures, |
Fariborz Jahanian | 17c1a2e | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5298 | NULL, |
| 5299 | false, false, false, false, false, |
| 5300 | /*EncodePointerToObjCTypedef*/true); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5301 | return; |
| 5302 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5303 | |
| 5304 | S += '@'; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5305 | if (OPT->getInterfaceDecl() && |
| 5306 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5307 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5308 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5309 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 5310 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5311 | S += '<'; |
| 5312 | S += (*I)->getNameAsString(); |
| 5313 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5314 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5315 | S += '"'; |
| 5316 | } |
| 5317 | return; |
| 5318 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5319 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 5320 | // gcc just blithely ignores member pointers. |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5321 | // FIXME: we shoul do better than that. 'M' is available. |
| 5322 | case Type::MemberPointer: |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 5323 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5324 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5325 | case Type::Vector: |
| 5326 | case Type::ExtVector: |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5327 | // This matches gcc's encoding, even though technically it is |
| 5328 | // insufficient. |
| 5329 | // FIXME. We should do a better job than gcc. |
| 5330 | return; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5331 | |
| 5332 | #define ABSTRACT_TYPE(KIND, BASE) |
| 5333 | #define TYPE(KIND, BASE) |
| 5334 | #define DEPENDENT_TYPE(KIND, BASE) \ |
| 5335 | case Type::KIND: |
| 5336 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
| 5337 | case Type::KIND: |
| 5338 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
| 5339 | case Type::KIND: |
| 5340 | #include "clang/AST/TypeNodes.def" |
| 5341 | llvm_unreachable("@encode for dependent type!"); |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5342 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5343 | llvm_unreachable("bad type kind!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5344 | } |
| 5345 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5346 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 5347 | std::string &S, |
| 5348 | const FieldDecl *FD, |
| 5349 | bool includeVBases) const { |
| 5350 | assert(RDecl && "Expected non-null RecordDecl"); |
| 5351 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 5352 | if (!RDecl->getDefinition()) |
| 5353 | return; |
| 5354 | |
| 5355 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 5356 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 5357 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 5358 | |
| 5359 | if (CXXRec) { |
| 5360 | for (CXXRecordDecl::base_class_iterator |
| 5361 | BI = CXXRec->bases_begin(), |
| 5362 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 5363 | if (!BI->isVirtual()) { |
| 5364 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5365 | if (base->isEmpty()) |
| 5366 | continue; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5367 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5368 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 5369 | std::make_pair(offs, base)); |
| 5370 | } |
| 5371 | } |
| 5372 | } |
| 5373 | |
| 5374 | unsigned i = 0; |
| 5375 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 5376 | FieldEnd = RDecl->field_end(); |
| 5377 | Field != FieldEnd; ++Field, ++i) { |
| 5378 | uint64_t offs = layout.getFieldOffset(i); |
| 5379 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5380 | std::make_pair(offs, *Field)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5381 | } |
| 5382 | |
| 5383 | if (CXXRec && includeVBases) { |
| 5384 | for (CXXRecordDecl::base_class_iterator |
| 5385 | BI = CXXRec->vbases_begin(), |
| 5386 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 5387 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5388 | if (base->isEmpty()) |
| 5389 | continue; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5390 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Argyrios Kyrtzidis | 19aa860 | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 5391 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 5392 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 5393 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5394 | } |
| 5395 | } |
| 5396 | |
| 5397 | CharUnits size; |
| 5398 | if (CXXRec) { |
| 5399 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 5400 | } else { |
| 5401 | size = layout.getSize(); |
| 5402 | } |
| 5403 | |
| 5404 | uint64_t CurOffs = 0; |
| 5405 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 5406 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 5407 | |
Douglas Gregor | 58db7a5 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 5408 | if (CXXRec && CXXRec->isDynamicClass() && |
| 5409 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5410 | if (FD) { |
| 5411 | S += "\"_vptr$"; |
| 5412 | std::string recname = CXXRec->getNameAsString(); |
| 5413 | if (recname.empty()) recname = "?"; |
| 5414 | S += recname; |
| 5415 | S += '"'; |
| 5416 | } |
| 5417 | S += "^^?"; |
| 5418 | CurOffs += getTypeSize(VoidPtrTy); |
| 5419 | } |
| 5420 | |
| 5421 | if (!RDecl->hasFlexibleArrayMember()) { |
| 5422 | // Mark the end of the structure. |
| 5423 | uint64_t offs = toBits(size); |
| 5424 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 5425 | std::make_pair(offs, (NamedDecl*)0)); |
| 5426 | } |
| 5427 | |
| 5428 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 5429 | assert(CurOffs <= CurLayObj->first); |
| 5430 | |
| 5431 | if (CurOffs < CurLayObj->first) { |
| 5432 | uint64_t padding = CurLayObj->first - CurOffs; |
| 5433 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 5434 | // packing/alignment of members is different that normal, in which case |
| 5435 | // the encoding will be out-of-sync with the real layout. |
| 5436 | // If the runtime switches to just consider the size of types without |
| 5437 | // taking into account alignment, we could make padding explicit in the |
| 5438 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 5439 | // longer then though. |
| 5440 | CurOffs += padding; |
| 5441 | } |
| 5442 | |
| 5443 | NamedDecl *dcl = CurLayObj->second; |
| 5444 | if (dcl == 0) |
| 5445 | break; // reached end of structure. |
| 5446 | |
| 5447 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 5448 | // We expand the bases without their virtual bases since those are going |
| 5449 | // in the initial structure. Note that this differs from gcc which |
| 5450 | // expands virtual bases each time one is encountered in the hierarchy, |
| 5451 | // making the encoding type bigger than it really is. |
| 5452 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5453 | assert(!base->isEmpty()); |
| 5454 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5455 | } else { |
| 5456 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 5457 | if (FD) { |
| 5458 | S += '"'; |
| 5459 | S += field->getNameAsString(); |
| 5460 | S += '"'; |
| 5461 | } |
| 5462 | |
| 5463 | if (field->isBitField()) { |
| 5464 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5465 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5466 | } else { |
| 5467 | QualType qt = field->getType(); |
| 5468 | getLegacyIntegralTypeEncoding(qt); |
| 5469 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 5470 | /*OutermostType*/false, |
| 5471 | /*EncodingProperty*/false, |
| 5472 | /*StructField*/true); |
| 5473 | CurOffs += getTypeSize(field->getType()); |
| 5474 | } |
| 5475 | } |
| 5476 | } |
| 5477 | } |
| 5478 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5479 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 5480 | std::string& S) const { |
| 5481 | if (QT & Decl::OBJC_TQ_In) |
| 5482 | S += 'n'; |
| 5483 | if (QT & Decl::OBJC_TQ_Inout) |
| 5484 | S += 'N'; |
| 5485 | if (QT & Decl::OBJC_TQ_Out) |
| 5486 | S += 'o'; |
| 5487 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 5488 | S += 'O'; |
| 5489 | if (QT & Decl::OBJC_TQ_Byref) |
| 5490 | S += 'R'; |
| 5491 | if (QT & Decl::OBJC_TQ_Oneway) |
| 5492 | S += 'V'; |
| 5493 | } |
| 5494 | |
Douglas Gregor | 4dfd02a | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 5495 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 5496 | if (!ObjCIdDecl) { |
| 5497 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 5498 | T = getObjCObjectPointerType(T); |
| 5499 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 5500 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5501 | getTranslationUnitDecl(), |
| 5502 | SourceLocation(), SourceLocation(), |
| 5503 | &Idents.get("id"), IdInfo); |
| 5504 | } |
| 5505 | |
| 5506 | return ObjCIdDecl; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 5507 | } |
| 5508 | |
Douglas Gregor | 7a27ea5 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 5509 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 5510 | if (!ObjCSelDecl) { |
| 5511 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 5512 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 5513 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5514 | getTranslationUnitDecl(), |
| 5515 | SourceLocation(), SourceLocation(), |
| 5516 | &Idents.get("SEL"), SelInfo); |
| 5517 | } |
| 5518 | return ObjCSelDecl; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 5519 | } |
| 5520 | |
Douglas Gregor | 79d6726 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 5521 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 5522 | if (!ObjCClassDecl) { |
| 5523 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 5524 | T = getObjCObjectPointerType(T); |
| 5525 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 5526 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5527 | getTranslationUnitDecl(), |
| 5528 | SourceLocation(), SourceLocation(), |
| 5529 | &Idents.get("Class"), ClassInfo); |
| 5530 | } |
| 5531 | |
| 5532 | return ObjCClassDecl; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 5533 | } |
| 5534 | |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 5535 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 5536 | if (!ObjCProtocolClassDecl) { |
| 5537 | ObjCProtocolClassDecl |
| 5538 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 5539 | SourceLocation(), |
| 5540 | &Idents.get("Protocol"), |
| 5541 | /*PrevDecl=*/0, |
| 5542 | SourceLocation(), true); |
| 5543 | } |
| 5544 | |
| 5545 | return ObjCProtocolClassDecl; |
| 5546 | } |
| 5547 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5548 | //===----------------------------------------------------------------------===// |
| 5549 | // __builtin_va_list Construction Functions |
| 5550 | //===----------------------------------------------------------------------===// |
| 5551 | |
| 5552 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5553 | // typedef char* __builtin_va_list; |
| 5554 | QualType CharPtrType = Context->getPointerType(Context->CharTy); |
| 5555 | TypeSourceInfo *TInfo |
| 5556 | = Context->getTrivialTypeSourceInfo(CharPtrType); |
| 5557 | |
| 5558 | TypedefDecl *VaListTypeDecl |
| 5559 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5560 | Context->getTranslationUnitDecl(), |
| 5561 | SourceLocation(), SourceLocation(), |
| 5562 | &Context->Idents.get("__builtin_va_list"), |
| 5563 | TInfo); |
| 5564 | return VaListTypeDecl; |
| 5565 | } |
| 5566 | |
| 5567 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5568 | // typedef void* __builtin_va_list; |
| 5569 | QualType VoidPtrType = Context->getPointerType(Context->VoidTy); |
| 5570 | TypeSourceInfo *TInfo |
| 5571 | = Context->getTrivialTypeSourceInfo(VoidPtrType); |
| 5572 | |
| 5573 | TypedefDecl *VaListTypeDecl |
| 5574 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5575 | Context->getTranslationUnitDecl(), |
| 5576 | SourceLocation(), SourceLocation(), |
| 5577 | &Context->Idents.get("__builtin_va_list"), |
| 5578 | TInfo); |
| 5579 | return VaListTypeDecl; |
| 5580 | } |
| 5581 | |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5582 | static TypedefDecl * |
| 5583 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5584 | RecordDecl *VaListTagDecl; |
| 5585 | if (Context->getLangOpts().CPlusPlus) { |
| 5586 | // namespace std { struct __va_list { |
| 5587 | NamespaceDecl *NS; |
| 5588 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 5589 | Context->getTranslationUnitDecl(), |
| 5590 | /*Inline*/false, SourceLocation(), |
| 5591 | SourceLocation(), &Context->Idents.get("std"), |
| 5592 | /*PrevDecl*/0); |
| 5593 | |
| 5594 | VaListTagDecl = CXXRecordDecl::Create(*Context, TTK_Struct, |
| 5595 | Context->getTranslationUnitDecl(), |
| 5596 | SourceLocation(), SourceLocation(), |
| 5597 | &Context->Idents.get("__va_list")); |
| 5598 | VaListTagDecl->setDeclContext(NS); |
| 5599 | } else { |
| 5600 | // struct __va_list |
| 5601 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5602 | Context->getTranslationUnitDecl(), |
| 5603 | &Context->Idents.get("__va_list")); |
| 5604 | } |
| 5605 | |
| 5606 | VaListTagDecl->startDefinition(); |
| 5607 | |
| 5608 | const size_t NumFields = 5; |
| 5609 | QualType FieldTypes[NumFields]; |
| 5610 | const char *FieldNames[NumFields]; |
| 5611 | |
| 5612 | // void *__stack; |
| 5613 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
| 5614 | FieldNames[0] = "__stack"; |
| 5615 | |
| 5616 | // void *__gr_top; |
| 5617 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
| 5618 | FieldNames[1] = "__gr_top"; |
| 5619 | |
| 5620 | // void *__vr_top; |
| 5621 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5622 | FieldNames[2] = "__vr_top"; |
| 5623 | |
| 5624 | // int __gr_offs; |
| 5625 | FieldTypes[3] = Context->IntTy; |
| 5626 | FieldNames[3] = "__gr_offs"; |
| 5627 | |
| 5628 | // int __vr_offs; |
| 5629 | FieldTypes[4] = Context->IntTy; |
| 5630 | FieldNames[4] = "__vr_offs"; |
| 5631 | |
| 5632 | // Create fields |
| 5633 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5634 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5635 | VaListTagDecl, |
| 5636 | SourceLocation(), |
| 5637 | SourceLocation(), |
| 5638 | &Context->Idents.get(FieldNames[i]), |
| 5639 | FieldTypes[i], /*TInfo=*/0, |
| 5640 | /*BitWidth=*/0, |
| 5641 | /*Mutable=*/false, |
| 5642 | ICIS_NoInit); |
| 5643 | Field->setAccess(AS_public); |
| 5644 | VaListTagDecl->addDecl(Field); |
| 5645 | } |
| 5646 | VaListTagDecl->completeDefinition(); |
| 5647 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 5648 | Context->VaListTagTy = VaListTagType; |
| 5649 | |
| 5650 | // } __builtin_va_list; |
| 5651 | TypedefDecl *VaListTypedefDecl |
| 5652 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5653 | Context->getTranslationUnitDecl(), |
| 5654 | SourceLocation(), SourceLocation(), |
| 5655 | &Context->Idents.get("__builtin_va_list"), |
| 5656 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5657 | |
| 5658 | return VaListTypedefDecl; |
| 5659 | } |
| 5660 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5661 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5662 | // typedef struct __va_list_tag { |
| 5663 | RecordDecl *VaListTagDecl; |
| 5664 | |
| 5665 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5666 | Context->getTranslationUnitDecl(), |
| 5667 | &Context->Idents.get("__va_list_tag")); |
| 5668 | VaListTagDecl->startDefinition(); |
| 5669 | |
| 5670 | const size_t NumFields = 5; |
| 5671 | QualType FieldTypes[NumFields]; |
| 5672 | const char *FieldNames[NumFields]; |
| 5673 | |
| 5674 | // unsigned char gpr; |
| 5675 | FieldTypes[0] = Context->UnsignedCharTy; |
| 5676 | FieldNames[0] = "gpr"; |
| 5677 | |
| 5678 | // unsigned char fpr; |
| 5679 | FieldTypes[1] = Context->UnsignedCharTy; |
| 5680 | FieldNames[1] = "fpr"; |
| 5681 | |
| 5682 | // unsigned short reserved; |
| 5683 | FieldTypes[2] = Context->UnsignedShortTy; |
| 5684 | FieldNames[2] = "reserved"; |
| 5685 | |
| 5686 | // void* overflow_arg_area; |
| 5687 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5688 | FieldNames[3] = "overflow_arg_area"; |
| 5689 | |
| 5690 | // void* reg_save_area; |
| 5691 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 5692 | FieldNames[4] = "reg_save_area"; |
| 5693 | |
| 5694 | // Create fields |
| 5695 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5696 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 5697 | SourceLocation(), |
| 5698 | SourceLocation(), |
| 5699 | &Context->Idents.get(FieldNames[i]), |
| 5700 | FieldTypes[i], /*TInfo=*/0, |
| 5701 | /*BitWidth=*/0, |
| 5702 | /*Mutable=*/false, |
| 5703 | ICIS_NoInit); |
| 5704 | Field->setAccess(AS_public); |
| 5705 | VaListTagDecl->addDecl(Field); |
| 5706 | } |
| 5707 | VaListTagDecl->completeDefinition(); |
| 5708 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5709 | Context->VaListTagTy = VaListTagType; |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5710 | |
| 5711 | // } __va_list_tag; |
| 5712 | TypedefDecl *VaListTagTypedefDecl |
| 5713 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5714 | Context->getTranslationUnitDecl(), |
| 5715 | SourceLocation(), SourceLocation(), |
| 5716 | &Context->Idents.get("__va_list_tag"), |
| 5717 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5718 | QualType VaListTagTypedefType = |
| 5719 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5720 | |
| 5721 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5722 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5723 | QualType VaListTagArrayType |
| 5724 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5725 | Size, ArrayType::Normal, 0); |
| 5726 | TypeSourceInfo *TInfo |
| 5727 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5728 | TypedefDecl *VaListTypedefDecl |
| 5729 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5730 | Context->getTranslationUnitDecl(), |
| 5731 | SourceLocation(), SourceLocation(), |
| 5732 | &Context->Idents.get("__builtin_va_list"), |
| 5733 | TInfo); |
| 5734 | |
| 5735 | return VaListTypedefDecl; |
| 5736 | } |
| 5737 | |
| 5738 | static TypedefDecl * |
| 5739 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5740 | // typedef struct __va_list_tag { |
| 5741 | RecordDecl *VaListTagDecl; |
| 5742 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5743 | Context->getTranslationUnitDecl(), |
| 5744 | &Context->Idents.get("__va_list_tag")); |
| 5745 | VaListTagDecl->startDefinition(); |
| 5746 | |
| 5747 | const size_t NumFields = 4; |
| 5748 | QualType FieldTypes[NumFields]; |
| 5749 | const char *FieldNames[NumFields]; |
| 5750 | |
| 5751 | // unsigned gp_offset; |
| 5752 | FieldTypes[0] = Context->UnsignedIntTy; |
| 5753 | FieldNames[0] = "gp_offset"; |
| 5754 | |
| 5755 | // unsigned fp_offset; |
| 5756 | FieldTypes[1] = Context->UnsignedIntTy; |
| 5757 | FieldNames[1] = "fp_offset"; |
| 5758 | |
| 5759 | // void* overflow_arg_area; |
| 5760 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5761 | FieldNames[2] = "overflow_arg_area"; |
| 5762 | |
| 5763 | // void* reg_save_area; |
| 5764 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5765 | FieldNames[3] = "reg_save_area"; |
| 5766 | |
| 5767 | // Create fields |
| 5768 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5769 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5770 | VaListTagDecl, |
| 5771 | SourceLocation(), |
| 5772 | SourceLocation(), |
| 5773 | &Context->Idents.get(FieldNames[i]), |
| 5774 | FieldTypes[i], /*TInfo=*/0, |
| 5775 | /*BitWidth=*/0, |
| 5776 | /*Mutable=*/false, |
| 5777 | ICIS_NoInit); |
| 5778 | Field->setAccess(AS_public); |
| 5779 | VaListTagDecl->addDecl(Field); |
| 5780 | } |
| 5781 | VaListTagDecl->completeDefinition(); |
| 5782 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5783 | Context->VaListTagTy = VaListTagType; |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5784 | |
| 5785 | // } __va_list_tag; |
| 5786 | TypedefDecl *VaListTagTypedefDecl |
| 5787 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5788 | Context->getTranslationUnitDecl(), |
| 5789 | SourceLocation(), SourceLocation(), |
| 5790 | &Context->Idents.get("__va_list_tag"), |
| 5791 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5792 | QualType VaListTagTypedefType = |
| 5793 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5794 | |
| 5795 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5796 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5797 | QualType VaListTagArrayType |
| 5798 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5799 | Size, ArrayType::Normal,0); |
| 5800 | TypeSourceInfo *TInfo |
| 5801 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5802 | TypedefDecl *VaListTypedefDecl |
| 5803 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5804 | Context->getTranslationUnitDecl(), |
| 5805 | SourceLocation(), SourceLocation(), |
| 5806 | &Context->Idents.get("__builtin_va_list"), |
| 5807 | TInfo); |
| 5808 | |
| 5809 | return VaListTypedefDecl; |
| 5810 | } |
| 5811 | |
| 5812 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5813 | // typedef int __builtin_va_list[4]; |
| 5814 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
| 5815 | QualType IntArrayType |
| 5816 | = Context->getConstantArrayType(Context->IntTy, |
| 5817 | Size, ArrayType::Normal, 0); |
| 5818 | TypedefDecl *VaListTypedefDecl |
| 5819 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5820 | Context->getTranslationUnitDecl(), |
| 5821 | SourceLocation(), SourceLocation(), |
| 5822 | &Context->Idents.get("__builtin_va_list"), |
| 5823 | Context->getTrivialTypeSourceInfo(IntArrayType)); |
| 5824 | |
| 5825 | return VaListTypedefDecl; |
| 5826 | } |
| 5827 | |
Logan Chien | eae5a820 | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 5828 | static TypedefDecl * |
| 5829 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5830 | RecordDecl *VaListDecl; |
| 5831 | if (Context->getLangOpts().CPlusPlus) { |
| 5832 | // namespace std { struct __va_list { |
| 5833 | NamespaceDecl *NS; |
| 5834 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 5835 | Context->getTranslationUnitDecl(), |
| 5836 | /*Inline*/false, SourceLocation(), |
| 5837 | SourceLocation(), &Context->Idents.get("std"), |
| 5838 | /*PrevDecl*/0); |
| 5839 | |
| 5840 | VaListDecl = CXXRecordDecl::Create(*Context, TTK_Struct, |
| 5841 | Context->getTranslationUnitDecl(), |
| 5842 | SourceLocation(), SourceLocation(), |
| 5843 | &Context->Idents.get("__va_list")); |
| 5844 | |
| 5845 | VaListDecl->setDeclContext(NS); |
| 5846 | |
| 5847 | } else { |
| 5848 | // struct __va_list { |
| 5849 | VaListDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5850 | Context->getTranslationUnitDecl(), |
| 5851 | &Context->Idents.get("__va_list")); |
| 5852 | } |
| 5853 | |
| 5854 | VaListDecl->startDefinition(); |
| 5855 | |
| 5856 | // void * __ap; |
| 5857 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5858 | VaListDecl, |
| 5859 | SourceLocation(), |
| 5860 | SourceLocation(), |
| 5861 | &Context->Idents.get("__ap"), |
| 5862 | Context->getPointerType(Context->VoidTy), |
| 5863 | /*TInfo=*/0, |
| 5864 | /*BitWidth=*/0, |
| 5865 | /*Mutable=*/false, |
| 5866 | ICIS_NoInit); |
| 5867 | Field->setAccess(AS_public); |
| 5868 | VaListDecl->addDecl(Field); |
| 5869 | |
| 5870 | // }; |
| 5871 | VaListDecl->completeDefinition(); |
| 5872 | |
| 5873 | // typedef struct __va_list __builtin_va_list; |
| 5874 | TypeSourceInfo *TInfo |
| 5875 | = Context->getTrivialTypeSourceInfo(Context->getRecordType(VaListDecl)); |
| 5876 | |
| 5877 | TypedefDecl *VaListTypeDecl |
| 5878 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5879 | Context->getTranslationUnitDecl(), |
| 5880 | SourceLocation(), SourceLocation(), |
| 5881 | &Context->Idents.get("__builtin_va_list"), |
| 5882 | TInfo); |
| 5883 | |
| 5884 | return VaListTypeDecl; |
| 5885 | } |
| 5886 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5887 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 5888 | TargetInfo::BuiltinVaListKind Kind) { |
| 5889 | switch (Kind) { |
| 5890 | case TargetInfo::CharPtrBuiltinVaList: |
| 5891 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 5892 | case TargetInfo::VoidPtrBuiltinVaList: |
| 5893 | return CreateVoidPtrBuiltinVaListDecl(Context); |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5894 | case TargetInfo::AArch64ABIBuiltinVaList: |
| 5895 | return CreateAArch64ABIBuiltinVaListDecl(Context); |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5896 | case TargetInfo::PowerABIBuiltinVaList: |
| 5897 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 5898 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 5899 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 5900 | case TargetInfo::PNaClABIBuiltinVaList: |
| 5901 | return CreatePNaClABIBuiltinVaListDecl(Context); |
Logan Chien | eae5a820 | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 5902 | case TargetInfo::AAPCSABIBuiltinVaList: |
| 5903 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5904 | } |
| 5905 | |
| 5906 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 5907 | } |
| 5908 | |
| 5909 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
| 5910 | if (!BuiltinVaListDecl) |
| 5911 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
| 5912 | |
| 5913 | return BuiltinVaListDecl; |
| 5914 | } |
| 5915 | |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5916 | QualType ASTContext::getVaListTagType() const { |
| 5917 | // Force the creation of VaListTagTy by building the __builtin_va_list |
| 5918 | // declaration. |
| 5919 | if (VaListTagTy.isNull()) |
| 5920 | (void) getBuiltinVaListDecl(); |
| 5921 | |
| 5922 | return VaListTagTy; |
| 5923 | } |
| 5924 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5925 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5926 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 5927 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5928 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5929 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 5930 | } |
| 5931 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5932 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 5933 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5934 | TemplateName |
| 5935 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 5936 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5937 | unsigned size = End - Begin; |
| 5938 | assert(size > 1 && "set is not overloaded!"); |
| 5939 | |
| 5940 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 5941 | size * sizeof(FunctionTemplateDecl*)); |
| 5942 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 5943 | |
| 5944 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 5945 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5946 | NamedDecl *D = *I; |
| 5947 | assert(isa<FunctionTemplateDecl>(D) || |
| 5948 | (isa<UsingShadowDecl>(D) && |
| 5949 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 5950 | *Storage++ = D; |
| 5951 | } |
| 5952 | |
| 5953 | return TemplateName(OT); |
| 5954 | } |
| 5955 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5956 | /// \brief Retrieve the template name that represents a qualified |
| 5957 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5958 | TemplateName |
| 5959 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 5960 | bool TemplateKeyword, |
| 5961 | TemplateDecl *Template) const { |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 5962 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 5963 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5964 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5965 | llvm::FoldingSetNodeID ID; |
| 5966 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 5967 | |
| 5968 | void *InsertPos = 0; |
| 5969 | QualifiedTemplateName *QTN = |
| 5970 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5971 | if (!QTN) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 5972 | QTN = new (*this, llvm::alignOf<QualifiedTemplateName>()) |
| 5973 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5974 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 5975 | } |
| 5976 | |
| 5977 | return TemplateName(QTN); |
| 5978 | } |
| 5979 | |
| 5980 | /// \brief Retrieve the template name that represents a dependent |
| 5981 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5982 | TemplateName |
| 5983 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 5984 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5985 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 5986 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5987 | |
| 5988 | llvm::FoldingSetNodeID ID; |
| 5989 | DependentTemplateName::Profile(ID, NNS, Name); |
| 5990 | |
| 5991 | void *InsertPos = 0; |
| 5992 | DependentTemplateName *QTN = |
| 5993 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5994 | |
| 5995 | if (QTN) |
| 5996 | return TemplateName(QTN); |
| 5997 | |
| 5998 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5999 | if (CanonNNS == NNS) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6000 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6001 | DependentTemplateName(NNS, Name); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6002 | } else { |
| 6003 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6004 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6005 | DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6006 | DependentTemplateName *CheckQTN = |
| 6007 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6008 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 6009 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6010 | } |
| 6011 | |
| 6012 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 6013 | return TemplateName(QTN); |
| 6014 | } |
| 6015 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6016 | /// \brief Retrieve the template name that represents a dependent |
| 6017 | /// template name such as \c MetaFun::template operator+. |
| 6018 | TemplateName |
| 6019 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6020 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6021 | assert((!NNS || NNS->isDependent()) && |
| 6022 | "Nested name specifier must be dependent"); |
| 6023 | |
| 6024 | llvm::FoldingSetNodeID ID; |
| 6025 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 6026 | |
| 6027 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6028 | DependentTemplateName *QTN |
| 6029 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6030 | |
| 6031 | if (QTN) |
| 6032 | return TemplateName(QTN); |
| 6033 | |
| 6034 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 6035 | if (CanonNNS == NNS) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6036 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6037 | DependentTemplateName(NNS, Operator); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6038 | } else { |
| 6039 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6040 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6041 | DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6042 | |
| 6043 | DependentTemplateName *CheckQTN |
| 6044 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6045 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 6046 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
| 6049 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 6050 | return TemplateName(QTN); |
| 6051 | } |
| 6052 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6053 | TemplateName |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6054 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 6055 | TemplateName replacement) const { |
| 6056 | llvm::FoldingSetNodeID ID; |
| 6057 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 6058 | |
| 6059 | void *insertPos = 0; |
| 6060 | SubstTemplateTemplateParmStorage *subst |
| 6061 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 6062 | |
| 6063 | if (!subst) { |
| 6064 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 6065 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 6066 | } |
| 6067 | |
| 6068 | return TemplateName(subst); |
| 6069 | } |
| 6070 | |
| 6071 | TemplateName |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6072 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 6073 | const TemplateArgument &ArgPack) const { |
| 6074 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 6075 | llvm::FoldingSetNodeID ID; |
| 6076 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 6077 | |
| 6078 | void *InsertPos = 0; |
| 6079 | SubstTemplateTemplateParmPackStorage *Subst |
| 6080 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 6081 | |
| 6082 | if (!Subst) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6083 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6084 | ArgPack.pack_size(), |
| 6085 | ArgPack.pack_begin()); |
| 6086 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 6087 | } |
| 6088 | |
| 6089 | return TemplateName(Subst); |
| 6090 | } |
| 6091 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6092 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 6093 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 6094 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6095 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6096 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6097 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6098 | case TargetInfo::SignedShort: return ShortTy; |
| 6099 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 6100 | case TargetInfo::SignedInt: return IntTy; |
| 6101 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 6102 | case TargetInfo::SignedLong: return LongTy; |
| 6103 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 6104 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 6105 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 6106 | } |
| 6107 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6108 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6109 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 6110 | |
| 6111 | //===----------------------------------------------------------------------===// |
| 6112 | // Type Predicates. |
| 6113 | //===----------------------------------------------------------------------===// |
| 6114 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6115 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 6116 | /// garbage collection attribute. |
| 6117 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6118 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6119 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6120 | return Qualifiers::GCNone; |
| 6121 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6122 | assert(getLangOpts().ObjC1); |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6123 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 6124 | |
| 6125 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 6126 | // (or pointers to them) be treated as though they were declared |
| 6127 | // as __strong. |
| 6128 | if (GCAttrs == Qualifiers::GCNone) { |
| 6129 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 6130 | return Qualifiers::Strong; |
| 6131 | else if (Ty->isPointerType()) |
| 6132 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 6133 | } else { |
| 6134 | // It's not valid to set GC attributes on anything that isn't a |
| 6135 | // pointer. |
| 6136 | #ifndef NDEBUG |
| 6137 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 6138 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 6139 | CT = AT->getElementType(); |
| 6140 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 6141 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6142 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 6143 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6144 | } |
| 6145 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6146 | //===----------------------------------------------------------------------===// |
| 6147 | // Type Compatibility Testing |
| 6148 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 6149 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6150 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6151 | /// compatible. |
| 6152 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 6153 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 6154 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6155 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6156 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6157 | } |
| 6158 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6159 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 6160 | QualType SecondVec) { |
| 6161 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 6162 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 6163 | |
| 6164 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 6165 | return true; |
| 6166 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6167 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 6168 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6169 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 6170 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6171 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6172 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6173 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 6174 | First->getVectorKind() != VectorType::AltiVecBool && |
| 6175 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 6176 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6177 | return true; |
| 6178 | |
| 6179 | return false; |
| 6180 | } |
| 6181 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6182 | //===----------------------------------------------------------------------===// |
| 6183 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 6184 | //===----------------------------------------------------------------------===// |
| 6185 | |
| 6186 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 6187 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6188 | bool |
| 6189 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 6190 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 6191 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6192 | return true; |
| 6193 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 6194 | E = rProto->protocol_end(); PI != E; ++PI) |
| 6195 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 6196 | return true; |
| 6197 | return false; |
| 6198 | } |
| 6199 | |
Dmitri Gribenko | 4c3b8a3 | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 6200 | /// QualifiedIdConformsQualifiedId - compare id<pr,...> with id<pr1,...> |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6201 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 6202 | /// otherwise. |
| 6203 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 6204 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 6205 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 6206 | return false; |
| 6207 | } |
| 6208 | |
Dmitri Gribenko | 4c3b8a3 | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 6209 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
| 6210 | /// Class<pr1, ...>. |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6211 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 6212 | QualType rhs) { |
| 6213 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 6214 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 6215 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 6216 | |
| 6217 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6218 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6219 | bool match = false; |
| 6220 | ObjCProtocolDecl *lhsProto = *I; |
| 6221 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 6222 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 6223 | ObjCProtocolDecl *rhsProto = *J; |
| 6224 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 6225 | match = true; |
| 6226 | break; |
| 6227 | } |
| 6228 | } |
| 6229 | if (!match) |
| 6230 | return false; |
| 6231 | } |
| 6232 | return true; |
| 6233 | } |
| 6234 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6235 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 6236 | /// ObjCQualifiedIDType. |
| 6237 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 6238 | bool compare) { |
| 6239 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6240 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6241 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 6242 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6243 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6244 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 6245 | return true; |
| 6246 | |
| 6247 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6248 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6249 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6250 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6251 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6252 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6253 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6254 | // make sure we check the class hierarchy. |
| 6255 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 6256 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6257 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6258 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6259 | // see if static class implements all of id's protocols, directly or |
| 6260 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 6261 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6262 | return false; |
| 6263 | } |
| 6264 | } |
| 6265 | // If there are no qualifiers and no interface, we have an 'id'. |
| 6266 | return true; |
| 6267 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6268 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6269 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6270 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6271 | ObjCProtocolDecl *lhsProto = *I; |
| 6272 | bool match = false; |
| 6273 | |
| 6274 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6275 | // see if static class implements all of id's protocols, directly or |
| 6276 | // through its super class and categories. |
| 6277 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 6278 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 6279 | ObjCProtocolDecl *rhsProto = *J; |
| 6280 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6281 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6282 | match = true; |
| 6283 | break; |
| 6284 | } |
| 6285 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6286 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6287 | // make sure we check the class hierarchy. |
| 6288 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 6289 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6290 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6291 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6292 | // see if static class implements all of id's protocols, directly or |
| 6293 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 6294 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6295 | match = true; |
| 6296 | break; |
| 6297 | } |
| 6298 | } |
| 6299 | } |
| 6300 | if (!match) |
| 6301 | return false; |
| 6302 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6303 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6304 | return true; |
| 6305 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6306 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6307 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 6308 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 6309 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6310 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6311 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6312 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6313 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 6314 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 6315 | ObjCProtocolDecl *lhsProto = *I; |
| 6316 | bool match = false; |
| 6317 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6318 | // 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] | 6319 | // see if static class implements all of id's protocols, directly or |
| 6320 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6321 | // First, lhs protocols in the qualifier list must be found, direct |
| 6322 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6323 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 6324 | E = rhsQID->qual_end(); J != E; ++J) { |
| 6325 | ObjCProtocolDecl *rhsProto = *J; |
| 6326 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6327 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6328 | match = true; |
| 6329 | break; |
| 6330 | } |
| 6331 | } |
| 6332 | if (!match) |
| 6333 | return false; |
| 6334 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6335 | |
| 6336 | // Static class's protocols, or its super class or category protocols |
| 6337 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 6338 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 6339 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 6340 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 6341 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 6342 | // no type qualifier and its class has no static protocol(s) |
| 6343 | // assume that it is mismatch. |
| 6344 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 6345 | return false; |
| 6346 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6347 | LHSInheritedProtocols.begin(), |
| 6348 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 6349 | bool match = false; |
| 6350 | ObjCProtocolDecl *lhsProto = (*I); |
| 6351 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 6352 | E = rhsQID->qual_end(); J != E; ++J) { |
| 6353 | ObjCProtocolDecl *rhsProto = *J; |
| 6354 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6355 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6356 | match = true; |
| 6357 | break; |
| 6358 | } |
| 6359 | } |
| 6360 | if (!match) |
| 6361 | return false; |
| 6362 | } |
| 6363 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6364 | return true; |
| 6365 | } |
| 6366 | return false; |
| 6367 | } |
| 6368 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6369 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6370 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 6371 | /// protocol qualifiers on the LHS or RHS. |
| 6372 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6373 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 6374 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6375 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6376 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6377 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 6378 | // 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] | 6379 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 6380 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6381 | return true; |
| 6382 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6383 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6384 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6385 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6386 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6387 | |
| 6388 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 6389 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 6390 | QualType(RHSOPT,0)); |
| 6391 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6392 | // If we have 2 user-defined types, fall into that path. |
| 6393 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6394 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6395 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6396 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6397 | } |
| 6398 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6399 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 6400 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6401 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 6402 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 6403 | /// not OK. For the return type, the opposite is not OK. |
| 6404 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 6405 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6406 | const ObjCObjectPointerType *RHSOPT, |
| 6407 | bool BlockReturnType) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6408 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6409 | return true; |
| 6410 | |
| 6411 | if (LHSOPT->isObjCBuiltinType()) { |
| 6412 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 6413 | } |
| 6414 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6415 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6416 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6417 | QualType(RHSOPT,0), |
| 6418 | false); |
| 6419 | |
| 6420 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 6421 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 6422 | if (LHS && RHS) { // We have 2 user-defined types. |
| 6423 | if (LHS != RHS) { |
| 6424 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6425 | return BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6426 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6427 | return !BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6428 | } |
| 6429 | else |
| 6430 | return true; |
| 6431 | } |
| 6432 | return false; |
| 6433 | } |
| 6434 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6435 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 6436 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 6437 | /// It is used to build composite qualifier list of the composite type of |
| 6438 | /// the conditional expression involving two objective-c pointer objects. |
| 6439 | static |
| 6440 | void getIntersectionOfProtocols(ASTContext &Context, |
| 6441 | const ObjCObjectPointerType *LHSOPT, |
| 6442 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6443 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6444 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6445 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6446 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6447 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 6448 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6449 | |
| 6450 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 6451 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 6452 | if (LHSNumProtocols > 0) |
| 6453 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 6454 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6455 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6456 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 6457 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6458 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 6459 | LHSInheritedProtocols.end()); |
| 6460 | } |
| 6461 | |
| 6462 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 6463 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 6464 | ObjCProtocolDecl **RHSProtocols = |
| 6465 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6466 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 6467 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 6468 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 6469 | } else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6470 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6471 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 6472 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6473 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6474 | RHSInheritedProtocols.begin(), |
| 6475 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 6476 | if (InheritedProtocolSet.count((*I))) |
| 6477 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6478 | } |
| 6479 | } |
| 6480 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6481 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 6482 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 6483 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 6484 | /// warning is issued. So, its invokation is extremely rare. |
| 6485 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6486 | const ObjCObjectPointerType *Lptr, |
| 6487 | const ObjCObjectPointerType *Rptr) { |
| 6488 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 6489 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 6490 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 6491 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 6492 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6493 | return QualType(); |
| 6494 | |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 6495 | do { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6496 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6497 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6498 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6499 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 6500 | |
| 6501 | QualType Result = QualType(LHS, 0); |
| 6502 | if (!Protocols.empty()) |
| 6503 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 6504 | Result = getObjCObjectPointerType(Result); |
| 6505 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6506 | } |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 6507 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6508 | |
| 6509 | return QualType(); |
| 6510 | } |
| 6511 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6512 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 6513 | const ObjCObjectType *RHS) { |
| 6514 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 6515 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 6516 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6517 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 6518 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6519 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6520 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6521 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6522 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 6523 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 6524 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6525 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6526 | |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6527 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 6528 | // more detailed analysis is required. |
| 6529 | if (RHS->getNumProtocols() == 0) { |
| 6530 | // OK, if LHS is a superclass of RHS *and* |
| 6531 | // this superclass is assignment compatible with LHS. |
| 6532 | // false otherwise. |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 6533 | bool IsSuperClass = |
| 6534 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 6535 | if (IsSuperClass) { |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6536 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 6537 | // ; i.e., SuperClass may implement at least one of the protocols |
| 6538 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 6539 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 6540 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 6541 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6542 | // If super class has no protocols, it is not a match. |
| 6543 | if (SuperClassInheritedProtocols.empty()) |
| 6544 | return false; |
| 6545 | |
| 6546 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 6547 | LHSPE = LHS->qual_end(); |
| 6548 | LHSPI != LHSPE; LHSPI++) { |
| 6549 | bool SuperImplementsProtocol = false; |
| 6550 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 6551 | |
| 6552 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6553 | SuperClassInheritedProtocols.begin(), |
| 6554 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 6555 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 6556 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 6557 | SuperImplementsProtocol = true; |
| 6558 | break; |
| 6559 | } |
| 6560 | } |
| 6561 | if (!SuperImplementsProtocol) |
| 6562 | return false; |
| 6563 | } |
| 6564 | return true; |
| 6565 | } |
| 6566 | return false; |
| 6567 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6568 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6569 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 6570 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6571 | LHSPI != LHSPE; LHSPI++) { |
| 6572 | bool RHSImplementsProtocol = false; |
| 6573 | |
| 6574 | // If the RHS doesn't implement the protocol on the left, the types |
| 6575 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6576 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 6577 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 6578 | RHSPI != RHSPE; RHSPI++) { |
| 6579 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6580 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 6581 | break; |
| 6582 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6583 | } |
| 6584 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 6585 | if (!RHSImplementsProtocol) |
| 6586 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6587 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6588 | // The RHS implements all protocols listed on the LHS. |
| 6589 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6590 | } |
| 6591 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6592 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 6593 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6594 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 6595 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6596 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6597 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6598 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6599 | |
| 6600 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 6601 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6602 | } |
| 6603 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6604 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 6605 | return canAssignObjCInterfaces( |
| 6606 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 6607 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 6608 | } |
| 6609 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6610 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6611 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6612 | /// 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] | 6613 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6614 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 6615 | bool CompareUnqualified) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6616 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6617 | return hasSameType(LHS, RHS); |
| 6618 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6619 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6620 | } |
| 6621 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6622 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | 8237839 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 6623 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6624 | } |
| 6625 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6626 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 6627 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 6628 | } |
| 6629 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6630 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 6631 | /// of T is compatible with SubType, return the merged type, else return |
| 6632 | /// QualType() |
| 6633 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 6634 | bool OfBlockPointer, |
| 6635 | bool Unqualified) { |
| 6636 | if (const RecordType *UT = T->getAsUnionType()) { |
| 6637 | RecordDecl *UD = UT->getDecl(); |
| 6638 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 6639 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6640 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 6641 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6642 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 6643 | if (!MT.isNull()) |
| 6644 | return MT; |
| 6645 | } |
| 6646 | } |
| 6647 | } |
| 6648 | |
| 6649 | return QualType(); |
| 6650 | } |
| 6651 | |
| 6652 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 6653 | /// argument types |
| 6654 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 6655 | bool OfBlockPointer, |
| 6656 | bool Unqualified) { |
| 6657 | // GNU extension: two types are compatible if they appear as a function |
| 6658 | // argument, one of the types is a transparent union type and the other |
| 6659 | // type is compatible with a union member |
| 6660 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 6661 | Unqualified); |
| 6662 | if (!lmerge.isNull()) |
| 6663 | return lmerge; |
| 6664 | |
| 6665 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 6666 | Unqualified); |
| 6667 | if (!rmerge.isNull()) |
| 6668 | return rmerge; |
| 6669 | |
| 6670 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 6671 | } |
| 6672 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6673 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6674 | bool OfBlockPointer, |
| 6675 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6676 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 6677 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6678 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 6679 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6680 | bool allLTypes = true; |
| 6681 | bool allRTypes = true; |
| 6682 | |
| 6683 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6684 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6685 | if (OfBlockPointer) { |
| 6686 | QualType RHS = rbase->getResultType(); |
| 6687 | QualType LHS = lbase->getResultType(); |
| 6688 | bool UnqualifiedResult = Unqualified; |
| 6689 | if (!UnqualifiedResult) |
| 6690 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6691 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6692 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6693 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6694 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 6695 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6696 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6697 | |
| 6698 | if (Unqualified) |
| 6699 | retType = retType.getUnqualifiedType(); |
| 6700 | |
| 6701 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 6702 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 6703 | if (Unqualified) { |
| 6704 | LRetType = LRetType.getUnqualifiedType(); |
| 6705 | RRetType = RRetType.getUnqualifiedType(); |
| 6706 | } |
| 6707 | |
| 6708 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6709 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6710 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6711 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6712 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 6713 | // FIXME: double check this |
| 6714 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 6715 | // rbase->getRegParmAttr() != 0 && |
| 6716 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 6717 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 6718 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6719 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6720 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6721 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6722 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6723 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6724 | // Regparm is part of the calling convention. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 6725 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 6726 | return QualType(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6727 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 6728 | return QualType(); |
| 6729 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6730 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 6731 | return QualType(); |
| 6732 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6733 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 6734 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6735 | |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 6736 | if (lbaseInfo.getNoReturn() != NoReturn) |
| 6737 | allLTypes = false; |
| 6738 | if (rbaseInfo.getNoReturn() != NoReturn) |
| 6739 | allRTypes = false; |
| 6740 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6741 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6742 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6743 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6744 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 6745 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6746 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 6747 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 6748 | |
| 6749 | // Compatible functions must have the same number of arguments |
| 6750 | if (lproto_nargs != rproto_nargs) |
| 6751 | return QualType(); |
| 6752 | |
| 6753 | // Variadic and non-variadic functions aren't compatible |
| 6754 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 6755 | return QualType(); |
| 6756 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 6757 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 6758 | return QualType(); |
| 6759 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6760 | if (LangOpts.ObjCAutoRefCount && |
| 6761 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 6762 | return QualType(); |
| 6763 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6764 | // Check argument compatibility |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6765 | SmallVector<QualType, 10> types; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6766 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 6767 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 6768 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6769 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 6770 | OfBlockPointer, |
| 6771 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6772 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6773 | |
| 6774 | if (Unqualified) |
| 6775 | argtype = argtype.getUnqualifiedType(); |
| 6776 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6777 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6778 | if (Unqualified) { |
| 6779 | largtype = largtype.getUnqualifiedType(); |
| 6780 | rargtype = rargtype.getUnqualifiedType(); |
| 6781 | } |
| 6782 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6783 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 6784 | allLTypes = false; |
| 6785 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 6786 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6787 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6788 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6789 | if (allLTypes) return lhs; |
| 6790 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6791 | |
| 6792 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 6793 | EPI.ExtInfo = einfo; |
| 6794 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6795 | } |
| 6796 | |
| 6797 | if (lproto) allRTypes = false; |
| 6798 | if (rproto) allLTypes = false; |
| 6799 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6800 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6801 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6802 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6803 | if (proto->isVariadic()) return QualType(); |
| 6804 | // Check that the types are compatible with the types that |
| 6805 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 6806 | // The only types actually affected are promotable integer |
| 6807 | // types and floats, which would be passed as a different |
| 6808 | // type depending on whether the prototype is visible. |
| 6809 | unsigned proto_nargs = proto->getNumArgs(); |
| 6810 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 6811 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 6812 | |
Eli Friedman | c586d5d | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 6813 | // 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] | 6814 | // to pass enum values. |
Eli Friedman | c586d5d | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 6815 | if (const EnumType *Enum = argTy->getAs<EnumType>()) { |
| 6816 | argTy = Enum->getDecl()->getIntegerType(); |
| 6817 | if (argTy.isNull()) |
| 6818 | return QualType(); |
| 6819 | } |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 6820 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6821 | if (argTy->isPromotableIntegerType() || |
| 6822 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 6823 | return QualType(); |
| 6824 | } |
| 6825 | |
| 6826 | if (allLTypes) return lhs; |
| 6827 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6828 | |
| 6829 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 6830 | EPI.ExtInfo = einfo; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6831 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6832 | proto->getNumArgs(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6833 | } |
| 6834 | |
| 6835 | if (allLTypes) return lhs; |
| 6836 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6837 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6838 | } |
| 6839 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6840 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6841 | bool OfBlockPointer, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6842 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 6843 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 6844 | // type is adjusted to "T" prior to any further analysis, the expression |
| 6845 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6846 | // expression is an lvalue unless the reference is an rvalue reference and |
| 6847 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6848 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 6849 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6850 | |
| 6851 | if (Unqualified) { |
| 6852 | LHS = LHS.getUnqualifiedType(); |
| 6853 | RHS = RHS.getUnqualifiedType(); |
| 6854 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6855 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6856 | QualType LHSCan = getCanonicalType(LHS), |
| 6857 | RHSCan = getCanonicalType(RHS); |
| 6858 | |
| 6859 | // If two types are identical, they are compatible. |
| 6860 | if (LHSCan == RHSCan) |
| 6861 | return LHS; |
| 6862 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6863 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6864 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6865 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6866 | if (LQuals != RQuals) { |
| 6867 | // If any of these qualifiers are different, we have a type |
| 6868 | // mismatch. |
| 6869 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6870 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 6871 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6872 | return QualType(); |
| 6873 | |
| 6874 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6875 | // okay if the other type has no GC qualifier but is an Objective |
| 6876 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6877 | // this by pretending that the unqualified type was actually |
| 6878 | // qualified __strong. |
| 6879 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6880 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6881 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6882 | |
| 6883 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6884 | return QualType(); |
| 6885 | |
| 6886 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 6887 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 6888 | } |
| 6889 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 6890 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 6891 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6892 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6893 | } |
| 6894 | |
| 6895 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6896 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 6897 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 6898 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6899 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6900 | // We want to consider the two function types to be the same for these |
| 6901 | // comparisons, just force one to the other. |
| 6902 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 6903 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 6904 | |
| 6905 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 6906 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 6907 | LHSClass = Type::ConstantArray; |
| 6908 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 6909 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6910 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6911 | // ObjCInterfaces are just specialized ObjCObjects. |
| 6912 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 6913 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 6914 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 6915 | // Canonicalize ExtVector -> Vector. |
| 6916 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 6917 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6918 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 6919 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6920 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6921 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6922 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 6923 | // Compatibility is based on the underlying type, not the promotion |
| 6924 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6925 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Fariborz Jahanian | b918d6b | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 6926 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 6927 | if (!TINT.isNull() && hasSameType(TINT, RHSCan.getUnqualifiedType())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6928 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 6929 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6930 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Fariborz Jahanian | b918d6b | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 6931 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 6932 | if (!TINT.isNull() && hasSameType(TINT, LHSCan.getUnqualifiedType())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6933 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 6934 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 6935 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 4196363 | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 6936 | if (OfBlockPointer && !BlockReturnType) { |
| 6937 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 6938 | return LHS; |
| 6939 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 6940 | return RHS; |
| 6941 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 6942 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6943 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6944 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6945 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 6946 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6947 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6948 | #define TYPE(Class, Base) |
| 6949 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 6950 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6951 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 6952 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 6953 | #include "clang/AST/TypeNodes.def" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6954 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6955 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6956 | case Type::LValueReference: |
| 6957 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6958 | case Type::MemberPointer: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6959 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6960 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6961 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6962 | case Type::IncompleteArray: |
| 6963 | case Type::VariableArray: |
| 6964 | case Type::FunctionProto: |
| 6965 | case Type::ExtVector: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6966 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6967 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6968 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6969 | { |
| 6970 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6971 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 6972 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6973 | if (Unqualified) { |
| 6974 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 6975 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 6976 | } |
| 6977 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 6978 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6979 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 6980 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6981 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 6982 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6983 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6984 | return getPointerType(ResultType); |
| 6985 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6986 | case Type::BlockPointer: |
| 6987 | { |
| 6988 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6989 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 6990 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6991 | if (Unqualified) { |
| 6992 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 6993 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 6994 | } |
| 6995 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 6996 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6997 | if (ResultType.isNull()) return QualType(); |
| 6998 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 6999 | return LHS; |
| 7000 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 7001 | return RHS; |
| 7002 | return getBlockPointerType(ResultType); |
| 7003 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7004 | case Type::Atomic: |
| 7005 | { |
| 7006 | // Merge two pointer types, while trying to preserve typedef info |
| 7007 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 7008 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 7009 | if (Unqualified) { |
| 7010 | LHSValue = LHSValue.getUnqualifiedType(); |
| 7011 | RHSValue = RHSValue.getUnqualifiedType(); |
| 7012 | } |
| 7013 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 7014 | Unqualified); |
| 7015 | if (ResultType.isNull()) return QualType(); |
| 7016 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 7017 | return LHS; |
| 7018 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 7019 | return RHS; |
| 7020 | return getAtomicType(ResultType); |
| 7021 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7022 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7023 | { |
| 7024 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 7025 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 7026 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 7027 | return QualType(); |
| 7028 | |
| 7029 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 7030 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7031 | if (Unqualified) { |
| 7032 | LHSElem = LHSElem.getUnqualifiedType(); |
| 7033 | RHSElem = RHSElem.getUnqualifiedType(); |
| 7034 | } |
| 7035 | |
| 7036 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7037 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7038 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 7039 | return LHS; |
| 7040 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 7041 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 7042 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 7043 | ArrayType::ArraySizeModifier(), 0); |
| 7044 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 7045 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7046 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 7047 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7048 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 7049 | return LHS; |
| 7050 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 7051 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7052 | if (LVAT) { |
| 7053 | // FIXME: This isn't correct! But tricky to implement because |
| 7054 | // the array's size has to be the size of LHS, but the type |
| 7055 | // has to be different. |
| 7056 | return LHS; |
| 7057 | } |
| 7058 | if (RVAT) { |
| 7059 | // FIXME: This isn't correct! But tricky to implement because |
| 7060 | // the array's size has to be the size of RHS, but the type |
| 7061 | // has to be different. |
| 7062 | return RHS; |
| 7063 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 7064 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 7065 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 7066 | return getIncompleteArrayType(ResultType, |
| 7067 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7068 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7069 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7070 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7071 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7072 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7073 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7074 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 7075 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7076 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 7077 | case Type::Complex: |
| 7078 | // Distinct complex types are incompatible. |
| 7079 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 7080 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 7081 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 7082 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 7083 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7084 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7085 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7086 | case Type::ObjCObject: { |
| 7087 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 7088 | // FIXME: This should be type compatibility, e.g. whether |
| 7089 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7090 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 7091 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 7092 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 7093 | return LHS; |
| 7094 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7095 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 7096 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7097 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7098 | if (OfBlockPointer) { |
| 7099 | if (canAssignObjCInterfacesInBlockPointer( |
| 7100 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7101 | RHS->getAs<ObjCObjectPointerType>(), |
| 7102 | BlockReturnType)) |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7103 | return LHS; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7104 | return QualType(); |
| 7105 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7106 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 7107 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7108 | return LHS; |
| 7109 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 7110 | return QualType(); |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7111 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7112 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7113 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7114 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7115 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 7116 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7117 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 7118 | const FunctionProtoType *FromFunctionType, |
| 7119 | const FunctionProtoType *ToFunctionType) { |
| 7120 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 7121 | ToFunctionType->hasAnyConsumedArgs()) |
| 7122 | return false; |
| 7123 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 7124 | FromFunctionType->getExtProtoInfo(); |
| 7125 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 7126 | ToFunctionType->getExtProtoInfo(); |
| 7127 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 7128 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 7129 | ArgIdx != NumArgs; ++ArgIdx) { |
| 7130 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 7131 | ToEPI.ConsumedArguments[ArgIdx]) |
| 7132 | return false; |
| 7133 | } |
| 7134 | return true; |
| 7135 | } |
| 7136 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7137 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 7138 | /// 'RHS' attributes and returns the merged version; including for function |
| 7139 | /// return types. |
| 7140 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 7141 | QualType LHSCan = getCanonicalType(LHS), |
| 7142 | RHSCan = getCanonicalType(RHS); |
| 7143 | // If two types are identical, they are compatible. |
| 7144 | if (LHSCan == RHSCan) |
| 7145 | return LHS; |
| 7146 | if (RHSCan->isFunctionType()) { |
| 7147 | if (!LHSCan->isFunctionType()) |
| 7148 | return QualType(); |
| 7149 | QualType OldReturnType = |
| 7150 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 7151 | QualType NewReturnType = |
| 7152 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 7153 | QualType ResReturnType = |
| 7154 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 7155 | if (ResReturnType.isNull()) |
| 7156 | return QualType(); |
| 7157 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 7158 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 7159 | // In either case, use OldReturnType to build the new function type. |
| 7160 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 7161 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7162 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7163 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7164 | QualType ResultType |
| 7165 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7166 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7167 | return ResultType; |
| 7168 | } |
| 7169 | } |
| 7170 | return QualType(); |
| 7171 | } |
| 7172 | |
| 7173 | // If the qualifiers are different, the types can still be merged. |
| 7174 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 7175 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 7176 | if (LQuals != RQuals) { |
| 7177 | // If any of these qualifiers are different, we have a type mismatch. |
| 7178 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 7179 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 7180 | return QualType(); |
| 7181 | |
| 7182 | // Exactly one GC qualifier difference is allowed: __strong is |
| 7183 | // okay if the other type has no GC qualifier but is an Objective |
| 7184 | // C object pointer (i.e. implicitly strong by default). We fix |
| 7185 | // this by pretending that the unqualified type was actually |
| 7186 | // qualified __strong. |
| 7187 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 7188 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 7189 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 7190 | |
| 7191 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 7192 | return QualType(); |
| 7193 | |
| 7194 | if (GC_L == Qualifiers::Strong) |
| 7195 | return LHS; |
| 7196 | if (GC_R == Qualifiers::Strong) |
| 7197 | return RHS; |
| 7198 | return QualType(); |
| 7199 | } |
| 7200 | |
| 7201 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 7202 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7203 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7204 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 7205 | if (ResQT == LHSBaseQT) |
| 7206 | return LHS; |
| 7207 | if (ResQT == RHSBaseQT) |
| 7208 | return RHS; |
| 7209 | } |
| 7210 | return QualType(); |
| 7211 | } |
| 7212 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 7213 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7214 | // Integer Predicates |
| 7215 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 7216 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7217 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7218 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 7219 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7220 | if (T->isBooleanType()) |
| 7221 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 7222 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7223 | return (unsigned)getTypeSize(T); |
| 7224 | } |
| 7225 | |
Abramo Bagnara | 762f159 | 2012-09-09 10:21:24 +0000 | [diff] [blame] | 7226 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7227 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7228 | |
| 7229 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 7230 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 7231 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7232 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7233 | |
| 7234 | // For enums, we return the unsigned version of the base type. |
| 7235 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7236 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7237 | |
| 7238 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 7239 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7240 | switch (BTy->getKind()) { |
| 7241 | case BuiltinType::Char_S: |
| 7242 | case BuiltinType::SChar: |
| 7243 | return UnsignedCharTy; |
| 7244 | case BuiltinType::Short: |
| 7245 | return UnsignedShortTy; |
| 7246 | case BuiltinType::Int: |
| 7247 | return UnsignedIntTy; |
| 7248 | case BuiltinType::Long: |
| 7249 | return UnsignedLongTy; |
| 7250 | case BuiltinType::LongLong: |
| 7251 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 7252 | case BuiltinType::Int128: |
| 7253 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7254 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7255 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7256 | } |
| 7257 | } |
| 7258 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 7259 | ASTMutationListener::~ASTMutationListener() { } |
| 7260 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7261 | |
| 7262 | //===----------------------------------------------------------------------===// |
| 7263 | // Builtin Type Computation |
| 7264 | //===----------------------------------------------------------------------===// |
| 7265 | |
| 7266 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7267 | /// pointer over the consumed characters. This returns the resultant type. If |
| 7268 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 7269 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 7270 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7271 | /// |
| 7272 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 7273 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7274 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7275 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7276 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7277 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7278 | // Modifiers. |
| 7279 | int HowLong = 0; |
| 7280 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7281 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7282 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7283 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7284 | bool Done = false; |
| 7285 | while (!Done) { |
| 7286 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7287 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7288 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7289 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7290 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7291 | case 'S': |
| 7292 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 7293 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 7294 | Signed = true; |
| 7295 | break; |
| 7296 | case 'U': |
| 7297 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 7298 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 7299 | Unsigned = true; |
| 7300 | break; |
| 7301 | case 'L': |
| 7302 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 7303 | ++HowLong; |
| 7304 | break; |
| 7305 | } |
| 7306 | } |
| 7307 | |
| 7308 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7309 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7310 | // Read the base type. |
| 7311 | switch (*Str++) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7312 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7313 | case 'v': |
| 7314 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7315 | "Bad modifiers used with 'v'!"); |
| 7316 | Type = Context.VoidTy; |
| 7317 | break; |
| 7318 | case 'f': |
| 7319 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7320 | "Bad modifiers used with 'f'!"); |
| 7321 | Type = Context.FloatTy; |
| 7322 | break; |
| 7323 | case 'd': |
| 7324 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 7325 | "Bad modifiers used with 'd'!"); |
| 7326 | if (HowLong) |
| 7327 | Type = Context.LongDoubleTy; |
| 7328 | else |
| 7329 | Type = Context.DoubleTy; |
| 7330 | break; |
| 7331 | case 's': |
| 7332 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 7333 | if (Unsigned) |
| 7334 | Type = Context.UnsignedShortTy; |
| 7335 | else |
| 7336 | Type = Context.ShortTy; |
| 7337 | break; |
| 7338 | case 'i': |
| 7339 | if (HowLong == 3) |
| 7340 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 7341 | else if (HowLong == 2) |
| 7342 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 7343 | else if (HowLong == 1) |
| 7344 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 7345 | else |
| 7346 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 7347 | break; |
| 7348 | case 'c': |
| 7349 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 7350 | if (Signed) |
| 7351 | Type = Context.SignedCharTy; |
| 7352 | else if (Unsigned) |
| 7353 | Type = Context.UnsignedCharTy; |
| 7354 | else |
| 7355 | Type = Context.CharTy; |
| 7356 | break; |
| 7357 | case 'b': // boolean |
| 7358 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 7359 | Type = Context.BoolTy; |
| 7360 | break; |
| 7361 | case 'z': // size_t. |
| 7362 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 7363 | Type = Context.getSizeType(); |
| 7364 | break; |
| 7365 | case 'F': |
| 7366 | Type = Context.getCFConstantStringType(); |
| 7367 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 7368 | case 'G': |
| 7369 | Type = Context.getObjCIdType(); |
| 7370 | break; |
| 7371 | case 'H': |
| 7372 | Type = Context.getObjCSelType(); |
| 7373 | break; |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 7374 | case 'M': |
| 7375 | Type = Context.getObjCSuperType(); |
| 7376 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7377 | case 'a': |
| 7378 | Type = Context.getBuiltinVaListType(); |
| 7379 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 7380 | break; |
| 7381 | case 'A': |
| 7382 | // This is a "reference" to a va_list; however, what exactly |
| 7383 | // this means depends on how va_list is defined. There are two |
| 7384 | // different kinds of va_list: ones passed by value, and ones |
| 7385 | // passed by reference. An example of a by-value va_list is |
| 7386 | // x86, where va_list is a char*. An example of by-ref va_list |
| 7387 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 7388 | // we want this argument to be a char*&; for x86-64, we want |
| 7389 | // it to be a __va_list_tag*. |
| 7390 | Type = Context.getBuiltinVaListType(); |
| 7391 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7392 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7393 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7394 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7395 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7396 | break; |
| 7397 | case 'V': { |
| 7398 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7399 | unsigned NumElements = strtoul(Str, &End, 10); |
| 7400 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7401 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7402 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7403 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 7404 | RequiresICE, false); |
| 7405 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7406 | |
| 7407 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 7408 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7409 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7410 | break; |
| 7411 | } |
Douglas Gregor | b4bc99b | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 7412 | case 'E': { |
| 7413 | char *End; |
| 7414 | |
| 7415 | unsigned NumElements = strtoul(Str, &End, 10); |
| 7416 | assert(End != Str && "Missing vector size"); |
| 7417 | |
| 7418 | Str = End; |
| 7419 | |
| 7420 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 7421 | false); |
| 7422 | Type = Context.getExtVectorType(ElementType, NumElements); |
| 7423 | break; |
| 7424 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 7425 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7426 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 7427 | false); |
| 7428 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 7429 | Type = Context.getComplexType(ElementType); |
| 7430 | break; |
Fariborz Jahanian | cc075e4 | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 7431 | } |
| 7432 | case 'Y' : { |
| 7433 | Type = Context.getPointerDiffType(); |
| 7434 | break; |
| 7435 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 7436 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 7437 | Type = Context.getFILEType(); |
| 7438 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7439 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7440 | return QualType(); |
| 7441 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7442 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 7443 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7444 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 7445 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7446 | else |
| 7447 | Type = Context.getjmp_bufType(); |
| 7448 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7449 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7450 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7451 | return QualType(); |
| 7452 | } |
| 7453 | break; |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 7454 | case 'K': |
| 7455 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 7456 | Type = Context.getucontext_tType(); |
| 7457 | |
| 7458 | if (Type.isNull()) { |
| 7459 | Error = ASTContext::GE_Missing_ucontext; |
| 7460 | return QualType(); |
| 7461 | } |
| 7462 | break; |
Eli Friedman | 6902e41 | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 7463 | case 'p': |
| 7464 | Type = Context.getProcessIDType(); |
| 7465 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 7466 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7467 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7468 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 7469 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7470 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 7471 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7472 | default: Done = true; --Str; break; |
| 7473 | case '*': |
| 7474 | case '&': { |
| 7475 | // Both pointers and references can have their pointee types |
| 7476 | // qualified with an address space. |
| 7477 | char *End; |
| 7478 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 7479 | if (End != Str && AddrSpace != 0) { |
| 7480 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 7481 | Str = End; |
| 7482 | } |
| 7483 | if (c == '*') |
| 7484 | Type = Context.getPointerType(Type); |
| 7485 | else |
| 7486 | Type = Context.getLValueReferenceType(Type); |
| 7487 | break; |
| 7488 | } |
| 7489 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 7490 | case 'C': |
| 7491 | Type = Type.withConst(); |
| 7492 | break; |
| 7493 | case 'D': |
| 7494 | Type = Context.getVolatileType(Type); |
| 7495 | break; |
Ted Kremenek | 18932a0 | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 7496 | case 'R': |
| 7497 | Type = Type.withRestrict(); |
| 7498 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7499 | } |
| 7500 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7501 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7502 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7503 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7504 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7505 | return Type; |
| 7506 | } |
| 7507 | |
| 7508 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7509 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7510 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7511 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7512 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7513 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7514 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7515 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7516 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7517 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7518 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 7519 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7520 | if (Error != GE_None) |
| 7521 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7522 | |
| 7523 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 7524 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7525 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7526 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7527 | if (Error != GE_None) |
| 7528 | return QualType(); |
| 7529 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7530 | // If this argument is required to be an IntegerConstantExpression and the |
| 7531 | // caller cares, fill in the bitmask we return. |
| 7532 | if (RequiresICE && IntegerConstantArgs) |
| 7533 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 7534 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7535 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 7536 | if (Ty->isArrayType()) |
| 7537 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7538 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7539 | ArgTypes.push_back(Ty); |
| 7540 | } |
| 7541 | |
| 7542 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 7543 | "'.' should only occur at end of builtin type list!"); |
| 7544 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 7545 | FunctionType::ExtInfo EI; |
| 7546 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 7547 | |
| 7548 | bool Variadic = (TypeStr[0] == '.'); |
| 7549 | |
| 7550 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 7551 | if (ArgTypes.empty() && Variadic) |
| 7552 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 7553 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7554 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 7555 | EPI.ExtInfo = EI; |
| 7556 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7557 | |
| 7558 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7559 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 7560 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7561 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 7562 | GVALinkage External = GVA_StrongExternal; |
| 7563 | |
| 7564 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7565 | switch (L) { |
| 7566 | case NoLinkage: |
| 7567 | case InternalLinkage: |
| 7568 | case UniqueExternalLinkage: |
| 7569 | return GVA_Internal; |
| 7570 | |
| 7571 | case ExternalLinkage: |
| 7572 | switch (FD->getTemplateSpecializationKind()) { |
| 7573 | case TSK_Undeclared: |
| 7574 | case TSK_ExplicitSpecialization: |
| 7575 | External = GVA_StrongExternal; |
| 7576 | break; |
| 7577 | |
| 7578 | case TSK_ExplicitInstantiationDefinition: |
| 7579 | return GVA_ExplicitTemplateInstantiation; |
| 7580 | |
| 7581 | case TSK_ExplicitInstantiationDeclaration: |
| 7582 | case TSK_ImplicitInstantiation: |
| 7583 | External = GVA_TemplateInstantiation; |
| 7584 | break; |
| 7585 | } |
| 7586 | } |
| 7587 | |
| 7588 | if (!FD->isInlined()) |
| 7589 | return External; |
| 7590 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7591 | if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7592 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 7593 | // externally visible. |
| 7594 | if (FD->isInlineDefinitionExternallyVisible()) |
| 7595 | return External; |
| 7596 | |
| 7597 | // C99 inline semantics, where the symbol is not externally visible. |
| 7598 | return GVA_C99Inline; |
| 7599 | } |
| 7600 | |
| 7601 | // C++0x [temp.explicit]p9: |
| 7602 | // [ Note: The intent is that an inline function that is the subject of |
| 7603 | // an explicit instantiation declaration will still be implicitly |
| 7604 | // instantiated when used so that the body can be considered for |
| 7605 | // inlining, but that no out-of-line copy of the inline function would be |
| 7606 | // generated in the translation unit. -- end note ] |
| 7607 | if (FD->getTemplateSpecializationKind() |
| 7608 | == TSK_ExplicitInstantiationDeclaration) |
| 7609 | return GVA_C99Inline; |
| 7610 | |
| 7611 | return GVA_CXXInline; |
| 7612 | } |
| 7613 | |
| 7614 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 7615 | // If this is a static data member, compute the kind of template |
| 7616 | // specialization. Otherwise, this variable is not part of a |
| 7617 | // template. |
| 7618 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 7619 | if (VD->isStaticDataMember()) |
| 7620 | TSK = VD->getTemplateSpecializationKind(); |
| 7621 | |
| 7622 | Linkage L = VD->getLinkage(); |
Rafael Espindola | 62a833e | 2013-01-02 04:19:07 +0000 | [diff] [blame] | 7623 | assert (!(L == ExternalLinkage && getLangOpts().CPlusPlus && |
| 7624 | VD->getType()->getLinkage() == UniqueExternalLinkage)); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7625 | |
| 7626 | switch (L) { |
| 7627 | case NoLinkage: |
| 7628 | case InternalLinkage: |
| 7629 | case UniqueExternalLinkage: |
| 7630 | return GVA_Internal; |
| 7631 | |
| 7632 | case ExternalLinkage: |
| 7633 | switch (TSK) { |
| 7634 | case TSK_Undeclared: |
| 7635 | case TSK_ExplicitSpecialization: |
| 7636 | return GVA_StrongExternal; |
| 7637 | |
| 7638 | case TSK_ExplicitInstantiationDeclaration: |
| 7639 | llvm_unreachable("Variable should not be instantiated"); |
| 7640 | // Fall through to treat this like any other instantiation. |
| 7641 | |
| 7642 | case TSK_ExplicitInstantiationDefinition: |
| 7643 | return GVA_ExplicitTemplateInstantiation; |
| 7644 | |
| 7645 | case TSK_ImplicitInstantiation: |
| 7646 | return GVA_TemplateInstantiation; |
| 7647 | } |
| 7648 | } |
| 7649 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7650 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7651 | } |
| 7652 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 7653 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7654 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7655 | if (!VD->isFileVarDecl()) |
| 7656 | return false; |
Argyrios Kyrtzidis | 144b38a | 2011-09-13 21:35:00 +0000 | [diff] [blame] | 7657 | } else if (!isa<FunctionDecl>(D)) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7658 | return false; |
| 7659 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7660 | // Weak references don't produce any output by themselves. |
| 7661 | if (D->hasAttr<WeakRefAttr>()) |
| 7662 | return false; |
| 7663 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7664 | // Aliases and used decls are required. |
| 7665 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 7666 | return true; |
| 7667 | |
| 7668 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7669 | // Forward declarations aren't required. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 7670 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 7671 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7672 | |
| 7673 | // Constructors and destructors are required. |
| 7674 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 7675 | return true; |
| 7676 | |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7677 | // The key function for a class is required. This rule only comes |
| 7678 | // into play when inline functions can be key functions, though. |
| 7679 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
| 7680 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 7681 | const CXXRecordDecl *RD = MD->getParent(); |
| 7682 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 7683 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
| 7684 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 7685 | return true; |
| 7686 | } |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7687 | } |
| 7688 | } |
| 7689 | |
| 7690 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 7691 | |
| 7692 | // static, static inline, always_inline, and extern inline functions can |
| 7693 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 7694 | // Implicit template instantiations can also be deferred in C++. |
| 7695 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
Anton Yartsev | 3a5aca8 | 2012-02-02 06:06:34 +0000 | [diff] [blame] | 7696 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7697 | return false; |
| 7698 | return true; |
| 7699 | } |
Douglas Gregor | 94da158 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 7700 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7701 | const VarDecl *VD = cast<VarDecl>(D); |
| 7702 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 7703 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7704 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 7705 | return false; |
| 7706 | |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7707 | // Variables that can be needed in other TUs are required. |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7708 | GVALinkage L = GetGVALinkageForVariable(VD); |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7709 | if (L != GVA_Internal && L != GVA_TemplateInstantiation) |
| 7710 | return true; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7711 | |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7712 | // Variables that have destruction with side-effects are required. |
| 7713 | if (VD->getType().isDestructedType()) |
| 7714 | return true; |
| 7715 | |
| 7716 | // Variables that have initialization with side-effects are required. |
| 7717 | if (VD->getInit() && VD->getInit()->HasSideEffects(*this)) |
| 7718 | return true; |
| 7719 | |
| 7720 | return false; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7721 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7722 | |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7723 | CallingConv ASTContext::getDefaultCXXMethodCallConv(bool isVariadic) { |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7724 | // Pass through to the C++ ABI object |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7725 | return ABI->getDefaultMethodCallConv(isVariadic); |
| 7726 | } |
| 7727 | |
| 7728 | CallingConv ASTContext::getCanonicalCallConv(CallingConv CC) const { |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7729 | if (CC == CC_C && !LangOpts.MRTD && |
| 7730 | getTargetInfo().getCXXABI().isMemberFunctionCCDefault()) |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7731 | return CC_Default; |
| 7732 | return CC; |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7733 | } |
| 7734 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7735 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 7736 | // Pass through to the C++ ABI object |
| 7737 | return ABI->isNearlyEmpty(RD); |
| 7738 | } |
| 7739 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7740 | MangleContext *ASTContext::createMangleContext() { |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7741 | switch (Target->getCXXABI().getKind()) { |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7742 | case TargetCXXABI::GenericAArch64: |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7743 | case TargetCXXABI::GenericItanium: |
| 7744 | case TargetCXXABI::GenericARM: |
| 7745 | case TargetCXXABI::iOS: |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7746 | return createItaniumMangleContext(*this, getDiagnostics()); |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7747 | case TargetCXXABI::Microsoft: |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7748 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 7749 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7750 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7751 | } |
| 7752 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7753 | CXXABI::~CXXABI() {} |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7754 | |
| 7755 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 0c8cd1a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 7756 | return ASTRecordLayouts.getMemorySize() |
| 7757 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 7758 | + llvm::capacity_in_bytes(KeyFunctions) |
| 7759 | + llvm::capacity_in_bytes(ObjCImpls) |
| 7760 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 7761 | + llvm::capacity_in_bytes(DeclAttrs) |
| 7762 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 7763 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 7764 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 7765 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 7766 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 7767 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7768 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 7769 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7770 | } |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7771 | |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 7772 | void ASTContext::addUnnamedTag(const TagDecl *Tag) { |
| 7773 | // FIXME: This mangling should be applied to function local classes too |
| 7774 | if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl() || |
| 7775 | !isa<CXXRecordDecl>(Tag->getParent()) || Tag->getLinkage() != ExternalLinkage) |
| 7776 | return; |
| 7777 | |
| 7778 | std::pair<llvm::DenseMap<const DeclContext *, unsigned>::iterator, bool> P = |
| 7779 | UnnamedMangleContexts.insert(std::make_pair(Tag->getParent(), 0)); |
| 7780 | UnnamedMangleNumbers.insert(std::make_pair(Tag, P.first->second++)); |
| 7781 | } |
| 7782 | |
| 7783 | int ASTContext::getUnnamedTagManglingNumber(const TagDecl *Tag) const { |
| 7784 | llvm::DenseMap<const TagDecl *, unsigned>::const_iterator I = |
| 7785 | UnnamedMangleNumbers.find(Tag); |
| 7786 | return I != UnnamedMangleNumbers.end() ? I->second : -1; |
| 7787 | } |
| 7788 | |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 7789 | unsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) { |
| 7790 | CXXRecordDecl *Lambda = CallOperator->getParent(); |
| 7791 | return LambdaMangleContexts[Lambda->getDeclContext()] |
| 7792 | .getManglingNumber(CallOperator); |
| 7793 | } |
| 7794 | |
| 7795 | |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7796 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 7797 | ParamIndices[D] = index; |
| 7798 | } |
| 7799 | |
| 7800 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 7801 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 7802 | assert(I != ParamIndices.end() && |
| 7803 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 7804 | return I->second; |
| 7805 | } |