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); |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 600 | case TargetCXXABI::GenericItanium: |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 601 | return CreateItaniumCXXABI(*this); |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 602 | case TargetCXXABI::Microsoft: |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 603 | return CreateMicrosoftCXXABI(*this); |
| 604 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 605 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 608 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 609 | const LangOptions &LOpts) { |
| 610 | if (LOpts.FakeAddressSpaceMap) { |
| 611 | // The fake address space map must have a distinct entry for each |
| 612 | // language-specific address space. |
| 613 | static const unsigned FakeAddrSpaceMap[] = { |
| 614 | 1, // opencl_global |
| 615 | 2, // opencl_local |
Peter Collingbourne | 4dc34eb | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 616 | 3, // opencl_constant |
| 617 | 4, // cuda_device |
| 618 | 5, // cuda_constant |
| 619 | 6 // cuda_shared |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 620 | }; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 621 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 622 | } else { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 623 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Douglas Gregor | 3e3cd93 | 2011-09-01 20:23:19 +0000 | [diff] [blame] | 627 | ASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 628 | const TargetInfo *t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 629 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 630 | Builtin::Context &builtins, |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 631 | unsigned size_reserve, |
| 632 | bool DelayInitialization) |
| 633 | : FunctionProtoTypes(this_()), |
| 634 | TemplateSpecializationTypes(this_()), |
| 635 | DependentTemplateSpecializationTypes(this_()), |
| 636 | SubstTemplateTemplateParmPacks(this_()), |
| 637 | GlobalNestedNameSpecifier(0), |
| 638 | Int128Decl(0), UInt128Decl(0), |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 639 | BuiltinVaListDecl(0), |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 640 | ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0), |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 641 | BOOLDecl(0), |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 642 | CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 643 | FILEDecl(0), |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 644 | jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0), |
| 645 | BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 646 | cudaConfigureCallDecl(0), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 647 | NullTypeSourceInfo(QualType()), |
| 648 | FirstLocalImport(), LastLocalImport(), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 649 | SourceMgr(SM), LangOpts(LOpts), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 650 | AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 651 | Idents(idents), Selectors(sels), |
| 652 | BuiltinInfo(builtins), |
| 653 | DeclarationNames(*this), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 654 | ExternalSource(0), Listener(0), |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 655 | Comments(SM), CommentsLoaded(false), |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 656 | CommentCommandTraits(BumpAlloc), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 657 | LastSDM(0, 0), |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 658 | UniqueBlockByRefTypeID(0) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 659 | { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 661 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 662 | |
| 663 | if (!DelayInitialization) { |
| 664 | assert(t && "No target supplied for ASTContext initialization"); |
| 665 | InitBuiltinTypes(*t); |
| 666 | } |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 669 | ASTContext::~ASTContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 670 | // Release the DenseMaps associated with DeclContext objects. |
| 671 | // FIXME: Is this the ideal solution? |
| 672 | ReleaseDeclContextMaps(); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 673 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 674 | // Call all of the deallocation functions. |
| 675 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 676 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 677 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 678 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 679 | // because they can contain DenseMaps. |
| 680 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 681 | const ASTRecordLayout*>::iterator |
| 682 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 683 | // Increment in loop to prevent using deallocated memory. |
| 684 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 685 | R->Destroy(*this); |
| 686 | |
Ted Kremenek | dcfcfbe | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 687 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 688 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 689 | // Increment in loop to prevent using deallocated memory. |
| 690 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 691 | R->Destroy(*this); |
| 692 | } |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 693 | |
| 694 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 695 | AEnd = DeclAttrs.end(); |
| 696 | A != AEnd; ++A) |
| 697 | A->second->~AttrVec(); |
| 698 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 699 | |
Douglas Gregor | 0054531 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 700 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 701 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 702 | } |
| 703 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | void |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 705 | ASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 706 | ExternalSource.reset(Source.take()); |
| 707 | } |
| 708 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 709 | void ASTContext::PrintStats() const { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 710 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 711 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 712 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 713 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 715 | #define ABSTRACT_TYPE(Name, Parent) |
| 716 | #include "clang/AST/TypeNodes.def" |
| 717 | 0 // Extra |
| 718 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 719 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 720 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 721 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 722 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 725 | unsigned Idx = 0; |
| 726 | unsigned TotalBytes = 0; |
| 727 | #define TYPE(Name, Parent) \ |
| 728 | if (counts[Idx]) \ |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 729 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 730 | << " types\n"; \ |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 731 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 732 | ++Idx; |
| 733 | #define ABSTRACT_TYPE(Name, Parent) |
| 734 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 736 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 737 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 738 | // Implicit special member functions. |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 739 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 740 | << NumImplicitDefaultConstructors |
| 741 | << " implicit default constructors created\n"; |
| 742 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 743 | << NumImplicitCopyConstructors |
| 744 | << " implicit copy constructors created\n"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 745 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 746 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 747 | << NumImplicitMoveConstructors |
| 748 | << " implicit move constructors created\n"; |
| 749 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 750 | << NumImplicitCopyAssignmentOperators |
| 751 | << " implicit copy assignment operators created\n"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 752 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 753 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 754 | << NumImplicitMoveAssignmentOperators |
| 755 | << " implicit move assignment operators created\n"; |
| 756 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 757 | << NumImplicitDestructors |
| 758 | << " implicit destructors created\n"; |
| 759 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 760 | if (ExternalSource.get()) { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 761 | llvm::errs() << "\n"; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 762 | ExternalSource->PrintStats(); |
| 763 | } |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 764 | |
Douglas Gregor | 63fe86b | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 765 | BumpAlloc.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Douglas Gregor | 772eeae | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 768 | TypedefDecl *ASTContext::getInt128Decl() const { |
| 769 | if (!Int128Decl) { |
| 770 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty); |
| 771 | Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 772 | getTranslationUnitDecl(), |
| 773 | SourceLocation(), |
| 774 | SourceLocation(), |
| 775 | &Idents.get("__int128_t"), |
| 776 | TInfo); |
| 777 | } |
| 778 | |
| 779 | return Int128Decl; |
| 780 | } |
| 781 | |
| 782 | TypedefDecl *ASTContext::getUInt128Decl() const { |
| 783 | if (!UInt128Decl) { |
| 784 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty); |
| 785 | UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 786 | getTranslationUnitDecl(), |
| 787 | SourceLocation(), |
| 788 | SourceLocation(), |
| 789 | &Idents.get("__uint128_t"), |
| 790 | TInfo); |
| 791 | } |
| 792 | |
| 793 | return UInt128Decl; |
| 794 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 795 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 796 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 797 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 798 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 799 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 802 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target) { |
| 803 | assert((!this->Target || this->Target == &Target) && |
| 804 | "Incorrect target reinitialization"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 805 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 807 | this->Target = &Target; |
| 808 | |
| 809 | ABI.reset(createCXXABI(Target)); |
| 810 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
| 811 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 812 | // C99 6.2.5p19. |
| 813 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 814 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 815 | // C99 6.2.5p2. |
| 816 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 817 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 818 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 819 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 820 | else |
| 821 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 822 | // C99 6.2.5p4. |
| 823 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 824 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 825 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 826 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 827 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 828 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 829 | // C99 6.2.5p6. |
| 830 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 831 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 832 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 833 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 834 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 836 | // C99 6.2.5p10. |
| 837 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 838 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 839 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 840 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 841 | // GNU extension, 128-bit integers. |
| 842 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 843 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 844 | |
Abramo Bagnara | e75bb61 | 2012-09-09 10:13:32 +0000 | [diff] [blame] | 845 | if (LangOpts.CPlusPlus && LangOpts.WChar) { // C++ 3.9.1p5 |
Eli Friedman | d3d77cd | 2011-04-30 19:24:24 +0000 | [diff] [blame] | 846 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 847 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 848 | else // -fshort-wchar makes wchar_t be unsigned. |
| 849 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
Abramo Bagnara | e75bb61 | 2012-09-09 10:13:32 +0000 | [diff] [blame] | 850 | } else // C99 (or C++ using -fno-wchar) |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 851 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 852 | |
James Molloy | 392da48 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 853 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 854 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 855 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 856 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 857 | else // C99 |
| 858 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 859 | |
| 860 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 861 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 862 | else // C99 |
| 863 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 864 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 865 | // Placeholder type for type-dependent expressions whose type is |
| 866 | // completely unknown. No code should ever check a type against |
| 867 | // DependentTy and users should never see it; however, it is here to |
| 868 | // help diagnose failures to properly check for type-dependent |
| 869 | // expressions. |
| 870 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 871 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 872 | // Placeholder type for functions. |
| 873 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 874 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 875 | // Placeholder type for bound members. |
| 876 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 877 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 878 | // Placeholder type for pseudo-objects. |
| 879 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 880 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 881 | // "any" type; useful for debugger-like clients. |
| 882 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 883 | |
John McCall | 0ddaeb9 | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 884 | // Placeholder type for unbridged ARC casts. |
| 885 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 886 | |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 887 | // Placeholder type for builtin functions. |
| 888 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
| 889 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 890 | // C99 6.2.5p11. |
| 891 | FloatComplexTy = getComplexType(FloatTy); |
| 892 | DoubleComplexTy = getComplexType(DoubleTy); |
| 893 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 894 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 895 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 896 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 897 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 898 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 899 | |
| 900 | if (LangOpts.OpenCL) { |
| 901 | InitBuiltinType(OCLImage1dTy, BuiltinType::OCLImage1d); |
| 902 | InitBuiltinType(OCLImage1dArrayTy, BuiltinType::OCLImage1dArray); |
| 903 | InitBuiltinType(OCLImage1dBufferTy, BuiltinType::OCLImage1dBuffer); |
| 904 | InitBuiltinType(OCLImage2dTy, BuiltinType::OCLImage2d); |
| 905 | InitBuiltinType(OCLImage2dArrayTy, BuiltinType::OCLImage2dArray); |
| 906 | InitBuiltinType(OCLImage3dTy, BuiltinType::OCLImage3d); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 907 | |
| 908 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 909 | } |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 910 | |
| 911 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 93a4994 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 912 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 913 | SignedCharTy : BoolTy); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 914 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 915 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 916 | |
| 917 | ObjCSuperType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 918 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 919 | // void * type |
| 920 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 921 | |
| 922 | // nullptr type (C++0x 2.14.7) |
| 923 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 924 | |
| 925 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 926 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 927 | |
| 928 | // Builtin type used to help define __builtin_va_list. |
| 929 | VaListTagTy = QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 930 | } |
| 931 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 932 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 933 | return SourceMgr.getDiagnostics(); |
| 934 | } |
| 935 | |
Douglas Gregor | 6320064 | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 936 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 937 | AttrVec *&Result = DeclAttrs[D]; |
| 938 | if (!Result) { |
| 939 | void *Mem = Allocate(sizeof(AttrVec)); |
| 940 | Result = new (Mem) AttrVec; |
| 941 | } |
| 942 | |
| 943 | return *Result; |
| 944 | } |
| 945 | |
| 946 | /// \brief Erase the attributes corresponding to the given declaration. |
| 947 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 948 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 949 | if (Pos != DeclAttrs.end()) { |
| 950 | Pos->second->~AttrVec(); |
| 951 | DeclAttrs.erase(Pos); |
| 952 | } |
| 953 | } |
| 954 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 955 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 956 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 957 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 958 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 959 | = InstantiatedFromStaticDataMember.find(Var); |
| 960 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 961 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 963 | return Pos->second; |
| 964 | } |
| 965 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 967 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 968 | TemplateSpecializationKind TSK, |
| 969 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 970 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 971 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 972 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 973 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 974 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | 9421adc | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 975 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 978 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 979 | const FunctionDecl *FD){ |
| 980 | assert(FD && "Specialization is 0"); |
| 981 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 982 | = ClassScopeSpecializationPattern.find(FD); |
| 983 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 984 | return 0; |
| 985 | |
| 986 | return Pos->second; |
| 987 | } |
| 988 | |
| 989 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 990 | FunctionDecl *Pattern) { |
| 991 | assert(FD && "Specialization is 0"); |
| 992 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 993 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 994 | } |
| 995 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 996 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 997 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 998 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 999 | = InstantiatedFromUsingDecl.find(UUD); |
| 1000 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1001 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1003 | return Pos->second; |
| 1004 | } |
| 1005 | |
| 1006 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1007 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 1008 | assert((isa<UsingDecl>(Pattern) || |
| 1009 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 1010 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 1011 | "pattern decl is not a using decl"); |
| 1012 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 1013 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 1014 | } |
| 1015 | |
| 1016 | UsingShadowDecl * |
| 1017 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 1018 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 1019 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 1020 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 1021 | return 0; |
| 1022 | |
| 1023 | return Pos->second; |
| 1024 | } |
| 1025 | |
| 1026 | void |
| 1027 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 1028 | UsingShadowDecl *Pattern) { |
| 1029 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 1030 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1033 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 1034 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 1035 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 1036 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 1037 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1038 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1039 | return Pos->second; |
| 1040 | } |
| 1041 | |
| 1042 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 1043 | FieldDecl *Tmpl) { |
| 1044 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 1045 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 1046 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 1047 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1049 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 1050 | } |
| 1051 | |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 1052 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 1053 | const FieldDecl *LastFD) const { |
| 1054 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1055 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 14d56ef | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 1058 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 1059 | const FieldDecl *LastFD) const { |
| 1060 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1061 | FD->getBitWidthValue(*this) == 0 && |
| 1062 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | 340fa24 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1065 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 1066 | const FieldDecl *LastFD) const { |
| 1067 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1068 | FD->getBitWidthValue(*this) && |
| 1069 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 9b3acaa | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1072 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1073 | const FieldDecl *LastFD) const { |
| 1074 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1075 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Chad Rosier | dd7fddb | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 1078 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1079 | const FieldDecl *LastFD) const { |
| 1080 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1081 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | 52bbe7a | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1084 | ASTContext::overridden_cxx_method_iterator |
| 1085 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 1086 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1087 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1088 | if (Pos == OverriddenMethods.end()) |
| 1089 | return 0; |
| 1090 | |
| 1091 | return Pos->second.begin(); |
| 1092 | } |
| 1093 | |
| 1094 | ASTContext::overridden_cxx_method_iterator |
| 1095 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 1096 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1097 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1098 | if (Pos == OverriddenMethods.end()) |
| 1099 | return 0; |
| 1100 | |
| 1101 | return Pos->second.end(); |
| 1102 | } |
| 1103 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1104 | unsigned |
| 1105 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 1106 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1107 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1108 | if (Pos == OverriddenMethods.end()) |
| 1109 | return 0; |
| 1110 | |
| 1111 | return Pos->second.size(); |
| 1112 | } |
| 1113 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1114 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 1115 | const CXXMethodDecl *Overridden) { |
Argyrios Kyrtzidis | 38eb1e1 | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1116 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1117 | OverriddenMethods[Method].push_back(Overridden); |
| 1118 | } |
| 1119 | |
Dmitri Gribenko | 1e905da | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 1120 | void ASTContext::getOverriddenMethods( |
| 1121 | const NamedDecl *D, |
| 1122 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1123 | assert(D); |
| 1124 | |
| 1125 | if (const CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | bc0a2bb | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1126 | Overridden.append(CXXMethod->begin_overridden_methods(), |
| 1127 | CXXMethod->end_overridden_methods()); |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1128 | return; |
| 1129 | } |
| 1130 | |
| 1131 | const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); |
| 1132 | if (!Method) |
| 1133 | return; |
| 1134 | |
Argyrios Kyrtzidis | 740ae67 | 2012-10-09 18:19:01 +0000 | [diff] [blame] | 1135 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
| 1136 | Method->getOverriddenMethods(OverDecls); |
Argyrios Kyrtzidis | bc0a2bb | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1137 | Overridden.append(OverDecls.begin(), OverDecls.end()); |
Argyrios Kyrtzidis | 21c3607 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1140 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 1141 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 1142 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 1143 | if (!FirstLocalImport) { |
| 1144 | FirstLocalImport = Import; |
| 1145 | LastLocalImport = Import; |
| 1146 | return; |
| 1147 | } |
| 1148 | |
| 1149 | LastLocalImport->NextLocalImport = Import; |
| 1150 | LastLocalImport = Import; |
| 1151 | } |
| 1152 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1153 | //===----------------------------------------------------------------------===// |
| 1154 | // Type Sizing and Analysis |
| 1155 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1156 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1157 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 1158 | /// scalar floating point type. |
| 1159 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1160 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1161 | assert(BT && "Not a floating point type!"); |
| 1162 | switch (BT->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1163 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1164 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1165 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 1166 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 1167 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1171 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1172 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 1173 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1174 | /// If @p RefAsPointee, references are treated like their underlying type |
| 1175 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1176 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1177 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1178 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1179 | bool UseAlignAttrOnly = false; |
| 1180 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 1181 | Align = AlignFromAttr; |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1182 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1183 | // __attribute__((aligned)) can increase or decrease alignment |
| 1184 | // *except* on a struct or struct member, where it only increases |
| 1185 | // alignment unless 'packed' is also specified. |
| 1186 | // |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1187 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1188 | // ignore that possibility; Sema should diagnose it. |
| 1189 | if (isa<FieldDecl>(D)) { |
| 1190 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 1191 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 1192 | } else { |
| 1193 | UseAlignAttrOnly = true; |
| 1194 | } |
| 1195 | } |
Fariborz Jahanian | 78a7d7d | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1196 | else if (isa<FieldDecl>(D)) |
| 1197 | UseAlignAttrOnly = |
| 1198 | D->hasAttr<PackedAttr>() || |
| 1199 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1200 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1201 | // If we're using the align attribute only, just ignore everything |
| 1202 | // else about the declaration and its type. |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1203 | if (UseAlignAttrOnly) { |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1204 | // do nothing |
| 1205 | |
John McCall | 4081a5c | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1206 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1207 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1208 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1209 | if (RefAsPointee) |
| 1210 | T = RT->getPointeeType(); |
| 1211 | else |
| 1212 | T = getPointerType(RT->getPointeeType()); |
| 1213 | } |
| 1214 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1215 | // Adjust alignments of declarations with array type by the |
| 1216 | // large-array alignment on the target. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1217 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1218 | const ArrayType *arrayType; |
| 1219 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 1220 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1221 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1222 | else if (isa<ConstantArrayType>(arrayType) && |
| 1223 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1224 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1225 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1226 | // Walk through any array types while we're at it. |
| 1227 | T = getBaseElementType(arrayType); |
| 1228 | } |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 1229 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1230 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1231 | |
| 1232 | // Fields can be subject to extra alignment constraints, like if |
| 1233 | // the field is packed, the struct is packed, or the struct has a |
| 1234 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 1235 | // the actual alignment of the field within the struct, and then |
| 1236 | // (as we're expected to) constrain that by the alignment of the type. |
| 1237 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 1238 | // So calculate the alignment of the field. |
| 1239 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 1240 | |
| 1241 | // Start with the record's overall alignment. |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1242 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1243 | |
| 1244 | // Use the GCD of that and the offset within the record. |
| 1245 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 1246 | if (offset > 0) { |
| 1247 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 1248 | // which means we get to do this crazy thing instead of Euclid's. |
| 1249 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 1250 | if (lowBitOfOffset < fieldAlign) |
| 1251 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 1252 | } |
| 1253 | |
| 1254 | Align = std::min(Align, fieldAlign); |
Charles Davis | 05f6247 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 1255 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1256 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1257 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1258 | return toCharUnitsFromBits(Align); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1259 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1260 | |
John McCall | 929bbfb | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1261 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
| 1262 | // chars. If the type is a record, its data size is returned. This is |
| 1263 | // the size of the memcpy that's performed when assigning this type |
| 1264 | // using a trivial copy/move assignment operator. |
| 1265 | std::pair<CharUnits, CharUnits> |
| 1266 | ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
| 1267 | std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T); |
| 1268 | |
| 1269 | // In C++, objects can sometimes be allocated into the tail padding |
| 1270 | // of a base-class subobject. We decide whether that's possible |
| 1271 | // during class layout, so here we can just trust the layout results. |
| 1272 | if (getLangOpts().CPlusPlus) { |
| 1273 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1274 | const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl()); |
| 1275 | sizeAndAlign.first = layout.getDataSize(); |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | return sizeAndAlign; |
| 1280 | } |
| 1281 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1282 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1283 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1284 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1285 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 1286 | toCharUnitsFromBits(Info.second)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | std::pair<CharUnits, CharUnits> |
Ken Dyck | bee5a79 | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1290 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1291 | return getTypeInfoInChars(T.getTypePtr()); |
| 1292 | } |
| 1293 | |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1294 | std::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const { |
| 1295 | TypeInfoMap::iterator it = MemoizedTypeInfo.find(T); |
| 1296 | if (it != MemoizedTypeInfo.end()) |
| 1297 | return it->second; |
| 1298 | |
| 1299 | std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T); |
| 1300 | MemoizedTypeInfo.insert(std::make_pair(T, Info)); |
| 1301 | return Info; |
| 1302 | } |
| 1303 | |
| 1304 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 1305 | /// method does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1306 | /// |
| 1307 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 1308 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1309 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1310 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1311 | ASTContext::getTypeInfoImpl(const Type *T) const { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 1312 | uint64_t Width=0; |
| 1313 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1314 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1315 | #define TYPE(Class, Base) |
| 1316 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1317 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1318 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1319 | #include "clang/AST/TypeNodes.def" |
John McCall | d3d49bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1320 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1321 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1322 | case Type::FunctionNoProto: |
| 1323 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1324 | // GCC extension: alignof(function) = 32 bits |
| 1325 | Width = 0; |
| 1326 | Align = 32; |
| 1327 | break; |
| 1328 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1329 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1330 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1331 | Width = 0; |
| 1332 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1333 | break; |
| 1334 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1335 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1336 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1338 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1339 | uint64_t Size = CAT->getSize().getZExtValue(); |
Daniel Dunbar | bc5419a | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1340 | assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && |
| 1341 | "Overflow in array type bit size evaluation"); |
Abramo Bagnara | fea966a | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1342 | Width = EltInfo.first*Size; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1343 | Align = EltInfo.second; |
Argyrios Kyrtzidis | cd88b41 | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 1344 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1345 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1346 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1347 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1348 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1349 | const VectorType *VT = cast<VectorType>(T); |
| 1350 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 1351 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1352 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1353 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1354 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | 8eefcd3 | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1355 | if (Align & (Align-1)) { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1356 | Align = llvm::NextPowerOf2(Align); |
| 1357 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 1358 | } |
Chad Rosier | f9e9af7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1359 | // Adjust the alignment based on the target max. |
| 1360 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1361 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1362 | Align = TargetVectorAlign; |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1363 | break; |
| 1364 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1365 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1366 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1367 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1368 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1369 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1370 | // GCC extension: alignof(void) = 8 bits. |
| 1371 | Width = 0; |
| 1372 | Align = 8; |
| 1373 | break; |
| 1374 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1375 | case BuiltinType::Bool: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1376 | Width = Target->getBoolWidth(); |
| 1377 | Align = Target->getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1378 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1379 | case BuiltinType::Char_S: |
| 1380 | case BuiltinType::Char_U: |
| 1381 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1382 | case BuiltinType::SChar: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1383 | Width = Target->getCharWidth(); |
| 1384 | Align = Target->getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1385 | break; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1386 | case BuiltinType::WChar_S: |
| 1387 | case BuiltinType::WChar_U: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1388 | Width = Target->getWCharWidth(); |
| 1389 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1390 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1391 | case BuiltinType::Char16: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1392 | Width = Target->getChar16Width(); |
| 1393 | Align = Target->getChar16Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1394 | break; |
| 1395 | case BuiltinType::Char32: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1396 | Width = Target->getChar32Width(); |
| 1397 | Align = Target->getChar32Align(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1398 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1399 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1400 | case BuiltinType::Short: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1401 | Width = Target->getShortWidth(); |
| 1402 | Align = Target->getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1403 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1404 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1405 | case BuiltinType::Int: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1406 | Width = Target->getIntWidth(); |
| 1407 | Align = Target->getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1408 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1409 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1410 | case BuiltinType::Long: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1411 | Width = Target->getLongWidth(); |
| 1412 | Align = Target->getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1413 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1414 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1415 | case BuiltinType::LongLong: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1416 | Width = Target->getLongLongWidth(); |
| 1417 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1418 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1419 | case BuiltinType::Int128: |
| 1420 | case BuiltinType::UInt128: |
| 1421 | Width = 128; |
| 1422 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1423 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1424 | case BuiltinType::Half: |
| 1425 | Width = Target->getHalfWidth(); |
| 1426 | Align = Target->getHalfAlign(); |
| 1427 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1428 | case BuiltinType::Float: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1429 | Width = Target->getFloatWidth(); |
| 1430 | Align = Target->getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1431 | break; |
| 1432 | case BuiltinType::Double: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1433 | Width = Target->getDoubleWidth(); |
| 1434 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1435 | break; |
| 1436 | case BuiltinType::LongDouble: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1437 | Width = Target->getLongDoubleWidth(); |
| 1438 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1439 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1440 | case BuiltinType::NullPtr: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1441 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1442 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1443 | break; |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1444 | case BuiltinType::ObjCId: |
| 1445 | case BuiltinType::ObjCClass: |
| 1446 | case BuiltinType::ObjCSel: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1447 | Width = Target->getPointerWidth(0); |
| 1448 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | e04f5fc | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1449 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1450 | case BuiltinType::OCLEvent: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1451 | case BuiltinType::OCLImage1d: |
| 1452 | case BuiltinType::OCLImage1dArray: |
| 1453 | case BuiltinType::OCLImage1dBuffer: |
| 1454 | case BuiltinType::OCLImage2d: |
| 1455 | case BuiltinType::OCLImage2dArray: |
| 1456 | case BuiltinType::OCLImage3d: |
| 1457 | // Currently these types are pointers to opaque types. |
| 1458 | Width = Target->getPointerWidth(0); |
| 1459 | Align = Target->getPointerAlign(0); |
| 1460 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1461 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1462 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1463 | case Type::ObjCObjectPointer: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1464 | Width = Target->getPointerWidth(0); |
| 1465 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1466 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1467 | case Type::BlockPointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1468 | unsigned AS = getTargetAddressSpace( |
| 1469 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1470 | Width = Target->getPointerWidth(AS); |
| 1471 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1472 | break; |
| 1473 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1474 | case Type::LValueReference: |
| 1475 | case Type::RValueReference: { |
| 1476 | // alignof and sizeof should never enter this code path here, so we go |
| 1477 | // the pointer route. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1478 | unsigned AS = getTargetAddressSpace( |
| 1479 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1480 | Width = Target->getPointerWidth(AS); |
| 1481 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1482 | break; |
| 1483 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1484 | case Type::Pointer: { |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1485 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1486 | Width = Target->getPointerWidth(AS); |
| 1487 | Align = Target->getPointerAlign(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1488 | break; |
| 1489 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1490 | case Type::MemberPointer: { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1491 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1493 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1494 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1495 | Align = PtrDiffInfo.second; |
| 1496 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1497 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1498 | case Type::Complex: { |
| 1499 | // Complex types have the same alignment as their elements, but twice the |
| 1500 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1502 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1503 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1504 | Align = EltInfo.second; |
| 1505 | break; |
| 1506 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1507 | case Type::ObjCObject: |
| 1508 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1509 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1510 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1511 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1512 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1513 | Align = toBits(Layout.getAlignment()); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1514 | break; |
| 1515 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1516 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1517 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1518 | const TagType *TT = cast<TagType>(T); |
| 1519 | |
| 1520 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 22ce41d | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1521 | Width = 8; |
| 1522 | Align = 8; |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1523 | break; |
| 1524 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1526 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1527 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1528 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1529 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1530 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1531 | Width = toBits(Layout.getSize()); |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1532 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1533 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1534 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1536 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1537 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1538 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1539 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1540 | case Type::Auto: { |
| 1541 | const AutoType *A = cast<AutoType>(T); |
| 1542 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | dc856af | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1543 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1546 | case Type::Paren: |
| 1547 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1548 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1549 | case Type::Typedef: { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1550 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1551 | std::pair<uint64_t, unsigned> Info |
| 1552 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | c1de52d | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1553 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1554 | // alignment we have. This violates the GCC documentation (which says that |
| 1555 | // attribute(aligned) can only round up) but matches its implementation. |
| 1556 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1557 | Align = AttrAlign; |
| 1558 | else |
| 1559 | Align = Info.second; |
Douglas Gregor | df1367a | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1560 | Width = Info.first; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1561 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1562 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1563 | |
| 1564 | case Type::TypeOfExpr: |
| 1565 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1566 | .getTypePtr()); |
| 1567 | |
| 1568 | case Type::TypeOf: |
| 1569 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1570 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1571 | case Type::Decltype: |
| 1572 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1573 | .getTypePtr()); |
| 1574 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1575 | case Type::UnaryTransform: |
| 1576 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1577 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1578 | case Type::Elaborated: |
| 1579 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1581 | case Type::Attributed: |
| 1582 | return getTypeInfo( |
| 1583 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1584 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1585 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1587 | "Cannot request the size of a dependent type"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1588 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1589 | // A type alias template specialization may refer to a typedef with the |
| 1590 | // aligned attribute on it. |
| 1591 | if (TST->isTypeAlias()) |
| 1592 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1593 | else |
| 1594 | return getTypeInfo(getCanonicalType(T)); |
| 1595 | } |
| 1596 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1597 | case Type::Atomic: { |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1598 | std::pair<uint64_t, unsigned> Info |
| 1599 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1600 | Width = Info.first; |
| 1601 | Align = Info.second; |
| 1602 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() && |
| 1603 | llvm::isPowerOf2_64(Width)) { |
| 1604 | // We can potentially perform lock-free atomic operations for this |
| 1605 | // type; promote the alignment appropriately. |
| 1606 | // FIXME: We could potentially promote the width here as well... |
| 1607 | // is that worthwhile? (Non-struct atomic types generally have |
| 1608 | // power-of-two size anyway, but structs might not. Requires a bit |
| 1609 | // of implementation work to make sure we zero out the extra bits.) |
| 1610 | Align = static_cast<unsigned>(Width); |
| 1611 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1614 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1615 | |
Eli Friedman | 2be4607 | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1616 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1617 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1620 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1621 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1622 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1623 | } |
| 1624 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1625 | /// toBits - Convert a size in characters to a size in characters. |
| 1626 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1627 | return CharSize.getQuantity() * getCharWidth(); |
| 1628 | } |
| 1629 | |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1630 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1631 | /// This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1632 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1633 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1634 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1635 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1636 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Ken Dyck | 16e20cc | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1639 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1640 | /// characters. This method does not work on incomplete types. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1641 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1642 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1643 | } |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1644 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | eb6f5dc | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1645 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 86fa431 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1648 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1649 | /// type for the current target in bits. This can be different than the ABI |
| 1650 | /// alignment in cases where it is beneficial for performance to overalign |
| 1651 | /// a data type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1652 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1653 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1654 | |
| 1655 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1656 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1657 | T = CT->getElementType().getTypePtr(); |
| 1658 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | cde7a1d | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1659 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1660 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1661 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1662 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1663 | return ABIAlign; |
| 1664 | } |
| 1665 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1666 | /// DeepCollectObjCIvars - |
| 1667 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1668 | /// super class and then collects all ivars, including those synthesized for |
| 1669 | /// current class. This routine is used for implementation of current class |
| 1670 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1671 | /// |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1672 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1673 | bool leafClass, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1674 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1675 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1676 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1677 | if (!leafClass) { |
| 1678 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1679 | E = OI->ivar_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1680 | Ivars.push_back(*I); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1681 | } else { |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1682 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1683 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1684 | Iv= Iv->getNextIvar()) |
| 1685 | Ivars.push_back(Iv); |
| 1686 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1689 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1690 | /// those inherited by it. |
| 1691 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1692 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1693 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1694 | // We can use protocol_iterator here instead of |
| 1695 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1696 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1697 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1698 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1699 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1700 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1701 | PE = Proto->protocol_end(); P != PE; ++P) { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1702 | Protocols.insert((*P)->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1703 | CollectInheritedProtocols(*P, Protocols); |
| 1704 | } |
Fariborz Jahanian | b2f8121 | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1705 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1706 | |
| 1707 | // Categories of this Interface. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1708 | for (ObjCInterfaceDecl::visible_categories_iterator |
| 1709 | Cat = OI->visible_categories_begin(), |
| 1710 | CatEnd = OI->visible_categories_end(); |
| 1711 | Cat != CatEnd; ++Cat) { |
| 1712 | CollectInheritedProtocols(*Cat, Protocols); |
| 1713 | } |
| 1714 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1715 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1716 | while (SD) { |
| 1717 | CollectInheritedProtocols(SD, Protocols); |
| 1718 | SD = SD->getSuperClass(); |
| 1719 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1720 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1721 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1722 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1723 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1724 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1725 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1726 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1727 | CollectInheritedProtocols(*P, Protocols); |
| 1728 | } |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1729 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1730 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1731 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1732 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1733 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1734 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1735 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1736 | CollectInheritedProtocols(*P, Protocols); |
| 1737 | } |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1741 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1742 | unsigned count = 0; |
| 1743 | // Count ivars declared in class extension. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1744 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 1745 | Ext = OI->known_extensions_begin(), |
| 1746 | ExtEnd = OI->known_extensions_end(); |
| 1747 | Ext != ExtEnd; ++Ext) { |
| 1748 | count += Ext->ivar_size(); |
| 1749 | } |
| 1750 | |
Fariborz Jahanian | 3bfacdf | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1751 | // Count ivar defined in this class's implementation. This |
| 1752 | // includes synthesized ivars. |
| 1753 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | b170ca5 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1754 | count += ImplDecl->ivar_size(); |
| 1755 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1756 | return count; |
| 1757 | } |
| 1758 | |
Argyrios Kyrtzidis | 8deabc1 | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 1759 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 1760 | if (!E) |
| 1761 | return false; |
| 1762 | |
| 1763 | // nullptr_t is always treated as null. |
| 1764 | if (E->getType()->isNullPtrType()) return true; |
| 1765 | |
| 1766 | if (E->getType()->isAnyPointerType() && |
| 1767 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 1768 | Expr::NPC_ValueDependentIsNull)) |
| 1769 | return true; |
| 1770 | |
| 1771 | // Unfortunately, __null has type 'int'. |
| 1772 | if (isa<GNUNullExpr>(E)) return true; |
| 1773 | |
| 1774 | return false; |
| 1775 | } |
| 1776 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1777 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1778 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1779 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1780 | I = ObjCImpls.find(D); |
| 1781 | if (I != ObjCImpls.end()) |
| 1782 | return cast<ObjCImplementationDecl>(I->second); |
| 1783 | return 0; |
| 1784 | } |
| 1785 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1786 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1787 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1788 | I = ObjCImpls.find(D); |
| 1789 | if (I != ObjCImpls.end()) |
| 1790 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
| 1794 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1795 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1796 | ObjCImplementationDecl *ImplD) { |
| 1797 | assert(IFaceD && ImplD && "Passed null params"); |
| 1798 | ObjCImpls[IFaceD] = ImplD; |
| 1799 | } |
| 1800 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1801 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1802 | ObjCCategoryImplDecl *ImplD) { |
| 1803 | assert(CatD && ImplD && "Passed null params"); |
| 1804 | ObjCImpls[CatD] = ImplD; |
| 1805 | } |
| 1806 | |
Argyrios Kyrtzidis | 87ec9c2 | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1807 | ObjCInterfaceDecl *ASTContext::getObjContainingInterface(NamedDecl *ND) const { |
| 1808 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
| 1809 | return ID; |
| 1810 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
| 1811 | return CD->getClassInterface(); |
| 1812 | if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
| 1813 | return IMD->getClassInterface(); |
| 1814 | |
| 1815 | return 0; |
| 1816 | } |
| 1817 | |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1818 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1819 | /// none exists. |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1820 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1821 | assert(VD && "Passed null params"); |
| 1822 | assert(VD->hasAttr<BlocksAttr>() && |
| 1823 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1824 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1825 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1826 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1827 | } |
| 1828 | |
| 1829 | /// \brief Set the copy inialization expression of a block var decl. |
| 1830 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1831 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | d016ec2 | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1832 | assert(VD->hasAttr<BlocksAttr>() && |
| 1833 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 1ceee5c | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1834 | BlockVarCopyInits[VD] = Init; |
| 1835 | } |
| 1836 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1837 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1838 | unsigned DataSize) const { |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1839 | if (!DataSize) |
| 1840 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1841 | else |
| 1842 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1843 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1844 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1845 | TypeSourceInfo *TInfo = |
| 1846 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1847 | new (TInfo) TypeSourceInfo(T); |
| 1848 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1851 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1852 | SourceLocation L) const { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1853 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1854 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1855 | return DI; |
| 1856 | } |
| 1857 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1858 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1859 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1860 | return getObjCLayout(D, 0); |
| 1861 | } |
| 1862 | |
| 1863 | const ASTRecordLayout & |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1864 | ASTContext::getASTObjCImplementationLayout( |
| 1865 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1866 | return getObjCLayout(D->getClassInterface(), D); |
| 1867 | } |
| 1868 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1869 | //===----------------------------------------------------------------------===// |
| 1870 | // Type creation/memoization methods |
| 1871 | //===----------------------------------------------------------------------===// |
| 1872 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1873 | QualType |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1874 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1875 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1876 | quals.removeFastQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1877 | |
| 1878 | // Check if we've already instantiated this type. |
| 1879 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1880 | ExtQuals::Profile(ID, baseType, quals); |
| 1881 | void *insertPos = 0; |
| 1882 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1883 | assert(eq->getQualifiers() == quals); |
| 1884 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1887 | // If the base type is not canonical, make the appropriate canonical type. |
| 1888 | QualType canon; |
| 1889 | if (!baseType->isCanonicalUnqualified()) { |
| 1890 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1891 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 1892 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1893 | |
| 1894 | // Re-find the insert position. |
| 1895 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1896 | } |
| 1897 | |
| 1898 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1899 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1900 | return QualType(eq, fastQuals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1903 | QualType |
| 1904 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1905 | QualType CanT = getCanonicalType(T); |
| 1906 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1907 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1908 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1909 | // If we are composing extended qualifiers together, merge together |
| 1910 | // into one ExtQuals node. |
| 1911 | QualifierCollector Quals; |
| 1912 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1913 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1914 | // If this type already has an address space specified, it cannot get |
| 1915 | // another one. |
| 1916 | assert(!Quals.hasAddressSpace() && |
| 1917 | "Type cannot be in multiple addr spaces!"); |
| 1918 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1920 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1923 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1924 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1925 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1926 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1927 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1929 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1930 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1931 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1932 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1933 | return getPointerType(ResultType); |
| 1934 | } |
| 1935 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1937 | // If we are composing extended qualifiers together, merge together |
| 1938 | // into one ExtQuals node. |
| 1939 | QualifierCollector Quals; |
| 1940 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1941 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1942 | // If this type already has an ObjCGC specified, it cannot get |
| 1943 | // another one. |
| 1944 | assert(!Quals.hasObjCGCAttr() && |
| 1945 | "Type cannot have multiple ObjCGCs!"); |
| 1946 | Quals.addObjCGCAttr(GCAttr); |
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 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1949 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1950 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1951 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1952 | FunctionType::ExtInfo Info) { |
| 1953 | if (T->getExtInfo() == Info) |
| 1954 | return T; |
| 1955 | |
| 1956 | QualType Result; |
| 1957 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1958 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1959 | } else { |
| 1960 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1961 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1962 | EPI.ExtInfo = Info; |
| 1963 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1964 | FPT->getNumArgs(), EPI); |
| 1965 | } |
| 1966 | |
| 1967 | return cast<FunctionType>(Result.getTypePtr()); |
| 1968 | } |
| 1969 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1970 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1971 | /// number with the specified element type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1972 | QualType ASTContext::getComplexType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1973 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1974 | // structure. |
| 1975 | llvm::FoldingSetNodeID ID; |
| 1976 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1978 | void *InsertPos = 0; |
| 1979 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1980 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1982 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1983 | // so fill in the canonical type field. |
| 1984 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1985 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1986 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1988 | // Get the new insert position for the node we care about. |
| 1989 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1990 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1991 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1992 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1993 | Types.push_back(New); |
| 1994 | ComplexTypes.InsertNode(New, InsertPos); |
| 1995 | return QualType(New, 0); |
| 1996 | } |
| 1997 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1998 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1999 | /// the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2000 | QualType ASTContext::getPointerType(QualType T) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2001 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2002 | // structure. |
| 2003 | llvm::FoldingSetNodeID ID; |
| 2004 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2006 | void *InsertPos = 0; |
| 2007 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2008 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2010 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2011 | // so fill in the canonical type field. |
| 2012 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2013 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2014 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2015 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2016 | // Get the new insert position for the node we care about. |
| 2017 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2018 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2019 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2020 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2021 | Types.push_back(New); |
| 2022 | PointerTypes.InsertNode(New, InsertPos); |
| 2023 | return QualType(New, 0); |
| 2024 | } |
| 2025 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2026 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2027 | /// a pointer to the specified block. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2028 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2029 | assert(T->isFunctionType() && "block of function types only"); |
| 2030 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2031 | // structure. |
| 2032 | llvm::FoldingSetNodeID ID; |
| 2033 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2035 | void *InsertPos = 0; |
| 2036 | if (BlockPointerType *PT = |
| 2037 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2038 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2039 | |
| 2040 | // 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] | 2041 | // type either so fill in the canonical type field. |
| 2042 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2043 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2044 | Canonical = getBlockPointerType(getCanonicalType(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 | // Get the new insert position for the node we care about. |
| 2047 | BlockPointerType *NewIP = |
| 2048 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2049 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2050 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2051 | BlockPointerType *New |
| 2052 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2053 | Types.push_back(New); |
| 2054 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 2055 | return QualType(New, 0); |
| 2056 | } |
| 2057 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2058 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 2059 | /// lvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2060 | QualType |
| 2061 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 2062 | assert(getCanonicalType(T) != OverloadTy && |
| 2063 | "Unresolved overloaded function type"); |
| 2064 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2065 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2066 | // structure. |
| 2067 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2068 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2069 | |
| 2070 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2071 | if (LValueReferenceType *RT = |
| 2072 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2073 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2074 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2075 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2076 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2077 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2078 | // either, so fill in the canonical type field. |
| 2079 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2080 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 2081 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2082 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2083 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2084 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2085 | LValueReferenceType *NewIP = |
| 2086 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2087 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2090 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2091 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 2092 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2093 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2094 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2095 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2096 | return QualType(New, 0); |
| 2097 | } |
| 2098 | |
| 2099 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 2100 | /// rvalue reference to the specified type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2101 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2102 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2103 | // structure. |
| 2104 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2105 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2106 | |
| 2107 | void *InsertPos = 0; |
| 2108 | if (RValueReferenceType *RT = |
| 2109 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2110 | return QualType(RT, 0); |
| 2111 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2112 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2113 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2114 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2115 | // either, so fill in the canonical type field. |
| 2116 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2117 | if (InnerRef || !T.isCanonical()) { |
| 2118 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2119 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2120 | |
| 2121 | // Get the new insert position for the node we care about. |
| 2122 | RValueReferenceType *NewIP = |
| 2123 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2124 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2127 | RValueReferenceType *New |
| 2128 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2129 | Types.push_back(New); |
| 2130 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2131 | return QualType(New, 0); |
| 2132 | } |
| 2133 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2134 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 2135 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2136 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2137 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2138 | // structure. |
| 2139 | llvm::FoldingSetNodeID ID; |
| 2140 | MemberPointerType::Profile(ID, T, Cls); |
| 2141 | |
| 2142 | void *InsertPos = 0; |
| 2143 | if (MemberPointerType *PT = |
| 2144 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2145 | return QualType(PT, 0); |
| 2146 | |
| 2147 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 2148 | // type either, so fill in the canonical type field. |
| 2149 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2150 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2151 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 2152 | |
| 2153 | // Get the new insert position for the node we care about. |
| 2154 | MemberPointerType *NewIP = |
| 2155 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2156 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2157 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2158 | MemberPointerType *New |
| 2159 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2160 | Types.push_back(New); |
| 2161 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 2162 | return QualType(New, 0); |
| 2163 | } |
| 2164 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2166 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2167 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2168 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2169 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2170 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 2171 | assert((EltTy->isDependentType() || |
| 2172 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 2173 | "Constant array of VLAs is illegal!"); |
| 2174 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2175 | // Convert the array size into a canonical width matching the pointer size for |
| 2176 | // the target. |
| 2177 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2178 | ArySize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2179 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2180 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2181 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2182 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2183 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2184 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2186 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2187 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2188 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2189 | // If the element type isn't canonical or has qualifiers, this won't |
| 2190 | // be a canonical type either, so fill in the canonical type field. |
| 2191 | QualType Canon; |
| 2192 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2193 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2194 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2195 | ASM, IndexTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2196 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2197 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2198 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2200 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2201 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2202 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2204 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2205 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2206 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2207 | Types.push_back(New); |
| 2208 | return QualType(New, 0); |
| 2209 | } |
| 2210 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2211 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 2212 | /// variably-modified, into the corresponding type with all the known |
| 2213 | /// sizes replaced with [*]. |
| 2214 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 2215 | // Vastly most common case. |
| 2216 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2217 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2218 | QualType result; |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2219 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2220 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2221 | const Type *ty = split.Ty; |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2222 | switch (ty->getTypeClass()) { |
| 2223 | #define TYPE(Class, Base) |
| 2224 | #define ABSTRACT_TYPE(Class, Base) |
| 2225 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2226 | #include "clang/AST/TypeNodes.def" |
| 2227 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 2228 | |
| 2229 | // These types should never be variably-modified. |
| 2230 | case Type::Builtin: |
| 2231 | case Type::Complex: |
| 2232 | case Type::Vector: |
| 2233 | case Type::ExtVector: |
| 2234 | case Type::DependentSizedExtVector: |
| 2235 | case Type::ObjCObject: |
| 2236 | case Type::ObjCInterface: |
| 2237 | case Type::ObjCObjectPointer: |
| 2238 | case Type::Record: |
| 2239 | case Type::Enum: |
| 2240 | case Type::UnresolvedUsing: |
| 2241 | case Type::TypeOfExpr: |
| 2242 | case Type::TypeOf: |
| 2243 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2244 | case Type::UnaryTransform: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2245 | case Type::DependentName: |
| 2246 | case Type::InjectedClassName: |
| 2247 | case Type::TemplateSpecialization: |
| 2248 | case Type::DependentTemplateSpecialization: |
| 2249 | case Type::TemplateTypeParm: |
| 2250 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2251 | case Type::Auto: |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2252 | case Type::PackExpansion: |
| 2253 | llvm_unreachable("type should never be variably-modified"); |
| 2254 | |
| 2255 | // These types can be variably-modified but should never need to |
| 2256 | // further decay. |
| 2257 | case Type::FunctionNoProto: |
| 2258 | case Type::FunctionProto: |
| 2259 | case Type::BlockPointer: |
| 2260 | case Type::MemberPointer: |
| 2261 | return type; |
| 2262 | |
| 2263 | // These types can be variably-modified. All these modifications |
| 2264 | // preserve structure except as noted by comments. |
| 2265 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 2266 | // optimizations available here. |
| 2267 | case Type::Pointer: |
| 2268 | result = getPointerType(getVariableArrayDecayedType( |
| 2269 | cast<PointerType>(ty)->getPointeeType())); |
| 2270 | break; |
| 2271 | |
| 2272 | case Type::LValueReference: { |
| 2273 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 2274 | result = getLValueReferenceType( |
| 2275 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 2276 | lv->isSpelledAsLValue()); |
| 2277 | break; |
| 2278 | } |
| 2279 | |
| 2280 | case Type::RValueReference: { |
| 2281 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 2282 | result = getRValueReferenceType( |
| 2283 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 2284 | break; |
| 2285 | } |
| 2286 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2287 | case Type::Atomic: { |
| 2288 | const AtomicType *at = cast<AtomicType>(ty); |
| 2289 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 2290 | break; |
| 2291 | } |
| 2292 | |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2293 | case Type::ConstantArray: { |
| 2294 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 2295 | result = getConstantArrayType( |
| 2296 | getVariableArrayDecayedType(cat->getElementType()), |
| 2297 | cat->getSize(), |
| 2298 | cat->getSizeModifier(), |
| 2299 | cat->getIndexTypeCVRQualifiers()); |
| 2300 | break; |
| 2301 | } |
| 2302 | |
| 2303 | case Type::DependentSizedArray: { |
| 2304 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 2305 | result = getDependentSizedArrayType( |
| 2306 | getVariableArrayDecayedType(dat->getElementType()), |
| 2307 | dat->getSizeExpr(), |
| 2308 | dat->getSizeModifier(), |
| 2309 | dat->getIndexTypeCVRQualifiers(), |
| 2310 | dat->getBracketsRange()); |
| 2311 | break; |
| 2312 | } |
| 2313 | |
| 2314 | // Turn incomplete types into [*] types. |
| 2315 | case Type::IncompleteArray: { |
| 2316 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 2317 | result = getVariableArrayType( |
| 2318 | getVariableArrayDecayedType(iat->getElementType()), |
| 2319 | /*size*/ 0, |
| 2320 | ArrayType::Normal, |
| 2321 | iat->getIndexTypeCVRQualifiers(), |
| 2322 | SourceRange()); |
| 2323 | break; |
| 2324 | } |
| 2325 | |
| 2326 | // Turn VLA types into [*] types. |
| 2327 | case Type::VariableArray: { |
| 2328 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 2329 | result = getVariableArrayType( |
| 2330 | getVariableArrayDecayedType(vat->getElementType()), |
| 2331 | /*size*/ 0, |
| 2332 | ArrayType::Star, |
| 2333 | vat->getIndexTypeCVRQualifiers(), |
| 2334 | vat->getBracketsRange()); |
| 2335 | break; |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | // Apply the top-level qualifiers from the original. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2340 | return getQualifiedType(result, split.Quals); |
John McCall | ce88903 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2341 | } |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2342 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 2343 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 2344 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2345 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 2346 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2347 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2348 | unsigned IndexTypeQuals, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2349 | SourceRange Brackets) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2350 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 2351 | // that have an expression provided for their size. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2352 | QualType Canon; |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2353 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2354 | // Be sure to pull qualifiers off the element type. |
| 2355 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2356 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2357 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2358 | IndexTypeQuals, Brackets); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2359 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 715e9c8 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2362 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2363 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2364 | |
| 2365 | VariableArrayTypes.push_back(New); |
| 2366 | Types.push_back(New); |
| 2367 | return QualType(New, 0); |
| 2368 | } |
| 2369 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2370 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 2371 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 2372 | /// type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2373 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 2374 | Expr *numElements, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2375 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2376 | unsigned elementTypeQuals, |
| 2377 | SourceRange brackets) const { |
| 2378 | assert((!numElements || numElements->isTypeDependent() || |
| 2379 | numElements->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2380 | "Size must be type- or value-dependent!"); |
| 2381 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2382 | // Dependently-sized array types that do not have a specified number |
| 2383 | // of elements will have their sizes deduced from a dependent |
| 2384 | // initializer. We do no canonicalization here at all, which is okay |
| 2385 | // because they can't be used in most locations. |
| 2386 | if (!numElements) { |
| 2387 | DependentSizedArrayType *newType |
| 2388 | = new (*this, TypeAlignment) |
| 2389 | DependentSizedArrayType(*this, elementType, QualType(), |
| 2390 | numElements, ASM, elementTypeQuals, |
| 2391 | brackets); |
| 2392 | Types.push_back(newType); |
| 2393 | return QualType(newType, 0); |
| 2394 | } |
| 2395 | |
| 2396 | // Otherwise, we actually build a new type every time, but we |
| 2397 | // also build a canonical type. |
| 2398 | |
| 2399 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 2400 | |
| 2401 | void *insertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2402 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2403 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2404 | QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2405 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2406 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2407 | // Look for an existing type with these properties. |
| 2408 | DependentSizedArrayType *canonTy = |
| 2409 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2410 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2411 | // If we don't have one, build one. |
| 2412 | if (!canonTy) { |
| 2413 | canonTy = new (*this, TypeAlignment) |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2414 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2415 | QualType(), numElements, ASM, elementTypeQuals, |
| 2416 | brackets); |
| 2417 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 2418 | Types.push_back(canonTy); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2421 | // Apply qualifiers from the element type to the array. |
| 2422 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2423 | canonElementType.Quals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2424 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2425 | // If we didn't need extra canonicalization for the element type, |
| 2426 | // then just use that as our result. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2427 | if (QualType(canonElementType.Ty, 0) == elementType) |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2428 | return canon; |
| 2429 | |
| 2430 | // Otherwise, we need to build a type which follows the spelling |
| 2431 | // of the element type. |
| 2432 | DependentSizedArrayType *sugaredType |
| 2433 | = new (*this, TypeAlignment) |
| 2434 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 2435 | ASM, elementTypeQuals, brackets); |
| 2436 | Types.push_back(sugaredType); |
| 2437 | return QualType(sugaredType, 0); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2440 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2441 | ArrayType::ArraySizeModifier ASM, |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2442 | unsigned elementTypeQuals) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2443 | llvm::FoldingSetNodeID ID; |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2444 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2445 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2446 | void *insertPos = 0; |
| 2447 | if (IncompleteArrayType *iat = |
| 2448 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 2449 | return QualType(iat, 0); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2450 | |
| 2451 | // 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] | 2452 | // either, so fill in the canonical type field. We also have to pull |
| 2453 | // qualifiers off the element type. |
| 2454 | QualType canon; |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2455 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2456 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 2457 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2458 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2459 | ASM, elementTypeQuals); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2460 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2461 | |
| 2462 | // Get the new insert position for the node we care about. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2463 | IncompleteArrayType *existing = |
| 2464 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 2465 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 2466 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2467 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2468 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 2469 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2470 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2471 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 2472 | Types.push_back(newType); |
| 2473 | return QualType(newType, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2476 | /// getVectorType - Return the unique reference to a vector type of |
| 2477 | /// 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] | 2478 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2479 | VectorType::VectorKind VecKind) const { |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2480 | assert(vecType->isBuiltinType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2482 | // Check if we've already instantiated a vector of this type. |
| 2483 | llvm::FoldingSetNodeID ID; |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2484 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2486 | void *InsertPos = 0; |
| 2487 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2488 | return QualType(VTP, 0); |
| 2489 | |
| 2490 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2491 | // so fill in the canonical type field. |
| 2492 | QualType Canonical; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2493 | if (!vecType.isCanonical()) { |
Bob Wilson | 231da7e | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2494 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2495 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2496 | // Get the new insert position for the node we care about. |
| 2497 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2498 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2499 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2500 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2501 | VectorType(vecType, NumElts, Canonical, VecKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2502 | VectorTypes.InsertNode(New, InsertPos); |
| 2503 | Types.push_back(New); |
| 2504 | return QualType(New, 0); |
| 2505 | } |
| 2506 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2507 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2508 | /// 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] | 2509 | QualType |
| 2510 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2511 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2512 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2513 | // Check if we've already instantiated a vector of this type. |
| 2514 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2515 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2516 | VectorType::GenericVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2517 | void *InsertPos = 0; |
| 2518 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2519 | return QualType(VTP, 0); |
| 2520 | |
| 2521 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2522 | // so fill in the canonical type field. |
| 2523 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2524 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2525 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2527 | // Get the new insert position for the node we care about. |
| 2528 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2529 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2530 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2531 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2532 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2533 | VectorTypes.InsertNode(New, InsertPos); |
| 2534 | Types.push_back(New); |
| 2535 | return QualType(New, 0); |
| 2536 | } |
| 2537 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2538 | QualType |
| 2539 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2540 | Expr *SizeExpr, |
| 2541 | SourceLocation AttrLoc) const { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2542 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2543 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2544 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2545 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2546 | void *InsertPos = 0; |
| 2547 | DependentSizedExtVectorType *Canon |
| 2548 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2549 | DependentSizedExtVectorType *New; |
| 2550 | if (Canon) { |
| 2551 | // We already have a canonical version of this array type; use it as |
| 2552 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2553 | New = new (*this, TypeAlignment) |
| 2554 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2555 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2556 | } else { |
| 2557 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2558 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2559 | New = new (*this, TypeAlignment) |
| 2560 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2561 | AttrLoc); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2562 | |
| 2563 | DependentSizedExtVectorType *CanonCheck |
| 2564 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2565 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2566 | (void)CanonCheck; |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2567 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2568 | } else { |
| 2569 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2570 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2571 | New = new (*this, TypeAlignment) |
| 2572 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2573 | } |
| 2574 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2576 | Types.push_back(New); |
| 2577 | return QualType(New, 0); |
| 2578 | } |
| 2579 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2580 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2581 | /// |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2582 | QualType |
| 2583 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2584 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2585 | const CallingConv DefaultCC = Info.getCC(); |
| 2586 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2587 | CC_X86StdCall : DefaultCC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2588 | // Unique functions, to guarantee there is only one function of a particular |
| 2589 | // structure. |
| 2590 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2591 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2593 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2594 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2595 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2596 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2597 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2598 | QualType Canonical; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2599 | if (!ResultTy.isCanonical() || |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2600 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2601 | Canonical = |
| 2602 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2603 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2604 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2605 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2606 | FunctionNoProtoType *NewIP = |
| 2607 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2608 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2609 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2610 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2611 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2612 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2613 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2614 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2615 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2616 | return QualType(New, 0); |
| 2617 | } |
| 2618 | |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2619 | /// \brief Determine whether \p T is canonical as the result type of a function. |
| 2620 | static bool isCanonicalResultType(QualType T) { |
| 2621 | return T.isCanonical() && |
| 2622 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
| 2623 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
| 2624 | } |
| 2625 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2626 | /// getFunctionType - Return a normal function type with a typed argument |
| 2627 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2628 | QualType |
| 2629 | ASTContext::getFunctionType(QualType ResultTy, |
| 2630 | const QualType *ArgArray, unsigned NumArgs, |
| 2631 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2632 | // Unique functions, to guarantee there is only one function of a particular |
| 2633 | // structure. |
| 2634 | llvm::FoldingSetNodeID ID; |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2635 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2636 | |
| 2637 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2638 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2639 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2640 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2641 | |
| 2642 | // Determine whether the type being created is already canonical or not. |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2643 | bool isCanonical = |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2644 | EPI.ExceptionSpecType == EST_None && isCanonicalResultType(ResultTy) && |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2645 | !EPI.HasTrailingReturn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2646 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2647 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2648 | isCanonical = false; |
| 2649 | |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2650 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2651 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2652 | CC_X86StdCall : DefaultCC; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2653 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2654 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2655 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2656 | QualType Canonical; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2657 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2658 | SmallVector<QualType, 16> CanonicalArgs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2659 | CanonicalArgs.reserve(NumArgs); |
| 2660 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2661 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2662 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2663 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2664 | CanonicalEPI.HasTrailingReturn = false; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2665 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2666 | CanonicalEPI.NumExceptions = 0; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2667 | CanonicalEPI.ExtInfo |
| 2668 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2669 | |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2670 | // Result types do not have ARC lifetime qualifiers. |
| 2671 | QualType CanResultTy = getCanonicalType(ResultTy); |
| 2672 | if (ResultTy.getQualifiers().hasObjCLifetime()) { |
| 2673 | Qualifiers Qs = CanResultTy.getQualifiers(); |
| 2674 | Qs.removeObjCLifetime(); |
| 2675 | CanResultTy = getQualifiedType(CanResultTy.getUnqualifiedType(), Qs); |
| 2676 | } |
| 2677 | |
| 2678 | Canonical = getFunctionType(CanResultTy, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2679 | CanonicalArgs.data(), NumArgs, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2680 | CanonicalEPI); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2681 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2682 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2683 | FunctionProtoType *NewIP = |
| 2684 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2685 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2686 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2687 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2688 | // FunctionProtoType objects are allocated with extra bytes after |
| 2689 | // them for three variable size arrays at the end: |
| 2690 | // - parameter types |
| 2691 | // - exception types |
| 2692 | // - consumed-arguments flags |
| 2693 | // Instead of the exception types, there could be a noexcept |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2694 | // expression, or information used to resolve the exception |
| 2695 | // specification. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2696 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2697 | NumArgs * sizeof(QualType); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2698 | if (EPI.ExceptionSpecType == EST_Dynamic) { |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2699 | Size += EPI.NumExceptions * sizeof(QualType); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2700 | } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2701 | Size += sizeof(Expr*); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2702 | } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2703 | Size += 2 * sizeof(FunctionDecl*); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2704 | } else if (EPI.ExceptionSpecType == EST_Unevaluated) { |
| 2705 | Size += sizeof(FunctionDecl*); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2706 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2707 | if (EPI.ConsumedArguments) |
| 2708 | Size += NumArgs * sizeof(bool); |
| 2709 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2710 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | cfe9af2 | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2711 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2712 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2713 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2714 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2715 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2716 | return QualType(FTP, 0); |
| 2717 | } |
| 2718 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2719 | #ifndef NDEBUG |
| 2720 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2721 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2722 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2723 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2724 | return true; |
| 2725 | if (RD->getDescribedClassTemplate() && |
| 2726 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2727 | return true; |
| 2728 | return false; |
| 2729 | } |
| 2730 | #endif |
| 2731 | |
| 2732 | /// getInjectedClassNameType - Return the unique reference to the |
| 2733 | /// injected class name type for the specified templated declaration. |
| 2734 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2735 | QualType TST) const { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2736 | assert(NeedsInjectedClassNameType(Decl)); |
| 2737 | if (Decl->TypeForDecl) { |
| 2738 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2739 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2740 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2741 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2742 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2743 | } else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2744 | Type *newType = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2745 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2746 | Decl->TypeForDecl = newType; |
| 2747 | Types.push_back(newType); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2748 | } |
| 2749 | return QualType(Decl->TypeForDecl, 0); |
| 2750 | } |
| 2751 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2752 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2753 | /// specified type declaration. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2754 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2755 | assert(Decl && "Passed null for Decl param"); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2756 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2757 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2758 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2759 | return getTypedefType(Typedef); |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2760 | |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2761 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2762 | "Template type parameter types are always available."); |
| 2763 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2764 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2765 | assert(!Record->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2766 | "struct/union has previous declaration"); |
| 2767 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2768 | return getRecordType(Record); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2769 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2770 | assert(!Enum->getPreviousDecl() && |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2771 | "enum has previous declaration"); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2772 | return getEnumType(Enum); |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2773 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2774 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2775 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2776 | Decl->TypeForDecl = newType; |
| 2777 | Types.push_back(newType); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2778 | } else |
John McCall | becb8d5 | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2779 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2780 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2781 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2784 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2785 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2786 | QualType |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2787 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2788 | QualType Canonical) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2789 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2791 | if (Canonical.isNull()) |
| 2792 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2793 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2794 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2795 | Decl->TypeForDecl = newType; |
| 2796 | Types.push_back(newType); |
| 2797 | return QualType(newType, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2800 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2801 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2802 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2803 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2804 | if (PrevDecl->TypeForDecl) |
| 2805 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2806 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2807 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2808 | Decl->TypeForDecl = newType; |
| 2809 | Types.push_back(newType); |
| 2810 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2813 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2814 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2815 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2816 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2817 | if (PrevDecl->TypeForDecl) |
| 2818 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2819 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2820 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2821 | Decl->TypeForDecl = newType; |
| 2822 | Types.push_back(newType); |
| 2823 | return QualType(newType, 0); |
Argyrios Kyrtzidis | 400f512 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2826 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2827 | QualType modifiedType, |
| 2828 | QualType equivalentType) { |
| 2829 | llvm::FoldingSetNodeID id; |
| 2830 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2831 | |
| 2832 | void *insertPos = 0; |
| 2833 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2834 | if (type) return QualType(type, 0); |
| 2835 | |
| 2836 | QualType canon = getCanonicalType(equivalentType); |
| 2837 | type = new (*this, TypeAlignment) |
| 2838 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2839 | |
| 2840 | Types.push_back(type); |
| 2841 | AttributedTypes.InsertNode(type, insertPos); |
| 2842 | |
| 2843 | return QualType(type, 0); |
| 2844 | } |
| 2845 | |
| 2846 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2847 | /// \brief Retrieve a substitution-result type. |
| 2848 | QualType |
| 2849 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2850 | QualType Replacement) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2851 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2852 | && "replacement types must always be canonical"); |
| 2853 | |
| 2854 | llvm::FoldingSetNodeID ID; |
| 2855 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2856 | void *InsertPos = 0; |
| 2857 | SubstTemplateTypeParmType *SubstParm |
| 2858 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2859 | |
| 2860 | if (!SubstParm) { |
| 2861 | SubstParm = new (*this, TypeAlignment) |
| 2862 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2863 | Types.push_back(SubstParm); |
| 2864 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2865 | } |
| 2866 | |
| 2867 | return QualType(SubstParm, 0); |
| 2868 | } |
| 2869 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2870 | /// \brief Retrieve a |
| 2871 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2872 | const TemplateTypeParmType *Parm, |
| 2873 | const TemplateArgument &ArgPack) { |
| 2874 | #ifndef NDEBUG |
| 2875 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2876 | PEnd = ArgPack.pack_end(); |
| 2877 | P != PEnd; ++P) { |
| 2878 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2879 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2880 | } |
| 2881 | #endif |
| 2882 | |
| 2883 | llvm::FoldingSetNodeID ID; |
| 2884 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2885 | void *InsertPos = 0; |
| 2886 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2887 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2888 | return QualType(SubstParm, 0); |
| 2889 | |
| 2890 | QualType Canon; |
| 2891 | if (!Parm->isCanonicalUnqualified()) { |
| 2892 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2893 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2894 | ArgPack); |
| 2895 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2896 | } |
| 2897 | |
| 2898 | SubstTemplateTypeParmPackType *SubstParm |
| 2899 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2900 | ArgPack); |
| 2901 | Types.push_back(SubstParm); |
| 2902 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2903 | return QualType(SubstParm, 0); |
| 2904 | } |
| 2905 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2906 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2908 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2909 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2910 | bool ParameterPack, |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2911 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2912 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2913 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2914 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2915 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2916 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2917 | |
| 2918 | if (TypeParm) |
| 2919 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2920 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2921 | if (TTPDecl) { |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2922 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2923 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2924 | |
| 2925 | TemplateTypeParmType *TypeCheck |
| 2926 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2927 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2928 | (void)TypeCheck; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2929 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2930 | TypeParm = new (*this, TypeAlignment) |
| 2931 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2932 | |
| 2933 | Types.push_back(TypeParm); |
| 2934 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2935 | |
| 2936 | return QualType(TypeParm, 0); |
| 2937 | } |
| 2938 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2939 | TypeSourceInfo * |
| 2940 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2941 | SourceLocation NameLoc, |
| 2942 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2943 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2944 | assert(!Name.getAsDependentTemplateName() && |
| 2945 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2946 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2947 | |
| 2948 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2949 | TemplateSpecializationTypeLoc TL |
| 2950 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2951 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2952 | TL.setTemplateNameLoc(NameLoc); |
| 2953 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2954 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2955 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2956 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2957 | return DI; |
| 2958 | } |
| 2959 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2960 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2961 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2962 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2963 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2964 | assert(!Template.getAsDependentTemplateName() && |
| 2965 | "No dependent template names here!"); |
| 2966 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2967 | unsigned NumArgs = Args.size(); |
| 2968 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2969 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2970 | ArgVec.reserve(NumArgs); |
| 2971 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2972 | ArgVec.push_back(Args[i].getArgument()); |
| 2973 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2974 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2975 | Underlying); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2978 | #ifndef NDEBUG |
| 2979 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 2980 | unsigned NumArgs) { |
| 2981 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2982 | if (Args[I].isPackExpansion()) |
| 2983 | return true; |
| 2984 | |
| 2985 | return true; |
| 2986 | } |
| 2987 | #endif |
| 2988 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2989 | QualType |
| 2990 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2991 | const TemplateArgument *Args, |
| 2992 | unsigned NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2993 | QualType Underlying) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2994 | assert(!Template.getAsDependentTemplateName() && |
| 2995 | "No dependent template names here!"); |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2996 | // Look through qualified template names. |
| 2997 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2998 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2999 | |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3000 | bool IsTypeAlias = |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3001 | Template.getAsTemplateDecl() && |
| 3002 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3003 | QualType CanonType; |
| 3004 | if (!Underlying.isNull()) |
| 3005 | CanonType = getCanonicalType(Underlying); |
| 3006 | else { |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3007 | // We can get here with an alias template when the specialization contains |
| 3008 | // a pack expansion that does not match up with a parameter pack. |
| 3009 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 3010 | "Caller must compute aliased type"); |
| 3011 | IsTypeAlias = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3012 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 3013 | NumArgs); |
| 3014 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3015 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3016 | // Allocate the (non-canonical) template specialization type, but don't |
| 3017 | // try to unique it: these types typically have location information that |
| 3018 | // we don't unique and don't want to lose. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3019 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 3020 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3021 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3022 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | TemplateSpecializationType *Spec |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3024 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 3025 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3026 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3027 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3028 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3031 | QualType |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3032 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 3033 | const TemplateArgument *Args, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3034 | unsigned NumArgs) const { |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3035 | assert(!Template.getAsDependentTemplateName() && |
| 3036 | "No dependent template names here!"); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3037 | |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3038 | // Look through qualified template names. |
| 3039 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3040 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3041 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3042 | // Build the canonical template specialization type. |
| 3043 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3044 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3045 | CanonArgs.reserve(NumArgs); |
| 3046 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3047 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 3048 | |
| 3049 | // Determine whether this canonical template specialization type already |
| 3050 | // exists. |
| 3051 | llvm::FoldingSetNodeID ID; |
| 3052 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 3053 | CanonArgs.data(), NumArgs, *this); |
| 3054 | |
| 3055 | void *InsertPos = 0; |
| 3056 | TemplateSpecializationType *Spec |
| 3057 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3058 | |
| 3059 | if (!Spec) { |
| 3060 | // Allocate a new canonical template specialization type. |
| 3061 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 3062 | sizeof(TemplateArgument) * NumArgs), |
| 3063 | TypeAlignment); |
| 3064 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 3065 | CanonArgs.data(), NumArgs, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3066 | QualType(), QualType()); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3067 | Types.push_back(Spec); |
| 3068 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 3069 | } |
| 3070 | |
| 3071 | assert(Spec->isDependentType() && |
| 3072 | "Non-dependent template-id type must have a canonical type"); |
| 3073 | return QualType(Spec, 0); |
| 3074 | } |
| 3075 | |
| 3076 | QualType |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3077 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 3078 | NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3079 | QualType NamedType) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3080 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3081 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3082 | |
| 3083 | void *InsertPos = 0; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3084 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3085 | if (T) |
| 3086 | return QualType(T, 0); |
| 3087 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3088 | QualType Canon = NamedType; |
| 3089 | if (!Canon.isCanonical()) { |
| 3090 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3091 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3092 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3093 | (void)CheckT; |
| 3094 | } |
| 3095 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3096 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3097 | Types.push_back(T); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3098 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3099 | return QualType(T, 0); |
| 3100 | } |
| 3101 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3102 | QualType |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3103 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3104 | llvm::FoldingSetNodeID ID; |
| 3105 | ParenType::Profile(ID, InnerType); |
| 3106 | |
| 3107 | void *InsertPos = 0; |
| 3108 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3109 | if (T) |
| 3110 | return QualType(T, 0); |
| 3111 | |
| 3112 | QualType Canon = InnerType; |
| 3113 | if (!Canon.isCanonical()) { |
| 3114 | Canon = getCanonicalType(InnerType); |
| 3115 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3116 | assert(!CheckT && "Paren canonical type broken"); |
| 3117 | (void)CheckT; |
| 3118 | } |
| 3119 | |
| 3120 | T = new (*this) ParenType(InnerType, Canon); |
| 3121 | Types.push_back(T); |
| 3122 | ParenTypes.InsertNode(T, InsertPos); |
| 3123 | return QualType(T, 0); |
| 3124 | } |
| 3125 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3126 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 3127 | NestedNameSpecifier *NNS, |
| 3128 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3129 | QualType Canon) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3130 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 3131 | |
| 3132 | if (Canon.isNull()) { |
| 3133 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3134 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3135 | if (Keyword == ETK_None) |
| 3136 | CanonKeyword = ETK_Typename; |
| 3137 | |
| 3138 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 3139 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3143 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3144 | |
| 3145 | void *InsertPos = 0; |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3146 | DependentNameType *T |
| 3147 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3148 | if (T) |
| 3149 | return QualType(T, 0); |
| 3150 | |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3151 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3152 | Types.push_back(T); |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3153 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3154 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3157 | QualType |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3158 | ASTContext::getDependentTemplateSpecializationType( |
| 3159 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3160 | NestedNameSpecifier *NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3161 | const IdentifierInfo *Name, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3162 | const TemplateArgumentListInfo &Args) const { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3163 | // TODO: avoid this copy |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3164 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3165 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 3166 | ArgCopy.push_back(Args[I].getArgument()); |
| 3167 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 3168 | ArgCopy.size(), |
| 3169 | ArgCopy.data()); |
| 3170 | } |
| 3171 | |
| 3172 | QualType |
| 3173 | ASTContext::getDependentTemplateSpecializationType( |
| 3174 | ElaboratedTypeKeyword Keyword, |
| 3175 | NestedNameSpecifier *NNS, |
| 3176 | const IdentifierInfo *Name, |
| 3177 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3178 | const TemplateArgument *Args) const { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3179 | assert((!NNS || NNS->isDependent()) && |
| 3180 | "nested-name-specifier must be dependent"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3181 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3182 | llvm::FoldingSetNodeID ID; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3183 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 3184 | Name, NumArgs, Args); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3185 | |
| 3186 | void *InsertPos = 0; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3187 | DependentTemplateSpecializationType *T |
| 3188 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3189 | if (T) |
| 3190 | return QualType(T, 0); |
| 3191 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3192 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3193 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3194 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3195 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 3196 | |
| 3197 | bool AnyNonCanonArgs = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3198 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3199 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 3200 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 3201 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 3202 | AnyNonCanonArgs = true; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3205 | QualType Canon; |
| 3206 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 3207 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 3208 | Name, NumArgs, |
| 3209 | CanonArgs.data()); |
| 3210 | |
| 3211 | // Find the insert position again. |
| 3212 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3213 | } |
| 3214 | |
| 3215 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 3216 | sizeof(TemplateArgument) * NumArgs), |
| 3217 | TypeAlignment); |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 3218 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3219 | Name, NumArgs, Args, Canon); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3220 | Types.push_back(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3221 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3222 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3225 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 3226 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3227 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3228 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3229 | |
| 3230 | assert(Pattern->containsUnexpandedParameterPack() && |
| 3231 | "Pack expansions must expand one or more parameter packs"); |
| 3232 | void *InsertPos = 0; |
| 3233 | PackExpansionType *T |
| 3234 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3235 | if (T) |
| 3236 | return QualType(T, 0); |
| 3237 | |
| 3238 | QualType Canon; |
| 3239 | if (!Pattern.isCanonical()) { |
Richard Smith | d8672ef | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3240 | Canon = getCanonicalType(Pattern); |
| 3241 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 3242 | // contains an alias template specialization which ignores one of its |
| 3243 | // parameters. |
| 3244 | if (Canon->containsUnexpandedParameterPack()) { |
| 3245 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3246 | |
Richard Smith | d8672ef | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3247 | // Find the insert position again, in case we inserted an element into |
| 3248 | // PackExpansionTypes and invalidated our insert position. |
| 3249 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3250 | } |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3253 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3254 | Types.push_back(T); |
| 3255 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 3256 | return QualType(T, 0); |
| 3257 | } |
| 3258 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3259 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 3260 | /// alphabetically. |
| 3261 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 3262 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 3263 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3266 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3267 | unsigned NumProtocols) { |
| 3268 | if (NumProtocols == 0) return true; |
| 3269 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3270 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 3271 | return false; |
| 3272 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3273 | for (unsigned i = 1; i != NumProtocols; ++i) |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3274 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) || |
| 3275 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3276 | return false; |
| 3277 | return true; |
| 3278 | } |
| 3279 | |
| 3280 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3281 | unsigned &NumProtocols) { |
| 3282 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3283 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3284 | // Sort protocols, keyed by name. |
| 3285 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 3286 | |
Douglas Gregor | 61cc296 | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3287 | // Canonicalize. |
| 3288 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 3289 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 3290 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3291 | // Remove duplicates. |
| 3292 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 3293 | NumProtocols = ProtocolsEnd-Protocols; |
| 3294 | } |
| 3295 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3296 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 3297 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3298 | unsigned NumProtocols) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3299 | // If the base type is an interface and there aren't any protocols |
| 3300 | // to add, then the interface type will do just fine. |
| 3301 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 3302 | return BaseType; |
| 3303 | |
| 3304 | // Look in the folding set for an existing type. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3305 | llvm::FoldingSetNodeID ID; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3306 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3307 | void *InsertPos = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3308 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3309 | return QualType(QT, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3310 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3311 | // Build the canonical type, which has the canonical base type and |
| 3312 | // a sorted-and-uniqued list of protocols. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3313 | QualType Canonical; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3314 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 3315 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 3316 | if (!ProtocolsSorted) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3317 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3318 | Protocols + NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3319 | unsigned UniqueCount = NumProtocols; |
| 3320 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3321 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3322 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3323 | &Sorted[0], UniqueCount); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3324 | } else { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3325 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3326 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | // Regenerate InsertPos. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3330 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3331 | } |
| 3332 | |
| 3333 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 3334 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 3335 | void *Mem = Allocate(Size, TypeAlignment); |
| 3336 | ObjCObjectTypeImpl *T = |
| 3337 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 3338 | |
| 3339 | Types.push_back(T); |
| 3340 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 3341 | return QualType(T, 0); |
| 3342 | } |
| 3343 | |
| 3344 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 3345 | /// the given object type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3346 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3347 | llvm::FoldingSetNodeID ID; |
| 3348 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 3349 | |
| 3350 | void *InsertPos = 0; |
| 3351 | if (ObjCObjectPointerType *QT = |
| 3352 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3353 | return QualType(QT, 0); |
| 3354 | |
| 3355 | // Find the canonical object type. |
| 3356 | QualType Canonical; |
| 3357 | if (!ObjectT.isCanonical()) { |
| 3358 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 3359 | |
| 3360 | // Regenerate InsertPos. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3361 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3362 | } |
| 3363 | |
Douglas Gregor | fd6a088 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 3364 | // No match. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3365 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 3366 | ObjCObjectPointerType *QType = |
| 3367 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3368 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3369 | Types.push_back(QType); |
| 3370 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3371 | return QualType(QType, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3372 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3373 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3374 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 3375 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3376 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 3377 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3378 | if (Decl->TypeForDecl) |
| 3379 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3380 | |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3381 | if (PrevDecl) { |
| 3382 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 3383 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3384 | return QualType(PrevDecl->TypeForDecl, 0); |
| 3385 | } |
| 3386 | |
Douglas Gregor | 8d2dbbf | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 3387 | // Prefer the definition, if there is one. |
| 3388 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 3389 | Decl = Def; |
| 3390 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3391 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 3392 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 3393 | Decl->TypeForDecl = T; |
| 3394 | Types.push_back(T); |
| 3395 | return QualType(T, 0); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3398 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 3399 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3400 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3401 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3402 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3403 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3404 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3405 | if (tofExpr->isTypeDependent()) { |
| 3406 | llvm::FoldingSetNodeID ID; |
| 3407 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3408 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3409 | void *InsertPos = 0; |
| 3410 | DependentTypeOfExprType *Canon |
| 3411 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3412 | if (Canon) { |
| 3413 | // We already have a "canonical" version of an identical, dependent |
| 3414 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3415 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3416 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3417 | } else { |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3418 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3419 | Canon |
| 3420 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3421 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 3422 | toe = Canon; |
| 3423 | } |
| 3424 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3425 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3426 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3427 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3428 | Types.push_back(toe); |
| 3429 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3432 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 3433 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 3434 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3435 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3436 | /// on canonical type's (which are always unique). |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3437 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3438 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3439 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3440 | Types.push_back(tot); |
| 3441 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 3444 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3445 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 3446 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 3447 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3448 | /// an issue. This doesn't effect the type checker, since it operates |
David Blaikie | 39e0203 | 2011-11-06 22:28:03 +0000 | [diff] [blame] | 3449 | /// on canonical types (which are always unique). |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3450 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3451 | DecltypeType *dt; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3452 | |
| 3453 | // C++0x [temp.type]p2: |
| 3454 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 3455 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 3456 | // type only if their expressions are equivalent (14.5.6.1). |
| 3457 | if (e->isInstantiationDependent()) { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3458 | llvm::FoldingSetNodeID ID; |
| 3459 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3460 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3461 | void *InsertPos = 0; |
| 3462 | DependentDecltypeType *Canon |
| 3463 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3464 | if (Canon) { |
| 3465 | // We already have a "canonical" version of an equivalent, dependent |
| 3466 | // decltype type. Use that as our canonical type. |
Richard Smith | 0d72910 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 3467 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3468 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3469 | } else { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3470 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3471 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3472 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 3473 | dt = Canon; |
| 3474 | } |
| 3475 | } else { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3476 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
| 3477 | getCanonicalType(UnderlyingType)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3478 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3479 | Types.push_back(dt); |
| 3480 | return QualType(dt, 0); |
| 3481 | } |
| 3482 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3483 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 3484 | /// savings are minimal and these are rare. |
| 3485 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 3486 | QualType UnderlyingType, |
| 3487 | UnaryTransformType::UTTKind Kind) |
| 3488 | const { |
| 3489 | UnaryTransformType *Ty = |
Douglas Gregor | 69d9775 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 3490 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 3491 | Kind, |
| 3492 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 12fc4b0 | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 3493 | QualType() : getCanonicalType(UnderlyingType)); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3494 | Types.push_back(Ty); |
| 3495 | return QualType(Ty, 0); |
| 3496 | } |
| 3497 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3498 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3499 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3500 | void *InsertPos = 0; |
| 3501 | if (!DeducedType.isNull()) { |
| 3502 | // Look in the folding set for an existing type. |
| 3503 | llvm::FoldingSetNodeID ID; |
| 3504 | AutoType::Profile(ID, DeducedType); |
| 3505 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3506 | return QualType(AT, 0); |
| 3507 | } |
| 3508 | |
| 3509 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 3510 | Types.push_back(AT); |
| 3511 | if (InsertPos) |
| 3512 | AutoTypes.InsertNode(AT, InsertPos); |
| 3513 | return QualType(AT, 0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3514 | } |
| 3515 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3516 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3517 | /// the given value type. |
| 3518 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3519 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3520 | // structure. |
| 3521 | llvm::FoldingSetNodeID ID; |
| 3522 | AtomicType::Profile(ID, T); |
| 3523 | |
| 3524 | void *InsertPos = 0; |
| 3525 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3526 | return QualType(AT, 0); |
| 3527 | |
| 3528 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3529 | // either, so fill in the canonical type field. |
| 3530 | QualType Canonical; |
| 3531 | if (!T.isCanonical()) { |
| 3532 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3533 | |
| 3534 | // Get the new insert position for the node we care about. |
| 3535 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3536 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 3537 | } |
| 3538 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3539 | Types.push_back(New); |
| 3540 | AtomicTypes.InsertNode(New, InsertPos); |
| 3541 | return QualType(New, 0); |
| 3542 | } |
| 3543 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3544 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3545 | QualType ASTContext::getAutoDeductType() const { |
| 3546 | if (AutoDeductTy.isNull()) |
| 3547 | AutoDeductTy = getAutoType(QualType()); |
| 3548 | assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern"); |
| 3549 | return AutoDeductTy; |
| 3550 | } |
| 3551 | |
| 3552 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3553 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3554 | if (AutoRRefDeductTy.isNull()) |
| 3555 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3556 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3557 | return AutoRRefDeductTy; |
| 3558 | } |
| 3559 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3560 | /// getTagDeclType - Return the unique reference to the type for the |
| 3561 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3562 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3563 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3564 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3565 | // away const? mutable? |
| 3566 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3569 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3570 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3571 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3572 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3573 | return getFromTargetType(Target->getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3576 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3577 | CanQualType ASTContext::getIntMaxType() const { |
| 3578 | return getFromTargetType(Target->getIntMaxType()); |
| 3579 | } |
| 3580 | |
| 3581 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3582 | CanQualType ASTContext::getUIntMaxType() const { |
| 3583 | return getFromTargetType(Target->getUIntMaxType()); |
| 3584 | } |
| 3585 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3586 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3587 | /// Used when in C++, as a GCC extension. |
| 3588 | QualType ASTContext::getSignedWCharType() const { |
| 3589 | // FIXME: derive from "Target" ? |
| 3590 | return WCharTy; |
| 3591 | } |
| 3592 | |
| 3593 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3594 | /// Used when in C++, as a GCC extension. |
| 3595 | QualType ASTContext::getUnsignedWCharType() const { |
| 3596 | // FIXME: derive from "Target" ? |
| 3597 | return UnsignedIntTy; |
| 3598 | } |
| 3599 | |
Enea Zaffanella | 9677eb8 | 2013-01-26 17:08:37 +0000 | [diff] [blame] | 3600 | QualType ASTContext::getIntPtrType() const { |
| 3601 | return getFromTargetType(Target->getIntPtrType()); |
| 3602 | } |
| 3603 | |
| 3604 | QualType ASTContext::getUIntPtrType() const { |
| 3605 | return getCorrespondingUnsignedType(getIntPtrType()); |
| 3606 | } |
| 3607 | |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3608 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3609 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3610 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3611 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
Eli Friedman | 6902e41 | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 3614 | /// \brief Return the unique type for "pid_t" defined in |
| 3615 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
| 3616 | QualType ASTContext::getProcessIDType() const { |
| 3617 | return getFromTargetType(Target->getProcessIDType()); |
| 3618 | } |
| 3619 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3620 | //===----------------------------------------------------------------------===// |
| 3621 | // Type Operators |
| 3622 | //===----------------------------------------------------------------------===// |
| 3623 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3624 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3625 | // Push qualifiers into arrays, and then discard any remaining |
| 3626 | // qualifiers. |
| 3627 | T = getCanonicalType(T); |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3628 | T = getVariableArrayDecayedType(T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3629 | const Type *Ty = T.getTypePtr(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3630 | QualType Result; |
| 3631 | if (isa<ArrayType>(Ty)) { |
| 3632 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3633 | } else if (isa<FunctionType>(Ty)) { |
| 3634 | Result = getPointerType(QualType(Ty, 0)); |
| 3635 | } else { |
| 3636 | Result = QualType(Ty, 0); |
| 3637 | } |
| 3638 | |
| 3639 | return CanQualType::CreateUnsafe(Result); |
| 3640 | } |
| 3641 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3642 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3643 | Qualifiers &quals) { |
| 3644 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3645 | |
| 3646 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3647 | // the unqualified desugared type and then drops it on the floor. |
| 3648 | // We then have to strip that sugar back off with |
| 3649 | // getUnqualifiedDesugaredType(), which is silly. |
| 3650 | const ArrayType *AT = |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3651 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3652 | |
| 3653 | // 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] | 3654 | if (!AT) { |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3655 | quals = splitType.Quals; |
| 3656 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3659 | // Otherwise, recurse on the array's element type. |
| 3660 | QualType elementType = AT->getElementType(); |
| 3661 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3662 | |
| 3663 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3664 | // can just use the results in splitType. |
| 3665 | if (elementType == unqualElementType) { |
| 3666 | assert(quals.empty()); // from the recursive call |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3667 | quals = splitType.Quals; |
| 3668 | return QualType(splitType.Ty, 0); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3672 | // build the type back up. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3673 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3674 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3675 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3676 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3677 | CAT->getSizeModifier(), 0); |
| 3678 | } |
| 3679 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3680 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3681 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3682 | } |
| 3683 | |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3684 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3685 | return getVariableArrayType(unqualElementType, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3686 | VAT->getSizeExpr(), |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3687 | VAT->getSizeModifier(), |
| 3688 | VAT->getIndexTypeCVRQualifiers(), |
| 3689 | VAT->getBracketsRange()); |
| 3690 | } |
| 3691 | |
| 3692 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3693 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3694 | DSAT->getSizeModifier(), 0, |
| 3695 | SourceRange()); |
| 3696 | } |
| 3697 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3698 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3699 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3700 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3701 | /// or pointer-to-member types, or if they are not similar at this |
| 3702 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3703 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3704 | /// be called in a loop that successively "unwraps" pointer and |
| 3705 | /// pointer-to-member types to compare them at each level. |
| 3706 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3707 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3708 | *T2PtrType = T2->getAs<PointerType>(); |
| 3709 | if (T1PtrType && T2PtrType) { |
| 3710 | T1 = T1PtrType->getPointeeType(); |
| 3711 | T2 = T2PtrType->getPointeeType(); |
| 3712 | return true; |
| 3713 | } |
| 3714 | |
| 3715 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3716 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3717 | if (T1MPType && T2MPType && |
| 3718 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3719 | QualType(T2MPType->getClass(), 0))) { |
| 3720 | T1 = T1MPType->getPointeeType(); |
| 3721 | T2 = T2MPType->getPointeeType(); |
| 3722 | return true; |
| 3723 | } |
| 3724 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3725 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3726 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3727 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3728 | if (T1OPType && T2OPType) { |
| 3729 | T1 = T1OPType->getPointeeType(); |
| 3730 | T2 = T2OPType->getPointeeType(); |
| 3731 | return true; |
| 3732 | } |
| 3733 | } |
| 3734 | |
| 3735 | // FIXME: Block pointers, too? |
| 3736 | |
| 3737 | return false; |
| 3738 | } |
| 3739 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3740 | DeclarationNameInfo |
| 3741 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3742 | SourceLocation NameLoc) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3743 | switch (Name.getKind()) { |
| 3744 | case TemplateName::QualifiedTemplate: |
| 3745 | case TemplateName::Template: |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3746 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3747 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3748 | NameLoc); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3749 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3750 | case TemplateName::OverloadedTemplate: { |
| 3751 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3752 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3753 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3754 | } |
| 3755 | |
| 3756 | case TemplateName::DependentTemplate: { |
| 3757 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3758 | DeclarationName DName; |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3759 | if (DTN->isIdentifier()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3760 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3761 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3762 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3763 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3764 | // DNInfo work in progress: FIXME: source locations? |
| 3765 | DeclarationNameLoc DNLoc; |
| 3766 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3767 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3768 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3769 | } |
| 3770 | } |
| 3771 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3772 | case TemplateName::SubstTemplateTemplateParm: { |
| 3773 | SubstTemplateTemplateParmStorage *subst |
| 3774 | = Name.getAsSubstTemplateTemplateParm(); |
| 3775 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3776 | NameLoc); |
| 3777 | } |
| 3778 | |
| 3779 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3780 | SubstTemplateTemplateParmPackStorage *subst |
| 3781 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3782 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3783 | NameLoc); |
| 3784 | } |
| 3785 | } |
| 3786 | |
| 3787 | llvm_unreachable("bad template name kind!"); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3788 | } |
| 3789 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3790 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3791 | switch (Name.getKind()) { |
| 3792 | case TemplateName::QualifiedTemplate: |
| 3793 | case TemplateName::Template: { |
| 3794 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3795 | if (TemplateTemplateParmDecl *TTP |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3796 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3797 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3798 | |
| 3799 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3800 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 3e1274f | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3801 | } |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3802 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3803 | case TemplateName::OverloadedTemplate: |
| 3804 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3805 | |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3806 | case TemplateName::DependentTemplate: { |
| 3807 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3808 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3809 | return DTN->CanonicalTemplateName; |
| 3810 | } |
| 3811 | |
| 3812 | case TemplateName::SubstTemplateTemplateParm: { |
| 3813 | SubstTemplateTemplateParmStorage *subst |
| 3814 | = Name.getAsSubstTemplateTemplateParm(); |
| 3815 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3816 | } |
| 3817 | |
| 3818 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3819 | SubstTemplateTemplateParmPackStorage *subst |
| 3820 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3821 | TemplateTemplateParmDecl *canonParameter |
| 3822 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3823 | TemplateArgument canonArgPack |
| 3824 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3825 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3826 | } |
| 3827 | } |
| 3828 | |
| 3829 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3830 | } |
| 3831 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3832 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3833 | X = getCanonicalTemplateName(X); |
| 3834 | Y = getCanonicalTemplateName(Y); |
| 3835 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3836 | } |
| 3837 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3838 | TemplateArgument |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3839 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3840 | switch (Arg.getKind()) { |
| 3841 | case TemplateArgument::Null: |
| 3842 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3843 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3844 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3845 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3846 | |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3847 | case TemplateArgument::Declaration: { |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3848 | ValueDecl *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
| 3849 | return TemplateArgument(D, Arg.isDeclForReferenceParam()); |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3850 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3851 | |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3852 | case TemplateArgument::NullPtr: |
| 3853 | return TemplateArgument(getCanonicalType(Arg.getNullPtrType()), |
| 3854 | /*isNullPtr*/true); |
| 3855 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3856 | case TemplateArgument::Template: |
| 3857 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3858 | |
| 3859 | case TemplateArgument::TemplateExpansion: |
| 3860 | return TemplateArgument(getCanonicalTemplateName( |
| 3861 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3862 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3863 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3864 | case TemplateArgument::Integral: |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 3865 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3866 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3867 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3868 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3869 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3870 | case TemplateArgument::Pack: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3871 | if (Arg.pack_size() == 0) |
| 3872 | return Arg; |
| 3873 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3874 | TemplateArgument *CanonArgs |
| 3875 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3876 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3878 | AEnd = Arg.pack_end(); |
| 3879 | A != AEnd; (void)++A, ++Idx) |
| 3880 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3882 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | // Silence GCC warning |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3887 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3890 | NestedNameSpecifier * |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3891 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3893 | return 0; |
| 3894 | |
| 3895 | switch (NNS->getKind()) { |
| 3896 | case NestedNameSpecifier::Identifier: |
| 3897 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3898 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3899 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3900 | NNS->getAsIdentifier()); |
| 3901 | |
| 3902 | case NestedNameSpecifier::Namespace: |
| 3903 | // A namespace is canonical; build a nested-name-specifier with |
| 3904 | // this namespace and no prefix. |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3905 | return NestedNameSpecifier::Create(*this, 0, |
| 3906 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3907 | |
| 3908 | case NestedNameSpecifier::NamespaceAlias: |
| 3909 | // A namespace is canonical; build a nested-name-specifier with |
| 3910 | // this namespace and no prefix. |
| 3911 | return NestedNameSpecifier::Create(*this, 0, |
| 3912 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3913 | ->getOriginalNamespace()); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3914 | |
| 3915 | case NestedNameSpecifier::TypeSpec: |
| 3916 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3917 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3918 | |
| 3919 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3920 | // break it apart into its prefix and identifier, then reconsititute those |
| 3921 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3922 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3923 | // types, e.g., |
| 3924 | // typedef typename T::type T1; |
| 3925 | // typedef typename T1::type T2; |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3926 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 3927 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3928 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 264bf66 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3929 | |
Eli Friedman | 16412ef | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3930 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 3931 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 3932 | // first place? |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3933 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3934 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | case NestedNameSpecifier::Global: |
| 3938 | // The global specifier is canonical and unique. |
| 3939 | return NNS; |
| 3940 | } |
| 3941 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3942 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3945 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3946 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3947 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3948 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3949 | // Handle the common positive case fast. |
| 3950 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3951 | return AT; |
| 3952 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3953 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3954 | // Handle the common negative case fast. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3955 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3956 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3957 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3958 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3959 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3960 | // 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] | 3961 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3962 | // If we get here, we either have type qualifiers on the type, or we have |
| 3963 | // 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] | 3964 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3965 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3966 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3967 | Qualifiers qs = split.Quals; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3968 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3969 | // If we have a simple case, just return now. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3970 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3971 | if (ATy == 0 || qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3972 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3973 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3974 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3975 | // qualifiers into the array element type and return a new array type. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3976 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3977 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3978 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3979 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3980 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3981 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3982 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3983 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3984 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3985 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3986 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3988 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3989 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3990 | getDependentSizedArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3991 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3992 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3993 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3994 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3995 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3996 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3997 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3998 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3999 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4000 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4001 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
Abramo Bagnara | ad9689f | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4004 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4005 | // C99 6.7.5.3p7: |
| 4006 | // A declaration of a parameter as "array of type" shall be |
| 4007 | // adjusted to "qualified pointer to type", where the type |
| 4008 | // qualifiers (if any) are those specified within the [ and ] of |
| 4009 | // the array type derivation. |
| 4010 | if (T->isArrayType()) |
| 4011 | return getArrayDecayedType(T); |
| 4012 | |
| 4013 | // C99 6.7.5.3p8: |
| 4014 | // A declaration of a parameter as "function returning type" |
| 4015 | // shall be adjusted to "pointer to function returning type", as |
| 4016 | // in 6.3.2.1. |
| 4017 | if (T->isFunctionType()) |
| 4018 | return getPointerType(T); |
| 4019 | |
| 4020 | return T; |
| 4021 | } |
| 4022 | |
Abramo Bagnara | ad9689f | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4023 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4024 | T = getVariableArrayDecayedType(T); |
| 4025 | T = getAdjustedParameterType(T); |
| 4026 | return T.getUnqualifiedType(); |
| 4027 | } |
| 4028 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4029 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 4030 | /// specified array type to a pointer. This operation is non-trivial when |
| 4031 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 4032 | /// this returns a pointer to a properly qualified element of the array. |
| 4033 | /// |
| 4034 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4035 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4036 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 4037 | // typedefs in the element type of the array. This also handles propagation |
| 4038 | // of type qualifiers from the array type into the element type if present |
| 4039 | // (C99 6.7.3p8). |
| 4040 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 4041 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4042 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4043 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4044 | |
| 4045 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4046 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4049 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 4050 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4053 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 4054 | Qualifiers qs; |
| 4055 | while (true) { |
| 4056 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4057 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4058 | if (!array) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4059 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4060 | type = array->getElementType(); |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4061 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4062 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4063 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4064 | return getQualifiedType(type, qs); |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 4065 | } |
| 4066 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4067 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4068 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4069 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 4070 | uint64_t ElementCount = 1; |
| 4071 | do { |
| 4072 | ElementCount *= CA->getSize().getZExtValue(); |
Richard Smith | d5e8394 | 2012-12-06 03:04:50 +0000 | [diff] [blame] | 4073 | CA = dyn_cast_or_null<ConstantArrayType>( |
| 4074 | CA->getElementType()->getAsArrayTypeUnsafe()); |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4075 | } while (CA); |
| 4076 | return ElementCount; |
| 4077 | } |
| 4078 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4079 | /// getFloatingRank - Return a relative rank for floating point types. |
| 4080 | /// 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] | 4081 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4082 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4083 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4084 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4085 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 4086 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4087 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4088 | case BuiltinType::Half: return HalfRank; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4089 | case BuiltinType::Float: return FloatRank; |
| 4090 | case BuiltinType::Double: return DoubleRank; |
| 4091 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 4092 | } |
| 4093 | } |
| 4094 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4095 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 4096 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 4097 | /// 'typeDomain' is a real floating point or complex type. |
| 4098 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4099 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 4100 | QualType Domain) const { |
| 4101 | FloatingRank EltRank = getFloatingRank(Size); |
| 4102 | if (Domain->isComplexType()) { |
| 4103 | switch (EltRank) { |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4104 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4105 | case FloatRank: return FloatComplexTy; |
| 4106 | case DoubleRank: return DoubleComplexTy; |
| 4107 | case LongDoubleRank: return LongDoubleComplexTy; |
| 4108 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4109 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4110 | |
| 4111 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 4112 | switch (EltRank) { |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 4113 | case HalfRank: return HalfTy; |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4114 | case FloatRank: return FloatTy; |
| 4115 | case DoubleRank: return DoubleTy; |
| 4116 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4117 | } |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4118 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4121 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 4122 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 4123 | /// '_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] | 4124 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4125 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4126 | FloatingRank LHSR = getFloatingRank(LHS); |
| 4127 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4128 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4129 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4130 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4131 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4132 | return 1; |
| 4133 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4136 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 4137 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 4138 | /// or if it is not canonicalized. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4139 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4140 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4141 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4142 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4143 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4144 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4145 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4146 | case BuiltinType::Char_S: |
| 4147 | case BuiltinType::Char_U: |
| 4148 | case BuiltinType::SChar: |
| 4149 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4150 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4151 | case BuiltinType::Short: |
| 4152 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4153 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4154 | case BuiltinType::Int: |
| 4155 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4156 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4157 | case BuiltinType::Long: |
| 4158 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4159 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4160 | case BuiltinType::LongLong: |
| 4161 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4162 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4163 | case BuiltinType::Int128: |
| 4164 | case BuiltinType::UInt128: |
| 4165 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4166 | } |
| 4167 | } |
| 4168 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4169 | /// \brief Whether this is a promotable bitfield reference according |
| 4170 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 4171 | /// |
| 4172 | /// \returns the type this bit-field will promote to, or NULL if no |
| 4173 | /// promotion occurs. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4174 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | ceafbde | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 4175 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4176 | return QualType(); |
| 4177 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4178 | FieldDecl *Field = E->getBitField(); |
| 4179 | if (!Field) |
| 4180 | return QualType(); |
| 4181 | |
| 4182 | QualType FT = Field->getType(); |
| 4183 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4184 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4185 | uint64_t IntSize = getTypeSize(IntTy); |
| 4186 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 4187 | // to the size of int, it gets promoted no matter what its type is. |
| 4188 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 4189 | if (BitWidth < IntSize) |
| 4190 | return IntTy; |
| 4191 | |
| 4192 | if (BitWidth == IntSize) |
| 4193 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 4194 | |
| 4195 | // Types bigger than int are not subject to promotions, and therefore act |
| 4196 | // like the base type. |
| 4197 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 4198 | // is ridiculous. |
| 4199 | return QualType(); |
| 4200 | } |
| 4201 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4202 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 4203 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 4204 | /// integer type. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4205 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4206 | assert(!Promotable.isNull()); |
| 4207 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4208 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 4209 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 68a2dc4 | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 4210 | |
| 4211 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 4212 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 4213 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 4214 | // types that can represent all the values of its underlying type: |
| 4215 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 4216 | // unsigned long long int [...] |
| 4217 | // FIXME: Is there some better way to compute this? |
| 4218 | if (BT->getKind() == BuiltinType::WChar_S || |
| 4219 | BT->getKind() == BuiltinType::WChar_U || |
| 4220 | BT->getKind() == BuiltinType::Char16 || |
| 4221 | BT->getKind() == BuiltinType::Char32) { |
| 4222 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 4223 | uint64_t FromSize = getTypeSize(BT); |
| 4224 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 4225 | LongLongTy, UnsignedLongLongTy }; |
| 4226 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 4227 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 4228 | if (FromSize < ToSize || |
| 4229 | (FromSize == ToSize && |
| 4230 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 4231 | return PromoteTypes[Idx]; |
| 4232 | } |
| 4233 | llvm_unreachable("char type should fit into long long"); |
| 4234 | } |
| 4235 | } |
| 4236 | |
| 4237 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4238 | if (Promotable->isSignedIntegerType()) |
| 4239 | return IntTy; |
Eli Friedman | 5b64e77 | 2012-11-15 01:21:59 +0000 | [diff] [blame] | 4240 | uint64_t PromotableSize = getIntWidth(Promotable); |
| 4241 | uint64_t IntSize = getIntWidth(IntTy); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4242 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 4243 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 4244 | } |
| 4245 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4246 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 4247 | /// type and returns its ownership. |
| 4248 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 4249 | while (!T.isNull()) { |
| 4250 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 4251 | return T.getObjCLifetime(); |
| 4252 | if (T->isArrayType()) |
| 4253 | T = getBaseElementType(T); |
| 4254 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 4255 | T = PT->getPointeeType(); |
| 4256 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 28445f0 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 4257 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4258 | else |
| 4259 | break; |
| 4260 | } |
| 4261 | |
| 4262 | return Qualifiers::OCL_None; |
| 4263 | } |
| 4264 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4265 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4266 | /// 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] | 4267 | /// LHS < RHS, return -1. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4268 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4269 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 4270 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4271 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4273 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 4274 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4275 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4276 | unsigned LHSRank = getIntegerRank(LHSC); |
| 4277 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4278 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4279 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 4280 | if (LHSRank == RHSRank) return 0; |
| 4281 | return LHSRank > RHSRank ? 1 : -1; |
| 4282 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4283 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4284 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 4285 | if (LHSUnsigned) { |
| 4286 | // If the unsigned [LHS] type is larger, return it. |
| 4287 | if (LHSRank >= RHSRank) |
| 4288 | return 1; |
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 the signed type can represent all values of the unsigned type, it |
| 4291 | // 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] | 4292 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4293 | return -1; |
| 4294 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4295 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4296 | // If the unsigned [RHS] type is larger, return it. |
| 4297 | if (RHSRank >= LHSRank) |
| 4298 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4299 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4300 | // If the signed type can represent all values of the unsigned type, it |
| 4301 | // 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] | 4302 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4303 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4304 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4305 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4306 | static RecordDecl * |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4307 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 4308 | DeclContext *DC, IdentifierInfo *Id) { |
| 4309 | SourceLocation Loc; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4310 | if (Ctx.getLangOpts().CPlusPlus) |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4311 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4312 | else |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4313 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4314 | } |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4315 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4316 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4317 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4318 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4319 | CFConstantStringTypeDecl = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4320 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4321 | &Idents.get("NSConstantString")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4322 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4323 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4324 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4325 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4326 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4327 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4328 | // int flags; |
| 4329 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4330 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4331 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4332 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4333 | FieldTypes[3] = LongTy; |
| 4334 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4335 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4336 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4337 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4338 | SourceLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4339 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4340 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4341 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4342 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4343 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4344 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4345 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4348 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4349 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4350 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4351 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 4352 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4353 | |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 4354 | QualType ASTContext::getObjCSuperType() const { |
| 4355 | if (ObjCSuperType.isNull()) { |
| 4356 | RecordDecl *ObjCSuperTypeDecl = |
| 4357 | CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get("objc_super")); |
| 4358 | TUDecl->addDecl(ObjCSuperTypeDecl); |
| 4359 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
| 4360 | } |
| 4361 | return ObjCSuperType; |
| 4362 | } |
| 4363 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4364 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4365 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4366 | assert(Rec && "Invalid CFConstantStringType"); |
| 4367 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 4368 | } |
| 4369 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4370 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4371 | if (BlockDescriptorType) |
| 4372 | return getTagDeclType(BlockDescriptorType); |
| 4373 | |
| 4374 | RecordDecl *T; |
| 4375 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4376 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4377 | &Idents.get("__block_descriptor")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4378 | T->startDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4379 | |
| 4380 | QualType FieldTypes[] = { |
| 4381 | UnsignedLongTy, |
| 4382 | UnsignedLongTy, |
| 4383 | }; |
| 4384 | |
| 4385 | const char *FieldNames[] = { |
| 4386 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4387 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4388 | }; |
| 4389 | |
| 4390 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4391 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4392 | SourceLocation(), |
| 4393 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4394 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4395 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4396 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4397 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4398 | Field->setAccess(AS_public); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4399 | T->addDecl(Field); |
| 4400 | } |
| 4401 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4402 | T->completeDefinition(); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4403 | |
| 4404 | BlockDescriptorType = T; |
| 4405 | |
| 4406 | return getTagDeclType(BlockDescriptorType); |
| 4407 | } |
| 4408 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4409 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4410 | if (BlockDescriptorExtendedType) |
| 4411 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4412 | |
| 4413 | RecordDecl *T; |
| 4414 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4415 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4416 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4417 | T->startDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4418 | |
| 4419 | QualType FieldTypes[] = { |
| 4420 | UnsignedLongTy, |
| 4421 | UnsignedLongTy, |
| 4422 | getPointerType(VoidPtrTy), |
| 4423 | getPointerType(VoidPtrTy) |
| 4424 | }; |
| 4425 | |
| 4426 | const char *FieldNames[] = { |
| 4427 | "reserved", |
| 4428 | "Size", |
| 4429 | "CopyFuncPtr", |
| 4430 | "DestroyFuncPtr" |
| 4431 | }; |
| 4432 | |
| 4433 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4434 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4435 | SourceLocation(), |
| 4436 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4437 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4438 | /*BitWidth=*/0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4439 | /*Mutable=*/false, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4440 | ICIS_NoInit); |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4441 | Field->setAccess(AS_public); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4442 | T->addDecl(Field); |
| 4443 | } |
| 4444 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4445 | T->completeDefinition(); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4446 | |
| 4447 | BlockDescriptorExtendedType = T; |
| 4448 | |
| 4449 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4450 | } |
| 4451 | |
Fariborz Jahanian | b15c898 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4452 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
| 4453 | /// requires copy/dispose. Note that this must match the logic |
| 4454 | /// in buildByrefHelpers. |
| 4455 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
| 4456 | const VarDecl *D) { |
| 4457 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
| 4458 | const Expr *copyExpr = getBlockVarCopyInits(D); |
| 4459 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
| 4460 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4461 | return true; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4462 | } |
Fariborz Jahanian | b15c898 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4463 | |
| 4464 | if (!Ty->isObjCRetainableType()) return false; |
| 4465 | |
| 4466 | Qualifiers qs = Ty.getQualifiers(); |
| 4467 | |
| 4468 | // If we have lifetime, that dominates. |
| 4469 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
| 4470 | assert(getLangOpts().ObjCAutoRefCount); |
| 4471 | |
| 4472 | switch (lifetime) { |
| 4473 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
| 4474 | |
| 4475 | // These are just bits as far as the runtime is concerned. |
| 4476 | case Qualifiers::OCL_ExplicitNone: |
| 4477 | case Qualifiers::OCL_Autoreleasing: |
| 4478 | return false; |
| 4479 | |
| 4480 | // Tell the runtime that this is ARC __weak, called by the |
| 4481 | // byref routines. |
| 4482 | case Qualifiers::OCL_Weak: |
| 4483 | // ARC __strong __block variables need to be retained. |
| 4484 | case Qualifiers::OCL_Strong: |
| 4485 | return true; |
| 4486 | } |
| 4487 | llvm_unreachable("fell out of lifetime switch!"); |
| 4488 | } |
| 4489 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
| 4490 | Ty->isObjCObjectPointerType()); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4493 | bool ASTContext::getByrefLifetime(QualType Ty, |
| 4494 | Qualifiers::ObjCLifetime &LifeTime, |
| 4495 | bool &HasByrefExtendedLayout) const { |
| 4496 | |
| 4497 | if (!getLangOpts().ObjC1 || |
| 4498 | getLangOpts().getGC() != LangOptions::NonGC) |
| 4499 | return false; |
| 4500 | |
| 4501 | HasByrefExtendedLayout = false; |
Fariborz Jahanian | 34db84f | 2012-12-11 19:58:01 +0000 | [diff] [blame] | 4502 | if (Ty->isRecordType()) { |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4503 | HasByrefExtendedLayout = true; |
| 4504 | LifeTime = Qualifiers::OCL_None; |
| 4505 | } |
| 4506 | else if (getLangOpts().ObjCAutoRefCount) |
| 4507 | LifeTime = Ty.getObjCLifetime(); |
| 4508 | // MRR. |
| 4509 | else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 4510 | LifeTime = Qualifiers::OCL_ExplicitNone; |
| 4511 | else |
| 4512 | LifeTime = Qualifiers::OCL_None; |
| 4513 | return true; |
| 4514 | } |
| 4515 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4516 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 4517 | if (!ObjCInstanceTypeDecl) |
| 4518 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 4519 | getTranslationUnitDecl(), |
| 4520 | SourceLocation(), |
| 4521 | SourceLocation(), |
| 4522 | &Idents.get("instancetype"), |
| 4523 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 4524 | return ObjCInstanceTypeDecl; |
| 4525 | } |
| 4526 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4527 | // This returns true if a type has been typedefed to BOOL: |
| 4528 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 4529 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4530 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 4531 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 4532 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4534 | return false; |
| 4535 | } |
| 4536 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4537 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4538 | /// purpose. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4539 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4540 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4541 | return CharUnits::Zero(); |
| 4542 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4543 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4544 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4545 | // 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] | 4546 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4547 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4548 | // Treat arrays as pointers, since that's how they're passed in. |
| 4549 | else if (type->isArrayType()) |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4550 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4551 | return sz; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4552 | } |
| 4553 | |
| 4554 | static inline |
| 4555 | std::string charUnitsToString(const CharUnits &CU) { |
| 4556 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4557 | } |
| 4558 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4559 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4560 | /// declaration. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4561 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4562 | std::string S; |
| 4563 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4564 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4565 | QualType BlockTy = |
| 4566 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4567 | // Encode result type. |
Fariborz Jahanian | 3d145f6 | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 4568 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 06cffc0 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 4569 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, |
| 4570 | BlockTy->getAs<FunctionType>()->getResultType(), |
| 4571 | S, true /*Extended*/); |
| 4572 | else |
| 4573 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), |
| 4574 | S); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4575 | // Compute size of all parameters. |
| 4576 | // Start with computing size of a pointer in number of bytes. |
| 4577 | // FIXME: There might(should) be a better way of doing this computation! |
| 4578 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4579 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4580 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 6f46c26 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 4581 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4582 | E = Decl->param_end(); PI != E; ++PI) { |
| 4583 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4584 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 075a543 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 4585 | if (sz.isZero()) |
| 4586 | continue; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4587 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4588 | ParmOffset += sz; |
| 4589 | } |
| 4590 | // Size of the argument frame |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4591 | S += charUnitsToString(ParmOffset); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4592 | // Block pointer and offset. |
| 4593 | S += "@?0"; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4594 | |
| 4595 | // Argument types. |
| 4596 | ParmOffset = PtrSize; |
| 4597 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 4598 | Decl->param_end(); PI != E; ++PI) { |
| 4599 | ParmVarDecl *PVDecl = *PI; |
| 4600 | QualType PType = PVDecl->getOriginalType(); |
| 4601 | if (const ArrayType *AT = |
| 4602 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4603 | // Use array's original type only if it has known number of |
| 4604 | // elements. |
| 4605 | if (!isa<ConstantArrayType>(AT)) |
| 4606 | PType = PVDecl->getType(); |
| 4607 | } else if (PType->isFunctionType()) |
| 4608 | PType = PVDecl->getType(); |
Fariborz Jahanian | 3d145f6 | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 4609 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 06cffc0 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 4610 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType, |
| 4611 | S, true /*Extended*/); |
| 4612 | else |
| 4613 | getObjCEncodingForType(PType, S); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4614 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4615 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4616 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4617 | |
| 4618 | return S; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4619 | } |
| 4620 | |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4621 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4622 | std::string& S) { |
| 4623 | // Encode result type. |
| 4624 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4625 | CharUnits ParmOffset; |
| 4626 | // Compute size of all parameters. |
| 4627 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4628 | E = Decl->param_end(); PI != E; ++PI) { |
| 4629 | QualType PType = (*PI)->getType(); |
| 4630 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4631 | if (sz.isZero()) |
Fariborz Jahanian | 7e68ba5 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4632 | continue; |
| 4633 | |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4634 | assert (sz.isPositive() && |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4635 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4636 | ParmOffset += sz; |
| 4637 | } |
| 4638 | S += charUnitsToString(ParmOffset); |
| 4639 | ParmOffset = CharUnits::Zero(); |
| 4640 | |
| 4641 | // Argument types. |
| 4642 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4643 | E = Decl->param_end(); PI != E; ++PI) { |
| 4644 | ParmVarDecl *PVDecl = *PI; |
| 4645 | QualType PType = PVDecl->getOriginalType(); |
| 4646 | if (const ArrayType *AT = |
| 4647 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4648 | // Use array's original type only if it has known number of |
| 4649 | // elements. |
| 4650 | if (!isa<ConstantArrayType>(AT)) |
| 4651 | PType = PVDecl->getType(); |
| 4652 | } else if (PType->isFunctionType()) |
| 4653 | PType = PVDecl->getType(); |
| 4654 | getObjCEncodingForType(PType, S); |
| 4655 | S += charUnitsToString(ParmOffset); |
| 4656 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4657 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4658 | |
| 4659 | return false; |
David Chisnall | 5389f48 | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4662 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 4663 | /// method parameter or return type. If Extended, include class names and |
| 4664 | /// block object types. |
| 4665 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 4666 | QualType T, std::string& S, |
| 4667 | bool Extended) const { |
| 4668 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 4669 | getObjCEncodingForTypeQualifier(QT, S); |
| 4670 | // Encode parameter type. |
| 4671 | getObjCEncodingForTypeImpl(T, S, true, true, 0, |
| 4672 | true /*OutermostType*/, |
| 4673 | false /*EncodingProperty*/, |
| 4674 | false /*StructField*/, |
| 4675 | Extended /*EncodeBlockParameters*/, |
| 4676 | Extended /*EncodeClassNames*/); |
| 4677 | } |
| 4678 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4679 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4680 | /// declaration. |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4681 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4682 | std::string& S, |
| 4683 | bool Extended) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4684 | // FIXME: This is not very efficient. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4685 | // Encode return type. |
| 4686 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 4687 | Decl->getResultType(), S, Extended); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4688 | // Compute size of all parameters. |
| 4689 | // Start with computing size of a pointer in number of bytes. |
| 4690 | // FIXME: There might(should) be a better way of doing this computation! |
| 4691 | SourceLocation Loc; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4692 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4693 | // The first two arguments (self and _cmd) are pointers; account for |
| 4694 | // their size. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4695 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4696 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4697 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4698 | QualType PType = (*PI)->getType(); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4699 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4700 | if (sz.isZero()) |
Fariborz Jahanian | 7e68ba5 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4701 | continue; |
| 4702 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4703 | assert (sz.isPositive() && |
| 4704 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4705 | ParmOffset += sz; |
| 4706 | } |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4707 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4708 | S += "@0:"; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4709 | S += charUnitsToString(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4710 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4711 | // Argument types. |
| 4712 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4713 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4714 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4715 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4716 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4717 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4718 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4719 | // Use array's original type only if it has known number of |
| 4720 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4721 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4722 | PType = PVDecl->getType(); |
| 4723 | } else if (PType->isFunctionType()) |
| 4724 | PType = PVDecl->getType(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4725 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 4726 | PType, S, Extended); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4727 | S += charUnitsToString(ParmOffset); |
Ken Dyck | aa8741a | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4728 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4729 | } |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4730 | |
| 4731 | return false; |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4732 | } |
| 4733 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4734 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4735 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4736 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4737 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4738 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4739 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4740 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4741 | /// encoded as single characters, followed by an identifier. Property types |
| 4742 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4743 | /// these attributes are defined by the following enumeration: |
| 4744 | /// @code |
| 4745 | /// enum PropertyAttributes { |
| 4746 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4747 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4748 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4749 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4750 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4751 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4752 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 0d4cb85 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 4753 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4754 | /// kPropertyWeak = 'W' // 'weak' property |
| 4755 | /// kPropertyStrong = 'P' // property GC'able |
| 4756 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4757 | /// }; |
| 4758 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4759 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4760 | const Decl *Container, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4761 | std::string& S) const { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4762 | // Collect information from the property implementation decl(s). |
| 4763 | bool Dynamic = false; |
| 4764 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4765 | |
| 4766 | // FIXME: Duplicated code due to poor abstraction. |
| 4767 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4768 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4769 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4770 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4771 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4772 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4773 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4774 | if (PID->getPropertyDecl() == PD) { |
| 4775 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4776 | Dynamic = true; |
| 4777 | } else { |
| 4778 | SynthesizePID = PID; |
| 4779 | } |
| 4780 | } |
| 4781 | } |
| 4782 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4783 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4784 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4785 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4786 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4787 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4788 | if (PID->getPropertyDecl() == PD) { |
| 4789 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4790 | Dynamic = true; |
| 4791 | } else { |
| 4792 | SynthesizePID = PID; |
| 4793 | } |
| 4794 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4795 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4796 | } |
| 4797 | } |
| 4798 | |
| 4799 | // FIXME: This is not very efficient. |
| 4800 | S = "T"; |
| 4801 | |
| 4802 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4803 | // GCC has some special rules regarding encoding of properties which |
| 4804 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4805 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4806 | true /* outermost type */, |
| 4807 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4808 | |
| 4809 | if (PD->isReadOnly()) { |
| 4810 | S += ",R"; |
| 4811 | } else { |
| 4812 | switch (PD->getSetterKind()) { |
| 4813 | case ObjCPropertyDecl::Assign: break; |
| 4814 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4815 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 3a02b44 | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4816 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4817 | } |
| 4818 | } |
| 4819 | |
| 4820 | // It really isn't clear at all what this means, since properties |
| 4821 | // are "dynamic by default". |
| 4822 | if (Dynamic) |
| 4823 | S += ",D"; |
| 4824 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4825 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4826 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4827 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4828 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4829 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4830 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4831 | } |
| 4832 | |
| 4833 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4834 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4835 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | if (SynthesizePID) { |
| 4839 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4840 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4841 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4842 | } |
| 4843 | |
| 4844 | // FIXME: OBJCGC: weak & strong |
| 4845 | } |
| 4846 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4847 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4848 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4849 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4850 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4851 | /// |
| 4852 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4853 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4854 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4855 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4856 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4857 | else |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4858 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4859 | PointeeTy = IntTy; |
| 4860 | } |
| 4861 | } |
| 4862 | } |
| 4863 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4864 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4865 | const FieldDecl *Field) const { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4866 | // We follow the behavior of gcc, expanding structures which are |
| 4867 | // directly pointed to, and expanding embedded structures. Note that |
| 4868 | // these rules are sufficient to prevent recursive encoding of the |
| 4869 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4870 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4871 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4874 | static char getObjCEncodingForPrimitiveKind(const ASTContext *C, |
| 4875 | BuiltinType::Kind kind) { |
| 4876 | switch (kind) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4877 | case BuiltinType::Void: return 'v'; |
| 4878 | case BuiltinType::Bool: return 'B'; |
| 4879 | case BuiltinType::Char_U: |
| 4880 | case BuiltinType::UChar: return 'C'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4881 | case BuiltinType::Char16: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4882 | case BuiltinType::UShort: return 'S'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4883 | case BuiltinType::Char32: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4884 | case BuiltinType::UInt: return 'I'; |
| 4885 | case BuiltinType::ULong: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4886 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4887 | case BuiltinType::UInt128: return 'T'; |
| 4888 | case BuiltinType::ULongLong: return 'Q'; |
| 4889 | case BuiltinType::Char_S: |
| 4890 | case BuiltinType::SChar: return 'c'; |
| 4891 | case BuiltinType::Short: return 's'; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4892 | case BuiltinType::WChar_S: |
| 4893 | case BuiltinType::WChar_U: |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4894 | case BuiltinType::Int: return 'i'; |
| 4895 | case BuiltinType::Long: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4896 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4897 | case BuiltinType::LongLong: return 'q'; |
| 4898 | case BuiltinType::Int128: return 't'; |
| 4899 | case BuiltinType::Float: return 'f'; |
| 4900 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 3a0be84 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4901 | case BuiltinType::LongDouble: return 'D'; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4902 | case BuiltinType::NullPtr: return '*'; // like char* |
| 4903 | |
| 4904 | case BuiltinType::Half: |
| 4905 | // FIXME: potentially need @encodes for these! |
| 4906 | return ' '; |
| 4907 | |
| 4908 | case BuiltinType::ObjCId: |
| 4909 | case BuiltinType::ObjCClass: |
| 4910 | case BuiltinType::ObjCSel: |
| 4911 | llvm_unreachable("@encoding ObjC primitive type"); |
| 4912 | |
| 4913 | // OpenCL and placeholder types don't need @encodings. |
| 4914 | case BuiltinType::OCLImage1d: |
| 4915 | case BuiltinType::OCLImage1dArray: |
| 4916 | case BuiltinType::OCLImage1dBuffer: |
| 4917 | case BuiltinType::OCLImage2d: |
| 4918 | case BuiltinType::OCLImage2dArray: |
| 4919 | case BuiltinType::OCLImage3d: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4920 | case BuiltinType::OCLEvent: |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4921 | case BuiltinType::Dependent: |
| 4922 | #define BUILTIN_TYPE(KIND, ID) |
| 4923 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
| 4924 | case BuiltinType::KIND: |
| 4925 | #include "clang/AST/BuiltinTypes.def" |
| 4926 | llvm_unreachable("invalid builtin type for @encode"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4927 | } |
David Blaikie | 719e53f | 2013-01-09 17:48:41 +0000 | [diff] [blame] | 4928 | llvm_unreachable("invalid BuiltinType::Kind value"); |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4931 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 4932 | EnumDecl *Enum = ET->getDecl(); |
| 4933 | |
| 4934 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 4935 | if (!Enum->isFixed()) |
| 4936 | return 'i'; |
| 4937 | |
| 4938 | // The encoding of a fixed enum type matches its fixed underlying type. |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4939 | const BuiltinType *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
| 4940 | return getObjCEncodingForPrimitiveKind(C, BT->getKind()); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4943 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4944 | QualType T, const FieldDecl *FD) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4945 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4946 | S += 'b'; |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4947 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4948 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4949 | // then the offset (in bits) of the first element, then the type of the |
| 4950 | // bitfield, then the size in bits. For example, in this structure: |
| 4951 | // |
| 4952 | // struct |
| 4953 | // { |
| 4954 | // int integer; |
| 4955 | // int flags:2; |
| 4956 | // }; |
| 4957 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4958 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4959 | // information is not especially sensible, but we're stuck with it for |
| 4960 | // compatibility with GCC, although providing it breaks anything that |
| 4961 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4962 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4963 | const RecordDecl *RD = FD->getParent(); |
| 4964 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | 8290574 | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 4965 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4966 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 4967 | S += ObjCEncodingForEnumType(Ctx, ET); |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4968 | else { |
| 4969 | const BuiltinType *BT = T->castAs<BuiltinType>(); |
| 4970 | S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind()); |
| 4971 | } |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4972 | } |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4973 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4974 | } |
| 4975 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4976 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4977 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4978 | bool ExpandPointedToStructures, |
| 4979 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4980 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4981 | bool OutermostType, |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4982 | bool EncodingProperty, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4983 | bool StructField, |
| 4984 | bool EncodeBlockParameters, |
| 4985 | bool EncodeClassNames) const { |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4986 | CanQualType CT = getCanonicalType(T); |
| 4987 | switch (CT->getTypeClass()) { |
| 4988 | case Type::Builtin: |
| 4989 | case Type::Enum: |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4990 | if (FD && FD->isBitField()) |
David Chisnall | 64fd7e8 | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4991 | return EncodeBitField(this, S, T, FD); |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4992 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CT)) |
| 4993 | S += getObjCEncodingForPrimitiveKind(this, BT->getKind()); |
| 4994 | else |
| 4995 | S += ObjCEncodingForEnumType(this, cast<EnumType>(CT)); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4996 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4997 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 4998 | case Type::Complex: { |
| 4999 | const ComplexType *CT = T->castAs<ComplexType>(); |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 5000 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5001 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 5002 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5003 | return; |
| 5004 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5005 | |
| 5006 | case Type::Atomic: { |
| 5007 | const AtomicType *AT = T->castAs<AtomicType>(); |
| 5008 | S += 'A'; |
| 5009 | getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, 0, |
| 5010 | false, false); |
| 5011 | return; |
Fariborz Jahanian | aa1d761 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 5012 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5013 | |
| 5014 | // encoding for pointer or reference types. |
| 5015 | case Type::Pointer: |
| 5016 | case Type::LValueReference: |
| 5017 | case Type::RValueReference: { |
| 5018 | QualType PointeeTy; |
| 5019 | if (isa<PointerType>(CT)) { |
| 5020 | const PointerType *PT = T->castAs<PointerType>(); |
| 5021 | if (PT->isObjCSelType()) { |
| 5022 | S += ':'; |
| 5023 | return; |
| 5024 | } |
| 5025 | PointeeTy = PT->getPointeeType(); |
| 5026 | } else { |
| 5027 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
| 5028 | } |
| 5029 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5030 | bool isReadOnly = false; |
| 5031 | // For historical/compatibility reasons, the read-only qualifier of the |
| 5032 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 5033 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5034 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 5035 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5036 | if (OutermostType && T.isConstQualified()) { |
| 5037 | isReadOnly = true; |
| 5038 | S += 'r'; |
| 5039 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 5040 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5041 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5042 | while (P->getAs<PointerType>()) |
| 5043 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5044 | if (P.isConstQualified()) { |
| 5045 | isReadOnly = true; |
| 5046 | S += 'r'; |
| 5047 | } |
| 5048 | } |
| 5049 | if (isReadOnly) { |
| 5050 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 5051 | // combinations need to be rearranged. |
| 5052 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5053 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 0237941 | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 5054 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5055 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5056 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5057 | if (PointeeTy->isCharType()) { |
| 5058 | // char pointer types should be encoded as '*' unless it is a |
| 5059 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 5060 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5061 | S += '*'; |
| 5062 | return; |
| 5063 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5064 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 5065 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 5066 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 5067 | S += '#'; |
| 5068 | return; |
| 5069 | } |
| 5070 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 5071 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 5072 | S += '@'; |
| 5073 | return; |
| 5074 | } |
| 5075 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5076 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5077 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5078 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 5079 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5080 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5081 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5082 | return; |
| 5083 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5084 | |
| 5085 | case Type::ConstantArray: |
| 5086 | case Type::IncompleteArray: |
| 5087 | case Type::VariableArray: { |
| 5088 | const ArrayType *AT = cast<ArrayType>(CT); |
| 5089 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5090 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5091 | // Incomplete arrays are encoded as a pointer to the array element. |
| 5092 | S += '^'; |
| 5093 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5094 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5095 | false, ExpandStructures, FD); |
| 5096 | } else { |
| 5097 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5098 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5099 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 5100 | if (getTypeSize(CAT->getElementType()) == 0) |
| 5101 | S += '0'; |
| 5102 | else |
| 5103 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 5104 | } else { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5105 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5106 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 5107 | "Unknown array type!"); |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5108 | S += '0'; |
| 5109 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5110 | |
| 5111 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5112 | false, ExpandStructures, FD); |
| 5113 | S += ']'; |
| 5114 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5115 | return; |
| 5116 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5117 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5118 | case Type::FunctionNoProto: |
| 5119 | case Type::FunctionProto: |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 5120 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5121 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5122 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5123 | case Type::Record: { |
| 5124 | RecordDecl *RDecl = cast<RecordType>(CT)->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5125 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5126 | // Anonymous structures print as '?' |
| 5127 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 5128 | S += II->getName(); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5129 | if (ClassTemplateSpecializationDecl *Spec |
| 5130 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 5131 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 5132 | std::string TemplateArgsStr |
| 5133 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5134 | TemplateArgs.data(), |
| 5135 | TemplateArgs.size(), |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 5136 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | 6fb9439 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5137 | |
| 5138 | S += TemplateArgsStr; |
| 5139 | } |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5140 | } else { |
| 5141 | S += '?'; |
| 5142 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 5143 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5144 | S += '='; |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5145 | if (!RDecl->isUnion()) { |
| 5146 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 5147 | } else { |
| 5148 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 5149 | FieldEnd = RDecl->field_end(); |
| 5150 | Field != FieldEnd; ++Field) { |
| 5151 | if (FD) { |
| 5152 | S += '"'; |
| 5153 | S += Field->getNameAsString(); |
| 5154 | S += '"'; |
| 5155 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5156 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5157 | // Special case bit-fields. |
| 5158 | if (Field->isBitField()) { |
| 5159 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5160 | *Field); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5161 | } else { |
| 5162 | QualType qt = Field->getType(); |
| 5163 | getLegacyIntegralTypeEncoding(qt); |
| 5164 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 5165 | FD, /*OutermostType*/false, |
| 5166 | /*EncodingProperty*/false, |
| 5167 | /*StructField*/true); |
| 5168 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5169 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5170 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 5171 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5172 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5173 | return; |
| 5174 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5175 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5176 | case Type::BlockPointer: { |
| 5177 | const BlockPointerType *BT = T->castAs<BlockPointerType>(); |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 5178 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5179 | if (EncodeBlockParameters) { |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5180 | const FunctionType *FT = BT->getPointeeType()->castAs<FunctionType>(); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5181 | |
| 5182 | S += '<'; |
| 5183 | // Block return type |
| 5184 | getObjCEncodingForTypeImpl(FT->getResultType(), S, |
| 5185 | ExpandPointedToStructures, ExpandStructures, |
| 5186 | FD, |
| 5187 | false /* OutermostType */, |
| 5188 | EncodingProperty, |
| 5189 | false /* StructField */, |
| 5190 | EncodeBlockParameters, |
| 5191 | EncodeClassNames); |
| 5192 | // Block self |
| 5193 | S += "@?"; |
| 5194 | // Block parameters |
| 5195 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 5196 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(), |
| 5197 | E = FPT->arg_type_end(); I && (I != E); ++I) { |
| 5198 | getObjCEncodingForTypeImpl(*I, S, |
| 5199 | ExpandPointedToStructures, |
| 5200 | ExpandStructures, |
| 5201 | FD, |
| 5202 | false /* OutermostType */, |
| 5203 | EncodingProperty, |
| 5204 | false /* StructField */, |
| 5205 | EncodeBlockParameters, |
| 5206 | EncodeClassNames); |
| 5207 | } |
| 5208 | } |
| 5209 | S += '>'; |
| 5210 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5211 | return; |
| 5212 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5213 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5214 | case Type::ObjCObject: |
| 5215 | case Type::ObjCInterface: { |
| 5216 | // Ignore protocol qualifiers when mangling at this level. |
| 5217 | T = T->castAs<ObjCObjectType>()->getBaseType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5218 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5219 | // The assumption seems to be that this assert will succeed |
| 5220 | // because nested levels will have filtered out 'id' and 'Class'. |
| 5221 | const ObjCInterfaceType *OIT = T->castAs<ObjCInterfaceType>(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5222 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5223 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5224 | S += '{'; |
| 5225 | const IdentifierInfo *II = OI->getIdentifier(); |
| 5226 | S += II->getName(); |
| 5227 | S += '='; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5228 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5229 | DeepCollectObjCIvars(OI, true, Ivars); |
| 5230 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5231 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5232 | if (Field->isBitField()) |
| 5233 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5234 | else |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5235 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5236 | } |
| 5237 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5238 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5239 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5240 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5241 | case Type::ObjCObjectPointer: { |
| 5242 | const ObjCObjectPointerType *OPT = T->castAs<ObjCObjectPointerType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5243 | if (OPT->isObjCIdType()) { |
| 5244 | S += '@'; |
| 5245 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5246 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5247 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5248 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 5249 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 5250 | // Since this is a binary compatibility issue, need to consult with runtime |
| 5251 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5252 | S += '#'; |
| 5253 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5254 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5255 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5256 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5257 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5258 | ExpandPointedToStructures, |
| 5259 | ExpandStructures, FD); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5260 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5261 | // Note that we do extended encoding of protocol qualifer list |
| 5262 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5263 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5264 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 5265 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5266 | S += '<'; |
| 5267 | S += (*I)->getNameAsString(); |
| 5268 | S += '>'; |
| 5269 | } |
| 5270 | S += '"'; |
| 5271 | } |
| 5272 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5273 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5274 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5275 | QualType PointeeTy = OPT->getPointeeType(); |
| 5276 | if (!EncodingProperty && |
| 5277 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 5278 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5279 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5280 | // {...}; |
| 5281 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5282 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 5283 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5284 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5285 | return; |
| 5286 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5287 | |
| 5288 | S += '@'; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5289 | if (OPT->getInterfaceDecl() && |
| 5290 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5291 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5292 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5293 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 5294 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5295 | S += '<'; |
| 5296 | S += (*I)->getNameAsString(); |
| 5297 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5298 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5299 | S += '"'; |
| 5300 | } |
| 5301 | return; |
| 5302 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5303 | |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 5304 | // gcc just blithely ignores member pointers. |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5305 | // FIXME: we shoul do better than that. 'M' is available. |
| 5306 | case Type::MemberPointer: |
John McCall | 532ec7b | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 5307 | return; |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5308 | |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5309 | case Type::Vector: |
| 5310 | case Type::ExtVector: |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5311 | // This matches gcc's encoding, even though technically it is |
| 5312 | // insufficient. |
| 5313 | // FIXME. We should do a better job than gcc. |
| 5314 | return; |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5315 | |
| 5316 | #define ABSTRACT_TYPE(KIND, BASE) |
| 5317 | #define TYPE(KIND, BASE) |
| 5318 | #define DEPENDENT_TYPE(KIND, BASE) \ |
| 5319 | case Type::KIND: |
| 5320 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
| 5321 | case Type::KIND: |
| 5322 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
| 5323 | case Type::KIND: |
| 5324 | #include "clang/AST/TypeNodes.def" |
| 5325 | llvm_unreachable("@encode for dependent type!"); |
Fariborz Jahanian | e6012c7 | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5326 | } |
John McCall | 3624e9e | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5327 | llvm_unreachable("bad type kind!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5328 | } |
| 5329 | |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5330 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 5331 | std::string &S, |
| 5332 | const FieldDecl *FD, |
| 5333 | bool includeVBases) const { |
| 5334 | assert(RDecl && "Expected non-null RecordDecl"); |
| 5335 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 5336 | if (!RDecl->getDefinition()) |
| 5337 | return; |
| 5338 | |
| 5339 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 5340 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 5341 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 5342 | |
| 5343 | if (CXXRec) { |
| 5344 | for (CXXRecordDecl::base_class_iterator |
| 5345 | BI = CXXRec->bases_begin(), |
| 5346 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 5347 | if (!BI->isVirtual()) { |
| 5348 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5349 | if (base->isEmpty()) |
| 5350 | continue; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5351 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5352 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 5353 | std::make_pair(offs, base)); |
| 5354 | } |
| 5355 | } |
| 5356 | } |
| 5357 | |
| 5358 | unsigned i = 0; |
| 5359 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 5360 | FieldEnd = RDecl->field_end(); |
| 5361 | Field != FieldEnd; ++Field, ++i) { |
| 5362 | uint64_t offs = layout.getFieldOffset(i); |
| 5363 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5364 | std::make_pair(offs, *Field)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
| 5367 | if (CXXRec && includeVBases) { |
| 5368 | for (CXXRecordDecl::base_class_iterator |
| 5369 | BI = CXXRec->vbases_begin(), |
| 5370 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 5371 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5372 | if (base->isEmpty()) |
| 5373 | continue; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5374 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Argyrios Kyrtzidis | 19aa860 | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 5375 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 5376 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 5377 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5378 | } |
| 5379 | } |
| 5380 | |
| 5381 | CharUnits size; |
| 5382 | if (CXXRec) { |
| 5383 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 5384 | } else { |
| 5385 | size = layout.getSize(); |
| 5386 | } |
| 5387 | |
| 5388 | uint64_t CurOffs = 0; |
| 5389 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 5390 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 5391 | |
Douglas Gregor | 58db7a5 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 5392 | if (CXXRec && CXXRec->isDynamicClass() && |
| 5393 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5394 | if (FD) { |
| 5395 | S += "\"_vptr$"; |
| 5396 | std::string recname = CXXRec->getNameAsString(); |
| 5397 | if (recname.empty()) recname = "?"; |
| 5398 | S += recname; |
| 5399 | S += '"'; |
| 5400 | } |
| 5401 | S += "^^?"; |
| 5402 | CurOffs += getTypeSize(VoidPtrTy); |
| 5403 | } |
| 5404 | |
| 5405 | if (!RDecl->hasFlexibleArrayMember()) { |
| 5406 | // Mark the end of the structure. |
| 5407 | uint64_t offs = toBits(size); |
| 5408 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 5409 | std::make_pair(offs, (NamedDecl*)0)); |
| 5410 | } |
| 5411 | |
| 5412 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 5413 | assert(CurOffs <= CurLayObj->first); |
| 5414 | |
| 5415 | if (CurOffs < CurLayObj->first) { |
| 5416 | uint64_t padding = CurLayObj->first - CurOffs; |
| 5417 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 5418 | // packing/alignment of members is different that normal, in which case |
| 5419 | // the encoding will be out-of-sync with the real layout. |
| 5420 | // If the runtime switches to just consider the size of types without |
| 5421 | // taking into account alignment, we could make padding explicit in the |
| 5422 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 5423 | // longer then though. |
| 5424 | CurOffs += padding; |
| 5425 | } |
| 5426 | |
| 5427 | NamedDecl *dcl = CurLayObj->second; |
| 5428 | if (dcl == 0) |
| 5429 | break; // reached end of structure. |
| 5430 | |
| 5431 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 5432 | // We expand the bases without their virtual bases since those are going |
| 5433 | // in the initial structure. Note that this differs from gcc which |
| 5434 | // expands virtual bases each time one is encountered in the hierarchy, |
| 5435 | // making the encoding type bigger than it really is. |
| 5436 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 829f200 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5437 | assert(!base->isEmpty()); |
| 5438 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5439 | } else { |
| 5440 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 5441 | if (FD) { |
| 5442 | S += '"'; |
| 5443 | S += field->getNameAsString(); |
| 5444 | S += '"'; |
| 5445 | } |
| 5446 | |
| 5447 | if (field->isBitField()) { |
| 5448 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5449 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 2636197 | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5450 | } else { |
| 5451 | QualType qt = field->getType(); |
| 5452 | getLegacyIntegralTypeEncoding(qt); |
| 5453 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 5454 | /*OutermostType*/false, |
| 5455 | /*EncodingProperty*/false, |
| 5456 | /*StructField*/true); |
| 5457 | CurOffs += getTypeSize(field->getType()); |
| 5458 | } |
| 5459 | } |
| 5460 | } |
| 5461 | } |
| 5462 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5463 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 5464 | std::string& S) const { |
| 5465 | if (QT & Decl::OBJC_TQ_In) |
| 5466 | S += 'n'; |
| 5467 | if (QT & Decl::OBJC_TQ_Inout) |
| 5468 | S += 'N'; |
| 5469 | if (QT & Decl::OBJC_TQ_Out) |
| 5470 | S += 'o'; |
| 5471 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 5472 | S += 'O'; |
| 5473 | if (QT & Decl::OBJC_TQ_Byref) |
| 5474 | S += 'R'; |
| 5475 | if (QT & Decl::OBJC_TQ_Oneway) |
| 5476 | S += 'V'; |
| 5477 | } |
| 5478 | |
Douglas Gregor | 4dfd02a | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 5479 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 5480 | if (!ObjCIdDecl) { |
| 5481 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 5482 | T = getObjCObjectPointerType(T); |
| 5483 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 5484 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5485 | getTranslationUnitDecl(), |
| 5486 | SourceLocation(), SourceLocation(), |
| 5487 | &Idents.get("id"), IdInfo); |
| 5488 | } |
| 5489 | |
| 5490 | return ObjCIdDecl; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 5491 | } |
| 5492 | |
Douglas Gregor | 7a27ea5 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 5493 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 5494 | if (!ObjCSelDecl) { |
| 5495 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 5496 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 5497 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5498 | getTranslationUnitDecl(), |
| 5499 | SourceLocation(), SourceLocation(), |
| 5500 | &Idents.get("SEL"), SelInfo); |
| 5501 | } |
| 5502 | return ObjCSelDecl; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 5503 | } |
| 5504 | |
Douglas Gregor | 79d6726 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 5505 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 5506 | if (!ObjCClassDecl) { |
| 5507 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 5508 | T = getObjCObjectPointerType(T); |
| 5509 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 5510 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5511 | getTranslationUnitDecl(), |
| 5512 | SourceLocation(), SourceLocation(), |
| 5513 | &Idents.get("Class"), ClassInfo); |
| 5514 | } |
| 5515 | |
| 5516 | return ObjCClassDecl; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 5517 | } |
| 5518 | |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 5519 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 5520 | if (!ObjCProtocolClassDecl) { |
| 5521 | ObjCProtocolClassDecl |
| 5522 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 5523 | SourceLocation(), |
| 5524 | &Idents.get("Protocol"), |
| 5525 | /*PrevDecl=*/0, |
| 5526 | SourceLocation(), true); |
| 5527 | } |
| 5528 | |
| 5529 | return ObjCProtocolClassDecl; |
| 5530 | } |
| 5531 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5532 | //===----------------------------------------------------------------------===// |
| 5533 | // __builtin_va_list Construction Functions |
| 5534 | //===----------------------------------------------------------------------===// |
| 5535 | |
| 5536 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5537 | // typedef char* __builtin_va_list; |
| 5538 | QualType CharPtrType = Context->getPointerType(Context->CharTy); |
| 5539 | TypeSourceInfo *TInfo |
| 5540 | = Context->getTrivialTypeSourceInfo(CharPtrType); |
| 5541 | |
| 5542 | TypedefDecl *VaListTypeDecl |
| 5543 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5544 | Context->getTranslationUnitDecl(), |
| 5545 | SourceLocation(), SourceLocation(), |
| 5546 | &Context->Idents.get("__builtin_va_list"), |
| 5547 | TInfo); |
| 5548 | return VaListTypeDecl; |
| 5549 | } |
| 5550 | |
| 5551 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5552 | // typedef void* __builtin_va_list; |
| 5553 | QualType VoidPtrType = Context->getPointerType(Context->VoidTy); |
| 5554 | TypeSourceInfo *TInfo |
| 5555 | = Context->getTrivialTypeSourceInfo(VoidPtrType); |
| 5556 | |
| 5557 | TypedefDecl *VaListTypeDecl |
| 5558 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5559 | Context->getTranslationUnitDecl(), |
| 5560 | SourceLocation(), SourceLocation(), |
| 5561 | &Context->Idents.get("__builtin_va_list"), |
| 5562 | TInfo); |
| 5563 | return VaListTypeDecl; |
| 5564 | } |
| 5565 | |
| 5566 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5567 | // typedef struct __va_list_tag { |
| 5568 | RecordDecl *VaListTagDecl; |
| 5569 | |
| 5570 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5571 | Context->getTranslationUnitDecl(), |
| 5572 | &Context->Idents.get("__va_list_tag")); |
| 5573 | VaListTagDecl->startDefinition(); |
| 5574 | |
| 5575 | const size_t NumFields = 5; |
| 5576 | QualType FieldTypes[NumFields]; |
| 5577 | const char *FieldNames[NumFields]; |
| 5578 | |
| 5579 | // unsigned char gpr; |
| 5580 | FieldTypes[0] = Context->UnsignedCharTy; |
| 5581 | FieldNames[0] = "gpr"; |
| 5582 | |
| 5583 | // unsigned char fpr; |
| 5584 | FieldTypes[1] = Context->UnsignedCharTy; |
| 5585 | FieldNames[1] = "fpr"; |
| 5586 | |
| 5587 | // unsigned short reserved; |
| 5588 | FieldTypes[2] = Context->UnsignedShortTy; |
| 5589 | FieldNames[2] = "reserved"; |
| 5590 | |
| 5591 | // void* overflow_arg_area; |
| 5592 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5593 | FieldNames[3] = "overflow_arg_area"; |
| 5594 | |
| 5595 | // void* reg_save_area; |
| 5596 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 5597 | FieldNames[4] = "reg_save_area"; |
| 5598 | |
| 5599 | // Create fields |
| 5600 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5601 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 5602 | SourceLocation(), |
| 5603 | SourceLocation(), |
| 5604 | &Context->Idents.get(FieldNames[i]), |
| 5605 | FieldTypes[i], /*TInfo=*/0, |
| 5606 | /*BitWidth=*/0, |
| 5607 | /*Mutable=*/false, |
| 5608 | ICIS_NoInit); |
| 5609 | Field->setAccess(AS_public); |
| 5610 | VaListTagDecl->addDecl(Field); |
| 5611 | } |
| 5612 | VaListTagDecl->completeDefinition(); |
| 5613 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5614 | Context->VaListTagTy = VaListTagType; |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5615 | |
| 5616 | // } __va_list_tag; |
| 5617 | TypedefDecl *VaListTagTypedefDecl |
| 5618 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5619 | Context->getTranslationUnitDecl(), |
| 5620 | SourceLocation(), SourceLocation(), |
| 5621 | &Context->Idents.get("__va_list_tag"), |
| 5622 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5623 | QualType VaListTagTypedefType = |
| 5624 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5625 | |
| 5626 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5627 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5628 | QualType VaListTagArrayType |
| 5629 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5630 | Size, ArrayType::Normal, 0); |
| 5631 | TypeSourceInfo *TInfo |
| 5632 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5633 | TypedefDecl *VaListTypedefDecl |
| 5634 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5635 | Context->getTranslationUnitDecl(), |
| 5636 | SourceLocation(), SourceLocation(), |
| 5637 | &Context->Idents.get("__builtin_va_list"), |
| 5638 | TInfo); |
| 5639 | |
| 5640 | return VaListTypedefDecl; |
| 5641 | } |
| 5642 | |
| 5643 | static TypedefDecl * |
| 5644 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5645 | // typedef struct __va_list_tag { |
| 5646 | RecordDecl *VaListTagDecl; |
| 5647 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5648 | Context->getTranslationUnitDecl(), |
| 5649 | &Context->Idents.get("__va_list_tag")); |
| 5650 | VaListTagDecl->startDefinition(); |
| 5651 | |
| 5652 | const size_t NumFields = 4; |
| 5653 | QualType FieldTypes[NumFields]; |
| 5654 | const char *FieldNames[NumFields]; |
| 5655 | |
| 5656 | // unsigned gp_offset; |
| 5657 | FieldTypes[0] = Context->UnsignedIntTy; |
| 5658 | FieldNames[0] = "gp_offset"; |
| 5659 | |
| 5660 | // unsigned fp_offset; |
| 5661 | FieldTypes[1] = Context->UnsignedIntTy; |
| 5662 | FieldNames[1] = "fp_offset"; |
| 5663 | |
| 5664 | // void* overflow_arg_area; |
| 5665 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5666 | FieldNames[2] = "overflow_arg_area"; |
| 5667 | |
| 5668 | // void* reg_save_area; |
| 5669 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5670 | FieldNames[3] = "reg_save_area"; |
| 5671 | |
| 5672 | // Create fields |
| 5673 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5674 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5675 | VaListTagDecl, |
| 5676 | SourceLocation(), |
| 5677 | SourceLocation(), |
| 5678 | &Context->Idents.get(FieldNames[i]), |
| 5679 | FieldTypes[i], /*TInfo=*/0, |
| 5680 | /*BitWidth=*/0, |
| 5681 | /*Mutable=*/false, |
| 5682 | ICIS_NoInit); |
| 5683 | Field->setAccess(AS_public); |
| 5684 | VaListTagDecl->addDecl(Field); |
| 5685 | } |
| 5686 | VaListTagDecl->completeDefinition(); |
| 5687 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5688 | Context->VaListTagTy = VaListTagType; |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5689 | |
| 5690 | // } __va_list_tag; |
| 5691 | TypedefDecl *VaListTagTypedefDecl |
| 5692 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5693 | Context->getTranslationUnitDecl(), |
| 5694 | SourceLocation(), SourceLocation(), |
| 5695 | &Context->Idents.get("__va_list_tag"), |
| 5696 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5697 | QualType VaListTagTypedefType = |
| 5698 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5699 | |
| 5700 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5701 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5702 | QualType VaListTagArrayType |
| 5703 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5704 | Size, ArrayType::Normal,0); |
| 5705 | TypeSourceInfo *TInfo |
| 5706 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5707 | TypedefDecl *VaListTypedefDecl |
| 5708 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5709 | Context->getTranslationUnitDecl(), |
| 5710 | SourceLocation(), SourceLocation(), |
| 5711 | &Context->Idents.get("__builtin_va_list"), |
| 5712 | TInfo); |
| 5713 | |
| 5714 | return VaListTypedefDecl; |
| 5715 | } |
| 5716 | |
| 5717 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5718 | // typedef int __builtin_va_list[4]; |
| 5719 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
| 5720 | QualType IntArrayType |
| 5721 | = Context->getConstantArrayType(Context->IntTy, |
| 5722 | Size, ArrayType::Normal, 0); |
| 5723 | TypedefDecl *VaListTypedefDecl |
| 5724 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5725 | Context->getTranslationUnitDecl(), |
| 5726 | SourceLocation(), SourceLocation(), |
| 5727 | &Context->Idents.get("__builtin_va_list"), |
| 5728 | Context->getTrivialTypeSourceInfo(IntArrayType)); |
| 5729 | |
| 5730 | return VaListTypedefDecl; |
| 5731 | } |
| 5732 | |
Logan Chien | eae5a820 | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 5733 | static TypedefDecl * |
| 5734 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5735 | RecordDecl *VaListDecl; |
| 5736 | if (Context->getLangOpts().CPlusPlus) { |
| 5737 | // namespace std { struct __va_list { |
| 5738 | NamespaceDecl *NS; |
| 5739 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 5740 | Context->getTranslationUnitDecl(), |
| 5741 | /*Inline*/false, SourceLocation(), |
| 5742 | SourceLocation(), &Context->Idents.get("std"), |
| 5743 | /*PrevDecl*/0); |
| 5744 | |
| 5745 | VaListDecl = CXXRecordDecl::Create(*Context, TTK_Struct, |
| 5746 | Context->getTranslationUnitDecl(), |
| 5747 | SourceLocation(), SourceLocation(), |
| 5748 | &Context->Idents.get("__va_list")); |
| 5749 | |
| 5750 | VaListDecl->setDeclContext(NS); |
| 5751 | |
| 5752 | } else { |
| 5753 | // struct __va_list { |
| 5754 | VaListDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5755 | Context->getTranslationUnitDecl(), |
| 5756 | &Context->Idents.get("__va_list")); |
| 5757 | } |
| 5758 | |
| 5759 | VaListDecl->startDefinition(); |
| 5760 | |
| 5761 | // void * __ap; |
| 5762 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5763 | VaListDecl, |
| 5764 | SourceLocation(), |
| 5765 | SourceLocation(), |
| 5766 | &Context->Idents.get("__ap"), |
| 5767 | Context->getPointerType(Context->VoidTy), |
| 5768 | /*TInfo=*/0, |
| 5769 | /*BitWidth=*/0, |
| 5770 | /*Mutable=*/false, |
| 5771 | ICIS_NoInit); |
| 5772 | Field->setAccess(AS_public); |
| 5773 | VaListDecl->addDecl(Field); |
| 5774 | |
| 5775 | // }; |
| 5776 | VaListDecl->completeDefinition(); |
| 5777 | |
| 5778 | // typedef struct __va_list __builtin_va_list; |
| 5779 | TypeSourceInfo *TInfo |
| 5780 | = Context->getTrivialTypeSourceInfo(Context->getRecordType(VaListDecl)); |
| 5781 | |
| 5782 | TypedefDecl *VaListTypeDecl |
| 5783 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5784 | Context->getTranslationUnitDecl(), |
| 5785 | SourceLocation(), SourceLocation(), |
| 5786 | &Context->Idents.get("__builtin_va_list"), |
| 5787 | TInfo); |
| 5788 | |
| 5789 | return VaListTypeDecl; |
| 5790 | } |
| 5791 | |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5792 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 5793 | TargetInfo::BuiltinVaListKind Kind) { |
| 5794 | switch (Kind) { |
| 5795 | case TargetInfo::CharPtrBuiltinVaList: |
| 5796 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 5797 | case TargetInfo::VoidPtrBuiltinVaList: |
| 5798 | return CreateVoidPtrBuiltinVaListDecl(Context); |
| 5799 | case TargetInfo::PowerABIBuiltinVaList: |
| 5800 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 5801 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 5802 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 5803 | case TargetInfo::PNaClABIBuiltinVaList: |
| 5804 | return CreatePNaClABIBuiltinVaListDecl(Context); |
Logan Chien | eae5a820 | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 5805 | case TargetInfo::AAPCSABIBuiltinVaList: |
| 5806 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
Meador Inge | c5613b2 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5807 | } |
| 5808 | |
| 5809 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 5810 | } |
| 5811 | |
| 5812 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
| 5813 | if (!BuiltinVaListDecl) |
| 5814 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
| 5815 | |
| 5816 | return BuiltinVaListDecl; |
| 5817 | } |
| 5818 | |
Meador Inge | fb40e3f | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5819 | QualType ASTContext::getVaListTagType() const { |
| 5820 | // Force the creation of VaListTagTy by building the __builtin_va_list |
| 5821 | // declaration. |
| 5822 | if (VaListTagTy.isNull()) |
| 5823 | (void) getBuiltinVaListDecl(); |
| 5824 | |
| 5825 | return VaListTagTy; |
| 5826 | } |
| 5827 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5828 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5829 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 5830 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5831 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5832 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 5833 | } |
| 5834 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5835 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 5836 | /// lookup. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5837 | TemplateName |
| 5838 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 5839 | UnresolvedSetIterator End) const { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5840 | unsigned size = End - Begin; |
| 5841 | assert(size > 1 && "set is not overloaded!"); |
| 5842 | |
| 5843 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 5844 | size * sizeof(FunctionTemplateDecl*)); |
| 5845 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 5846 | |
| 5847 | NamedDecl **Storage = OT->getStorage(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 5848 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5849 | NamedDecl *D = *I; |
| 5850 | assert(isa<FunctionTemplateDecl>(D) || |
| 5851 | (isa<UsingShadowDecl>(D) && |
| 5852 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 5853 | *Storage++ = D; |
| 5854 | } |
| 5855 | |
| 5856 | return TemplateName(OT); |
| 5857 | } |
| 5858 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5859 | /// \brief Retrieve the template name that represents a qualified |
| 5860 | /// template name such as \c std::vector. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5861 | TemplateName |
| 5862 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 5863 | bool TemplateKeyword, |
| 5864 | TemplateDecl *Template) const { |
Douglas Gregor | 0f0ea2a | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 5865 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 5866 | |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5867 | // FIXME: Canonicalization? |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5868 | llvm::FoldingSetNodeID ID; |
| 5869 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 5870 | |
| 5871 | void *InsertPos = 0; |
| 5872 | QualifiedTemplateName *QTN = |
| 5873 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5874 | if (!QTN) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 5875 | QTN = new (*this, llvm::alignOf<QualifiedTemplateName>()) |
| 5876 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5877 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 5878 | } |
| 5879 | |
| 5880 | return TemplateName(QTN); |
| 5881 | } |
| 5882 | |
| 5883 | /// \brief Retrieve the template name that represents a dependent |
| 5884 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5885 | TemplateName |
| 5886 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 5887 | const IdentifierInfo *Name) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5888 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 5889 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5890 | |
| 5891 | llvm::FoldingSetNodeID ID; |
| 5892 | DependentTemplateName::Profile(ID, NNS, Name); |
| 5893 | |
| 5894 | void *InsertPos = 0; |
| 5895 | DependentTemplateName *QTN = |
| 5896 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5897 | |
| 5898 | if (QTN) |
| 5899 | return TemplateName(QTN); |
| 5900 | |
| 5901 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5902 | if (CanonNNS == NNS) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 5903 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 5904 | DependentTemplateName(NNS, Name); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5905 | } else { |
| 5906 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 5907 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 5908 | DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5909 | DependentTemplateName *CheckQTN = |
| 5910 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5911 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 5912 | (void)CheckQTN; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5913 | } |
| 5914 | |
| 5915 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5916 | return TemplateName(QTN); |
| 5917 | } |
| 5918 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5919 | /// \brief Retrieve the template name that represents a dependent |
| 5920 | /// template name such as \c MetaFun::template operator+. |
| 5921 | TemplateName |
| 5922 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5923 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5924 | assert((!NNS || NNS->isDependent()) && |
| 5925 | "Nested name specifier must be dependent"); |
| 5926 | |
| 5927 | llvm::FoldingSetNodeID ID; |
| 5928 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 5929 | |
| 5930 | void *InsertPos = 0; |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5931 | DependentTemplateName *QTN |
| 5932 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5933 | |
| 5934 | if (QTN) |
| 5935 | return TemplateName(QTN); |
| 5936 | |
| 5937 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5938 | if (CanonNNS == NNS) { |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 5939 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 5940 | DependentTemplateName(NNS, Operator); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5941 | } else { |
| 5942 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
Richard Smith | 2f47cab | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 5943 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 5944 | DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | 789b1f6 | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5945 | |
| 5946 | DependentTemplateName *CheckQTN |
| 5947 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5948 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 5949 | (void)CheckQTN; |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5950 | } |
| 5951 | |
| 5952 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5953 | return TemplateName(QTN); |
| 5954 | } |
| 5955 | |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5956 | TemplateName |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5957 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 5958 | TemplateName replacement) const { |
| 5959 | llvm::FoldingSetNodeID ID; |
| 5960 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 5961 | |
| 5962 | void *insertPos = 0; |
| 5963 | SubstTemplateTemplateParmStorage *subst |
| 5964 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 5965 | |
| 5966 | if (!subst) { |
| 5967 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 5968 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 5969 | } |
| 5970 | |
| 5971 | return TemplateName(subst); |
| 5972 | } |
| 5973 | |
| 5974 | TemplateName |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5975 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 5976 | const TemplateArgument &ArgPack) const { |
| 5977 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 5978 | llvm::FoldingSetNodeID ID; |
| 5979 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 5980 | |
| 5981 | void *InsertPos = 0; |
| 5982 | SubstTemplateTemplateParmPackStorage *Subst |
| 5983 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 5984 | |
| 5985 | if (!Subst) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5986 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5987 | ArgPack.pack_size(), |
| 5988 | ArgPack.pack_begin()); |
| 5989 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 5990 | } |
| 5991 | |
| 5992 | return TemplateName(Subst); |
| 5993 | } |
| 5994 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5995 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 5996 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 5997 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5998 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5999 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6000 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6001 | case TargetInfo::SignedShort: return ShortTy; |
| 6002 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 6003 | case TargetInfo::SignedInt: return IntTy; |
| 6004 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 6005 | case TargetInfo::SignedLong: return LongTy; |
| 6006 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 6007 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 6008 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 6009 | } |
| 6010 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6011 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6012 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 6013 | |
| 6014 | //===----------------------------------------------------------------------===// |
| 6015 | // Type Predicates. |
| 6016 | //===----------------------------------------------------------------------===// |
| 6017 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6018 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 6019 | /// garbage collection attribute. |
| 6020 | /// |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6021 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6022 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6023 | return Qualifiers::GCNone; |
| 6024 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6025 | assert(getLangOpts().ObjC1); |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6026 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 6027 | |
| 6028 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 6029 | // (or pointers to them) be treated as though they were declared |
| 6030 | // as __strong. |
| 6031 | if (GCAttrs == Qualifiers::GCNone) { |
| 6032 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 6033 | return Qualifiers::Strong; |
| 6034 | else if (Ty->isPointerType()) |
| 6035 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 6036 | } else { |
| 6037 | // It's not valid to set GC attributes on anything that isn't a |
| 6038 | // pointer. |
| 6039 | #ifndef NDEBUG |
| 6040 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 6041 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 6042 | CT = AT->getElementType(); |
| 6043 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 6044 | #endif |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6045 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 6046 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6049 | //===----------------------------------------------------------------------===// |
| 6050 | // Type Compatibility Testing |
| 6051 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 6052 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6053 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6054 | /// compatible. |
| 6055 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 6056 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 6057 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6058 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6059 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6060 | } |
| 6061 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6062 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 6063 | QualType SecondVec) { |
| 6064 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 6065 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 6066 | |
| 6067 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 6068 | return true; |
| 6069 | |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6070 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 6071 | // equivalent GCC vector types. |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6072 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 6073 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6074 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6075 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | f69eb7c | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6076 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 6077 | First->getVectorKind() != VectorType::AltiVecBool && |
| 6078 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 6079 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6080 | return true; |
| 6081 | |
| 6082 | return false; |
| 6083 | } |
| 6084 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6085 | //===----------------------------------------------------------------------===// |
| 6086 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 6087 | //===----------------------------------------------------------------------===// |
| 6088 | |
| 6089 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 6090 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6091 | bool |
| 6092 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 6093 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 3fc73ee | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 6094 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6095 | return true; |
| 6096 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 6097 | E = rProto->protocol_end(); PI != E; ++PI) |
| 6098 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 6099 | return true; |
| 6100 | return false; |
| 6101 | } |
| 6102 | |
Dmitri Gribenko | 4c3b8a3 | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 6103 | /// QualifiedIdConformsQualifiedId - compare id<pr,...> with id<pr1,...> |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6104 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 6105 | /// otherwise. |
| 6106 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 6107 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 6108 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 6109 | return false; |
| 6110 | } |
| 6111 | |
Dmitri Gribenko | 4c3b8a3 | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 6112 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
| 6113 | /// Class<pr1, ...>. |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6114 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 6115 | QualType rhs) { |
| 6116 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 6117 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 6118 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 6119 | |
| 6120 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6121 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6122 | bool match = false; |
| 6123 | ObjCProtocolDecl *lhsProto = *I; |
| 6124 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 6125 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 6126 | ObjCProtocolDecl *rhsProto = *J; |
| 6127 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 6128 | match = true; |
| 6129 | break; |
| 6130 | } |
| 6131 | } |
| 6132 | if (!match) |
| 6133 | return false; |
| 6134 | } |
| 6135 | return true; |
| 6136 | } |
| 6137 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6138 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 6139 | /// ObjCQualifiedIDType. |
| 6140 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 6141 | bool compare) { |
| 6142 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6143 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6144 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 6145 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6146 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6147 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 6148 | return true; |
| 6149 | |
| 6150 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6151 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6152 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6153 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6154 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6155 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6156 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6157 | // make sure we check the class hierarchy. |
| 6158 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 6159 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6160 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6161 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6162 | // see if static class implements all of id's protocols, directly or |
| 6163 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 6164 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6165 | return false; |
| 6166 | } |
| 6167 | } |
| 6168 | // If there are no qualifiers and no interface, we have an 'id'. |
| 6169 | return true; |
| 6170 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6171 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6172 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6173 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6174 | ObjCProtocolDecl *lhsProto = *I; |
| 6175 | bool match = false; |
| 6176 | |
| 6177 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6178 | // see if static class implements all of id's protocols, directly or |
| 6179 | // through its super class and categories. |
| 6180 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 6181 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 6182 | ObjCProtocolDecl *rhsProto = *J; |
| 6183 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6184 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6185 | match = true; |
| 6186 | break; |
| 6187 | } |
| 6188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6189 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6190 | // make sure we check the class hierarchy. |
| 6191 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 6192 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 6193 | E = lhsQID->qual_end(); I != E; ++I) { |
| 6194 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6195 | // see if static class implements all of id's protocols, directly or |
| 6196 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 6197 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6198 | match = true; |
| 6199 | break; |
| 6200 | } |
| 6201 | } |
| 6202 | } |
| 6203 | if (!match) |
| 6204 | return false; |
| 6205 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6206 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6207 | return true; |
| 6208 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6209 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6210 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 6211 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 6212 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6213 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6214 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6215 | // If both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6216 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 6217 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 6218 | ObjCProtocolDecl *lhsProto = *I; |
| 6219 | bool match = false; |
| 6220 | |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6221 | // 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] | 6222 | // see if static class implements all of id's protocols, directly or |
| 6223 | // through its super class and categories. |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6224 | // First, lhs protocols in the qualifier list must be found, direct |
| 6225 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6226 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 6227 | E = rhsQID->qual_end(); J != E; ++J) { |
| 6228 | ObjCProtocolDecl *rhsProto = *J; |
| 6229 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6230 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6231 | match = true; |
| 6232 | break; |
| 6233 | } |
| 6234 | } |
| 6235 | if (!match) |
| 6236 | return false; |
| 6237 | } |
Fariborz Jahanian | de5b17e | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6238 | |
| 6239 | // Static class's protocols, or its super class or category protocols |
| 6240 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 6241 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 6242 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 6243 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 6244 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 6245 | // no type qualifier and its class has no static protocol(s) |
| 6246 | // assume that it is mismatch. |
| 6247 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 6248 | return false; |
| 6249 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6250 | LHSInheritedProtocols.begin(), |
| 6251 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 6252 | bool match = false; |
| 6253 | ObjCProtocolDecl *lhsProto = (*I); |
| 6254 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 6255 | E = rhsQID->qual_end(); J != E; ++J) { |
| 6256 | ObjCProtocolDecl *rhsProto = *J; |
| 6257 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6258 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6259 | match = true; |
| 6260 | break; |
| 6261 | } |
| 6262 | } |
| 6263 | if (!match) |
| 6264 | return false; |
| 6265 | } |
| 6266 | } |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6267 | return true; |
| 6268 | } |
| 6269 | return false; |
| 6270 | } |
| 6271 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6272 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6273 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 6274 | /// protocol qualifiers on the LHS or RHS. |
| 6275 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6276 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 6277 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6278 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6279 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6280 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 6281 | // 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] | 6282 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 6283 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6284 | return true; |
| 6285 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6286 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6287 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6288 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6289 | false); |
Fariborz Jahanian | a8f8dac | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6290 | |
| 6291 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 6292 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 6293 | QualType(RHSOPT,0)); |
| 6294 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6295 | // If we have 2 user-defined types, fall into that path. |
| 6296 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6297 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6298 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6299 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6300 | } |
| 6301 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6302 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 6303 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6304 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 6305 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 6306 | /// not OK. For the return type, the opposite is not OK. |
| 6307 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 6308 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6309 | const ObjCObjectPointerType *RHSOPT, |
| 6310 | bool BlockReturnType) { |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6311 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6312 | return true; |
| 6313 | |
| 6314 | if (LHSOPT->isObjCBuiltinType()) { |
| 6315 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 6316 | } |
| 6317 | |
Fariborz Jahanian | a983448 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6318 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6319 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6320 | QualType(RHSOPT,0), |
| 6321 | false); |
| 6322 | |
| 6323 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 6324 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 6325 | if (LHS && RHS) { // We have 2 user-defined types. |
| 6326 | if (LHS != RHS) { |
| 6327 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6328 | return BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6329 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6330 | return !BlockReturnType; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6331 | } |
| 6332 | else |
| 6333 | return true; |
| 6334 | } |
| 6335 | return false; |
| 6336 | } |
| 6337 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6338 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 6339 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 6340 | /// It is used to build composite qualifier list of the composite type of |
| 6341 | /// the conditional expression involving two objective-c pointer objects. |
| 6342 | static |
| 6343 | void getIntersectionOfProtocols(ASTContext &Context, |
| 6344 | const ObjCObjectPointerType *LHSOPT, |
| 6345 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6346 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6347 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6348 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6349 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6350 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 6351 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6352 | |
| 6353 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 6354 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 6355 | if (LHSNumProtocols > 0) |
| 6356 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 6357 | else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6358 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6359 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 6360 | LHSInheritedProtocols); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6361 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 6362 | LHSInheritedProtocols.end()); |
| 6363 | } |
| 6364 | |
| 6365 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 6366 | if (RHSNumProtocols > 0) { |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 6367 | ObjCProtocolDecl **RHSProtocols = |
| 6368 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6369 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 6370 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 6371 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 6372 | } else { |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6373 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6374 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 6375 | RHSInheritedProtocols); |
Fariborz Jahanian | 432a889 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6376 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6377 | RHSInheritedProtocols.begin(), |
| 6378 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 6379 | if (InheritedProtocolSet.count((*I))) |
| 6380 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6381 | } |
| 6382 | } |
| 6383 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6384 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 6385 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 6386 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 6387 | /// warning is issued. So, its invokation is extremely rare. |
| 6388 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6389 | const ObjCObjectPointerType *Lptr, |
| 6390 | const ObjCObjectPointerType *Rptr) { |
| 6391 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 6392 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 6393 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 6394 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 6395 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6396 | return QualType(); |
| 6397 | |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 6398 | do { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6399 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6400 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6401 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6402 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 6403 | |
| 6404 | QualType Result = QualType(LHS, 0); |
| 6405 | if (!Protocols.empty()) |
| 6406 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 6407 | Result = getObjCObjectPointerType(Result); |
| 6408 | return Result; |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6409 | } |
Fariborz Jahanian | 7c2bdcb | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 6410 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6411 | |
| 6412 | return QualType(); |
| 6413 | } |
| 6414 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6415 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 6416 | const ObjCObjectType *RHS) { |
| 6417 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 6418 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 6419 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6420 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 6421 | // the LHS. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6422 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6423 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6424 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6425 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 6426 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 6427 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6428 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6429 | |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6430 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 6431 | // more detailed analysis is required. |
| 6432 | if (RHS->getNumProtocols() == 0) { |
| 6433 | // OK, if LHS is a superclass of RHS *and* |
| 6434 | // this superclass is assignment compatible with LHS. |
| 6435 | // false otherwise. |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 6436 | bool IsSuperClass = |
| 6437 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 6438 | if (IsSuperClass) { |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6439 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 6440 | // ; i.e., SuperClass may implement at least one of the protocols |
| 6441 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 6442 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 6443 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 627788c | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 6444 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | b7bc34a | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 6445 | // If super class has no protocols, it is not a match. |
| 6446 | if (SuperClassInheritedProtocols.empty()) |
| 6447 | return false; |
| 6448 | |
| 6449 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 6450 | LHSPE = LHS->qual_end(); |
| 6451 | LHSPI != LHSPE; LHSPI++) { |
| 6452 | bool SuperImplementsProtocol = false; |
| 6453 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 6454 | |
| 6455 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 6456 | SuperClassInheritedProtocols.begin(), |
| 6457 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 6458 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 6459 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 6460 | SuperImplementsProtocol = true; |
| 6461 | break; |
| 6462 | } |
| 6463 | } |
| 6464 | if (!SuperImplementsProtocol) |
| 6465 | return false; |
| 6466 | } |
| 6467 | return true; |
| 6468 | } |
| 6469 | return false; |
| 6470 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6471 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6472 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 6473 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6474 | LHSPI != LHSPE; LHSPI++) { |
| 6475 | bool RHSImplementsProtocol = false; |
| 6476 | |
| 6477 | // If the RHS doesn't implement the protocol on the left, the types |
| 6478 | // are incompatible. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6479 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 6480 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 6481 | RHSPI != RHSPE; RHSPI++) { |
| 6482 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6483 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 6484 | break; |
| 6485 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6486 | } |
| 6487 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 6488 | if (!RHSImplementsProtocol) |
| 6489 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6490 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6491 | // The RHS implements all protocols listed on the LHS. |
| 6492 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6493 | } |
| 6494 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6495 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 6496 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6497 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 6498 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6499 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6500 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6501 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6502 | |
| 6503 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 6504 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6505 | } |
| 6506 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6507 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 6508 | return canAssignObjCInterfaces( |
| 6509 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 6510 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 6511 | } |
| 6512 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6513 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6514 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6515 | /// 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] | 6516 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6517 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 6518 | bool CompareUnqualified) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6519 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6520 | return hasSameType(LHS, RHS); |
| 6521 | |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6522 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6523 | } |
| 6524 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6525 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | 8237839 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 6526 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6527 | } |
| 6528 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6529 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 6530 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 6531 | } |
| 6532 | |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6533 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 6534 | /// of T is compatible with SubType, return the merged type, else return |
| 6535 | /// QualType() |
| 6536 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 6537 | bool OfBlockPointer, |
| 6538 | bool Unqualified) { |
| 6539 | if (const RecordType *UT = T->getAsUnionType()) { |
| 6540 | RecordDecl *UD = UT->getDecl(); |
| 6541 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 6542 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6543 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | f91d757 | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 6544 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6545 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 6546 | if (!MT.isNull()) |
| 6547 | return MT; |
| 6548 | } |
| 6549 | } |
| 6550 | } |
| 6551 | |
| 6552 | return QualType(); |
| 6553 | } |
| 6554 | |
| 6555 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 6556 | /// argument types |
| 6557 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 6558 | bool OfBlockPointer, |
| 6559 | bool Unqualified) { |
| 6560 | // GNU extension: two types are compatible if they appear as a function |
| 6561 | // argument, one of the types is a transparent union type and the other |
| 6562 | // type is compatible with a union member |
| 6563 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 6564 | Unqualified); |
| 6565 | if (!lmerge.isNull()) |
| 6566 | return lmerge; |
| 6567 | |
| 6568 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 6569 | Unqualified); |
| 6570 | if (!rmerge.isNull()) |
| 6571 | return rmerge; |
| 6572 | |
| 6573 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 6574 | } |
| 6575 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6576 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6577 | bool OfBlockPointer, |
| 6578 | bool Unqualified) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6579 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 6580 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6581 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 6582 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6583 | bool allLTypes = true; |
| 6584 | bool allRTypes = true; |
| 6585 | |
| 6586 | // Check return type |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6587 | QualType retType; |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6588 | if (OfBlockPointer) { |
| 6589 | QualType RHS = rbase->getResultType(); |
| 6590 | QualType LHS = lbase->getResultType(); |
| 6591 | bool UnqualifiedResult = Unqualified; |
| 6592 | if (!UnqualifiedResult) |
| 6593 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6594 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | d263fd1 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6595 | } |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6596 | else |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6597 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 6598 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6599 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6600 | |
| 6601 | if (Unqualified) |
| 6602 | retType = retType.getUnqualifiedType(); |
| 6603 | |
| 6604 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 6605 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 6606 | if (Unqualified) { |
| 6607 | LRetType = LRetType.getUnqualifiedType(); |
| 6608 | RRetType = RRetType.getUnqualifiedType(); |
| 6609 | } |
| 6610 | |
| 6611 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6612 | allLTypes = false; |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6613 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6614 | allRTypes = false; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6615 | |
Daniel Dunbar | 6a15c85 | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 6616 | // FIXME: double check this |
| 6617 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 6618 | // rbase->getRegParmAttr() != 0 && |
| 6619 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 6620 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 6621 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6622 | |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6623 | // Compatible functions must have compatible calling conventions |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6624 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6625 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6626 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6627 | // Regparm is part of the calling convention. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 6628 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 6629 | return QualType(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6630 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 6631 | return QualType(); |
| 6632 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6633 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 6634 | return QualType(); |
| 6635 | |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6636 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 6637 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6638 | |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 6639 | if (lbaseInfo.getNoReturn() != NoReturn) |
| 6640 | allLTypes = false; |
| 6641 | if (rbaseInfo.getNoReturn() != NoReturn) |
| 6642 | allRTypes = false; |
| 6643 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6644 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6645 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6646 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6647 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 6648 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6649 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 6650 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 6651 | |
| 6652 | // Compatible functions must have the same number of arguments |
| 6653 | if (lproto_nargs != rproto_nargs) |
| 6654 | return QualType(); |
| 6655 | |
| 6656 | // Variadic and non-variadic functions aren't compatible |
| 6657 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 6658 | return QualType(); |
| 6659 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 6660 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 6661 | return QualType(); |
| 6662 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6663 | if (LangOpts.ObjCAutoRefCount && |
| 6664 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 6665 | return QualType(); |
| 6666 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6667 | // Check argument compatibility |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6668 | SmallVector<QualType, 10> types; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6669 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 6670 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 6671 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | 4846675 | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6672 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 6673 | OfBlockPointer, |
| 6674 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6675 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6676 | |
| 6677 | if (Unqualified) |
| 6678 | argtype = argtype.getUnqualifiedType(); |
| 6679 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6680 | types.push_back(argtype); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6681 | if (Unqualified) { |
| 6682 | largtype = largtype.getUnqualifiedType(); |
| 6683 | rargtype = rargtype.getUnqualifiedType(); |
| 6684 | } |
| 6685 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6686 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 6687 | allLTypes = false; |
| 6688 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 6689 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6690 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6691 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6692 | if (allLTypes) return lhs; |
| 6693 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6694 | |
| 6695 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 6696 | EPI.ExtInfo = einfo; |
| 6697 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6698 | } |
| 6699 | |
| 6700 | if (lproto) allRTypes = false; |
| 6701 | if (rproto) allLTypes = false; |
| 6702 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6703 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6704 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6705 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6706 | if (proto->isVariadic()) return QualType(); |
| 6707 | // Check that the types are compatible with the types that |
| 6708 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 6709 | // The only types actually affected are promotable integer |
| 6710 | // types and floats, which would be passed as a different |
| 6711 | // type depending on whether the prototype is visible. |
| 6712 | unsigned proto_nargs = proto->getNumArgs(); |
| 6713 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 6714 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 6715 | |
Eli Friedman | c586d5d | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 6716 | // 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] | 6717 | // to pass enum values. |
Eli Friedman | c586d5d | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 6718 | if (const EnumType *Enum = argTy->getAs<EnumType>()) { |
| 6719 | argTy = Enum->getDecl()->getIntegerType(); |
| 6720 | if (argTy.isNull()) |
| 6721 | return QualType(); |
| 6722 | } |
Douglas Gregor | b0f8eac | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 6723 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6724 | if (argTy->isPromotableIntegerType() || |
| 6725 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 6726 | return QualType(); |
| 6727 | } |
| 6728 | |
| 6729 | if (allLTypes) return lhs; |
| 6730 | if (allRTypes) return rhs; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6731 | |
| 6732 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 6733 | EPI.ExtInfo = einfo; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6734 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6735 | proto->getNumArgs(), EPI); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6736 | } |
| 6737 | |
| 6738 | if (allLTypes) return lhs; |
| 6739 | if (allRTypes) return rhs; |
John McCall | 8cc246c | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6740 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6741 | } |
| 6742 | |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6743 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6744 | bool OfBlockPointer, |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6745 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 6746 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 6747 | // type is adjusted to "T" prior to any further analysis, the expression |
| 6748 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6749 | // expression is an lvalue unless the reference is an rvalue reference and |
| 6750 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6751 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 6752 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6753 | |
| 6754 | if (Unqualified) { |
| 6755 | LHS = LHS.getUnqualifiedType(); |
| 6756 | RHS = RHS.getUnqualifiedType(); |
| 6757 | } |
Douglas Gregor | 0e709ab | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6758 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6759 | QualType LHSCan = getCanonicalType(LHS), |
| 6760 | RHSCan = getCanonicalType(RHS); |
| 6761 | |
| 6762 | // If two types are identical, they are compatible. |
| 6763 | if (LHSCan == RHSCan) |
| 6764 | return LHS; |
| 6765 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6766 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6767 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6768 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6769 | if (LQuals != RQuals) { |
| 6770 | // If any of these qualifiers are different, we have a type |
| 6771 | // mismatch. |
| 6772 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6773 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 6774 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6775 | return QualType(); |
| 6776 | |
| 6777 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6778 | // okay if the other type has no GC qualifier but is an Objective |
| 6779 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6780 | // this by pretending that the unqualified type was actually |
| 6781 | // qualified __strong. |
| 6782 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6783 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6784 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6785 | |
| 6786 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6787 | return QualType(); |
| 6788 | |
| 6789 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 6790 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 6791 | } |
| 6792 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 6793 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 6794 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6795 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6796 | } |
| 6797 | |
| 6798 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6799 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 6800 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 6801 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6802 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6803 | // We want to consider the two function types to be the same for these |
| 6804 | // comparisons, just force one to the other. |
| 6805 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 6806 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 6807 | |
| 6808 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 6809 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 6810 | LHSClass = Type::ConstantArray; |
| 6811 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 6812 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6813 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6814 | // ObjCInterfaces are just specialized ObjCObjects. |
| 6815 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 6816 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 6817 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 6818 | // Canonicalize ExtVector -> Vector. |
| 6819 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 6820 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6821 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 6822 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6823 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6824 | // 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] | 6825 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 6826 | // Compatibility is based on the underlying type, not the promotion |
| 6827 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6828 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Fariborz Jahanian | b918d6b | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 6829 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 6830 | if (!TINT.isNull() && hasSameType(TINT, RHSCan.getUnqualifiedType())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6831 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 6832 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6833 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Fariborz Jahanian | b918d6b | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 6834 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 6835 | if (!TINT.isNull() && hasSameType(TINT, LHSCan.getUnqualifiedType())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6836 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 6837 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 6838 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 4196363 | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 6839 | if (OfBlockPointer && !BlockReturnType) { |
| 6840 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 6841 | return LHS; |
| 6842 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 6843 | return RHS; |
| 6844 | } |
Fariborz Jahanian | e7cff2c | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 6845 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6846 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6847 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6848 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 6849 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6850 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6851 | #define TYPE(Class, Base) |
| 6852 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 6853 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6854 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 6855 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 6856 | #include "clang/AST/TypeNodes.def" |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6857 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6858 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6859 | case Type::LValueReference: |
| 6860 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6861 | case Type::MemberPointer: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6862 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6863 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6864 | case Type::ObjCInterface: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6865 | case Type::IncompleteArray: |
| 6866 | case Type::VariableArray: |
| 6867 | case Type::FunctionProto: |
| 6868 | case Type::ExtVector: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6869 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6870 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6871 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6872 | { |
| 6873 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6874 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 6875 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6876 | if (Unqualified) { |
| 6877 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 6878 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 6879 | } |
| 6880 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 6881 | Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6882 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 6883 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6884 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 6885 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6886 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6887 | return getPointerType(ResultType); |
| 6888 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6889 | case Type::BlockPointer: |
| 6890 | { |
| 6891 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6892 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 6893 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6894 | if (Unqualified) { |
| 6895 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 6896 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 6897 | } |
| 6898 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 6899 | Unqualified); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6900 | if (ResultType.isNull()) return QualType(); |
| 6901 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 6902 | return LHS; |
| 6903 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 6904 | return RHS; |
| 6905 | return getBlockPointerType(ResultType); |
| 6906 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6907 | case Type::Atomic: |
| 6908 | { |
| 6909 | // Merge two pointer types, while trying to preserve typedef info |
| 6910 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 6911 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 6912 | if (Unqualified) { |
| 6913 | LHSValue = LHSValue.getUnqualifiedType(); |
| 6914 | RHSValue = RHSValue.getUnqualifiedType(); |
| 6915 | } |
| 6916 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 6917 | Unqualified); |
| 6918 | if (ResultType.isNull()) return QualType(); |
| 6919 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 6920 | return LHS; |
| 6921 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 6922 | return RHS; |
| 6923 | return getAtomicType(ResultType); |
| 6924 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6925 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6926 | { |
| 6927 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 6928 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 6929 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 6930 | return QualType(); |
| 6931 | |
| 6932 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 6933 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6934 | if (Unqualified) { |
| 6935 | LHSElem = LHSElem.getUnqualifiedType(); |
| 6936 | RHSElem = RHSElem.getUnqualifiedType(); |
| 6937 | } |
| 6938 | |
| 6939 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6940 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6941 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6942 | return LHS; |
| 6943 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6944 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6945 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 6946 | ArrayType::ArraySizeModifier(), 0); |
| 6947 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 6948 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6949 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 6950 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6951 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6952 | return LHS; |
| 6953 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6954 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6955 | if (LVAT) { |
| 6956 | // FIXME: This isn't correct! But tricky to implement because |
| 6957 | // the array's size has to be the size of LHS, but the type |
| 6958 | // has to be different. |
| 6959 | return LHS; |
| 6960 | } |
| 6961 | if (RVAT) { |
| 6962 | // FIXME: This isn't correct! But tricky to implement because |
| 6963 | // the array's size has to be the size of RHS, but the type |
| 6964 | // has to be different. |
| 6965 | return RHS; |
| 6966 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6967 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 6968 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 6969 | return getIncompleteArrayType(ResultType, |
| 6970 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6971 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6972 | case Type::FunctionNoProto: |
Douglas Gregor | 447234d | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6973 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6974 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6975 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6976 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6977 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6978 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6979 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 6980 | case Type::Complex: |
| 6981 | // Distinct complex types are incompatible. |
| 6982 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6983 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6984 | // FIXME: The merged type should be an ExtVector! |
John McCall | 1c471f3 | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 6985 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 6986 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6987 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6988 | return QualType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6989 | case Type::ObjCObject: { |
| 6990 | // Check if the types are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6991 | // FIXME: This should be type compatibility, e.g. whether |
| 6992 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6993 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 6994 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 6995 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 6996 | return LHS; |
| 6997 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6998 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 6999 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7000 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7001 | if (OfBlockPointer) { |
| 7002 | if (canAssignObjCInterfacesInBlockPointer( |
| 7003 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | a4fdbfa | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7004 | RHS->getAs<ObjCObjectPointerType>(), |
| 7005 | BlockReturnType)) |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7006 | return LHS; |
Fariborz Jahanian | 132f2a2 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7007 | return QualType(); |
| 7008 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7009 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 7010 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7011 | return LHS; |
| 7012 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 7013 | return QualType(); |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7014 | } |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7015 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7016 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7017 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7018 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 7019 | |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7020 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 7021 | const FunctionProtoType *FromFunctionType, |
| 7022 | const FunctionProtoType *ToFunctionType) { |
| 7023 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 7024 | ToFunctionType->hasAnyConsumedArgs()) |
| 7025 | return false; |
| 7026 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 7027 | FromFunctionType->getExtProtoInfo(); |
| 7028 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 7029 | ToFunctionType->getExtProtoInfo(); |
| 7030 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 7031 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 7032 | ArgIdx != NumArgs; ++ArgIdx) { |
| 7033 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 7034 | ToEPI.ConsumedArguments[ArgIdx]) |
| 7035 | return false; |
| 7036 | } |
| 7037 | return true; |
| 7038 | } |
| 7039 | |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7040 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 7041 | /// 'RHS' attributes and returns the merged version; including for function |
| 7042 | /// return types. |
| 7043 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 7044 | QualType LHSCan = getCanonicalType(LHS), |
| 7045 | RHSCan = getCanonicalType(RHS); |
| 7046 | // If two types are identical, they are compatible. |
| 7047 | if (LHSCan == RHSCan) |
| 7048 | return LHS; |
| 7049 | if (RHSCan->isFunctionType()) { |
| 7050 | if (!LHSCan->isFunctionType()) |
| 7051 | return QualType(); |
| 7052 | QualType OldReturnType = |
| 7053 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 7054 | QualType NewReturnType = |
| 7055 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 7056 | QualType ResReturnType = |
| 7057 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 7058 | if (ResReturnType.isNull()) |
| 7059 | return QualType(); |
| 7060 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 7061 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 7062 | // In either case, use OldReturnType to build the new function type. |
| 7063 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 7064 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7065 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7066 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7067 | QualType ResultType |
| 7068 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7069 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | 2390a72 | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7070 | return ResultType; |
| 7071 | } |
| 7072 | } |
| 7073 | return QualType(); |
| 7074 | } |
| 7075 | |
| 7076 | // If the qualifiers are different, the types can still be merged. |
| 7077 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 7078 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 7079 | if (LQuals != RQuals) { |
| 7080 | // If any of these qualifiers are different, we have a type mismatch. |
| 7081 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 7082 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 7083 | return QualType(); |
| 7084 | |
| 7085 | // Exactly one GC qualifier difference is allowed: __strong is |
| 7086 | // okay if the other type has no GC qualifier but is an Objective |
| 7087 | // C object pointer (i.e. implicitly strong by default). We fix |
| 7088 | // this by pretending that the unqualified type was actually |
| 7089 | // qualified __strong. |
| 7090 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 7091 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 7092 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 7093 | |
| 7094 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 7095 | return QualType(); |
| 7096 | |
| 7097 | if (GC_L == Qualifiers::Strong) |
| 7098 | return LHS; |
| 7099 | if (GC_R == Qualifiers::Strong) |
| 7100 | return RHS; |
| 7101 | return QualType(); |
| 7102 | } |
| 7103 | |
| 7104 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 7105 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7106 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7107 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 7108 | if (ResQT == LHSBaseQT) |
| 7109 | return LHS; |
| 7110 | if (ResQT == RHSBaseQT) |
| 7111 | return RHS; |
| 7112 | } |
| 7113 | return QualType(); |
| 7114 | } |
| 7115 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 7116 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7117 | // Integer Predicates |
| 7118 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 7119 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7120 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7121 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 7122 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7123 | if (T->isBooleanType()) |
| 7124 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 7125 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7126 | return (unsigned)getTypeSize(T); |
| 7127 | } |
| 7128 | |
Abramo Bagnara | 762f159 | 2012-09-09 10:21:24 +0000 | [diff] [blame] | 7129 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7130 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7131 | |
| 7132 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 7133 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 7134 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7135 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7136 | |
| 7137 | // For enums, we return the unsigned version of the base type. |
| 7138 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7139 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7140 | |
| 7141 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 7142 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7143 | switch (BTy->getKind()) { |
| 7144 | case BuiltinType::Char_S: |
| 7145 | case BuiltinType::SChar: |
| 7146 | return UnsignedCharTy; |
| 7147 | case BuiltinType::Short: |
| 7148 | return UnsignedShortTy; |
| 7149 | case BuiltinType::Int: |
| 7150 | return UnsignedIntTy; |
| 7151 | case BuiltinType::Long: |
| 7152 | return UnsignedLongTy; |
| 7153 | case BuiltinType::LongLong: |
| 7154 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 7155 | case BuiltinType::Int128: |
| 7156 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7157 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7158 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7159 | } |
| 7160 | } |
| 7161 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 7162 | ASTMutationListener::~ASTMutationListener() { } |
| 7163 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7164 | |
| 7165 | //===----------------------------------------------------------------------===// |
| 7166 | // Builtin Type Computation |
| 7167 | //===----------------------------------------------------------------------===// |
| 7168 | |
| 7169 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7170 | /// pointer over the consumed characters. This returns the resultant type. If |
| 7171 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 7172 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 7173 | /// a vector of "i*". |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7174 | /// |
| 7175 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 7176 | /// to be an Integer Constant Expression. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7177 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7178 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7179 | bool &RequiresICE, |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7180 | bool AllowTypeModifiers) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7181 | // Modifiers. |
| 7182 | int HowLong = 0; |
| 7183 | bool Signed = false, Unsigned = false; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7184 | RequiresICE = false; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7185 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7186 | // Read the prefixed modifiers first. |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7187 | bool Done = false; |
| 7188 | while (!Done) { |
| 7189 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7190 | default: Done = true; --Str; break; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7191 | case 'I': |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7192 | RequiresICE = true; |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7193 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7194 | case 'S': |
| 7195 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 7196 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 7197 | Signed = true; |
| 7198 | break; |
| 7199 | case 'U': |
| 7200 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 7201 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 7202 | Unsigned = true; |
| 7203 | break; |
| 7204 | case 'L': |
| 7205 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 7206 | ++HowLong; |
| 7207 | break; |
| 7208 | } |
| 7209 | } |
| 7210 | |
| 7211 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7212 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7213 | // Read the base type. |
| 7214 | switch (*Str++) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7215 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7216 | case 'v': |
| 7217 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7218 | "Bad modifiers used with 'v'!"); |
| 7219 | Type = Context.VoidTy; |
| 7220 | break; |
| 7221 | case 'f': |
| 7222 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7223 | "Bad modifiers used with 'f'!"); |
| 7224 | Type = Context.FloatTy; |
| 7225 | break; |
| 7226 | case 'd': |
| 7227 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 7228 | "Bad modifiers used with 'd'!"); |
| 7229 | if (HowLong) |
| 7230 | Type = Context.LongDoubleTy; |
| 7231 | else |
| 7232 | Type = Context.DoubleTy; |
| 7233 | break; |
| 7234 | case 's': |
| 7235 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 7236 | if (Unsigned) |
| 7237 | Type = Context.UnsignedShortTy; |
| 7238 | else |
| 7239 | Type = Context.ShortTy; |
| 7240 | break; |
| 7241 | case 'i': |
| 7242 | if (HowLong == 3) |
| 7243 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 7244 | else if (HowLong == 2) |
| 7245 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 7246 | else if (HowLong == 1) |
| 7247 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 7248 | else |
| 7249 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 7250 | break; |
| 7251 | case 'c': |
| 7252 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 7253 | if (Signed) |
| 7254 | Type = Context.SignedCharTy; |
| 7255 | else if (Unsigned) |
| 7256 | Type = Context.UnsignedCharTy; |
| 7257 | else |
| 7258 | Type = Context.CharTy; |
| 7259 | break; |
| 7260 | case 'b': // boolean |
| 7261 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 7262 | Type = Context.BoolTy; |
| 7263 | break; |
| 7264 | case 'z': // size_t. |
| 7265 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 7266 | Type = Context.getSizeType(); |
| 7267 | break; |
| 7268 | case 'F': |
| 7269 | Type = Context.getCFConstantStringType(); |
| 7270 | break; |
Fariborz Jahanian | ba8bda0 | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 7271 | case 'G': |
| 7272 | Type = Context.getObjCIdType(); |
| 7273 | break; |
| 7274 | case 'H': |
| 7275 | Type = Context.getObjCSelType(); |
| 7276 | break; |
Fariborz Jahanian | f799213 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 7277 | case 'M': |
| 7278 | Type = Context.getObjCSuperType(); |
| 7279 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7280 | case 'a': |
| 7281 | Type = Context.getBuiltinVaListType(); |
| 7282 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 7283 | break; |
| 7284 | case 'A': |
| 7285 | // This is a "reference" to a va_list; however, what exactly |
| 7286 | // this means depends on how va_list is defined. There are two |
| 7287 | // different kinds of va_list: ones passed by value, and ones |
| 7288 | // passed by reference. An example of a by-value va_list is |
| 7289 | // x86, where va_list is a char*. An example of by-ref va_list |
| 7290 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 7291 | // we want this argument to be a char*&; for x86-64, we want |
| 7292 | // it to be a __va_list_tag*. |
| 7293 | Type = Context.getBuiltinVaListType(); |
| 7294 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7295 | if (Type->isArrayType()) |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7296 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7297 | else |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7298 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7299 | break; |
| 7300 | case 'V': { |
| 7301 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7302 | unsigned NumElements = strtoul(Str, &End, 10); |
| 7303 | assert(End != Str && "Missing vector size"); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7304 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7305 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7306 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 7307 | RequiresICE, false); |
| 7308 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7309 | |
| 7310 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 7311 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7312 | VectorType::GenericVector); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7313 | break; |
| 7314 | } |
Douglas Gregor | b4bc99b | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 7315 | case 'E': { |
| 7316 | char *End; |
| 7317 | |
| 7318 | unsigned NumElements = strtoul(Str, &End, 10); |
| 7319 | assert(End != Str && "Missing vector size"); |
| 7320 | |
| 7321 | Str = End; |
| 7322 | |
| 7323 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 7324 | false); |
| 7325 | Type = Context.getExtVectorType(ElementType, NumElements); |
| 7326 | break; |
| 7327 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 7328 | case 'X': { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7329 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 7330 | false); |
| 7331 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 7332 | Type = Context.getComplexType(ElementType); |
| 7333 | break; |
Fariborz Jahanian | cc075e4 | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 7334 | } |
| 7335 | case 'Y' : { |
| 7336 | Type = Context.getPointerDiffType(); |
| 7337 | break; |
| 7338 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 7339 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 7340 | Type = Context.getFILEType(); |
| 7341 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7342 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7343 | return QualType(); |
| 7344 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7345 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 7346 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7347 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 7348 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7349 | else |
| 7350 | Type = Context.getjmp_bufType(); |
| 7351 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7352 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7353 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7354 | return QualType(); |
| 7355 | } |
| 7356 | break; |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 7357 | case 'K': |
| 7358 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 7359 | Type = Context.getucontext_tType(); |
| 7360 | |
| 7361 | if (Type.isNull()) { |
| 7362 | Error = ASTContext::GE_Missing_ucontext; |
| 7363 | return QualType(); |
| 7364 | } |
| 7365 | break; |
Eli Friedman | 6902e41 | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 7366 | case 'p': |
| 7367 | Type = Context.getProcessIDType(); |
| 7368 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 7369 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7370 | |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7371 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 7372 | Done = !AllowTypeModifiers; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7373 | while (!Done) { |
John McCall | 187ab37 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 7374 | switch (char c = *Str++) { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7375 | default: Done = true; --Str; break; |
| 7376 | case '*': |
| 7377 | case '&': { |
| 7378 | // Both pointers and references can have their pointee types |
| 7379 | // qualified with an address space. |
| 7380 | char *End; |
| 7381 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 7382 | if (End != Str && AddrSpace != 0) { |
| 7383 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 7384 | Str = End; |
| 7385 | } |
| 7386 | if (c == '*') |
| 7387 | Type = Context.getPointerType(Type); |
| 7388 | else |
| 7389 | Type = Context.getLValueReferenceType(Type); |
| 7390 | break; |
| 7391 | } |
| 7392 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 7393 | case 'C': |
| 7394 | Type = Type.withConst(); |
| 7395 | break; |
| 7396 | case 'D': |
| 7397 | Type = Context.getVolatileType(Type); |
| 7398 | break; |
Ted Kremenek | 18932a0 | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 7399 | case 'R': |
| 7400 | Type = Type.withRestrict(); |
| 7401 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7402 | } |
| 7403 | } |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7404 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7405 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 393bd8e | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7406 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7407 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7408 | return Type; |
| 7409 | } |
| 7410 | |
| 7411 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7412 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7413 | GetBuiltinTypeError &Error, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7414 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | 33daae6 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7415 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7416 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7417 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7418 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7419 | bool RequiresICE = false; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7420 | Error = GE_None; |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7421 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 7422 | RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7423 | if (Error != GE_None) |
| 7424 | return QualType(); |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7425 | |
| 7426 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 7427 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7428 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7429 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7430 | if (Error != GE_None) |
| 7431 | return QualType(); |
| 7432 | |
Chris Lattner | 14e0e74 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7433 | // If this argument is required to be an IntegerConstantExpression and the |
| 7434 | // caller cares, fill in the bitmask we return. |
| 7435 | if (RequiresICE && IntegerConstantArgs) |
| 7436 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 7437 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7438 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 7439 | if (Ty->isArrayType()) |
| 7440 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7441 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7442 | ArgTypes.push_back(Ty); |
| 7443 | } |
| 7444 | |
| 7445 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 7446 | "'.' should only occur at end of builtin type list!"); |
| 7447 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 7448 | FunctionType::ExtInfo EI; |
| 7449 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 7450 | |
| 7451 | bool Variadic = (TypeStr[0] == '.'); |
| 7452 | |
| 7453 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 7454 | if (ArgTypes.empty() && Variadic) |
| 7455 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 7456 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7457 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 7458 | EPI.ExtInfo = EI; |
| 7459 | EPI.Variadic = Variadic; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7460 | |
| 7461 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7462 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 7463 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7464 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 7465 | GVALinkage External = GVA_StrongExternal; |
| 7466 | |
| 7467 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7468 | switch (L) { |
| 7469 | case NoLinkage: |
| 7470 | case InternalLinkage: |
| 7471 | case UniqueExternalLinkage: |
| 7472 | return GVA_Internal; |
| 7473 | |
| 7474 | case ExternalLinkage: |
| 7475 | switch (FD->getTemplateSpecializationKind()) { |
| 7476 | case TSK_Undeclared: |
| 7477 | case TSK_ExplicitSpecialization: |
| 7478 | External = GVA_StrongExternal; |
| 7479 | break; |
| 7480 | |
| 7481 | case TSK_ExplicitInstantiationDefinition: |
| 7482 | return GVA_ExplicitTemplateInstantiation; |
| 7483 | |
| 7484 | case TSK_ExplicitInstantiationDeclaration: |
| 7485 | case TSK_ImplicitInstantiation: |
| 7486 | External = GVA_TemplateInstantiation; |
| 7487 | break; |
| 7488 | } |
| 7489 | } |
| 7490 | |
| 7491 | if (!FD->isInlined()) |
| 7492 | return External; |
| 7493 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7494 | if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7495 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 7496 | // externally visible. |
| 7497 | if (FD->isInlineDefinitionExternallyVisible()) |
| 7498 | return External; |
| 7499 | |
| 7500 | // C99 inline semantics, where the symbol is not externally visible. |
| 7501 | return GVA_C99Inline; |
| 7502 | } |
| 7503 | |
| 7504 | // C++0x [temp.explicit]p9: |
| 7505 | // [ Note: The intent is that an inline function that is the subject of |
| 7506 | // an explicit instantiation declaration will still be implicitly |
| 7507 | // instantiated when used so that the body can be considered for |
| 7508 | // inlining, but that no out-of-line copy of the inline function would be |
| 7509 | // generated in the translation unit. -- end note ] |
| 7510 | if (FD->getTemplateSpecializationKind() |
| 7511 | == TSK_ExplicitInstantiationDeclaration) |
| 7512 | return GVA_C99Inline; |
| 7513 | |
| 7514 | return GVA_CXXInline; |
| 7515 | } |
| 7516 | |
| 7517 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 7518 | // If this is a static data member, compute the kind of template |
| 7519 | // specialization. Otherwise, this variable is not part of a |
| 7520 | // template. |
| 7521 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 7522 | if (VD->isStaticDataMember()) |
| 7523 | TSK = VD->getTemplateSpecializationKind(); |
| 7524 | |
| 7525 | Linkage L = VD->getLinkage(); |
Rafael Espindola | 62a833e | 2013-01-02 04:19:07 +0000 | [diff] [blame] | 7526 | assert (!(L == ExternalLinkage && getLangOpts().CPlusPlus && |
| 7527 | VD->getType()->getLinkage() == UniqueExternalLinkage)); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7528 | |
| 7529 | switch (L) { |
| 7530 | case NoLinkage: |
| 7531 | case InternalLinkage: |
| 7532 | case UniqueExternalLinkage: |
| 7533 | return GVA_Internal; |
| 7534 | |
| 7535 | case ExternalLinkage: |
| 7536 | switch (TSK) { |
| 7537 | case TSK_Undeclared: |
| 7538 | case TSK_ExplicitSpecialization: |
| 7539 | return GVA_StrongExternal; |
| 7540 | |
| 7541 | case TSK_ExplicitInstantiationDeclaration: |
| 7542 | llvm_unreachable("Variable should not be instantiated"); |
| 7543 | // Fall through to treat this like any other instantiation. |
| 7544 | |
| 7545 | case TSK_ExplicitInstantiationDefinition: |
| 7546 | return GVA_ExplicitTemplateInstantiation; |
| 7547 | |
| 7548 | case TSK_ImplicitInstantiation: |
| 7549 | return GVA_TemplateInstantiation; |
| 7550 | } |
| 7551 | } |
| 7552 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7553 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7554 | } |
| 7555 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 7556 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7557 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7558 | if (!VD->isFileVarDecl()) |
| 7559 | return false; |
Argyrios Kyrtzidis | 144b38a | 2011-09-13 21:35:00 +0000 | [diff] [blame] | 7560 | } else if (!isa<FunctionDecl>(D)) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7561 | return false; |
| 7562 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7563 | // Weak references don't produce any output by themselves. |
| 7564 | if (D->hasAttr<WeakRefAttr>()) |
| 7565 | return false; |
| 7566 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7567 | // Aliases and used decls are required. |
| 7568 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 7569 | return true; |
| 7570 | |
| 7571 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7572 | // Forward declarations aren't required. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 7573 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 7574 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7575 | |
| 7576 | // Constructors and destructors are required. |
| 7577 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 7578 | return true; |
| 7579 | |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 7580 | // The key function for a class is required. This rule only comes |
| 7581 | // into play when inline functions can be key functions, though. |
| 7582 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
| 7583 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 7584 | const CXXRecordDecl *RD = MD->getParent(); |
| 7585 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 7586 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
| 7587 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 7588 | return true; |
| 7589 | } |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7590 | } |
| 7591 | } |
| 7592 | |
| 7593 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 7594 | |
| 7595 | // static, static inline, always_inline, and extern inline functions can |
| 7596 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 7597 | // Implicit template instantiations can also be deferred in C++. |
| 7598 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
Anton Yartsev | 3a5aca8 | 2012-02-02 06:06:34 +0000 | [diff] [blame] | 7599 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7600 | return false; |
| 7601 | return true; |
| 7602 | } |
Douglas Gregor | 94da158 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 7603 | |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7604 | const VarDecl *VD = cast<VarDecl>(D); |
| 7605 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 7606 | |
Argyrios Kyrtzidis | ab411c8 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7607 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 7608 | return false; |
| 7609 | |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7610 | // Variables that can be needed in other TUs are required. |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7611 | GVALinkage L = GetGVALinkageForVariable(VD); |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7612 | if (L != GVA_Internal && L != GVA_TemplateInstantiation) |
| 7613 | return true; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7614 | |
Richard Smith | 5f9a7e3 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 7615 | // Variables that have destruction with side-effects are required. |
| 7616 | if (VD->getType().isDestructedType()) |
| 7617 | return true; |
| 7618 | |
| 7619 | // Variables that have initialization with side-effects are required. |
| 7620 | if (VD->getInit() && VD->getInit()->HasSideEffects(*this)) |
| 7621 | return true; |
| 7622 | |
| 7623 | return false; |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7624 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7625 | |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7626 | CallingConv ASTContext::getDefaultCXXMethodCallConv(bool isVariadic) { |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7627 | // Pass through to the C++ ABI object |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7628 | return ABI->getDefaultMethodCallConv(isVariadic); |
| 7629 | } |
| 7630 | |
| 7631 | CallingConv ASTContext::getCanonicalCallConv(CallingConv CC) const { |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7632 | if (CC == CC_C && !LangOpts.MRTD && |
| 7633 | getTargetInfo().getCXXABI().isMemberFunctionCCDefault()) |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7634 | return CC_Default; |
| 7635 | return CC; |
Charles Davis | ee743f9 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7636 | } |
| 7637 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7638 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 7639 | // Pass through to the C++ ABI object |
| 7640 | return ABI->isNearlyEmpty(RD); |
| 7641 | } |
| 7642 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7643 | MangleContext *ASTContext::createMangleContext() { |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7644 | switch (Target->getCXXABI().getKind()) { |
| 7645 | case TargetCXXABI::GenericItanium: |
| 7646 | case TargetCXXABI::GenericARM: |
| 7647 | case TargetCXXABI::iOS: |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7648 | return createItaniumMangleContext(*this, getDiagnostics()); |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 7649 | case TargetCXXABI::Microsoft: |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7650 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 7651 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7652 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7653 | } |
| 7654 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7655 | CXXABI::~CXXABI() {} |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7656 | |
| 7657 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 0c8cd1a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 7658 | return ASTRecordLayouts.getMemorySize() |
| 7659 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 7660 | + llvm::capacity_in_bytes(KeyFunctions) |
| 7661 | + llvm::capacity_in_bytes(ObjCImpls) |
| 7662 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 7663 | + llvm::capacity_in_bytes(DeclAttrs) |
| 7664 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 7665 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 7666 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 7667 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 7668 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 7669 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7670 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 0d95f0d | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 7671 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7672 | } |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7673 | |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 7674 | void ASTContext::addUnnamedTag(const TagDecl *Tag) { |
| 7675 | // FIXME: This mangling should be applied to function local classes too |
| 7676 | if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl() || |
| 7677 | !isa<CXXRecordDecl>(Tag->getParent()) || Tag->getLinkage() != ExternalLinkage) |
| 7678 | return; |
| 7679 | |
| 7680 | std::pair<llvm::DenseMap<const DeclContext *, unsigned>::iterator, bool> P = |
| 7681 | UnnamedMangleContexts.insert(std::make_pair(Tag->getParent(), 0)); |
| 7682 | UnnamedMangleNumbers.insert(std::make_pair(Tag, P.first->second++)); |
| 7683 | } |
| 7684 | |
| 7685 | int ASTContext::getUnnamedTagManglingNumber(const TagDecl *Tag) const { |
| 7686 | llvm::DenseMap<const TagDecl *, unsigned>::const_iterator I = |
| 7687 | UnnamedMangleNumbers.find(Tag); |
| 7688 | return I != UnnamedMangleNumbers.end() ? I->second : -1; |
| 7689 | } |
| 7690 | |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 7691 | unsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) { |
| 7692 | CXXRecordDecl *Lambda = CallOperator->getParent(); |
| 7693 | return LambdaMangleContexts[Lambda->getDeclContext()] |
| 7694 | .getManglingNumber(CallOperator); |
| 7695 | } |
| 7696 | |
| 7697 | |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7698 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 7699 | ParamIndices[D] = index; |
| 7700 | } |
| 7701 | |
| 7702 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 7703 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 7704 | assert(I != ParamIndices.end() && |
| 7705 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 7706 | return I->second; |
| 7707 | } |