Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 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 semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
| 16 | #include "clang/Sema/Lookup.h" |
| 17 | #include "clang/Sema/AnalysisBasedWarnings.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 23 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 27 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 29 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 32 | #include "clang/Lex/LiteralSupport.h" |
| 33 | #include "clang/Lex/Preprocessor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 34 | #include "clang/Sema/DeclSpec.h" |
| 35 | #include "clang/Sema/Designator.h" |
| 36 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 37 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 38 | #include "clang/Sema/ParsedTemplate.h" |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 39 | #include "clang/Sema/SemaFixItUtils.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 40 | #include "clang/Sema/Template.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 42 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 44 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 45 | /// \brief Determine whether the use of this declaration is valid, and |
| 46 | /// emit any corresponding diagnostics. |
| 47 | /// |
| 48 | /// This routine diagnoses various problems with referencing |
| 49 | /// declarations that can occur when using a declaration. For example, |
| 50 | /// it might warn if a deprecated or unavailable declaration is being |
| 51 | /// used, or produce an error (and return true) if a C++0x deleted |
| 52 | /// function is being used. |
| 53 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 54 | /// If IgnoreDeprecated is set to true, this should not warn about deprecated |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 55 | /// decls. |
| 56 | /// |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 57 | /// \returns true if there was an error (this declaration cannot be |
| 58 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 59 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 60 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 61 | const ObjCInterfaceDecl *UnknownObjCClass) { |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 62 | if (getLangOptions().CPlusPlus && isa<FunctionDecl>(D)) { |
| 63 | // If there were any diagnostics suppressed by template argument deduction, |
| 64 | // emit them now. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 65 | llvm::DenseMap<Decl *, SmallVector<PartialDiagnosticAt, 1> >::iterator |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 66 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 67 | if (Pos != SuppressedDiagnostics.end()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 68 | SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 69 | for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) |
| 70 | Diag(Suppressed[I].first, Suppressed[I].second); |
| 71 | |
| 72 | // Clear out the list of suppressed diagnostics, so that we don't emit |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 73 | // them again for this specialization. However, we don't obsolete this |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 74 | // entry from the table, because we want to avoid ever emitting these |
| 75 | // diagnostics again. |
| 76 | Suppressed.clear(); |
| 77 | } |
| 78 | } |
| 79 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 80 | // See if this is an auto-typed variable whose initializer we are parsing. |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 81 | if (ParsingInitForAutoVars.count(D)) { |
| 82 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 83 | << D->getDeclName(); |
| 84 | return true; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 87 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 88 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 89 | if (FD->isDeleted()) { |
| 90 | Diag(Loc, diag::err_deleted_function_use); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 91 | Diag(D->getLocation(), diag::note_unavailable_here) << 1 << true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 92 | return true; |
| 93 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 94 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 95 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 96 | // See if this declaration is unavailable or deprecated. |
| 97 | std::string Message; |
| 98 | switch (D->getAvailability(&Message)) { |
| 99 | case AR_Available: |
| 100 | case AR_NotYetIntroduced: |
| 101 | break; |
| 102 | |
| 103 | case AR_Deprecated: |
| 104 | EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass); |
| 105 | break; |
| 106 | |
| 107 | case AR_Unavailable: |
Argyrios Kyrtzidis | 12189f5 | 2011-06-17 17:28:30 +0000 | [diff] [blame] | 108 | if (cast<Decl>(CurContext)->getAvailability() != AR_Unavailable) { |
| 109 | if (Message.empty()) { |
| 110 | if (!UnknownObjCClass) |
| 111 | Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
| 112 | else |
| 113 | Diag(Loc, diag::warn_unavailable_fwdclass_message) |
| 114 | << D->getDeclName(); |
| 115 | } |
| 116 | else |
| 117 | Diag(Loc, diag::err_unavailable_message) |
| 118 | << D->getDeclName() << Message; |
| 119 | Diag(D->getLocation(), diag::note_unavailable_here) |
| 120 | << isa<FunctionDecl>(D) << false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 121 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 125 | // Warn if this is used but marked unused. |
| 126 | if (D->hasAttr<UnusedAttr>()) |
| 127 | Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
| 128 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 129 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 132 | /// \brief Retrieve the message suffix that should be added to a |
| 133 | /// diagnostic complaining about the given function being deleted or |
| 134 | /// unavailable. |
| 135 | std::string Sema::getDeletedOrUnavailableSuffix(const FunctionDecl *FD) { |
| 136 | // FIXME: C++0x implicitly-deleted special member functions could be |
| 137 | // detected here so that we could improve diagnostics to say, e.g., |
| 138 | // "base class 'A' had a deleted copy constructor". |
| 139 | if (FD->isDeleted()) |
| 140 | return std::string(); |
| 141 | |
| 142 | std::string Message; |
| 143 | if (FD->getAvailability(&Message)) |
| 144 | return ": " + Message; |
| 145 | |
| 146 | return std::string(); |
| 147 | } |
| 148 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 149 | /// DiagnoseSentinelCalls - This routine checks whether a call or |
| 150 | /// message-send is to a declaration with the sentinel attribute, and |
| 151 | /// if so, it checks that the requirements of the sentinel are |
| 152 | /// satisfied. |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 153 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 154 | Expr **args, unsigned numArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 155 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 157 | return; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 158 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 159 | // The number of formal parameters of the declaration. |
| 160 | unsigned numFormalParams; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 162 | // The kind of declaration. This is also an index into a %select in |
| 163 | // the diagnostic. |
| 164 | enum CalleeType { CT_Function, CT_Method, CT_Block } calleeType; |
| 165 | |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 166 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 167 | numFormalParams = MD->param_size(); |
| 168 | calleeType = CT_Method; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 169 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 170 | numFormalParams = FD->param_size(); |
| 171 | calleeType = CT_Function; |
| 172 | } else if (isa<VarDecl>(D)) { |
| 173 | QualType type = cast<ValueDecl>(D)->getType(); |
| 174 | const FunctionType *fn = 0; |
| 175 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 176 | fn = ptr->getPointeeType()->getAs<FunctionType>(); |
| 177 | if (!fn) return; |
| 178 | calleeType = CT_Function; |
| 179 | } else if (const BlockPointerType *ptr = type->getAs<BlockPointerType>()) { |
| 180 | fn = ptr->getPointeeType()->castAs<FunctionType>(); |
| 181 | calleeType = CT_Block; |
| 182 | } else { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 183 | return; |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 184 | } |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 185 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 186 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fn)) { |
| 187 | numFormalParams = proto->getNumArgs(); |
| 188 | } else { |
| 189 | numFormalParams = 0; |
| 190 | } |
| 191 | } else { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 192 | return; |
| 193 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 194 | |
| 195 | // "nullPos" is the number of formal parameters at the end which |
| 196 | // effectively count as part of the variadic arguments. This is |
| 197 | // useful if you would prefer to not have *any* formal parameters, |
| 198 | // but the language forces you to have at least one. |
| 199 | unsigned nullPos = attr->getNullPos(); |
| 200 | assert((nullPos == 0 || nullPos == 1) && "invalid null position on sentinel"); |
| 201 | numFormalParams = (nullPos > numFormalParams ? 0 : numFormalParams - nullPos); |
| 202 | |
| 203 | // The number of arguments which should follow the sentinel. |
| 204 | unsigned numArgsAfterSentinel = attr->getSentinel(); |
| 205 | |
| 206 | // If there aren't enough arguments for all the formal parameters, |
| 207 | // the sentinel, and the args after the sentinel, complain. |
| 208 | if (numArgs < numFormalParams + numArgsAfterSentinel + 1) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 209 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 210 | Diag(D->getLocation(), diag::note_sentinel_here) << calleeType; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 211 | return; |
| 212 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 213 | |
| 214 | // Otherwise, find the sentinel expression. |
| 215 | Expr *sentinelExpr = args[numArgs - numArgsAfterSentinel - 1]; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 216 | if (!sentinelExpr) return; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 217 | if (sentinelExpr->isValueDependent()) return; |
Anders Carlsson | 343e6ff | 2010-11-05 15:21:33 +0000 | [diff] [blame] | 218 | |
| 219 | // nullptr_t is always treated as null. |
| 220 | if (sentinelExpr->getType()->isNullPtrType()) return; |
| 221 | |
Fariborz Jahanian | 9ccd725 | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 222 | if (sentinelExpr->getType()->isAnyPointerType() && |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 223 | sentinelExpr->IgnoreParenCasts()->isNullPointerConstant(Context, |
| 224 | Expr::NPC_ValueDependentIsNull)) |
| 225 | return; |
| 226 | |
| 227 | // Unfortunately, __null has type 'int'. |
| 228 | if (isa<GNUNullExpr>(sentinelExpr)) return; |
| 229 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 230 | // Pick a reasonable string to insert. Optimistically use 'nil' or |
| 231 | // 'NULL' if those are actually defined in the context. Only use |
| 232 | // 'nil' for ObjC methods, where it's much more likely that the |
| 233 | // variadic arguments form a list of object pointers. |
| 234 | SourceLocation MissingNilLoc |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 235 | = PP.getLocForEndOfToken(sentinelExpr->getLocEnd()); |
| 236 | std::string NullValue; |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 237 | if (calleeType == CT_Method && |
| 238 | PP.getIdentifierInfo("nil")->hasMacroDefinition()) |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 239 | NullValue = "nil"; |
| 240 | else if (PP.getIdentifierInfo("NULL")->hasMacroDefinition()) |
| 241 | NullValue = "NULL"; |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 242 | else |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 243 | NullValue = "(void*) 0"; |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 244 | |
| 245 | Diag(MissingNilLoc, diag::warn_missing_sentinel) |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 246 | << calleeType |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 247 | << FixItHint::CreateInsertion(MissingNilLoc, ", " + NullValue); |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 248 | Diag(D->getLocation(), diag::note_sentinel_here) << calleeType; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 251 | SourceRange Sema::getExprRange(Expr *E) const { |
| 252 | return E ? E->getSourceRange() : SourceRange(); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 255 | //===----------------------------------------------------------------------===// |
| 256 | // Standard Promotions and Conversions |
| 257 | //===----------------------------------------------------------------------===// |
| 258 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 259 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 260 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 261 | QualType Ty = E->getType(); |
| 262 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 263 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 264 | if (Ty->isFunctionType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 265 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 266 | CK_FunctionToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 267 | else if (Ty->isArrayType()) { |
| 268 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 269 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 270 | // type 'array of type' is converted to an expression that has type 'pointer |
| 271 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 272 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 273 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 274 | // |
| 275 | // C++ 4.2p1: |
| 276 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 277 | // T" can be converted to an rvalue of type "pointer to T". |
| 278 | // |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 279 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || E->isLValue()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 280 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 281 | CK_ArrayToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 282 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 283 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 286 | static void CheckForNullPointerDereference(Sema &S, Expr *E) { |
| 287 | // Check to see if we are dereferencing a null pointer. If so, |
| 288 | // and if not volatile-qualified, this is undefined behavior that the |
| 289 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 290 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 291 | // only handles the pattern "*null", which is a very syntactic check. |
| 292 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 293 | if (UO->getOpcode() == UO_Deref && |
| 294 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 295 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) && |
| 296 | !UO->getType().isVolatileQualified()) { |
| 297 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 298 | S.PDiag(diag::warn_indirection_through_null) |
| 299 | << UO->getSubExpr()->getSourceRange()); |
| 300 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 301 | S.PDiag(diag::note_indirection_through_null)); |
| 302 | } |
| 303 | } |
| 304 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 305 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 306 | // C++ [conv.lval]p1: |
| 307 | // A glvalue of a non-function, non-array type T can be |
| 308 | // converted to a prvalue. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 309 | if (!E->isGLValue()) return Owned(E); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 310 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 311 | QualType T = E->getType(); |
| 312 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 313 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 314 | // Create a load out of an ObjCProperty l-value, if necessary. |
| 315 | if (E->getObjectKind() == OK_ObjCProperty) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 316 | ExprResult Res = ConvertPropertyForRValue(E); |
| 317 | if (Res.isInvalid()) |
| 318 | return Owned(E); |
| 319 | E = Res.take(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 320 | if (!E->isGLValue()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 321 | return Owned(E); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 322 | } |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 323 | |
| 324 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 325 | // expressions of certain types in C++. |
| 326 | if (getLangOptions().CPlusPlus && |
| 327 | (E->getType() == Context.OverloadTy || |
| 328 | T->isDependentType() || |
| 329 | T->isRecordType())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 330 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 331 | |
| 332 | // The C standard is actually really unclear on this point, and |
| 333 | // DR106 tells us what the result should be but not why. It's |
| 334 | // generally best to say that void types just doesn't undergo |
| 335 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 336 | // 'void' type are never l-values, but qualified void can be. |
| 337 | if (T->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 338 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 339 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 340 | CheckForNullPointerDereference(*this, E); |
| 341 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 342 | // C++ [conv.lval]p1: |
| 343 | // [...] If T is a non-class type, the type of the prvalue is the |
| 344 | // cv-unqualified version of T. Otherwise, the type of the |
| 345 | // rvalue is T. |
| 346 | // |
| 347 | // C99 6.3.2.1p2: |
| 348 | // If the lvalue has qualified type, the value has the unqualified |
| 349 | // version of the type of the lvalue; otherwise, the value has the |
| 350 | // type of the lvalue. |
| 351 | if (T.hasQualifiers()) |
| 352 | T = T.getUnqualifiedType(); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 353 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 354 | return Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 355 | E, 0, VK_RValue)); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 356 | } |
| 357 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 358 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 359 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 360 | if (Res.isInvalid()) |
| 361 | return ExprError(); |
| 362 | Res = DefaultLvalueConversion(Res.take()); |
| 363 | if (Res.isInvalid()) |
| 364 | return ExprError(); |
| 365 | return move(Res); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 369 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 371 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 372 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 373 | /// In these instances, this routine should *not* be called. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 374 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 375 | // First, convert to an r-value. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 376 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 377 | if (Res.isInvalid()) |
| 378 | return Owned(E); |
| 379 | E = Res.take(); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 380 | |
| 381 | QualType Ty = E->getType(); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 382 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 383 | |
| 384 | // Try to perform integral promotions if the object has a theoretically |
| 385 | // promotable type. |
| 386 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 387 | // C99 6.3.1.1p2: |
| 388 | // |
| 389 | // The following may be used in an expression wherever an int or |
| 390 | // unsigned int may be used: |
| 391 | // - an object or expression with an integer type whose integer |
| 392 | // conversion rank is less than or equal to the rank of int |
| 393 | // and unsigned int. |
| 394 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 395 | // |
| 396 | // If an int can represent all values of the original type, the |
| 397 | // value is converted to an int; otherwise, it is converted to an |
| 398 | // unsigned int. These are called the integer promotions. All |
| 399 | // other types are unchanged by the integer promotions. |
| 400 | |
| 401 | QualType PTy = Context.isPromotableBitField(E); |
| 402 | if (!PTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 403 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 404 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 405 | } |
| 406 | if (Ty->isPromotableIntegerType()) { |
| 407 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 408 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 409 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 410 | } |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 411 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 412 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 415 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | /// do not have a prototype. Arguments that have type float are promoted to |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 417 | /// double. All other argument types are converted by UsualUnaryConversions(). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 418 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 419 | QualType Ty = E->getType(); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 420 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 422 | ExprResult Res = UsualUnaryConversions(E); |
| 423 | if (Res.isInvalid()) |
| 424 | return Owned(E); |
| 425 | E = Res.take(); |
John McCall | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 427 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 428 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 429 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 430 | |
John McCall | 96a914a | 2011-08-27 22:06:17 +0000 | [diff] [blame] | 431 | // C++ performs lvalue-to-rvalue conversion as a default argument |
John McCall | 709bca8 | 2011-08-29 23:55:37 +0000 | [diff] [blame] | 432 | // promotion, even on class types, but note: |
| 433 | // C++11 [conv.lval]p2: |
| 434 | // When an lvalue-to-rvalue conversion occurs in an unevaluated |
| 435 | // operand or a subexpression thereof the value contained in the |
| 436 | // referenced object is not accessed. Otherwise, if the glvalue |
| 437 | // has a class type, the conversion copy-initializes a temporary |
| 438 | // of type T from the glvalue and the result of the conversion |
| 439 | // is a prvalue for the temporary. |
| 440 | // FIXME: add some way to gate this entire thing for correctness in |
| 441 | // potentially potentially evaluated contexts. |
John McCall | 96a914a | 2011-08-27 22:06:17 +0000 | [diff] [blame] | 442 | if (getLangOptions().CPlusPlus && E->isGLValue() && |
| 443 | ExprEvalContexts.back().Context != Unevaluated) { |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 444 | ExprResult Temp = PerformCopyInitialization( |
| 445 | InitializedEntity::InitializeTemporary(E->getType()), |
| 446 | E->getExprLoc(), |
| 447 | Owned(E)); |
| 448 | if (Temp.isInvalid()) |
| 449 | return ExprError(); |
| 450 | E = Temp.get(); |
| 451 | } |
| 452 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 453 | return Owned(E); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 456 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 457 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 458 | /// interfaces passed by value. |
| 459 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 460 | FunctionDecl *FDecl) { |
Douglas Gregor | 8d5e18c | 2011-06-17 00:15:10 +0000 | [diff] [blame] | 461 | ExprResult ExprRes = CheckPlaceholderExpr(E); |
| 462 | if (ExprRes.isInvalid()) |
| 463 | return ExprError(); |
| 464 | |
| 465 | ExprRes = DefaultArgumentPromotion(E); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 466 | if (ExprRes.isInvalid()) |
| 467 | return ExprError(); |
| 468 | E = ExprRes.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 470 | // Don't allow one to pass an Objective-C interface to a vararg. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 471 | if (E->getType()->isObjCObjectType() && |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 472 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 473 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 474 | << E->getType() << CT)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 475 | return ExprError(); |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 476 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 477 | if (!E->getType().isPODType(Context)) { |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 478 | // C++0x [expr.call]p7: |
| 479 | // Passing a potentially-evaluated argument of class type (Clause 9) |
| 480 | // having a non-trivial copy constructor, a non-trivial move constructor, |
| 481 | // or a non-trivial destructor, with no corresponding parameter, |
| 482 | // is conditionally-supported with implementation-defined semantics. |
| 483 | bool TrivialEnough = false; |
| 484 | if (getLangOptions().CPlusPlus0x && !E->getType()->isDependentType()) { |
| 485 | if (CXXRecordDecl *Record = E->getType()->getAsCXXRecordDecl()) { |
| 486 | if (Record->hasTrivialCopyConstructor() && |
| 487 | Record->hasTrivialMoveConstructor() && |
| 488 | Record->hasTrivialDestructor()) |
| 489 | TrivialEnough = true; |
| 490 | } |
| 491 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 492 | |
| 493 | if (!TrivialEnough && |
| 494 | getLangOptions().ObjCAutoRefCount && |
| 495 | E->getType()->isObjCLifetimeType()) |
| 496 | TrivialEnough = true; |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 497 | |
| 498 | if (TrivialEnough) { |
| 499 | // Nothing to diagnose. This is okay. |
| 500 | } else if (DiagRuntimeBehavior(E->getLocStart(), 0, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 501 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 502 | << getLangOptions().CPlusPlus0x << E->getType() |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 503 | << CT)) { |
| 504 | // Turn this into a trap. |
| 505 | CXXScopeSpec SS; |
| 506 | UnqualifiedId Name; |
| 507 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
| 508 | E->getLocStart()); |
| 509 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, Name, true, false); |
| 510 | if (TrapFn.isInvalid()) |
| 511 | return ExprError(); |
| 512 | |
| 513 | ExprResult Call = ActOnCallExpr(TUScope, TrapFn.get(), E->getLocStart(), |
| 514 | MultiExprArg(), E->getLocEnd()); |
| 515 | if (Call.isInvalid()) |
| 516 | return ExprError(); |
| 517 | |
| 518 | ExprResult Comma = ActOnBinOp(TUScope, E->getLocStart(), tok::comma, |
| 519 | Call.get(), E); |
| 520 | if (Comma.isInvalid()) |
John McCall | 66c2030 | 2011-08-26 18:41:18 +0000 | [diff] [blame] | 521 | return ExprError(); |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 522 | E = Comma.get(); |
| 523 | } |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 524 | } |
| 525 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 526 | return Owned(E); |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 529 | /// \brief Converts an integer to complex float type. Helper function of |
| 530 | /// UsualArithmeticConversions() |
| 531 | /// |
| 532 | /// \return false if the integer expression is an integer type and is |
| 533 | /// successfully converted to the complex type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 534 | static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr, |
| 535 | ExprResult &ComplexExpr, |
| 536 | QualType IntTy, |
| 537 | QualType ComplexTy, |
| 538 | bool SkipCast) { |
| 539 | if (IntTy->isComplexType() || IntTy->isRealFloatingType()) return true; |
| 540 | if (SkipCast) return false; |
| 541 | if (IntTy->isIntegerType()) { |
| 542 | QualType fpTy = cast<ComplexType>(ComplexTy)->getElementType(); |
| 543 | IntExpr = S.ImpCastExprToType(IntExpr.take(), fpTy, CK_IntegralToFloating); |
| 544 | IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 545 | CK_FloatingRealToComplex); |
| 546 | } else { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 547 | assert(IntTy->isComplexIntegerType()); |
| 548 | IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 549 | CK_IntegralComplexToFloatingComplex); |
| 550 | } |
| 551 | return false; |
| 552 | } |
| 553 | |
| 554 | /// \brief Takes two complex float types and converts them to the same type. |
| 555 | /// Helper function of UsualArithmeticConversions() |
| 556 | static QualType |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 557 | handleComplexFloatToComplexFloatConverstion(Sema &S, ExprResult &LHS, |
| 558 | ExprResult &RHS, QualType LHSType, |
| 559 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 560 | bool IsCompAssign) { |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 561 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 562 | |
| 563 | if (order < 0) { |
| 564 | // _Complex float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 565 | if (!IsCompAssign) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 566 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingComplexCast); |
| 567 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 568 | } |
| 569 | if (order > 0) |
| 570 | // _Complex float -> _Complex double |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 571 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingComplexCast); |
| 572 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /// \brief Converts otherExpr to complex float and promotes complexExpr if |
| 576 | /// necessary. Helper function of UsualArithmeticConversions() |
| 577 | static QualType handleOtherComplexFloatConversion(Sema &S, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 578 | ExprResult &ComplexExpr, |
| 579 | ExprResult &OtherExpr, |
| 580 | QualType ComplexTy, |
| 581 | QualType OtherTy, |
| 582 | bool ConvertComplexExpr, |
| 583 | bool ConvertOtherExpr) { |
| 584 | int order = S.Context.getFloatingTypeOrder(ComplexTy, OtherTy); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 585 | |
| 586 | // If just the complexExpr is complex, the otherExpr needs to be converted, |
| 587 | // and the complexExpr might need to be promoted. |
| 588 | if (order > 0) { // complexExpr is wider |
| 589 | // float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 590 | if (ConvertOtherExpr) { |
| 591 | QualType fp = cast<ComplexType>(ComplexTy)->getElementType(); |
| 592 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), fp, CK_FloatingCast); |
| 593 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 594 | CK_FloatingRealToComplex); |
| 595 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 596 | return ComplexTy; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // otherTy is at least as wide. Find its corresponding complex type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 600 | QualType result = (order == 0 ? ComplexTy : |
| 601 | S.Context.getComplexType(OtherTy)); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 602 | |
| 603 | // double -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 604 | if (ConvertOtherExpr) |
| 605 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 606 | CK_FloatingRealToComplex); |
| 607 | |
| 608 | // _Complex float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 609 | if (ConvertComplexExpr && order < 0) |
| 610 | ComplexExpr = S.ImpCastExprToType(ComplexExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 611 | CK_FloatingComplexCast); |
| 612 | |
| 613 | return result; |
| 614 | } |
| 615 | |
| 616 | /// \brief Handle arithmetic conversion with complex types. Helper function of |
| 617 | /// UsualArithmeticConversions() |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 618 | static QualType handleComplexFloatConversion(Sema &S, ExprResult &LHS, |
| 619 | ExprResult &RHS, QualType LHSType, |
| 620 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 621 | bool IsCompAssign) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 622 | // if we have an integer operand, the result is the complex type. |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 623 | if (!handleIntegerToComplexFloatConversion(S, RHS, LHS, RHSType, LHSType, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 624 | /*skipCast*/false)) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 625 | return LHSType; |
| 626 | if (!handleIntegerToComplexFloatConversion(S, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 627 | /*skipCast*/IsCompAssign)) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 628 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 629 | |
| 630 | // This handles complex/complex, complex/float, or float/complex. |
| 631 | // When both operands are complex, the shorter operand is converted to the |
| 632 | // type of the longer, and that is the type of the result. This corresponds |
| 633 | // to what is done when combining two real floating-point operands. |
| 634 | // The fun begins when size promotion occur across type domains. |
| 635 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 636 | // floating-point type, the less precise type is converted, within it's |
| 637 | // real or complex domain, to the precision of the other type. For example, |
| 638 | // when combining a "long double" with a "double _Complex", the |
| 639 | // "double _Complex" is promoted to "long double _Complex". |
| 640 | |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 641 | bool LHSComplexFloat = LHSType->isComplexType(); |
| 642 | bool RHSComplexFloat = RHSType->isComplexType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 643 | |
| 644 | // If both are complex, just cast to the more precise type. |
| 645 | if (LHSComplexFloat && RHSComplexFloat) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 646 | return handleComplexFloatToComplexFloatConverstion(S, LHS, RHS, |
| 647 | LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 648 | IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 649 | |
| 650 | // If only one operand is complex, promote it if necessary and convert the |
| 651 | // other operand to complex. |
| 652 | if (LHSComplexFloat) |
| 653 | return handleOtherComplexFloatConversion( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 654 | S, LHS, RHS, LHSType, RHSType, /*convertComplexExpr*/!IsCompAssign, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 655 | /*convertOtherExpr*/ true); |
| 656 | |
| 657 | assert(RHSComplexFloat); |
| 658 | return handleOtherComplexFloatConversion( |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 659 | S, RHS, LHS, RHSType, LHSType, /*convertComplexExpr*/true, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 660 | /*convertOtherExpr*/ !IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /// \brief Hande arithmetic conversion from integer to float. Helper function |
| 664 | /// of UsualArithmeticConversions() |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 665 | static QualType handleIntToFloatConversion(Sema &S, ExprResult &FloatExpr, |
| 666 | ExprResult &IntExpr, |
| 667 | QualType FloatTy, QualType IntTy, |
| 668 | bool ConvertFloat, bool ConvertInt) { |
| 669 | if (IntTy->isIntegerType()) { |
| 670 | if (ConvertInt) |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 671 | // Convert intExpr to the lhs floating point type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 672 | IntExpr = S.ImpCastExprToType(IntExpr.take(), FloatTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 673 | CK_IntegralToFloating); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 674 | return FloatTy; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | // Convert both sides to the appropriate complex float. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 678 | assert(IntTy->isComplexIntegerType()); |
| 679 | QualType result = S.Context.getComplexType(FloatTy); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 680 | |
| 681 | // _Complex int -> _Complex float |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 682 | if (ConvertInt) |
| 683 | IntExpr = S.ImpCastExprToType(IntExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 684 | CK_IntegralComplexToFloatingComplex); |
| 685 | |
| 686 | // float -> _Complex float |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 687 | if (ConvertFloat) |
| 688 | FloatExpr = S.ImpCastExprToType(FloatExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 689 | CK_FloatingRealToComplex); |
| 690 | |
| 691 | return result; |
| 692 | } |
| 693 | |
| 694 | /// \brief Handle arithmethic conversion with floating point types. Helper |
| 695 | /// function of UsualArithmeticConversions() |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 696 | static QualType handleFloatConversion(Sema &S, ExprResult &LHS, |
| 697 | ExprResult &RHS, QualType LHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 698 | QualType RHSType, bool IsCompAssign) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 699 | bool LHSFloat = LHSType->isRealFloatingType(); |
| 700 | bool RHSFloat = RHSType->isRealFloatingType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 701 | |
| 702 | // If we have two real floating types, convert the smaller operand |
| 703 | // to the bigger result. |
| 704 | if (LHSFloat && RHSFloat) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 705 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 706 | if (order > 0) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 707 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingCast); |
| 708 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | assert(order < 0 && "illegal float comparison"); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 712 | if (!IsCompAssign) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 713 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingCast); |
| 714 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | if (LHSFloat) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 718 | return handleIntToFloatConversion(S, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 719 | /*convertFloat=*/!IsCompAssign, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 720 | /*convertInt=*/ true); |
| 721 | assert(RHSFloat); |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 722 | return handleIntToFloatConversion(S, RHS, LHS, RHSType, LHSType, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 723 | /*convertInt=*/ true, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 724 | /*convertFloat=*/!IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /// \brief Handle conversions with GCC complex int extension. Helper function |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 728 | /// of UsualArithmeticConversions() |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 729 | // FIXME: if the operands are (int, _Complex long), we currently |
| 730 | // don't promote the complex. Also, signedness? |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 731 | static QualType handleComplexIntConversion(Sema &S, ExprResult &LHS, |
| 732 | ExprResult &RHS, QualType LHSType, |
| 733 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 734 | bool IsCompAssign) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 735 | const ComplexType *LHSComplexInt = LHSType->getAsComplexIntegerType(); |
| 736 | const ComplexType *RHSComplexInt = RHSType->getAsComplexIntegerType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 737 | |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 738 | if (LHSComplexInt && RHSComplexInt) { |
| 739 | int order = S.Context.getIntegerTypeOrder(LHSComplexInt->getElementType(), |
| 740 | RHSComplexInt->getElementType()); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 741 | assert(order && "inequal types with equal element ordering"); |
| 742 | if (order > 0) { |
| 743 | // _Complex int -> _Complex long |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 744 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralComplexCast); |
| 745 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 748 | if (!IsCompAssign) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 749 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralComplexCast); |
| 750 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 753 | if (LHSComplexInt) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 754 | // int -> _Complex int |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 755 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralRealToComplex); |
| 756 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 759 | assert(RHSComplexInt); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 760 | // int -> _Complex int |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 761 | if (!IsCompAssign) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 762 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralRealToComplex); |
| 763 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /// \brief Handle integer arithmetic conversions. Helper function of |
| 767 | /// UsualArithmeticConversions() |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 768 | static QualType handleIntegerConversion(Sema &S, ExprResult &LHS, |
| 769 | ExprResult &RHS, QualType LHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 770 | QualType RHSType, bool IsCompAssign) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 771 | // The rules for this case are in C99 6.3.1.8 |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 772 | int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); |
| 773 | bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); |
| 774 | bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); |
| 775 | if (LHSSigned == RHSSigned) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 776 | // Same signedness; use the higher-ranked type |
| 777 | if (order >= 0) { |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 778 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 779 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 780 | } else if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 781 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 782 | return RHSType; |
| 783 | } else if (order != (LHSSigned ? 1 : -1)) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 784 | // The unsigned type has greater than or equal rank to the |
| 785 | // signed type, so use the unsigned type |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 786 | if (RHSSigned) { |
| 787 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 788 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 789 | } else if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 790 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 791 | return RHSType; |
| 792 | } else if (S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 793 | // The two types are different widths; if we are here, that |
| 794 | // means the signed type is larger than the unsigned type, so |
| 795 | // use the signed type. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 796 | if (LHSSigned) { |
| 797 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 798 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 799 | } else if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 800 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 801 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 802 | } else { |
| 803 | // The signed type is higher-ranked than the unsigned type, |
| 804 | // but isn't actually any bigger (like unsigned int and long |
| 805 | // on most 32-bit systems). Use the unsigned type corresponding |
| 806 | // to the signed type. |
| 807 | QualType result = |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 808 | S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType : RHSType); |
| 809 | RHS = S.ImpCastExprToType(RHS.take(), result, CK_IntegralCast); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 810 | if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 811 | LHS = S.ImpCastExprToType(LHS.take(), result, CK_IntegralCast); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 812 | return result; |
| 813 | } |
| 814 | } |
| 815 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 816 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 817 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 818 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 819 | /// responsible for emitting appropriate error diagnostics. |
| 820 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 821 | /// GCC. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 822 | QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 823 | bool IsCompAssign) { |
| 824 | if (!IsCompAssign) { |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 825 | LHS = UsualUnaryConversions(LHS.take()); |
| 826 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 827 | return QualType(); |
| 828 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 829 | |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 830 | RHS = UsualUnaryConversions(RHS.take()); |
| 831 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 832 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 833 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 835 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 836 | QualType LHSType = |
| 837 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 838 | QualType RHSType = |
| 839 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 840 | |
| 841 | // If both types are identical, no conversion is needed. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 842 | if (LHSType == RHSType) |
| 843 | return LHSType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 844 | |
| 845 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 846 | // The caller can deal with this (e.g. pointer + int). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 847 | if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType()) |
| 848 | return LHSType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 849 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 850 | // Apply unary and bitfield promotions to the LHS's type. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 851 | QualType LHSUnpromotedType = LHSType; |
| 852 | if (LHSType->isPromotableIntegerType()) |
| 853 | LHSType = Context.getPromotedIntegerType(LHSType); |
| 854 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(LHS.get()); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 855 | if (!LHSBitfieldPromoteTy.isNull()) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 856 | LHSType = LHSBitfieldPromoteTy; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 857 | if (LHSType != LHSUnpromotedType && !IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 858 | LHS = ImpCastExprToType(LHS.take(), LHSType, CK_IntegralCast); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 859 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 860 | // If both types are identical, no conversion is needed. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 861 | if (LHSType == RHSType) |
| 862 | return LHSType; |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 863 | |
| 864 | // At this point, we have two different arithmetic types. |
| 865 | |
| 866 | // Handle complex types first (C99 6.3.1.8p1). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 867 | if (LHSType->isComplexType() || RHSType->isComplexType()) |
| 868 | return handleComplexFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 869 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 870 | |
| 871 | // Now handle "real" floating types (i.e. float, double, long double). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 872 | if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) |
| 873 | return handleFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 874 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 875 | |
| 876 | // Handle GCC complex int extension. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 877 | if (LHSType->isComplexIntegerType() || RHSType->isComplexIntegerType()) |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 878 | return handleComplexIntConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 879 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 880 | |
| 881 | // Finally, we have two differing integer types. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 882 | return handleIntegerConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 883 | IsCompAssign); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 886 | //===----------------------------------------------------------------------===// |
| 887 | // Semantic Analysis for various Expression Types |
| 888 | //===----------------------------------------------------------------------===// |
| 889 | |
| 890 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 891 | ExprResult |
| 892 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 893 | SourceLocation DefaultLoc, |
| 894 | SourceLocation RParenLoc, |
| 895 | Expr *ControllingExpr, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 896 | MultiTypeArg ArgTypes, |
| 897 | MultiExprArg ArgExprs) { |
| 898 | unsigned NumAssocs = ArgTypes.size(); |
| 899 | assert(NumAssocs == ArgExprs.size()); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 900 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 901 | ParsedType *ParsedTypes = ArgTypes.release(); |
| 902 | Expr **Exprs = ArgExprs.release(); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 903 | |
| 904 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 905 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 906 | if (ParsedTypes[i]) |
| 907 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 908 | else |
| 909 | Types[i] = 0; |
| 910 | } |
| 911 | |
| 912 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 913 | ControllingExpr, Types, Exprs, |
| 914 | NumAssocs); |
Benjamin Kramer | 5bf47f7 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 915 | delete [] Types; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 916 | return ER; |
| 917 | } |
| 918 | |
| 919 | ExprResult |
| 920 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 921 | SourceLocation DefaultLoc, |
| 922 | SourceLocation RParenLoc, |
| 923 | Expr *ControllingExpr, |
| 924 | TypeSourceInfo **Types, |
| 925 | Expr **Exprs, |
| 926 | unsigned NumAssocs) { |
| 927 | bool TypeErrorFound = false, |
| 928 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 929 | ContainsUnexpandedParameterPack |
| 930 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 931 | |
| 932 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 933 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 934 | ContainsUnexpandedParameterPack = true; |
| 935 | |
| 936 | if (Types[i]) { |
| 937 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 938 | ContainsUnexpandedParameterPack = true; |
| 939 | |
| 940 | if (Types[i]->getType()->isDependentType()) { |
| 941 | IsResultDependent = true; |
| 942 | } else { |
| 943 | // C1X 6.5.1.1p2 "The type name in a generic association shall specify a |
| 944 | // complete object type other than a variably modified type." |
| 945 | unsigned D = 0; |
| 946 | if (Types[i]->getType()->isIncompleteType()) |
| 947 | D = diag::err_assoc_type_incomplete; |
| 948 | else if (!Types[i]->getType()->isObjectType()) |
| 949 | D = diag::err_assoc_type_nonobject; |
| 950 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 951 | D = diag::err_assoc_type_variably_modified; |
| 952 | |
| 953 | if (D != 0) { |
| 954 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 955 | << Types[i]->getTypeLoc().getSourceRange() |
| 956 | << Types[i]->getType(); |
| 957 | TypeErrorFound = true; |
| 958 | } |
| 959 | |
| 960 | // C1X 6.5.1.1p2 "No two generic associations in the same generic |
| 961 | // selection shall specify compatible types." |
| 962 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 963 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 964 | Context.typesAreCompatible(Types[i]->getType(), |
| 965 | Types[j]->getType())) { |
| 966 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 967 | diag::err_assoc_compatible_types) |
| 968 | << Types[j]->getTypeLoc().getSourceRange() |
| 969 | << Types[j]->getType() |
| 970 | << Types[i]->getType(); |
| 971 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 972 | diag::note_compat_assoc) |
| 973 | << Types[i]->getTypeLoc().getSourceRange() |
| 974 | << Types[i]->getType(); |
| 975 | TypeErrorFound = true; |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | if (TypeErrorFound) |
| 981 | return ExprError(); |
| 982 | |
| 983 | // If we determined that the generic selection is result-dependent, don't |
| 984 | // try to compute the result expression. |
| 985 | if (IsResultDependent) |
| 986 | return Owned(new (Context) GenericSelectionExpr( |
| 987 | Context, KeyLoc, ControllingExpr, |
| 988 | Types, Exprs, NumAssocs, DefaultLoc, |
| 989 | RParenLoc, ContainsUnexpandedParameterPack)); |
| 990 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 991 | SmallVector<unsigned, 1> CompatIndices; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 992 | unsigned DefaultIndex = -1U; |
| 993 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 994 | if (!Types[i]) |
| 995 | DefaultIndex = i; |
| 996 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 997 | Types[i]->getType())) |
| 998 | CompatIndices.push_back(i); |
| 999 | } |
| 1000 | |
| 1001 | // C1X 6.5.1.1p2 "The controlling expression of a generic selection shall have |
| 1002 | // type compatible with at most one of the types named in its generic |
| 1003 | // association list." |
| 1004 | if (CompatIndices.size() > 1) { |
| 1005 | // We strip parens here because the controlling expression is typically |
| 1006 | // parenthesized in macro definitions. |
| 1007 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1008 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 1009 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 1010 | << (unsigned) CompatIndices.size(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1011 | for (SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1012 | E = CompatIndices.end(); I != E; ++I) { |
| 1013 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 1014 | diag::note_compat_assoc) |
| 1015 | << Types[*I]->getTypeLoc().getSourceRange() |
| 1016 | << Types[*I]->getType(); |
| 1017 | } |
| 1018 | return ExprError(); |
| 1019 | } |
| 1020 | |
| 1021 | // C1X 6.5.1.1p2 "If a generic selection has no default generic association, |
| 1022 | // its controlling expression shall have type compatible with exactly one of |
| 1023 | // the types named in its generic association list." |
| 1024 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 1025 | // We strip parens here because the controlling expression is typically |
| 1026 | // parenthesized in macro definitions. |
| 1027 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1028 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 1029 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 1030 | return ExprError(); |
| 1031 | } |
| 1032 | |
| 1033 | // C1X 6.5.1.1p3 "If a generic selection has a generic association with a |
| 1034 | // type name that is compatible with the type of the controlling expression, |
| 1035 | // then the result expression of the generic selection is the expression |
| 1036 | // in that generic association. Otherwise, the result expression of the |
| 1037 | // generic selection is the expression in the default generic association." |
| 1038 | unsigned ResultIndex = |
| 1039 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 1040 | |
| 1041 | return Owned(new (Context) GenericSelectionExpr( |
| 1042 | Context, KeyLoc, ControllingExpr, |
| 1043 | Types, Exprs, NumAssocs, DefaultLoc, |
| 1044 | RParenLoc, ContainsUnexpandedParameterPack, |
| 1045 | ResultIndex)); |
| 1046 | } |
| 1047 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1048 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1049 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 1050 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 1051 | /// multiple tokens. However, the common case is that StringToks points to one |
| 1052 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1053 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1054 | ExprResult |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1055 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1056 | assert(NumStringToks && "Must have at least one string!"); |
| 1057 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1058 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1059 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1060 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1061 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1062 | SmallVector<SourceLocation, 4> StringTokLocs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1063 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 1064 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1065 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1066 | QualType StrTy = Context.CharTy; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1067 | if (Literal.isWide()) |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1068 | StrTy = Context.getWCharType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1069 | else if (Literal.isUTF16()) |
| 1070 | StrTy = Context.Char16Ty; |
| 1071 | else if (Literal.isUTF32()) |
| 1072 | StrTy = Context.Char32Ty; |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1073 | else if (Literal.Pascal) |
| 1074 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1075 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1076 | StringLiteral::StringKind Kind = StringLiteral::Ascii; |
| 1077 | if (Literal.isWide()) |
| 1078 | Kind = StringLiteral::Wide; |
| 1079 | else if (Literal.isUTF8()) |
| 1080 | Kind = StringLiteral::UTF8; |
| 1081 | else if (Literal.isUTF16()) |
| 1082 | Kind = StringLiteral::UTF16; |
| 1083 | else if (Literal.isUTF32()) |
| 1084 | Kind = StringLiteral::UTF32; |
| 1085 | |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1086 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
Chris Lattner | 7dc480f | 2010-06-15 18:05:34 +0000 | [diff] [blame] | 1087 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1088 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1089 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1090 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 1091 | // the nul terminator character as well as the string length for pascal |
| 1092 | // strings. |
| 1093 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1094 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1095 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1097 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1098 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1099 | Kind, Literal.Pascal, StrTy, |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1100 | &StringTokLocs[0], |
| 1101 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1104 | enum CaptureResult { |
| 1105 | /// No capture is required. |
| 1106 | CR_NoCapture, |
| 1107 | |
| 1108 | /// A capture is required. |
| 1109 | CR_Capture, |
| 1110 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1111 | /// A by-ref capture is required. |
| 1112 | CR_CaptureByRef, |
| 1113 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1114 | /// An error occurred when trying to capture the given variable. |
| 1115 | CR_Error |
| 1116 | }; |
| 1117 | |
| 1118 | /// Diagnose an uncapturable value reference. |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1119 | /// |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1120 | /// \param var - the variable referenced |
| 1121 | /// \param DC - the context which we couldn't capture through |
| 1122 | static CaptureResult |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1123 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1124 | VarDecl *var, DeclContext *DC) { |
| 1125 | switch (S.ExprEvalContexts.back().Context) { |
| 1126 | case Sema::Unevaluated: |
| 1127 | // The argument will never be evaluated, so don't complain. |
| 1128 | return CR_NoCapture; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1130 | case Sema::PotentiallyEvaluated: |
| 1131 | case Sema::PotentiallyEvaluatedIfUsed: |
| 1132 | break; |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1133 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1134 | case Sema::PotentiallyPotentiallyEvaluated: |
| 1135 | // FIXME: delay these! |
| 1136 | break; |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 1137 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1139 | // Don't diagnose about capture if we're not actually in code right |
| 1140 | // now; in general, there are more appropriate places that will |
| 1141 | // diagnose this. |
| 1142 | if (!S.CurContext->isFunctionOrMethod()) return CR_NoCapture; |
| 1143 | |
John McCall | 4f38f41 | 2011-03-22 23:15:50 +0000 | [diff] [blame] | 1144 | // Certain madnesses can happen with parameter declarations, which |
| 1145 | // we want to ignore. |
| 1146 | if (isa<ParmVarDecl>(var)) { |
| 1147 | // - If the parameter still belongs to the translation unit, then |
| 1148 | // we're actually just using one parameter in the declaration of |
| 1149 | // the next. This is useful in e.g. VLAs. |
| 1150 | if (isa<TranslationUnitDecl>(var->getDeclContext())) |
| 1151 | return CR_NoCapture; |
| 1152 | |
| 1153 | // - This particular madness can happen in ill-formed default |
| 1154 | // arguments; claim it's okay and let downstream code handle it. |
| 1155 | if (S.CurContext == var->getDeclContext()->getParent()) |
| 1156 | return CR_NoCapture; |
| 1157 | } |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1158 | |
| 1159 | DeclarationName functionName; |
| 1160 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(var->getDeclContext())) |
| 1161 | functionName = fn->getDeclName(); |
| 1162 | // FIXME: variable from enclosing block that we couldn't capture from! |
| 1163 | |
| 1164 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 1165 | << var->getIdentifier() << functionName; |
| 1166 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 1167 | << var->getIdentifier(); |
| 1168 | |
| 1169 | return CR_Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1172 | /// There is a well-formed capture at a particular scope level; |
| 1173 | /// propagate it through all the nested blocks. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1174 | static CaptureResult propagateCapture(Sema &S, unsigned ValidScopeIndex, |
| 1175 | const BlockDecl::Capture &Capture) { |
| 1176 | VarDecl *var = Capture.getVariable(); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1177 | |
| 1178 | // Update all the inner blocks with the capture information. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1179 | for (unsigned i = ValidScopeIndex + 1, e = S.FunctionScopes.size(); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1180 | i != e; ++i) { |
| 1181 | BlockScopeInfo *innerBlock = cast<BlockScopeInfo>(S.FunctionScopes[i]); |
| 1182 | innerBlock->Captures.push_back( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1183 | BlockDecl::Capture(Capture.getVariable(), Capture.isByRef(), |
| 1184 | /*nested*/ true, Capture.getCopyExpr())); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1185 | innerBlock->CaptureMap[var] = innerBlock->Captures.size(); // +1 |
| 1186 | } |
| 1187 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1188 | return Capture.isByRef() ? CR_CaptureByRef : CR_Capture; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | /// shouldCaptureValueReference - Determine if a reference to the |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1192 | /// given value in the current context requires a variable capture. |
| 1193 | /// |
| 1194 | /// This also keeps the captures set in the BlockScopeInfo records |
| 1195 | /// up-to-date. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1196 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1197 | ValueDecl *Value) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1198 | // Only variables ever require capture. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1199 | VarDecl *var = dyn_cast<VarDecl>(Value); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1200 | if (!var) return CR_NoCapture; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1201 | |
| 1202 | // Fast path: variables from the current context never require capture. |
| 1203 | DeclContext *DC = S.CurContext; |
| 1204 | if (var->getDeclContext() == DC) return CR_NoCapture; |
| 1205 | |
| 1206 | // Only variables with local storage require capture. |
| 1207 | // FIXME: What about 'const' variables in C++? |
| 1208 | if (!var->hasLocalStorage()) return CR_NoCapture; |
| 1209 | |
| 1210 | // Otherwise, we need to capture. |
| 1211 | |
| 1212 | unsigned functionScopesIndex = S.FunctionScopes.size() - 1; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1213 | do { |
| 1214 | // Only blocks (and eventually C++0x closures) can capture; other |
| 1215 | // scopes don't work. |
| 1216 | if (!isa<BlockDecl>(DC)) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1217 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1218 | |
| 1219 | BlockScopeInfo *blockScope = |
| 1220 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1221 | assert(blockScope->TheDecl == static_cast<BlockDecl*>(DC)); |
| 1222 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1223 | // Check whether we've already captured it in this block. If so, |
| 1224 | // we're done. |
| 1225 | if (unsigned indexPlus1 = blockScope->CaptureMap[var]) |
| 1226 | return propagateCapture(S, functionScopesIndex, |
| 1227 | blockScope->Captures[indexPlus1 - 1]); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1228 | |
| 1229 | functionScopesIndex--; |
| 1230 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 1231 | } while (var->getDeclContext() != DC); |
| 1232 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1233 | // Okay, we descended all the way to the block that defines the variable. |
| 1234 | // Actually try to capture it. |
| 1235 | QualType type = var->getType(); |
| 1236 | |
| 1237 | // Prohibit variably-modified types. |
| 1238 | if (type->isVariablyModifiedType()) { |
| 1239 | S.Diag(loc, diag::err_ref_vm_type); |
| 1240 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1241 | return CR_Error; |
| 1242 | } |
| 1243 | |
| 1244 | // Prohibit arrays, even in __block variables, but not references to |
| 1245 | // them. |
| 1246 | if (type->isArrayType()) { |
| 1247 | S.Diag(loc, diag::err_ref_array_type); |
| 1248 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1249 | return CR_Error; |
| 1250 | } |
| 1251 | |
| 1252 | S.MarkDeclarationReferenced(loc, var); |
| 1253 | |
| 1254 | // The BlocksAttr indicates the variable is bound by-reference. |
| 1255 | bool byRef = var->hasAttr<BlocksAttr>(); |
| 1256 | |
| 1257 | // Build a copy expression. |
| 1258 | Expr *copyExpr = 0; |
John McCall | 642a75f | 2011-04-28 02:15:35 +0000 | [diff] [blame] | 1259 | const RecordType *rtype; |
| 1260 | if (!byRef && S.getLangOptions().CPlusPlus && !type->isDependentType() && |
| 1261 | (rtype = type->getAs<RecordType>())) { |
| 1262 | |
| 1263 | // The capture logic needs the destructor, so make sure we mark it. |
| 1264 | // Usually this is unnecessary because most local variables have |
| 1265 | // their destructors marked at declaration time, but parameters are |
| 1266 | // an exception because it's technically only the call site that |
| 1267 | // actually requires the destructor. |
| 1268 | if (isa<ParmVarDecl>(var)) |
| 1269 | S.FinalizeVarWithDestructor(var, rtype); |
| 1270 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1271 | // According to the blocks spec, the capture of a variable from |
| 1272 | // the stack requires a const copy constructor. This is not true |
| 1273 | // of the copy/move done to move a __block variable to the heap. |
| 1274 | type.addConst(); |
| 1275 | |
| 1276 | Expr *declRef = new (S.Context) DeclRefExpr(var, type, VK_LValue, loc); |
| 1277 | ExprResult result = |
| 1278 | S.PerformCopyInitialization( |
| 1279 | InitializedEntity::InitializeBlock(var->getLocation(), |
| 1280 | type, false), |
| 1281 | loc, S.Owned(declRef)); |
| 1282 | |
| 1283 | // Build a full-expression copy expression if initialization |
| 1284 | // succeeded and used a non-trivial constructor. Recover from |
| 1285 | // errors by pretending that the copy isn't necessary. |
| 1286 | if (!result.isInvalid() && |
| 1287 | !cast<CXXConstructExpr>(result.get())->getConstructor()->isTrivial()) { |
| 1288 | result = S.MaybeCreateExprWithCleanups(result); |
| 1289 | copyExpr = result.take(); |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | // We're currently at the declarer; go back to the closure. |
| 1294 | functionScopesIndex++; |
| 1295 | BlockScopeInfo *blockScope = |
| 1296 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1297 | |
| 1298 | // Build a valid capture in this scope. |
| 1299 | blockScope->Captures.push_back( |
| 1300 | BlockDecl::Capture(var, byRef, /*nested*/ false, copyExpr)); |
| 1301 | blockScope->CaptureMap[var] = blockScope->Captures.size(); // +1 |
| 1302 | |
| 1303 | // Propagate that to inner captures if necessary. |
| 1304 | return propagateCapture(S, functionScopesIndex, |
| 1305 | blockScope->Captures.back()); |
| 1306 | } |
| 1307 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1308 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *VD, |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1309 | const DeclarationNameInfo &NameInfo, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1310 | bool ByRef) { |
| 1311 | assert(isa<VarDecl>(VD) && "capturing non-variable"); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1312 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1313 | VarDecl *var = cast<VarDecl>(VD); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1314 | assert(var->hasLocalStorage() && "capturing non-local"); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1315 | assert(ByRef == var->hasAttr<BlocksAttr>() && "byref set wrong"); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1316 | |
| 1317 | QualType exprType = var->getType().getNonReferenceType(); |
| 1318 | |
| 1319 | BlockDeclRefExpr *BDRE; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1320 | if (!ByRef) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1321 | // The variable will be bound by copy; make it const within the |
| 1322 | // closure, but record that this was done in the expression. |
| 1323 | bool constAdded = !exprType.isConstQualified(); |
| 1324 | exprType.addConst(); |
| 1325 | |
| 1326 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1327 | NameInfo.getLoc(), false, |
| 1328 | constAdded); |
| 1329 | } else { |
| 1330 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1331 | NameInfo.getLoc(), true); |
| 1332 | } |
| 1333 | |
| 1334 | return S.Owned(BDRE); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1335 | } |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1336 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1337 | ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1338 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1339 | SourceLocation Loc, |
| 1340 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1341 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1342 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1345 | /// BuildDeclRefExpr - Build an expression that references a |
| 1346 | /// declaration that does not require a closure capture. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1347 | ExprResult |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1348 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1349 | const DeclarationNameInfo &NameInfo, |
| 1350 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1351 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1353 | Expr *E = DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1354 | SS? SS->getWithLocInContext(Context) |
| 1355 | : NestedNameSpecifierLoc(), |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1356 | D, NameInfo, Ty, VK); |
| 1357 | |
| 1358 | // Just in case we're building an illegal pointer-to-member. |
| 1359 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 1360 | E->setObjectKind(OK_BitField); |
| 1361 | |
| 1362 | return Owned(E); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1365 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1366 | /// possibly a list of template arguments. |
| 1367 | /// |
| 1368 | /// If this produces template arguments, it is permitted to call |
| 1369 | /// DecomposeTemplateName. |
| 1370 | /// |
| 1371 | /// This actually loses a lot of source location information for |
| 1372 | /// non-standard name kinds; we should consider preserving that in |
| 1373 | /// some way. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1374 | void |
| 1375 | Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, |
| 1376 | TemplateArgumentListInfo &Buffer, |
| 1377 | DeclarationNameInfo &NameInfo, |
| 1378 | const TemplateArgumentListInfo *&TemplateArgs) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1379 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1380 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1381 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1382 | |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1383 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1384 | Id.TemplateId->getTemplateArgs(), |
| 1385 | Id.TemplateId->NumArgs); |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1386 | translateTemplateArguments(TemplateArgsPtr, Buffer); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1387 | TemplateArgsPtr.release(); |
| 1388 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1389 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1390 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1391 | NameInfo = Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1392 | TemplateArgs = &Buffer; |
| 1393 | } else { |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1394 | NameInfo = GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1395 | TemplateArgs = 0; |
| 1396 | } |
| 1397 | } |
| 1398 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1399 | /// Diagnose an empty lookup. |
| 1400 | /// |
| 1401 | /// \return false if new lookup candidates were found |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1402 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1403 | CorrectTypoContext CTC, |
| 1404 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 1405 | Expr **Args, unsigned NumArgs) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1406 | DeclarationName Name = R.getLookupName(); |
| 1407 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1408 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1409 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1410 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1411 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1412 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1413 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1414 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1415 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1416 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1417 | // If the original lookup was an unqualified lookup, fake an |
| 1418 | // unqualified lookup. This is useful when (for example) the |
| 1419 | // original lookup would not have found something because it was a |
| 1420 | // dependent name. |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1421 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1422 | DC; DC = DC->getParent()) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1423 | if (isa<CXXRecordDecl>(DC)) { |
| 1424 | LookupQualifiedName(R, DC); |
| 1425 | |
| 1426 | if (!R.empty()) { |
| 1427 | // Don't give errors about ambiguities in this lookup. |
| 1428 | R.suppressDiagnostics(); |
| 1429 | |
| 1430 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1431 | bool isInstance = CurMethod && |
| 1432 | CurMethod->isInstance() && |
| 1433 | DC == CurMethod->getParent(); |
| 1434 | |
| 1435 | // Give a code modification hint to insert 'this->'. |
| 1436 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1437 | // Actually quite difficult! |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1438 | if (isInstance) { |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1439 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1440 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1441 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1442 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1443 | if (DepMethod) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1444 | if (getLangOptions().MicrosoftExt) |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 1445 | diagnostic = diag::warn_found_via_dependent_bases_lookup; |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1446 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1447 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1448 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1449 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1450 | R.getNameLoc(), DepThisType, false); |
| 1451 | TemplateArgumentListInfo TList; |
| 1452 | if (ULE->hasExplicitTemplateArgs()) |
| 1453 | ULE->copyTemplateArgumentsInto(TList); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1454 | |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1455 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1456 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1457 | CXXDependentScopeMemberExpr *DepExpr = |
| 1458 | CXXDependentScopeMemberExpr::Create( |
| 1459 | Context, DepThis, DepThisType, true, SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1460 | SS.getWithLocInContext(Context), NULL, |
Francois Pichet | f740012 | 2011-09-04 23:00:48 +0000 | [diff] [blame] | 1461 | R.getLookupNameInfo(), |
| 1462 | ULE->hasExplicitTemplateArgs() ? &TList : 0); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1463 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1464 | } else { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1465 | // FIXME: we should be able to handle this case too. It is correct |
| 1466 | // to add this-> here. This is a workaround for PR7947. |
| 1467 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1468 | } |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1469 | } else { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1470 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1471 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1472 | |
| 1473 | // Do we really want to note all of these? |
| 1474 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1475 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1476 | |
| 1477 | // Tell the callee to try to recover. |
| 1478 | return false; |
| 1479 | } |
Douglas Gregor | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1480 | |
| 1481 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1485 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1486 | TypoCorrection Corrected; |
| 1487 | if (S && (Corrected = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), |
| 1488 | S, &SS, NULL, false, CTC))) { |
| 1489 | std::string CorrectedStr(Corrected.getAsString(getLangOptions())); |
| 1490 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
| 1491 | R.setLookupName(Corrected.getCorrection()); |
| 1492 | |
Hans Wennborg | 701d1e7 | 2011-07-12 08:45:31 +0000 | [diff] [blame] | 1493 | if (NamedDecl *ND = Corrected.getCorrectionDecl()) { |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1494 | if (Corrected.isOverloaded()) { |
| 1495 | OverloadCandidateSet OCS(R.getNameLoc()); |
| 1496 | OverloadCandidateSet::iterator Best; |
| 1497 | for (TypoCorrection::decl_iterator CD = Corrected.begin(), |
| 1498 | CDEnd = Corrected.end(); |
| 1499 | CD != CDEnd; ++CD) { |
Kaelyn Uhrain | adc7a73 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1500 | if (FunctionTemplateDecl *FTD = |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1501 | dyn_cast<FunctionTemplateDecl>(*CD)) |
| 1502 | AddTemplateOverloadCandidate( |
| 1503 | FTD, DeclAccessPair::make(FTD, AS_none), ExplicitTemplateArgs, |
| 1504 | Args, NumArgs, OCS); |
Kaelyn Uhrain | adc7a73 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1505 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*CD)) |
| 1506 | if (!ExplicitTemplateArgs || ExplicitTemplateArgs->size() == 0) |
| 1507 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), |
| 1508 | Args, NumArgs, OCS); |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1509 | } |
| 1510 | switch (OCS.BestViableFunction(*this, R.getNameLoc(), Best)) { |
| 1511 | case OR_Success: |
| 1512 | ND = Best->Function; |
| 1513 | break; |
| 1514 | default: |
Kaelyn Uhrain | 844d572 | 2011-08-04 23:30:54 +0000 | [diff] [blame] | 1515 | break; |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1516 | } |
| 1517 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1518 | R.addDecl(ND); |
| 1519 | if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1520 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1521 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr |
| 1522 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1523 | else |
| 1524 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1525 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1526 | << SS.getRange() |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1527 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
| 1528 | if (ND) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1529 | Diag(ND->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1530 | << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1531 | |
| 1532 | // Tell the callee to try to recover. |
| 1533 | return false; |
| 1534 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1535 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1536 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1537 | // FIXME: If we ended up with a typo for a type name or |
| 1538 | // Objective-C class name, we're in trouble because the parser |
| 1539 | // is in the wrong place to recover. Suggest the typo |
| 1540 | // correction, but don't make it a fix-it since we're not going |
| 1541 | // to recover well anyway. |
| 1542 | if (SS.isEmpty()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1543 | Diag(R.getNameLoc(), diagnostic_suggest) |
| 1544 | << Name << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1545 | else |
| 1546 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1547 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1548 | << SS.getRange(); |
| 1549 | |
| 1550 | // Don't try to recover; it won't work. |
| 1551 | return true; |
| 1552 | } |
| 1553 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1554 | // FIXME: We found a keyword. Suggest it, but don't provide a fix-it |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1555 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1556 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1557 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1558 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1559 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1560 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1561 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1562 | return true; |
| 1563 | } |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1564 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1565 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1566 | |
| 1567 | // Emit a special diagnostic for failed member lookups. |
| 1568 | // FIXME: computing the declaration context might fail here (?) |
| 1569 | if (!SS.isEmpty()) { |
| 1570 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1571 | << Name << computeDeclContext(SS, false) |
| 1572 | << SS.getRange(); |
| 1573 | return true; |
| 1574 | } |
| 1575 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1576 | // Give up, we can't recover. |
| 1577 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1578 | return true; |
| 1579 | } |
| 1580 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1581 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1582 | CXXScopeSpec &SS, |
| 1583 | UnqualifiedId &Id, |
| 1584 | bool HasTrailingLParen, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1585 | bool IsAddressOfOperand) { |
| 1586 | assert(!(IsAddressOfOperand && HasTrailingLParen) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1587 | "cannot be direct & operand and have a trailing lparen"); |
| 1588 | |
| 1589 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1590 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1591 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1592 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1593 | |
| 1594 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1595 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1596 | const TemplateArgumentListInfo *TemplateArgs; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1597 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1598 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1599 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1600 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1601 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1602 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1603 | // C++ [temp.dep.expr]p3: |
| 1604 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1605 | // -- an identifier that was declared with a dependent type, |
| 1606 | // (note: handled after lookup) |
| 1607 | // -- a template-id that is dependent, |
| 1608 | // (note: handled in BuildTemplateIdExpr) |
| 1609 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1610 | // -- a nested-name-specifier that contains a class-name that |
| 1611 | // names a dependent type. |
| 1612 | // Determine whether this is a member of an unknown specialization; |
| 1613 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1614 | bool DependentID = false; |
| 1615 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1616 | Name.getCXXNameType()->isDependentType()) { |
| 1617 | DependentID = true; |
| 1618 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1619 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1620 | if (RequireCompleteDeclContext(SS, DC)) |
| 1621 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1622 | } else { |
| 1623 | DependentID = true; |
| 1624 | } |
| 1625 | } |
| 1626 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1627 | if (DependentID) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1628 | return ActOnDependentIdExpression(SS, NameInfo, IsAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1629 | TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1630 | |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1631 | bool IvarLookupFollowUp = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1632 | // Perform the required lookup. |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1633 | LookupResult R(*this, NameInfo, |
| 1634 | (Id.getKind() == UnqualifiedId::IK_ImplicitSelfParam) |
| 1635 | ? LookupObjCImplicitSelfParam : LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1636 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1637 | // Lookup the template name again to correctly establish the context in |
| 1638 | // which it was found. This is really unfortunate as we already did the |
| 1639 | // lookup to determine that it was a template name in the first place. If |
| 1640 | // this becomes a performance hit, we can work harder to preserve those |
| 1641 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1642 | bool MemberOfUnknownSpecialization; |
| 1643 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1644 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1645 | |
| 1646 | if (MemberOfUnknownSpecialization || |
| 1647 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1648 | return ActOnDependentIdExpression(SS, NameInfo, IsAddressOfOperand, |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1649 | TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1650 | } else { |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1651 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1652 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1654 | // If the result might be in a dependent base class, this is a dependent |
| 1655 | // id-expression. |
| 1656 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1657 | return ActOnDependentIdExpression(SS, NameInfo, IsAddressOfOperand, |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1658 | TemplateArgs); |
| 1659 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1660 | // If this reference is in an Objective-C method, then we need to do |
| 1661 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1662 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1663 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1664 | if (E.isInvalid()) |
| 1665 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1667 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1668 | return Owned(Ex); |
| 1669 | |
Fariborz Jahanian | f759b4d | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1670 | // for further use, this must be set to false if in class method. |
| 1671 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1672 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1673 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1674 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1675 | if (R.isAmbiguous()) |
| 1676 | return ExprError(); |
| 1677 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1678 | // Determine whether this name might be a candidate for |
| 1679 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1680 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1681 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1682 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1683 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1684 | // in C90, extension in C99, forbidden in C++). |
| 1685 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1686 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1687 | if (D) R.addDecl(D); |
| 1688 | } |
| 1689 | |
| 1690 | // If this name wasn't predeclared and if this is not a function |
| 1691 | // call, diagnose the problem. |
| 1692 | if (R.empty()) { |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1693 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1694 | return ExprError(); |
| 1695 | |
| 1696 | assert(!R.empty() && |
| 1697 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1698 | |
| 1699 | // If we found an Objective-C instance variable, let |
| 1700 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1701 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1702 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1703 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1704 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1705 | assert(E.isInvalid() || E.get()); |
| 1706 | return move(E); |
| 1707 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1708 | } |
| 1709 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1711 | // This is guaranteed from this point on. |
| 1712 | assert(!R.empty() || ADL); |
| 1713 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1714 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1715 | // C++ [class.mfct.non-static]p3: |
| 1716 | // When an id-expression that is not part of a class member access |
| 1717 | // syntax and not used to form a pointer to member is used in the |
| 1718 | // body of a non-static member function of class X, if name lookup |
| 1719 | // resolves the name in the id-expression to a non-static non-type |
| 1720 | // member of some class C, the id-expression is transformed into a |
| 1721 | // class member access expression using (*this) as the |
| 1722 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1723 | // |
| 1724 | // But we don't actually need to do this for '&' operands if R |
| 1725 | // resolved to a function or overloaded function set, because the |
| 1726 | // expression is ill-formed if it actually works out to be a |
| 1727 | // non-static member function: |
| 1728 | // |
| 1729 | // C++ [expr.ref]p4: |
| 1730 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1731 | // [t]he expression can be used only as the left-hand operand of a |
| 1732 | // member function call. |
| 1733 | // |
| 1734 | // There are other safeguards against such uses, but it's important |
| 1735 | // to get this right here so that we don't end up making a |
| 1736 | // spuriously dependent expression if we're inside a dependent |
| 1737 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1738 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1739 | bool MightBeImplicitMember; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1740 | if (!IsAddressOfOperand) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1741 | MightBeImplicitMember = true; |
| 1742 | else if (!SS.isEmpty()) |
| 1743 | MightBeImplicitMember = false; |
| 1744 | else if (R.isOverloadedResult()) |
| 1745 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1746 | else if (R.isUnresolvableResult()) |
| 1747 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1748 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1749 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1750 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1751 | |
| 1752 | if (MightBeImplicitMember) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1753 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1756 | if (TemplateArgs) |
| 1757 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1758 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1759 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1760 | } |
| 1761 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1762 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1763 | /// declaration name, generally during template instantiation. |
| 1764 | /// There's a large number of things which don't need to be done along |
| 1765 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1766 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1767 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1768 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1769 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1770 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1771 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1772 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1773 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1774 | return ExprError(); |
| 1775 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1776 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1777 | LookupQualifiedName(R, DC); |
| 1778 | |
| 1779 | if (R.isAmbiguous()) |
| 1780 | return ExprError(); |
| 1781 | |
| 1782 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1783 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1784 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1785 | return ExprError(); |
| 1786 | } |
| 1787 | |
| 1788 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1789 | } |
| 1790 | |
| 1791 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1792 | /// detected that we're currently inside an ObjC method. Perform some |
| 1793 | /// additional lookup. |
| 1794 | /// |
| 1795 | /// Ideally, most of this would be done by lookup, but there's |
| 1796 | /// actually quite a lot of extra work involved. |
| 1797 | /// |
| 1798 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1799 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1800 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1801 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1802 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1803 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1804 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1805 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1806 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1807 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1808 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1809 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1810 | |
| 1811 | // If we're in a class method, we don't normally want to look for |
| 1812 | // ivars. But if we don't find anything else, and there's an |
| 1813 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1814 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1815 | |
| 1816 | bool LookForIvars; |
| 1817 | if (Lookup.empty()) |
| 1818 | LookForIvars = true; |
| 1819 | else if (IsClassMethod) |
| 1820 | LookForIvars = false; |
| 1821 | else |
| 1822 | LookForIvars = (Lookup.isSingleResult() && |
| 1823 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1824 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1825 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1826 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1827 | ObjCInterfaceDecl *ClassDeclared; |
| 1828 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1829 | // Diagnose using an ivar in a class method. |
| 1830 | if (IsClassMethod) |
| 1831 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1832 | << IV->getDeclName()); |
| 1833 | |
| 1834 | // If we're referencing an invalid decl, just return this as a silent |
| 1835 | // error node. The error diagnostic was already emitted on the decl. |
| 1836 | if (IV->isInvalidDecl()) |
| 1837 | return ExprError(); |
| 1838 | |
| 1839 | // Check if referencing a field with __attribute__((deprecated)). |
| 1840 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1841 | return ExprError(); |
| 1842 | |
| 1843 | // Diagnose the use of an ivar outside of the declaring class. |
| 1844 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1845 | ClassDeclared != IFace) |
| 1846 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1847 | |
| 1848 | // FIXME: This should use a new expr for a direct reference, don't |
| 1849 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1850 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1851 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1852 | SelfName.setIdentifier(&II, SourceLocation()); |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1853 | SelfName.setKind(UnqualifiedId::IK_ImplicitSelfParam); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1854 | CXXScopeSpec SelfScopeSpec; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1855 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1856 | SelfName, false, false); |
| 1857 | if (SelfExpr.isInvalid()) |
| 1858 | return ExprError(); |
| 1859 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1860 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 1861 | if (SelfExpr.isInvalid()) |
| 1862 | return ExprError(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1863 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1864 | MarkDeclarationReferenced(Loc, IV); |
| 1865 | return Owned(new (Context) |
| 1866 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1867 | SelfExpr.take(), true, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1868 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1869 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1870 | // We should warn if a local variable hides an ivar. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1871 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1872 | ObjCInterfaceDecl *ClassDeclared; |
| 1873 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1874 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1875 | IFace == ClassDeclared) |
| 1876 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1877 | } |
| 1878 | } |
| 1879 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1880 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1881 | // FIXME. Consolidate this with similar code in LookupName. |
| 1882 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1883 | if (!(getLangOptions().CPlusPlus && |
| 1884 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1885 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1886 | S, Lookup.isForRedeclaration(), |
| 1887 | Lookup.getNameLoc()); |
| 1888 | if (D) Lookup.addDecl(D); |
| 1889 | } |
| 1890 | } |
| 1891 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1892 | // Sentinel value saying that we didn't do anything special. |
| 1893 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1894 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1895 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1896 | /// \brief Cast a base object to a member's actual type. |
| 1897 | /// |
| 1898 | /// Logically this happens in three phases: |
| 1899 | /// |
| 1900 | /// * First we cast from the base type to the naming class. |
| 1901 | /// The naming class is the class into which we were looking |
| 1902 | /// when we found the member; it's the qualifier type if a |
| 1903 | /// qualifier was provided, and otherwise it's the base type. |
| 1904 | /// |
| 1905 | /// * Next we cast from the naming class to the declaring class. |
| 1906 | /// If the member we found was brought into a class's scope by |
| 1907 | /// a using declaration, this is that class; otherwise it's |
| 1908 | /// the class declaring the member. |
| 1909 | /// |
| 1910 | /// * Finally we cast from the declaring class to the "true" |
| 1911 | /// declaring class of the member. This conversion does not |
| 1912 | /// obey access control. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1913 | ExprResult |
| 1914 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1915 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1916 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1917 | NamedDecl *Member) { |
| 1918 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 1919 | if (!RD) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1920 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1921 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1922 | QualType DestRecordType; |
| 1923 | QualType DestType; |
| 1924 | QualType FromRecordType; |
| 1925 | QualType FromType = From->getType(); |
| 1926 | bool PointerConversions = false; |
| 1927 | if (isa<FieldDecl>(Member)) { |
| 1928 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1929 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1930 | if (FromType->getAs<PointerType>()) { |
| 1931 | DestType = Context.getPointerType(DestRecordType); |
| 1932 | FromRecordType = FromType->getPointeeType(); |
| 1933 | PointerConversions = true; |
| 1934 | } else { |
| 1935 | DestType = DestRecordType; |
| 1936 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1937 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1938 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 1939 | if (Method->isStatic()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1940 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1942 | DestType = Method->getThisType(Context); |
| 1943 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1945 | if (FromType->getAs<PointerType>()) { |
| 1946 | FromRecordType = FromType->getPointeeType(); |
| 1947 | PointerConversions = true; |
| 1948 | } else { |
| 1949 | FromRecordType = FromType; |
| 1950 | DestType = DestRecordType; |
| 1951 | } |
| 1952 | } else { |
| 1953 | // No conversion necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1954 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1955 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1956 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1957 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1958 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1959 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1960 | // If the unqualified types are the same, no conversion is necessary. |
| 1961 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1962 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1963 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1964 | SourceRange FromRange = From->getSourceRange(); |
| 1965 | SourceLocation FromLoc = FromRange.getBegin(); |
| 1966 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1967 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1968 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1969 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1970 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1971 | // class name. |
| 1972 | // |
| 1973 | // If the member was a qualified name and the qualified referred to a |
| 1974 | // specific base subobject type, we'll cast to that intermediate type |
| 1975 | // first and then to the object in which the member is declared. That allows |
| 1976 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 1977 | // |
| 1978 | // class Base { public: int x; }; |
| 1979 | // class Derived1 : public Base { }; |
| 1980 | // class Derived2 : public Base { }; |
| 1981 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 1982 | // |
| 1983 | // void VeryDerived::f() { |
| 1984 | // x = 17; // error: ambiguous base subobjects |
| 1985 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 1986 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1987 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1988 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 1989 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 1990 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 1991 | |
| 1992 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 1993 | |
| 1994 | // In C++98, the qualifier type doesn't actually have to be a base |
| 1995 | // type of the object type, in which case we just ignore it. |
| 1996 | // Otherwise build the appropriate casts. |
| 1997 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1998 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1999 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2000 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2001 | return ExprError(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2002 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2003 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2004 | QType = Context.getPointerType(QType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2005 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2006 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2007 | |
| 2008 | FromType = QType; |
| 2009 | FromRecordType = QRecordType; |
| 2010 | |
| 2011 | // If the qualifier type was the same as the destination type, |
| 2012 | // we're done. |
| 2013 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2014 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2015 | } |
| 2016 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2017 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2018 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2019 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2020 | // If we actually found the member through a using declaration, cast |
| 2021 | // down to the using declaration's type. |
| 2022 | // |
| 2023 | // Pointer equality is fine here because only one declaration of a |
| 2024 | // class ever has member declarations. |
| 2025 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2026 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2027 | QualType URecordType = Context.getTypeDeclType( |
| 2028 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2029 | |
| 2030 | // We only need to do this if the naming-class to declaring-class |
| 2031 | // conversion is non-trivial. |
| 2032 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2033 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2034 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2035 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2036 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2037 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2038 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2039 | QualType UType = URecordType; |
| 2040 | if (PointerConversions) |
| 2041 | UType = Context.getPointerType(UType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2042 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2043 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2044 | FromType = UType; |
| 2045 | FromRecordType = URecordType; |
| 2046 | } |
| 2047 | |
| 2048 | // We don't do access control for the conversion from the |
| 2049 | // declaring class to the true declaring class. |
| 2050 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2051 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2052 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2053 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2054 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2055 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2056 | IgnoreAccess)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2057 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2058 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2059 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2060 | VK, &BasePath); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2061 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2062 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2063 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2064 | const LookupResult &R, |
| 2065 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2066 | // Only when used directly as the postfix-expression of a call. |
| 2067 | if (!HasTrailingLParen) |
| 2068 | return false; |
| 2069 | |
| 2070 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2071 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2072 | return false; |
| 2073 | |
| 2074 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2075 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2076 | return false; |
| 2077 | |
| 2078 | // Turn off ADL when we find certain kinds of declarations during |
| 2079 | // normal lookup: |
| 2080 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2081 | NamedDecl *D = *I; |
| 2082 | |
| 2083 | // C++0x [basic.lookup.argdep]p3: |
| 2084 | // -- a declaration of a class member |
| 2085 | // Since using decls preserve this property, we check this on the |
| 2086 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2087 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2088 | return false; |
| 2089 | |
| 2090 | // C++0x [basic.lookup.argdep]p3: |
| 2091 | // -- a block-scope function declaration that is not a |
| 2092 | // using-declaration |
| 2093 | // NOTE: we also trigger this for function templates (in fact, we |
| 2094 | // don't check the decl type at all, since all other decl types |
| 2095 | // turn off ADL anyway). |
| 2096 | if (isa<UsingShadowDecl>(D)) |
| 2097 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2098 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2099 | return false; |
| 2100 | |
| 2101 | // C++0x [basic.lookup.argdep]p3: |
| 2102 | // -- a declaration that is neither a function or a function |
| 2103 | // template |
| 2104 | // And also for builtin functions. |
| 2105 | if (isa<FunctionDecl>(D)) { |
| 2106 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2107 | |
| 2108 | // But also builtin functions. |
| 2109 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2110 | return false; |
| 2111 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2112 | return false; |
| 2113 | } |
| 2114 | |
| 2115 | return true; |
| 2116 | } |
| 2117 | |
| 2118 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2119 | /// Diagnoses obvious problems with the use of the given declaration |
| 2120 | /// as an expression. This is only actually called for lookups that |
| 2121 | /// were not overloaded, and it doesn't promise that the declaration |
| 2122 | /// will in fact be used. |
| 2123 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2124 | if (isa<TypedefNameDecl>(D)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2125 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2126 | return true; |
| 2127 | } |
| 2128 | |
| 2129 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2130 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2131 | return true; |
| 2132 | } |
| 2133 | |
| 2134 | if (isa<NamespaceDecl>(D)) { |
| 2135 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2136 | return true; |
| 2137 | } |
| 2138 | |
| 2139 | return false; |
| 2140 | } |
| 2141 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2142 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2143 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2144 | LookupResult &R, |
| 2145 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2146 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2147 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2148 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2149 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2150 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2151 | |
| 2152 | // We only need to check the declaration if there's exactly one |
| 2153 | // result, because in the overloaded case the results can only be |
| 2154 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2155 | if (R.isSingleResult() && |
| 2156 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2157 | return ExprError(); |
| 2158 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2159 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2160 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2161 | // we've picked a target. |
| 2162 | R.suppressDiagnostics(); |
| 2163 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2164 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2165 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2166 | SS.getWithLocInContext(Context), |
| 2167 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2168 | NeedsADL, R.isOverloadedResult(), |
| 2169 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2170 | |
| 2171 | return Owned(ULE); |
| 2172 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2173 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2174 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2175 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2176 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2177 | const DeclarationNameInfo &NameInfo, |
| 2178 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2179 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2180 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2181 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2182 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2183 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2184 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2185 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2186 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2187 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2188 | // Specifically diagnose references to class templates that are missing |
| 2189 | // a template argument list. |
| 2190 | Diag(Loc, diag::err_template_decl_ref) |
| 2191 | << Template << SS.getRange(); |
| 2192 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2193 | return ExprError(); |
| 2194 | } |
| 2195 | |
| 2196 | // Make sure that we're referring to a value. |
| 2197 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2198 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2199 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2200 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2201 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2202 | return ExprError(); |
| 2203 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2204 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2205 | // Check whether this declaration can be used. Note that we suppress |
| 2206 | // this check when we're going to perform argument-dependent lookup |
| 2207 | // on this function name, because this might not be the function |
| 2208 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2209 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2210 | return ExprError(); |
| 2211 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2212 | // Only create DeclRefExpr's for valid Decl's. |
| 2213 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2214 | return ExprError(); |
| 2215 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2216 | // Handle members of anonymous structs and unions. If we got here, |
| 2217 | // and the reference is to a class member indirect field, then this |
| 2218 | // must be the subject of a pointer-to-member expression. |
| 2219 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2220 | if (!indirectField->isCXXClassMember()) |
| 2221 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2222 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2223 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2224 | // If the identifier reference is inside a block, and it refers to a value |
| 2225 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2226 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2227 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2228 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2229 | // We do not do this for things like enum constants, global variables, etc, |
| 2230 | // as they do not get snapshotted. |
| 2231 | // |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2232 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2233 | case CR_Error: |
| 2234 | return ExprError(); |
Mike Stump | 0d6fd57 | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2235 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2236 | case CR_Capture: |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2237 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2238 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2239 | |
| 2240 | case CR_CaptureByRef: |
| 2241 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2242 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2243 | |
| 2244 | case CR_NoCapture: { |
| 2245 | // If this reference is not in a block or if the referenced |
| 2246 | // variable is within the block, create a normal DeclRefExpr. |
| 2247 | |
| 2248 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2249 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2250 | |
| 2251 | switch (D->getKind()) { |
| 2252 | // Ignore all the non-ValueDecl kinds. |
| 2253 | #define ABSTRACT_DECL(kind) |
| 2254 | #define VALUE(type, base) |
| 2255 | #define DECL(type, base) \ |
| 2256 | case Decl::type: |
| 2257 | #include "clang/AST/DeclNodes.inc" |
| 2258 | llvm_unreachable("invalid value decl kind"); |
| 2259 | return ExprError(); |
| 2260 | |
| 2261 | // These shouldn't make it here. |
| 2262 | case Decl::ObjCAtDefsField: |
| 2263 | case Decl::ObjCIvar: |
| 2264 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2265 | return ExprError(); |
| 2266 | |
| 2267 | // Enum constants are always r-values and never references. |
| 2268 | // Unresolved using declarations are dependent. |
| 2269 | case Decl::EnumConstant: |
| 2270 | case Decl::UnresolvedUsingValue: |
| 2271 | valueKind = VK_RValue; |
| 2272 | break; |
| 2273 | |
| 2274 | // Fields and indirect fields that got here must be for |
| 2275 | // pointer-to-member expressions; we just call them l-values for |
| 2276 | // internal consistency, because this subexpression doesn't really |
| 2277 | // exist in the high-level semantics. |
| 2278 | case Decl::Field: |
| 2279 | case Decl::IndirectField: |
| 2280 | assert(getLangOptions().CPlusPlus && |
| 2281 | "building reference to field in C?"); |
| 2282 | |
| 2283 | // These can't have reference type in well-formed programs, but |
| 2284 | // for internal consistency we do this anyway. |
| 2285 | type = type.getNonReferenceType(); |
| 2286 | valueKind = VK_LValue; |
| 2287 | break; |
| 2288 | |
| 2289 | // Non-type template parameters are either l-values or r-values |
| 2290 | // depending on the type. |
| 2291 | case Decl::NonTypeTemplateParm: { |
| 2292 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2293 | type = reftype->getPointeeType(); |
| 2294 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2295 | break; |
| 2296 | } |
| 2297 | |
| 2298 | // For non-references, we need to strip qualifiers just in case |
| 2299 | // the template parameter was declared as 'const int' or whatever. |
| 2300 | valueKind = VK_RValue; |
| 2301 | type = type.getUnqualifiedType(); |
| 2302 | break; |
| 2303 | } |
| 2304 | |
| 2305 | case Decl::Var: |
| 2306 | // In C, "extern void blah;" is valid and is an r-value. |
| 2307 | if (!getLangOptions().CPlusPlus && |
| 2308 | !type.hasQualifiers() && |
| 2309 | type->isVoidType()) { |
| 2310 | valueKind = VK_RValue; |
| 2311 | break; |
| 2312 | } |
| 2313 | // fallthrough |
| 2314 | |
| 2315 | case Decl::ImplicitParam: |
| 2316 | case Decl::ParmVar: |
| 2317 | // These are always l-values. |
| 2318 | valueKind = VK_LValue; |
| 2319 | type = type.getNonReferenceType(); |
| 2320 | break; |
| 2321 | |
| 2322 | case Decl::Function: { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2323 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2324 | |
| 2325 | // If we're referring to a function with an __unknown_anytype |
| 2326 | // result type, make the entire expression __unknown_anytype. |
| 2327 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2328 | type = Context.UnknownAnyTy; |
| 2329 | valueKind = VK_RValue; |
| 2330 | break; |
| 2331 | } |
| 2332 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2333 | // Functions are l-values in C++. |
| 2334 | if (getLangOptions().CPlusPlus) { |
| 2335 | valueKind = VK_LValue; |
| 2336 | break; |
| 2337 | } |
| 2338 | |
| 2339 | // C99 DR 316 says that, if a function type comes from a |
| 2340 | // function definition (without a prototype), that type is only |
| 2341 | // used for checking compatibility. Therefore, when referencing |
| 2342 | // the function, we pretend that we don't have the full function |
| 2343 | // type. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2344 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2345 | isa<FunctionProtoType>(fty)) |
| 2346 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2347 | fty->getExtInfo()); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2348 | |
| 2349 | // Functions are r-values in C. |
| 2350 | valueKind = VK_RValue; |
| 2351 | break; |
| 2352 | } |
| 2353 | |
| 2354 | case Decl::CXXMethod: |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2355 | // If we're referring to a method with an __unknown_anytype |
| 2356 | // result type, make the entire expression __unknown_anytype. |
| 2357 | // This should only be possible with a type written directly. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 2358 | if (const FunctionProtoType *proto |
| 2359 | = dyn_cast<FunctionProtoType>(VD->getType())) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2360 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2361 | type = Context.UnknownAnyTy; |
| 2362 | valueKind = VK_RValue; |
| 2363 | break; |
| 2364 | } |
| 2365 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2366 | // C++ methods are l-values if static, r-values if non-static. |
| 2367 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2368 | valueKind = VK_LValue; |
| 2369 | break; |
| 2370 | } |
| 2371 | // fallthrough |
| 2372 | |
| 2373 | case Decl::CXXConversion: |
| 2374 | case Decl::CXXDestructor: |
| 2375 | case Decl::CXXConstructor: |
| 2376 | valueKind = VK_RValue; |
| 2377 | break; |
| 2378 | } |
| 2379 | |
| 2380 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2381 | } |
| 2382 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2383 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2384 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2385 | llvm_unreachable("unknown capture result"); |
| 2386 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2389 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2390 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2391 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2392 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2393 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2394 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2395 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2396 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2397 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2398 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2399 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2400 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2401 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2402 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2403 | if (!currentDecl && getCurBlock()) |
| 2404 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2405 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2406 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2407 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2408 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2409 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2410 | QualType ResTy; |
| 2411 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2412 | ResTy = Context.DependentTy; |
| 2413 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2414 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2415 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2416 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2417 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2418 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2419 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2420 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2423 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2424 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2425 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2426 | StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2427 | if (Invalid) |
| 2428 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2429 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2430 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2431 | PP, Tok.getKind()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2432 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2433 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2434 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2435 | QualType Ty; |
| 2436 | if (!getLangOptions().CPlusPlus) |
| 2437 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2438 | else if (Literal.isWide()) |
| 2439 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2440 | else if (Literal.isUTF16()) |
| 2441 | Ty = Context.Char16Ty; // u'x' -> char16_t in C++0x. |
| 2442 | else if (Literal.isUTF32()) |
| 2443 | Ty = Context.Char32Ty; // U'x' -> char32_t in C++0x. |
Eli Friedman | 136b0cd | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2444 | else if (Literal.isMultiChar()) |
| 2445 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2446 | else |
| 2447 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2448 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2449 | CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; |
| 2450 | if (Literal.isWide()) |
| 2451 | Kind = CharacterLiteral::Wide; |
| 2452 | else if (Literal.isUTF16()) |
| 2453 | Kind = CharacterLiteral::UTF16; |
| 2454 | else if (Literal.isUTF32()) |
| 2455 | Kind = CharacterLiteral::UTF32; |
| 2456 | |
| 2457 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, |
| 2458 | Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2461 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2462 | // Fast path for a single digit (which is quite common). A single digit |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2463 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2464 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2465 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2466 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2467 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2468 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2469 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2470 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2471 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2472 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2473 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2474 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2475 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2476 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2477 | bool Invalid = false; |
| 2478 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2479 | if (Invalid) |
| 2480 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2481 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2482 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2483 | Tok.getLocation(), PP); |
| 2484 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2485 | return ExprError(); |
| 2486 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2487 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2488 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2489 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2490 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2491 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2492 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2493 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2494 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2495 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2496 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2497 | |
| 2498 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2499 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2500 | using llvm::APFloat; |
| 2501 | APFloat Val(Format); |
| 2502 | |
| 2503 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2504 | |
| 2505 | // Overflow is always an error, but underflow is only an error if |
| 2506 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2507 | if ((result & APFloat::opOverflow) || |
| 2508 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2509 | unsigned diagnostic; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2510 | llvm::SmallString<20> buffer; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2511 | if (result & APFloat::opOverflow) { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2512 | diagnostic = diag::warn_float_overflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2513 | APFloat::getLargest(Format).toString(buffer); |
| 2514 | } else { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2515 | diagnostic = diag::warn_float_underflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2516 | APFloat::getSmallest(Format).toString(buffer); |
| 2517 | } |
| 2518 | |
| 2519 | Diag(Tok.getLocation(), diagnostic) |
| 2520 | << Ty |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2521 | << StringRef(buffer.data(), buffer.size()); |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2525 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2526 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2527 | if (Ty == Context.DoubleTy) { |
| 2528 | if (getLangOptions().SinglePrecisionConstants) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2529 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2530 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2531 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2532 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2533 | } |
| 2534 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2535 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2536 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2537 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2538 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2539 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2540 | // long long is a C99 feature. |
| 2541 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2542 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2543 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2544 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2545 | // Get the value in the widest-possible width. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2546 | llvm::APInt ResultVal(Context.getTargetInfo().getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2547 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2548 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2549 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2550 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2551 | Ty = Context.UnsignedLongLongTy; |
| 2552 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2553 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2554 | } else { |
| 2555 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2556 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2557 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2558 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2559 | // be an unsigned int. |
| 2560 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2561 | |
| 2562 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2563 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2564 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2565 | // Are int/unsigned possibilities? |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2566 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2567 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2568 | // Does it fit in a unsigned int? |
| 2569 | if (ResultVal.isIntN(IntSize)) { |
| 2570 | // Does it fit in a signed int? |
| 2571 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2572 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2573 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2574 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2575 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2576 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2577 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2578 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2579 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2580 | if (Ty.isNull() && !Literal.isLongLong) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2581 | unsigned LongSize = Context.getTargetInfo().getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2582 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2583 | // Does it fit in a unsigned long? |
| 2584 | if (ResultVal.isIntN(LongSize)) { |
| 2585 | // Does it fit in a signed long? |
| 2586 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2587 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2588 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2589 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2590 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2591 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2594 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2595 | if (Ty.isNull()) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2596 | unsigned LongLongSize = Context.getTargetInfo().getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2597 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2598 | // Does it fit in a unsigned long long? |
| 2599 | if (ResultVal.isIntN(LongLongSize)) { |
| 2600 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2601 | // To be compatible with MSVC, hex integer literals ending with the |
| 2602 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2603 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2604 | (getLangOptions().MicrosoftExt && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2605 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2606 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2607 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2608 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2609 | } |
| 2610 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2611 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2612 | // If we still couldn't decide a type, we probably have something that |
| 2613 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2614 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2615 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2616 | Ty = Context.UnsignedLongLongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2617 | Width = Context.getTargetInfo().getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2618 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2619 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2620 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2621 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2622 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2623 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2624 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2625 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2626 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2627 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2628 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2629 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2630 | |
| 2631 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2634 | ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2635 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2636 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2639 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 2640 | SourceLocation Loc, |
| 2641 | SourceRange ArgRange) { |
| 2642 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 2643 | // scalar or vector data type argument..." |
| 2644 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 2645 | // type (C99 6.2.5p18) or void. |
| 2646 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 2647 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 2648 | << T << ArgRange; |
| 2649 | return true; |
| 2650 | } |
| 2651 | |
| 2652 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 2653 | "Scalar types should always be complete"); |
| 2654 | return false; |
| 2655 | } |
| 2656 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2657 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 2658 | SourceLocation Loc, |
| 2659 | SourceRange ArgRange, |
| 2660 | UnaryExprOrTypeTrait TraitKind) { |
| 2661 | // C99 6.5.3.4p1: |
| 2662 | if (T->isFunctionType()) { |
| 2663 | // alignof(function) is allowed as an extension. |
| 2664 | if (TraitKind == UETT_SizeOf) |
| 2665 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 2666 | return false; |
| 2667 | } |
| 2668 | |
| 2669 | // Allow sizeof(void)/alignof(void) as an extension. |
| 2670 | if (T->isVoidType()) { |
| 2671 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 2672 | return false; |
| 2673 | } |
| 2674 | |
| 2675 | return true; |
| 2676 | } |
| 2677 | |
| 2678 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 2679 | SourceLocation Loc, |
| 2680 | SourceRange ArgRange, |
| 2681 | UnaryExprOrTypeTrait TraitKind) { |
| 2682 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
| 2683 | if (S.LangOpts.ObjCNonFragileABI && T->isObjCObjectType()) { |
| 2684 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 2685 | << T << (TraitKind == UETT_SizeOf) |
| 2686 | << ArgRange; |
| 2687 | return true; |
| 2688 | } |
| 2689 | |
| 2690 | return false; |
| 2691 | } |
| 2692 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2693 | /// \brief Check the constrains on expression operands to unary type expression |
| 2694 | /// and type traits. |
| 2695 | /// |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2696 | /// Completes any types necessary and validates the constraints on the operand |
| 2697 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 2698 | /// the expression as it completes the type for that expression through template |
| 2699 | /// instantiation, etc. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2700 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2701 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2702 | QualType ExprTy = E->getType(); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2703 | |
| 2704 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2705 | // the result is the size of the referenced type." |
| 2706 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2707 | // result shall be the alignment of the referenced type." |
| 2708 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2709 | ExprTy = Ref->getPointeeType(); |
| 2710 | |
| 2711 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2712 | return CheckVecStepTraitOperandType(*this, ExprTy, E->getExprLoc(), |
| 2713 | E->getSourceRange()); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2714 | |
| 2715 | // Whitelist some types as extensions |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2716 | if (!CheckExtensionTraitOperandType(*this, ExprTy, E->getExprLoc(), |
| 2717 | E->getSourceRange(), ExprKind)) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2718 | return false; |
| 2719 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2720 | if (RequireCompleteExprType(E, |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2721 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2722 | << ExprKind << E->getSourceRange(), |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2723 | std::make_pair(SourceLocation(), PDiag(0)))) |
| 2724 | return true; |
| 2725 | |
| 2726 | // Completeing the expression's type may have changed it. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2727 | ExprTy = E->getType(); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2728 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2729 | ExprTy = Ref->getPointeeType(); |
| 2730 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2731 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, E->getExprLoc(), |
| 2732 | E->getSourceRange(), ExprKind)) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2733 | return true; |
| 2734 | |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2735 | if (ExprKind == UETT_SizeOf) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2736 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2737 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 2738 | QualType OType = PVD->getOriginalType(); |
| 2739 | QualType Type = PVD->getType(); |
| 2740 | if (Type->isPointerType() && OType->isArrayType()) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2741 | Diag(E->getExprLoc(), diag::warn_sizeof_array_param) |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2742 | << Type << OType; |
| 2743 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 2744 | } |
| 2745 | } |
| 2746 | } |
| 2747 | } |
| 2748 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2749 | return false; |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
| 2752 | /// \brief Check the constraints on operands to unary expression and type |
| 2753 | /// traits. |
| 2754 | /// |
| 2755 | /// This will complete any types necessary, and validate the various constraints |
| 2756 | /// on those operands. |
| 2757 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2758 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2759 | /// C99 6.3.2.1p[2-4] all state: |
| 2760 | /// Except when it is the operand of the sizeof operator ... |
| 2761 | /// |
| 2762 | /// C++ [expr.sizeof]p4 |
| 2763 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 2764 | /// standard conversions are not applied to the operand of sizeof. |
| 2765 | /// |
| 2766 | /// This policy is followed for all of the unary trait expressions. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2767 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2768 | SourceLocation OpLoc, |
| 2769 | SourceRange ExprRange, |
| 2770 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2771 | if (ExprType->isDependentType()) |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2772 | return false; |
| 2773 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2774 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2775 | // the result is the size of the referenced type." |
| 2776 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2777 | // result shall be the alignment of the referenced type." |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2778 | if (const ReferenceType *Ref = ExprType->getAs<ReferenceType>()) |
| 2779 | ExprType = Ref->getPointeeType(); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2780 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2781 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2782 | return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2783 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2784 | // Whitelist some types as extensions |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2785 | if (!CheckExtensionTraitOperandType(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2786 | ExprKind)) |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2787 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2788 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2789 | if (RequireCompleteType(OpLoc, ExprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 2790 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2791 | << ExprKind << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2792 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2793 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2794 | if (CheckObjCTraitOperandConstraints(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2795 | ExprKind)) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2796 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2797 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2798 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2801 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2802 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2803 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2805 | if (isa<DeclRefExpr>(E)) |
| 2806 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2807 | |
| 2808 | // Cannot know anything else if the expression is dependent. |
| 2809 | if (E->isTypeDependent()) |
| 2810 | return false; |
| 2811 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2812 | if (E->getBitField()) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2813 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 2814 | << 1 << E->getSourceRange(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2815 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2816 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2817 | |
| 2818 | // Alignment of a field access is always okay, so long as it isn't a |
| 2819 | // bit-field. |
| 2820 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2821 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2822 | return false; |
| 2823 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2824 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2827 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2828 | E = E->IgnoreParens(); |
| 2829 | |
| 2830 | // Cannot know anything else if the expression is dependent. |
| 2831 | if (E->isTypeDependent()) |
| 2832 | return false; |
| 2833 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2834 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2837 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2838 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2839 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 2840 | SourceLocation OpLoc, |
| 2841 | UnaryExprOrTypeTrait ExprKind, |
| 2842 | SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2843 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2844 | return ExprError(); |
| 2845 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2846 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2847 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2848 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2849 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2850 | return ExprError(); |
| 2851 | |
| 2852 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2853 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 2854 | Context.getSizeType(), |
| 2855 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | /// \brief Build a sizeof or alignof expression given an expression |
| 2859 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2860 | ExprResult |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2861 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 2862 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | 4f0845e | 2011-06-22 23:21:00 +0000 | [diff] [blame] | 2863 | ExprResult PE = CheckPlaceholderExpr(E); |
| 2864 | if (PE.isInvalid()) |
| 2865 | return ExprError(); |
| 2866 | |
| 2867 | E = PE.get(); |
| 2868 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2869 | // Verify that the operand is valid. |
| 2870 | bool isInvalid = false; |
| 2871 | if (E->isTypeDependent()) { |
| 2872 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2873 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2874 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2875 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2876 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2877 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2878 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2879 | isInvalid = true; |
| 2880 | } else { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2881 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | if (isInvalid) |
| 2885 | return ExprError(); |
| 2886 | |
| 2887 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2888 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2889 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2890 | E->getSourceRange().getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2893 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 2894 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2895 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2896 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2897 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2898 | UnaryExprOrTypeTrait ExprKind, bool IsType, |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2899 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2900 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2901 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2902 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2903 | if (IsType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2904 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2905 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2906 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2908 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2909 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2910 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2911 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2914 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2915 | bool IsReal) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2916 | if (V.get()->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2917 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2918 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2919 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2920 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 2921 | V = S.DefaultLvalueConversion(V.take()); |
| 2922 | if (V.isInvalid()) |
| 2923 | return QualType(); |
| 2924 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2925 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2926 | // These operators return the element type of a complex type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2927 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2928 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2929 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2930 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2931 | if (V.get()->getType()->isArithmeticType()) |
| 2932 | return V.get()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2933 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2934 | // Test for placeholders. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 2935 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2936 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2937 | if (PR.get() != V.get()) { |
| 2938 | V = move(PR); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2939 | return CheckRealImagOperand(S, V, Loc, IsReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2942 | // Reject anything else. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2943 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2944 | << (IsReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2945 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2949 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2950 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2951 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2952 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2953 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2954 | switch (Kind) { |
| 2955 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2956 | case tok::plusplus: Opc = UO_PostInc; break; |
| 2957 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2958 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2959 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2960 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2963 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2964 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 2965 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2966 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2967 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2968 | if (Result.isInvalid()) return ExprError(); |
| 2969 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2970 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2971 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2972 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2973 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2974 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2975 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2976 | Context.DependentTy, |
| 2977 | VK_LValue, OK_Ordinary, |
| 2978 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2981 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2982 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 2983 | LHSExp->getType()->isEnumeralType() || |
| 2984 | RHSExp->getType()->isRecordType() || |
| 2985 | RHSExp->getType()->isEnumeralType())) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2986 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2989 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2993 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2994 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2995 | Expr *Idx, SourceLocation RLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2996 | Expr *LHSExp = Base; |
| 2997 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2998 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2999 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3000 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3001 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3002 | if (Result.isInvalid()) |
| 3003 | return ExprError(); |
| 3004 | LHSExp = Result.take(); |
| 3005 | } |
| 3006 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3007 | if (Result.isInvalid()) |
| 3008 | return ExprError(); |
| 3009 | RHSExp = Result.take(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3010 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3011 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3012 | ExprValueKind VK = VK_LValue; |
| 3013 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3014 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3015 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3016 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3017 | // in the subscript position. As a result, we need to derive the array base |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3018 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3019 | Expr *BaseExpr, *IndexExpr; |
| 3020 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3021 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3022 | BaseExpr = LHSExp; |
| 3023 | IndexExpr = RHSExp; |
| 3024 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3025 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3026 | BaseExpr = LHSExp; |
| 3027 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3028 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3029 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3030 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3031 | BaseExpr = RHSExp; |
| 3032 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3033 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3034 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3035 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3036 | BaseExpr = LHSExp; |
| 3037 | IndexExpr = RHSExp; |
| 3038 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3039 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3040 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3041 | // Handle the uncommon case of "123[Ptr]". |
| 3042 | BaseExpr = RHSExp; |
| 3043 | IndexExpr = LHSExp; |
| 3044 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3045 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3046 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3047 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3048 | VK = LHSExp->getValueKind(); |
| 3049 | if (VK != VK_RValue) |
| 3050 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3051 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3052 | // FIXME: need to deal with const... |
| 3053 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3054 | } else if (LHSTy->isArrayType()) { |
| 3055 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3056 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3057 | // wasn't promoted because of the C90 rule that doesn't |
| 3058 | // allow promoting non-lvalue arrays. Warn, then |
| 3059 | // force the promotion here. |
| 3060 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3061 | LHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3062 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3063 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3064 | LHSTy = LHSExp->getType(); |
| 3065 | |
| 3066 | BaseExpr = LHSExp; |
| 3067 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3068 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3069 | } else if (RHSTy->isArrayType()) { |
| 3070 | // Same as previous, except for 123[f().a] case |
| 3071 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3072 | RHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3073 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3074 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3075 | RHSTy = RHSExp->getType(); |
| 3076 | |
| 3077 | BaseExpr = RHSExp; |
| 3078 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3079 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3080 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3081 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3082 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3083 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3084 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3085 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3086 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3087 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3088 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3089 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3090 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3091 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3092 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3093 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3094 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3095 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3096 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3097 | // incomplete types are not object types. |
| 3098 | if (ResultType->isFunctionType()) { |
| 3099 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3100 | << ResultType << BaseExpr->getSourceRange(); |
| 3101 | return ExprError(); |
| 3102 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3104 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3105 | // GNU extension: subscripting on pointer to void |
Chandler Carruth | 6628969 | 2011-06-27 16:32:27 +0000 | [diff] [blame] | 3106 | Diag(LLoc, diag::ext_gnu_subscript_void_type) |
| 3107 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3108 | |
| 3109 | // C forbids expressions of unqualified void type from being l-values. |
| 3110 | // See IsCForbiddenLValueType. |
| 3111 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3112 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3113 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3114 | PDiag(diag::err_subscript_incomplete_type) |
| 3115 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3116 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3118 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3119 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3120 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3121 | << ResultType << BaseExpr->getSourceRange(); |
| 3122 | return ExprError(); |
| 3123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3124 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3125 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 3126 | !ResultType.isCForbiddenLValueType()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3127 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3128 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3129 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3132 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3133 | FunctionDecl *FD, |
| 3134 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3135 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3136 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3137 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3138 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3139 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3140 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3141 | return ExprError(); |
| 3142 | } |
| 3143 | |
| 3144 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3145 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3146 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3147 | // Instantiate the expression. |
| 3148 | MultiLevelTemplateArgumentList ArgList |
| 3149 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3150 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3151 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3152 | = ArgList.getInnermost(); |
| 3153 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 3154 | Innermost.second); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3155 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3156 | ExprResult Result; |
| 3157 | { |
| 3158 | // C++ [dcl.fct.default]p5: |
| 3159 | // The names in the [default argument] expression are bound, and |
| 3160 | // the semantic constraints are checked, at the point where the |
| 3161 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3162 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3163 | Result = SubstExpr(UninstExpr, ArgList); |
| 3164 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3165 | if (Result.isInvalid()) |
| 3166 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3167 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3168 | // Check the expression as an initializer for the parameter. |
| 3169 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3170 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3171 | InitializationKind Kind |
| 3172 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 3173 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 3174 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3175 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3176 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 3177 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 3178 | MultiExprArg(*this, &ResultE, 1)); |
| 3179 | if (Result.isInvalid()) |
| 3180 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3181 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3182 | // Build the default argument expression. |
| 3183 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 3184 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3187 | // If the default expression creates temporaries, we need to |
| 3188 | // push them to the current stack of expression temporaries so they'll |
| 3189 | // be properly destroyed. |
| 3190 | // FIXME: We should really be rebuilding the default argument with new |
| 3191 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3192 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 3193 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 3194 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 3195 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 3196 | ExprTemporaries.push_back(Temporary); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3197 | ExprNeedsCleanups = true; |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3198 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3199 | |
| 3200 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 3201 | // Just mark all of the declarations in this potentially-evaluated expression |
| 3202 | // as being "referenced". |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3203 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3204 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3207 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3208 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3209 | /// function prototype Proto. Call is the call expression itself, and |
| 3210 | /// Fn is the function expression. For a C++ member function, this |
| 3211 | /// routine does not attempt to convert the object argument. Returns |
| 3212 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3213 | bool |
| 3214 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3215 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3216 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3217 | Expr **Args, unsigned NumArgs, |
| 3218 | SourceLocation RParenLoc) { |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3219 | // Bail out early if calling a builtin with custom typechecking. |
| 3220 | // We don't need to do this in the |
| 3221 | if (FDecl) |
| 3222 | if (unsigned ID = FDecl->getBuiltinID()) |
| 3223 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 3224 | return false; |
| 3225 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3226 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3227 | // assignment, to the types of the corresponding parameter, ... |
| 3228 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3229 | bool Invalid = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3230 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3231 | // If too few arguments are available (and we don't have default |
| 3232 | // arguments for the remaining parameters), don't make the call. |
| 3233 | if (NumArgs < NumArgsInProto) { |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3234 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) { |
| 3235 | Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3236 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 3237 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3238 | |
| 3239 | // Emit the location of the prototype. |
| 3240 | if (FDecl && !FDecl->getBuiltinID()) |
| 3241 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3242 | << FDecl; |
| 3243 | |
| 3244 | return true; |
| 3245 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3246 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | // If too many are passed and not variadic, error on the extras and drop |
| 3250 | // them. |
| 3251 | if (NumArgs > NumArgsInProto) { |
| 3252 | if (!Proto->isVariadic()) { |
| 3253 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3254 | diag::err_typecheck_call_too_many_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3255 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 3256 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3257 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3258 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3259 | |
| 3260 | // Emit the location of the prototype. |
| 3261 | if (FDecl && !FDecl->getBuiltinID()) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3262 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3263 | << FDecl; |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3264 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3265 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3266 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3267 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3268 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3269 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3270 | SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3271 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3272 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3273 | if (Fn->getType()->isBlockPointerType()) |
| 3274 | CallType = VariadicBlock; // Block |
| 3275 | else if (isa<MemberExpr>(Fn)) |
| 3276 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3277 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3278 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3279 | if (Invalid) |
| 3280 | return true; |
| 3281 | unsigned TotalNumArgs = AllArgs.size(); |
| 3282 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3283 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3284 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3285 | return false; |
| 3286 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3287 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3288 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3289 | FunctionDecl *FDecl, |
| 3290 | const FunctionProtoType *Proto, |
| 3291 | unsigned FirstProtoArg, |
| 3292 | Expr **Args, unsigned NumArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3293 | SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3294 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3295 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3296 | unsigned NumArgsToCheck = NumArgs; |
| 3297 | bool Invalid = false; |
| 3298 | if (NumArgs != NumArgsInProto) |
| 3299 | // Use default arguments for missing arguments |
| 3300 | NumArgsToCheck = NumArgsInProto; |
| 3301 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3302 | // Continue to check argument types (even if we have too few/many args). |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3303 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3304 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3305 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3306 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3307 | if (ArgIx < NumArgs) { |
| 3308 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3309 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3310 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3311 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3312 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3313 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3314 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3315 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3316 | // Pass the argument |
| 3317 | ParmVarDecl *Param = 0; |
| 3318 | if (FDecl && i < FDecl->getNumParams()) |
| 3319 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3320 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3321 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3322 | Param? InitializedEntity::InitializeParameter(Context, Param) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3323 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
| 3324 | Proto->isArgConsumed(i)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3325 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3326 | SourceLocation(), |
| 3327 | Owned(Arg)); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3328 | if (ArgE.isInvalid()) |
| 3329 | return true; |
| 3330 | |
| 3331 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3332 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 3333 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3334 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3335 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3336 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3337 | if (ArgExpr.isInvalid()) |
| 3338 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3339 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3340 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3341 | } |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3342 | |
| 3343 | // Check for array bounds violations for each argument to the call. This |
| 3344 | // check only triggers warnings when the argument isn't a more complex Expr |
| 3345 | // with its own checking, such as a BinaryOperator. |
| 3346 | CheckArrayAccess(Arg); |
| 3347 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3348 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3349 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3350 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3351 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3352 | if (CallType != VariadicDoesNotApply) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3353 | |
| 3354 | // Assume that extern "C" functions with variadic arguments that |
| 3355 | // return __unknown_anytype aren't *really* variadic. |
| 3356 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 3357 | FDecl && FDecl->isExternC()) { |
| 3358 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 3359 | ExprResult arg; |
| 3360 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 3361 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 3362 | else |
| 3363 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 3364 | Invalid |= arg.isInvalid(); |
| 3365 | AllArgs.push_back(arg.take()); |
| 3366 | } |
| 3367 | |
| 3368 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 3369 | } else { |
| 3370 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3371 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, |
| 3372 | FDecl); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3373 | Invalid |= Arg.isInvalid(); |
| 3374 | AllArgs.push_back(Arg.take()); |
| 3375 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3376 | } |
| 3377 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3378 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3381 | /// Given a function expression of unknown-any type, try to rebuild it |
| 3382 | /// to have a function type. |
| 3383 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 3384 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3385 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3386 | /// This provides the location of the left/right parens and a list of comma |
| 3387 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3388 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3389 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3390 | MultiExprArg ArgExprs, SourceLocation RParenLoc, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3391 | Expr *ExecConfig) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3392 | unsigned NumArgs = ArgExprs.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3393 | |
| 3394 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3395 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3396 | if (Result.isInvalid()) return ExprError(); |
| 3397 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3398 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3399 | Expr **Args = ArgExprs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3401 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3402 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3403 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3404 | if (NumArgs > 0) { |
| 3405 | // Pseudo-destructor calls should not have any arguments. |
| 3406 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3407 | << FixItHint::CreateRemoval( |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3408 | SourceRange(Args[0]->getLocStart(), |
| 3409 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3410 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3411 | NumArgs = 0; |
| 3412 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3414 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3415 | VK_RValue, RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3416 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3417 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3418 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3419 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3420 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3421 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3422 | bool Dependent = false; |
| 3423 | if (Fn->isTypeDependent()) |
| 3424 | Dependent = true; |
| 3425 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 3426 | Dependent = true; |
| 3427 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3428 | if (Dependent) { |
| 3429 | if (ExecConfig) { |
| 3430 | return Owned(new (Context) CUDAKernelCallExpr( |
| 3431 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 3432 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 3433 | } else { |
| 3434 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 3435 | Context.DependentTy, VK_RValue, |
| 3436 | RParenLoc)); |
| 3437 | } |
| 3438 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3439 | |
| 3440 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3441 | if (Fn->getType()->isRecordType()) |
| 3442 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3443 | RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3444 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3445 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3446 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3447 | if (result.isInvalid()) return ExprError(); |
| 3448 | Fn = result.take(); |
| 3449 | } |
| 3450 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3451 | if (Fn->getType() == Context.BoundMemberTy) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3452 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3453 | RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3454 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3455 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3456 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3457 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 3458 | if (Fn->getType() == Context.OverloadTy) { |
| 3459 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 3460 | |
| 3461 | // We aren't supposed to apply this logic if there's an '&' involved. |
| 3462 | if (!find.IsAddressOfOperand) { |
| 3463 | OverloadExpr *ovl = find.Expression; |
| 3464 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 3465 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
| 3466 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 3467 | RParenLoc, ExecConfig); |
| 3468 | } else { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3469 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3470 | RParenLoc); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3471 | } |
| 3472 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3475 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3476 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3477 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 3478 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3479 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 3480 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 3481 | if (UnOp->getOpcode() == UO_AddrOf) |
| 3482 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 3483 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3484 | if (isa<DeclRefExpr>(NakedFn)) |
| 3485 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3486 | else if (isa<MemberExpr>(NakedFn)) |
| 3487 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3488 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3489 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 3490 | ExecConfig); |
| 3491 | } |
| 3492 | |
| 3493 | ExprResult |
| 3494 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3495 | MultiExprArg ExecConfig, SourceLocation GGGLoc) { |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3496 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 3497 | if (!ConfigDecl) |
| 3498 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 3499 | << "cudaConfigureCall"); |
| 3500 | QualType ConfigQTy = ConfigDecl->getType(); |
| 3501 | |
| 3502 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 3503 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 3504 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3505 | return ActOnCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3508 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 3509 | /// |
| 3510 | /// __builtin_astype( value, dst type ) |
| 3511 | /// |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3512 | ExprResult Sema::ActOnAsTypeExpr(Expr *E, ParsedType ParsedDestTy, |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3513 | SourceLocation BuiltinLoc, |
| 3514 | SourceLocation RParenLoc) { |
| 3515 | ExprValueKind VK = VK_RValue; |
| 3516 | ExprObjectKind OK = OK_Ordinary; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3517 | QualType DstTy = GetTypeFromParser(ParsedDestTy); |
| 3518 | QualType SrcTy = E->getType(); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3519 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 3520 | return ExprError(Diag(BuiltinLoc, |
| 3521 | diag::err_invalid_astype_of_different_size) |
Peter Collingbourne | af9cddf | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 3522 | << DstTy |
| 3523 | << SrcTy |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3524 | << E->getSourceRange()); |
| 3525 | return Owned(new (Context) AsTypeExpr(E, DstTy, VK, OK, BuiltinLoc, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3526 | RParenLoc)); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3529 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3530 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3531 | /// unary-convert to an expression of function-pointer or |
| 3532 | /// block-pointer type. |
| 3533 | /// |
| 3534 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3535 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3536 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3537 | SourceLocation LParenLoc, |
| 3538 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3539 | SourceLocation RParenLoc, |
| 3540 | Expr *Config) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3541 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3542 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3543 | // Promote the function operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3544 | ExprResult Result = UsualUnaryConversions(Fn); |
| 3545 | if (Result.isInvalid()) |
| 3546 | return ExprError(); |
| 3547 | Fn = Result.take(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3548 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3549 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3550 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3551 | CallExpr *TheCall; |
| 3552 | if (Config) { |
| 3553 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 3554 | cast<CallExpr>(Config), |
| 3555 | Args, NumArgs, |
| 3556 | Context.BoolTy, |
| 3557 | VK_RValue, |
| 3558 | RParenLoc); |
| 3559 | } else { |
| 3560 | TheCall = new (Context) CallExpr(Context, Fn, |
| 3561 | Args, NumArgs, |
| 3562 | Context.BoolTy, |
| 3563 | VK_RValue, |
| 3564 | RParenLoc); |
| 3565 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3566 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3567 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 3568 | |
| 3569 | // Bail out early if calling a builtin with custom typechecking. |
| 3570 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 3571 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 3572 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3573 | retry: |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3574 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3575 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3576 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3577 | // have type pointer to function". |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3578 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3579 | if (FuncT == 0) |
| 3580 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3581 | << Fn->getType() << Fn->getSourceRange()); |
| 3582 | } else if (const BlockPointerType *BPT = |
| 3583 | Fn->getType()->getAs<BlockPointerType>()) { |
| 3584 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 3585 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3586 | // Handle calls to expressions of unknown-any type. |
| 3587 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3588 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3589 | if (rewrite.isInvalid()) return ExprError(); |
| 3590 | Fn = rewrite.take(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3591 | TheCall->setCallee(Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3592 | goto retry; |
| 3593 | } |
| 3594 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3595 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3596 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3597 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3598 | |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 3599 | if (getLangOptions().CUDA) { |
| 3600 | if (Config) { |
| 3601 | // CUDA: Kernel calls must be to global functions |
| 3602 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 3603 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 3604 | << FDecl->getName() << Fn->getSourceRange()); |
| 3605 | |
| 3606 | // CUDA: Kernel function must have 'void' return type |
| 3607 | if (!FuncT->getResultType()->isVoidType()) |
| 3608 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 3609 | << Fn->getType() << Fn->getSourceRange()); |
| 3610 | } |
| 3611 | } |
| 3612 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3613 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3614 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3615 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3616 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3617 | return ExprError(); |
| 3618 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3619 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3620 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3621 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3622 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3623 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3624 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3625 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3626 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3627 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3628 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3629 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3630 | if (FDecl) { |
| 3631 | // Check if we have too few/too many template arguments, based |
| 3632 | // on our knowledge of the function definition. |
| 3633 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3634 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3635 | const FunctionProtoType *Proto |
| 3636 | = Def->getType()->getAs<FunctionProtoType>(); |
| 3637 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3638 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3639 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3640 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3641 | |
| 3642 | // If the function we're calling isn't a function prototype, but we have |
| 3643 | // a function prototype from a prior declaratiom, use that prototype. |
| 3644 | if (!FDecl->hasPrototype()) |
| 3645 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3648 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3649 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3650 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3651 | |
| 3652 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3653 | InitializedEntity Entity |
| 3654 | = InitializedEntity::InitializeParameter(Context, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3655 | Proto->getArgType(i), |
| 3656 | Proto->isArgConsumed(i)); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3657 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 3658 | SourceLocation(), |
| 3659 | Owned(Arg)); |
| 3660 | if (ArgE.isInvalid()) |
| 3661 | return true; |
| 3662 | |
| 3663 | Arg = ArgE.takeAs<Expr>(); |
| 3664 | |
| 3665 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3666 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 3667 | |
| 3668 | if (ArgE.isInvalid()) |
| 3669 | return true; |
| 3670 | |
| 3671 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 3674 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3675 | Arg->getType(), |
| 3676 | PDiag(diag::err_call_incomplete_argument) |
| 3677 | << Arg->getSourceRange())) |
| 3678 | return ExprError(); |
| 3679 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3680 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3681 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3682 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3683 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3684 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3685 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3686 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3687 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3688 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3689 | // Check for sentinels |
| 3690 | if (NDecl) |
| 3691 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3692 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3693 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3694 | if (FDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3695 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3696 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3697 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3698 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 3699 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3700 | } else if (NDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3701 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3702 | return ExprError(); |
| 3703 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3704 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3705 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3708 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3709 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3710 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3711 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3712 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3713 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3714 | |
| 3715 | TypeSourceInfo *TInfo; |
| 3716 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 3717 | if (!TInfo) |
| 3718 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 3719 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3720 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3723 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3724 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3725 | SourceLocation RParenLoc, Expr *LiteralExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3726 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3727 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3728 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 3729 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 3730 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 3731 | << SourceRange(LParenLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3732 | LiteralExpr->getSourceRange().getEnd()))) |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 3733 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3734 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3735 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3736 | << SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3737 | } else if (!literalType->isDependentType() && |
| 3738 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3739 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3740 | << SourceRange(LParenLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3741 | LiteralExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3742 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3743 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3744 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3745 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3746 | InitializationKind Kind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3747 | = InitializationKind::CreateCStyleCast(LParenLoc, |
| 3748 | SourceRange(LParenLoc, RParenLoc)); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3749 | InitializationSequence InitSeq(*this, Entity, Kind, &LiteralExpr, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3750 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3751 | MultiExprArg(*this, &LiteralExpr, 1), |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3752 | &literalType); |
| 3753 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3754 | return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3755 | LiteralExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3756 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3757 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3758 | if (isFileScope) { // 6.5.2.5p3 |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3759 | if (CheckForConstantInitializer(LiteralExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3760 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3761 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3762 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3763 | // In C, compound literals are l-values for some reason. |
| 3764 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 3765 | |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 3766 | return MaybeBindToTemporary( |
| 3767 | new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3768 | VK, LiteralExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3771 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3772 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3773 | SourceLocation RBraceLoc) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3774 | unsigned NumInit = InitArgList.size(); |
| 3775 | Expr **InitList = InitArgList.release(); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3776 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3777 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3778 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3779 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 3780 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 3781 | NumInit, RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3782 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3783 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3786 | /// Do an explicit extend of the given block pointer if we're in ARC. |
| 3787 | static void maybeExtendBlockObject(Sema &S, ExprResult &E) { |
| 3788 | assert(E.get()->getType()->isBlockPointerType()); |
| 3789 | assert(E.get()->isRValue()); |
| 3790 | |
| 3791 | // Only do this in an r-value context. |
| 3792 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 3793 | |
| 3794 | E = ImplicitCastExpr::Create(S.Context, E.get()->getType(), |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3795 | CK_ARCExtendBlockObject, E.get(), |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3796 | /*base path*/ 0, VK_RValue); |
| 3797 | S.ExprNeedsCleanups = true; |
| 3798 | } |
| 3799 | |
| 3800 | /// Prepare a conversion of the given expression to an ObjC object |
| 3801 | /// pointer type. |
| 3802 | CastKind Sema::PrepareCastToObjCObjectPointer(ExprResult &E) { |
| 3803 | QualType type = E.get()->getType(); |
| 3804 | if (type->isObjCObjectPointerType()) { |
| 3805 | return CK_BitCast; |
| 3806 | } else if (type->isBlockPointerType()) { |
| 3807 | maybeExtendBlockObject(*this, E); |
| 3808 | return CK_BlockPointerToObjCPointerCast; |
| 3809 | } else { |
| 3810 | assert(type->isPointerType()); |
| 3811 | return CK_CPointerToObjCPointerCast; |
| 3812 | } |
| 3813 | } |
| 3814 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3815 | /// Prepares for a scalar cast, performing all the necessary stages |
| 3816 | /// except the final cast and returning the kind required. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3817 | static CastKind PrepareScalarCast(Sema &S, ExprResult &Src, QualType DestTy) { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3818 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 3819 | // Also, callers should have filtered out the invalid cases with |
| 3820 | // pointers. Everything else should be possible. |
| 3821 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3822 | QualType SrcTy = Src.get()->getType(); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3823 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3824 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3825 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3826 | switch (Type::ScalarTypeKind SrcKind = SrcTy->getScalarTypeKind()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3827 | case Type::STK_MemberPointer: |
| 3828 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3829 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3830 | case Type::STK_CPointer: |
| 3831 | case Type::STK_BlockPointer: |
| 3832 | case Type::STK_ObjCObjectPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3833 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3834 | case Type::STK_CPointer: |
| 3835 | return CK_BitCast; |
| 3836 | case Type::STK_BlockPointer: |
| 3837 | return (SrcKind == Type::STK_BlockPointer |
| 3838 | ? CK_BitCast : CK_AnyPointerToBlockPointerCast); |
| 3839 | case Type::STK_ObjCObjectPointer: |
| 3840 | if (SrcKind == Type::STK_ObjCObjectPointer) |
| 3841 | return CK_BitCast; |
| 3842 | else if (SrcKind == Type::STK_CPointer) |
| 3843 | return CK_CPointerToObjCPointerCast; |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3844 | else { |
| 3845 | maybeExtendBlockObject(S, Src); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3846 | return CK_BlockPointerToObjCPointerCast; |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3847 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3848 | case Type::STK_Bool: |
| 3849 | return CK_PointerToBoolean; |
| 3850 | case Type::STK_Integral: |
| 3851 | return CK_PointerToIntegral; |
| 3852 | case Type::STK_Floating: |
| 3853 | case Type::STK_FloatingComplex: |
| 3854 | case Type::STK_IntegralComplex: |
| 3855 | case Type::STK_MemberPointer: |
| 3856 | llvm_unreachable("illegal cast from pointer"); |
| 3857 | } |
| 3858 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3859 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3860 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 3861 | case Type::STK_Integral: |
| 3862 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3863 | case Type::STK_CPointer: |
| 3864 | case Type::STK_ObjCObjectPointer: |
| 3865 | case Type::STK_BlockPointer: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3866 | if (Src.get()->isNullPointerConstant(S.Context, |
| 3867 | Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3868 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3869 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3870 | case Type::STK_Bool: |
| 3871 | return CK_IntegralToBoolean; |
| 3872 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3873 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3874 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3875 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3876 | case Type::STK_IntegralComplex: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3877 | Src = S.ImpCastExprToType(Src.take(), |
| 3878 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3879 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3880 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3881 | case Type::STK_FloatingComplex: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3882 | Src = S.ImpCastExprToType(Src.take(), |
| 3883 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3884 | CK_IntegralToFloating); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3885 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3886 | case Type::STK_MemberPointer: |
| 3887 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3888 | } |
| 3889 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3890 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3891 | case Type::STK_Floating: |
| 3892 | switch (DestTy->getScalarTypeKind()) { |
| 3893 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3894 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3895 | case Type::STK_Bool: |
| 3896 | return CK_FloatingToBoolean; |
| 3897 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3898 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3899 | case Type::STK_FloatingComplex: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3900 | Src = S.ImpCastExprToType(Src.take(), |
| 3901 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3902 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3903 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3904 | case Type::STK_IntegralComplex: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3905 | Src = S.ImpCastExprToType(Src.take(), |
| 3906 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3907 | CK_FloatingToIntegral); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3908 | return CK_IntegralRealToComplex; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3909 | case Type::STK_CPointer: |
| 3910 | case Type::STK_ObjCObjectPointer: |
| 3911 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3912 | llvm_unreachable("valid float->pointer cast?"); |
| 3913 | case Type::STK_MemberPointer: |
| 3914 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3915 | } |
| 3916 | break; |
| 3917 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3918 | case Type::STK_FloatingComplex: |
| 3919 | switch (DestTy->getScalarTypeKind()) { |
| 3920 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3921 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3922 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3923 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3924 | case Type::STK_Floating: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3925 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3926 | if (S.Context.hasSameType(ET, DestTy)) |
| 3927 | return CK_FloatingComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3928 | Src = S.ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3929 | return CK_FloatingCast; |
| 3930 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3931 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3932 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3933 | case Type::STK_Integral: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3934 | Src = S.ImpCastExprToType(Src.take(), |
| 3935 | SrcTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3936 | CK_FloatingComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3937 | return CK_FloatingToIntegral; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3938 | case Type::STK_CPointer: |
| 3939 | case Type::STK_ObjCObjectPointer: |
| 3940 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3941 | llvm_unreachable("valid complex float->pointer cast?"); |
| 3942 | case Type::STK_MemberPointer: |
| 3943 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3944 | } |
| 3945 | break; |
| 3946 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3947 | case Type::STK_IntegralComplex: |
| 3948 | switch (DestTy->getScalarTypeKind()) { |
| 3949 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3950 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3951 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3952 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3953 | case Type::STK_Integral: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3954 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3955 | if (S.Context.hasSameType(ET, DestTy)) |
| 3956 | return CK_IntegralComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3957 | Src = S.ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3958 | return CK_IntegralCast; |
| 3959 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3960 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3961 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3962 | case Type::STK_Floating: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3963 | Src = S.ImpCastExprToType(Src.take(), |
| 3964 | SrcTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3965 | CK_IntegralComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3966 | return CK_IntegralToFloating; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3967 | case Type::STK_CPointer: |
| 3968 | case Type::STK_ObjCObjectPointer: |
| 3969 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3970 | llvm_unreachable("valid complex int->pointer cast?"); |
| 3971 | case Type::STK_MemberPointer: |
| 3972 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3973 | } |
| 3974 | break; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3975 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3976 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3977 | llvm_unreachable("Unhandled scalar cast"); |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3980 | /// CheckCastTypes - Check type constraints for casting between types. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3981 | ExprResult Sema::CheckCastTypes(SourceLocation CastStartLoc, |
| 3982 | SourceRange TypeRange, QualType CastType, |
| 3983 | Expr *CastExpr, CastKind &Kind, |
| 3984 | ExprValueKind &VK, CXXCastPath &BasePath, |
| 3985 | bool FunctionalStyle) { |
| 3986 | if (CastExpr->getType() == Context.UnknownAnyTy) |
| 3987 | return checkUnknownAnyCast(TypeRange, CastType, CastExpr, Kind, VK, |
| 3988 | BasePath); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3989 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3990 | if (getLangOptions().CPlusPlus) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3991 | return CXXCheckCStyleCast(SourceRange(CastStartLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3992 | CastExpr->getLocEnd()), |
| 3993 | CastType, VK, CastExpr, Kind, BasePath, |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3994 | FunctionalStyle); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3995 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3996 | assert(!CastExpr->getType()->isPlaceholderType()); |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3997 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3998 | // We only support r-value casts in C. |
| 3999 | VK = VK_RValue; |
| 4000 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4001 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 4002 | // type needs to be scalar. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4003 | if (CastType->isVoidType()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4004 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4005 | ExprResult castExprRes = IgnoredValueConversions(CastExpr); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4006 | if (castExprRes.isInvalid()) |
| 4007 | return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4008 | CastExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4009 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4010 | // Cast to void allows any expr type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4011 | Kind = CK_ToVoid; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4012 | return Owned(CastExpr); |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 4013 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4014 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4015 | ExprResult castExprRes = DefaultFunctionArrayLvalueConversion(CastExpr); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4016 | if (castExprRes.isInvalid()) |
| 4017 | return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4018 | CastExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4019 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4020 | if (RequireCompleteType(TypeRange.getBegin(), CastType, |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 4021 | diag::err_typecheck_cast_to_incomplete)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4022 | return ExprError(); |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 4023 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4024 | if (!CastType->isScalarType() && !CastType->isVectorType()) { |
| 4025 | if (Context.hasSameUnqualifiedType(CastType, CastExpr->getType()) && |
| 4026 | (CastType->isStructureType() || CastType->isUnionType())) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4027 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4028 | // FIXME: Check that the cast destination type is complete. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4029 | Diag(TypeRange.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 4030 | << CastType << CastExpr->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4031 | Kind = CK_NoOp; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4032 | return Owned(CastExpr); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4033 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4034 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4035 | if (CastType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4036 | // GCC cast to union extension |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4037 | RecordDecl *RD = CastType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4038 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4039 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4040 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4041 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4042 | CastExpr->getType()) && |
Abramo Bagnara | 8c4bfe5 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 4043 | !Field->isUnnamedBitfield()) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4044 | Diag(TypeRange.getBegin(), diag::ext_typecheck_cast_to_union) |
| 4045 | << CastExpr->getSourceRange(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4046 | break; |
| 4047 | } |
| 4048 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4049 | if (Field == FieldEnd) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4050 | Diag(TypeRange.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 4051 | << CastExpr->getType() << CastExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4052 | return ExprError(); |
| 4053 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4054 | Kind = CK_ToUnion; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4055 | return Owned(CastExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4056 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4057 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4058 | // Reject any other conversions to non-scalar types. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4059 | Diag(TypeRange.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 4060 | << CastType << CastExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4061 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4062 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4063 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4064 | // The type we're casting to is known to be a scalar or vector. |
| 4065 | |
| 4066 | // Require the operand to be a scalar or vector. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4067 | if (!CastExpr->getType()->isScalarType() && |
| 4068 | !CastExpr->getType()->isVectorType()) { |
| 4069 | Diag(CastExpr->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4070 | diag::err_typecheck_expect_scalar_operand) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4071 | << CastExpr->getType() << CastExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4072 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4073 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4074 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4075 | if (CastType->isExtVectorType()) |
| 4076 | return CheckExtVectorCast(TypeRange, CastType, CastExpr, Kind); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4077 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4078 | if (CastType->isVectorType()) { |
| 4079 | if (CastType->getAs<VectorType>()->getVectorKind() == |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4080 | VectorType::AltiVecVector && |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4081 | (CastExpr->getType()->isIntegerType() || |
| 4082 | CastExpr->getType()->isFloatingType())) { |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4083 | Kind = CK_VectorSplat; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4084 | return Owned(CastExpr); |
| 4085 | } else if (CheckVectorCast(TypeRange, CastType, CastExpr->getType(), |
| 4086 | Kind)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4087 | return ExprError(); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4088 | } else |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4089 | return Owned(CastExpr); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4090 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4091 | if (CastExpr->getType()->isVectorType()) { |
| 4092 | if (CheckVectorCast(TypeRange, CastExpr->getType(), CastType, Kind)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4093 | return ExprError(); |
| 4094 | else |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4095 | return Owned(CastExpr); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4096 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4097 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4098 | // The source and target types are both scalars, i.e. |
| 4099 | // - arithmetic types (fundamental, enum, and complex) |
| 4100 | // - all kinds of pointers |
| 4101 | // Note that member pointers were filtered out with C++, above. |
| 4102 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4103 | if (isa<ObjCSelectorExpr>(CastExpr)) { |
| 4104 | Diag(CastExpr->getLocStart(), diag::err_cast_selector_expr); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4105 | return ExprError(); |
| 4106 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4107 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4108 | // If either type is a pointer, the other type has to be either an |
| 4109 | // integer or a pointer. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4110 | QualType CastExprType = CastExpr->getType(); |
| 4111 | if (!CastType->isArithmeticType()) { |
| 4112 | if (!CastExprType->isIntegralType(Context) && |
| 4113 | CastExprType->isArithmeticType()) { |
| 4114 | Diag(CastExpr->getLocStart(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4115 | diag::err_cast_pointer_from_non_pointer_int) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4116 | << CastExprType << CastExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4117 | return ExprError(); |
| 4118 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4119 | } else if (!CastExpr->getType()->isArithmeticType()) { |
| 4120 | if (!CastType->isIntegralType(Context) && CastType->isArithmeticType()) { |
| 4121 | Diag(CastExpr->getLocStart(), diag::err_cast_pointer_to_non_pointer_int) |
| 4122 | << CastType << CastExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4123 | return ExprError(); |
| 4124 | } |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4125 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4126 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4127 | if (getLangOptions().ObjCAutoRefCount) { |
| 4128 | // Diagnose problems with Objective-C casts involving lifetime qualifiers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4129 | CheckObjCARCConversion(SourceRange(CastStartLoc, CastExpr->getLocEnd()), |
| 4130 | CastType, CastExpr, CCK_CStyleCast); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4131 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4132 | if (const PointerType *CastPtr = CastType->getAs<PointerType>()) { |
| 4133 | if (const PointerType *ExprPtr = CastExprType->getAs<PointerType>()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4134 | Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); |
| 4135 | Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); |
| 4136 | if (CastPtr->getPointeeType()->isObjCLifetimeType() && |
| 4137 | ExprPtr->getPointeeType()->isObjCLifetimeType() && |
| 4138 | !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4139 | Diag(CastExpr->getLocStart(), |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 4140 | diag::err_typecheck_incompatible_ownership) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4141 | << CastExprType << CastType << AA_Casting |
| 4142 | << CastExpr->getSourceRange(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4143 | |
| 4144 | return ExprError(); |
| 4145 | } |
| 4146 | } |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 4147 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4148 | else if (!CheckObjCARCUnavailableWeakConversion(CastType, CastExprType)) { |
| 4149 | Diag(CastExpr->getLocStart(), |
Fariborz Jahanian | 82007c3 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 4150 | diag::err_arc_convesion_of_weak_unavailable) << 1 |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4151 | << CastExprType << CastType |
| 4152 | << CastExpr->getSourceRange(); |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 4153 | return ExprError(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4154 | } |
| 4155 | } |
| 4156 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4157 | castExprRes = Owned(CastExpr); |
| 4158 | Kind = PrepareScalarCast(*this, castExprRes, CastType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4159 | if (castExprRes.isInvalid()) |
| 4160 | return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4161 | CastExpr = castExprRes.take(); |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4162 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4163 | if (Kind == CK_BitCast) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4164 | CheckCastAlign(CastExpr, CastType, TypeRange); |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4165 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4166 | return Owned(CastExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4169 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4170 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4171 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4172 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4173 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4174 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4175 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4176 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4177 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4178 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4179 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4180 | } else |
| 4181 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4182 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4183 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4184 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4185 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4186 | return false; |
| 4187 | } |
| 4188 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4189 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 4190 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4191 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4192 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4193 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4194 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4195 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 4196 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4197 | if (SrcTy->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4198 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) { |
| 4199 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4200 | << DestTy << SrcTy << R; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4201 | return ExprError(); |
| 4202 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4203 | Kind = CK_BitCast; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4204 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4207 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4208 | // conversion will take place first from scalar to elt type, and then |
| 4209 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4210 | if (SrcTy->isPointerType()) |
| 4211 | return Diag(R.getBegin(), |
| 4212 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 4213 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4214 | |
| 4215 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4216 | ExprResult CastExprRes = Owned(CastExpr); |
| 4217 | CastKind CK = PrepareScalarCast(*this, CastExprRes, DestElemTy); |
| 4218 | if (CastExprRes.isInvalid()) |
| 4219 | return ExprError(); |
| 4220 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4221 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4222 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4223 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4226 | ExprResult |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4227 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, |
| 4228 | Declarator &D, ParsedType &Ty, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4229 | SourceLocation RParenLoc, Expr *CastExpr) { |
| 4230 | assert(!D.isInvalidType() && (CastExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4231 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4232 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4233 | TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, CastExpr->getType()); |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4234 | if (D.isInvalidType()) |
| 4235 | return ExprError(); |
| 4236 | |
| 4237 | if (getLangOptions().CPlusPlus) { |
| 4238 | // Check that there are no default arguments (C++ only). |
| 4239 | CheckExtraCXXDefaultArguments(D); |
| 4240 | } |
| 4241 | |
| 4242 | QualType castType = castTInfo->getType(); |
| 4243 | Ty = CreateParsedType(castType, castTInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4244 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4245 | bool isVectorLiteral = false; |
| 4246 | |
| 4247 | // Check for an altivec or OpenCL literal, |
| 4248 | // i.e. all the elements are integer constants. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4249 | ParenExpr *PE = dyn_cast<ParenExpr>(CastExpr); |
| 4250 | ParenListExpr *PLE = dyn_cast<ParenListExpr>(CastExpr); |
Tobias Grosser | 37c31c2 | 2011-09-21 18:28:29 +0000 | [diff] [blame^] | 4251 | if ((getLangOptions().AltiVec || getLangOptions().OpenCL) |
| 4252 | && castType->isVectorType() && (PE || PLE)) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4253 | if (PLE && PLE->getNumExprs() == 0) { |
| 4254 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4255 | return ExprError(); |
| 4256 | } |
| 4257 | if (PE || PLE->getNumExprs() == 1) { |
| 4258 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); |
| 4259 | if (!E->getType()->isVectorType()) |
| 4260 | isVectorLiteral = true; |
| 4261 | } |
| 4262 | else |
| 4263 | isVectorLiteral = true; |
| 4264 | } |
| 4265 | |
| 4266 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
| 4267 | // then handle it as such. |
| 4268 | if (isVectorLiteral) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4269 | return BuildVectorLiteral(LParenLoc, RParenLoc, CastExpr, castTInfo); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4270 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4271 | // If the Expr being casted is a ParenListExpr, handle it specially. |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4272 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 4273 | // sequence of BinOp comma operators. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4274 | if (isa<ParenListExpr>(CastExpr)) { |
| 4275 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, CastExpr); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4276 | if (Result.isInvalid()) return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4277 | CastExpr = Result.take(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4278 | } |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4279 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4280 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, CastExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4283 | ExprResult |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4284 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4285 | SourceLocation RParenLoc, Expr *CastExpr) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4286 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4287 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4288 | CXXCastPath BasePath; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4289 | ExprResult CastResult = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4290 | CheckCastTypes(LParenLoc, SourceRange(LParenLoc, RParenLoc), Ty->getType(), |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4291 | CastExpr, Kind, VK, BasePath); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4292 | if (CastResult.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4293 | return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4294 | CastExpr = CastResult.take(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 4295 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4296 | return Owned(CStyleCastExpr::Create( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4297 | Context, Ty->getType().getNonLValueExprType(Context), VK, Kind, CastExpr, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4298 | &BasePath, Ty, LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4301 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
| 4302 | SourceLocation RParenLoc, Expr *E, |
| 4303 | TypeSourceInfo *TInfo) { |
| 4304 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && |
| 4305 | "Expected paren or paren list expression"); |
| 4306 | |
| 4307 | Expr **exprs; |
| 4308 | unsigned numExprs; |
| 4309 | Expr *subExpr; |
| 4310 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { |
| 4311 | exprs = PE->getExprs(); |
| 4312 | numExprs = PE->getNumExprs(); |
| 4313 | } else { |
| 4314 | subExpr = cast<ParenExpr>(E)->getSubExpr(); |
| 4315 | exprs = &subExpr; |
| 4316 | numExprs = 1; |
| 4317 | } |
| 4318 | |
| 4319 | QualType Ty = TInfo->getType(); |
| 4320 | assert(Ty->isVectorType() && "Expected vector type"); |
| 4321 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4322 | SmallVector<Expr *, 8> initExprs; |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4323 | const VectorType *VTy = Ty->getAs<VectorType>(); |
| 4324 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 4325 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4326 | // '(...)' form of vector initialization in AltiVec: the number of |
| 4327 | // initializers must be one or must match the size of the vector. |
| 4328 | // If a single value is specified in the initializer then it will be |
| 4329 | // replicated to all the components of the vector |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4330 | if (VTy->getVectorKind() == VectorType::AltiVecVector) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4331 | // The number of initializers must be one or must match the size of the |
| 4332 | // vector. If a single value is specified in the initializer then it will |
| 4333 | // be replicated to all the components of the vector |
| 4334 | if (numExprs == 1) { |
| 4335 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4336 | ExprResult Literal = Owned(exprs[0]); |
| 4337 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4338 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4339 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4340 | } |
| 4341 | else if (numExprs < numElems) { |
| 4342 | Diag(E->getExprLoc(), |
| 4343 | diag::err_incorrect_number_of_vector_initializers); |
| 4344 | return ExprError(); |
| 4345 | } |
| 4346 | else |
| 4347 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4348 | initExprs.push_back(exprs[i]); |
| 4349 | } |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4350 | else { |
| 4351 | // For OpenCL, when the number of initializers is a single value, |
| 4352 | // it will be replicated to all components of the vector. |
| 4353 | if (getLangOptions().OpenCL && |
| 4354 | VTy->getVectorKind() == VectorType::GenericVector && |
| 4355 | numExprs == 1) { |
| 4356 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4357 | ExprResult Literal = Owned(exprs[0]); |
| 4358 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4359 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4360 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4361 | } |
| 4362 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4363 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4364 | initExprs.push_back(exprs[i]); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4365 | } |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4366 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4367 | // braces instead of the original commas. |
| 4368 | InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc, |
| 4369 | &initExprs[0], |
| 4370 | initExprs.size(), RParenLoc); |
| 4371 | initE->setType(Ty); |
| 4372 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); |
| 4373 | } |
| 4374 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4375 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 4376 | /// of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4377 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4378 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *OrigExpr) { |
| 4379 | ParenListExpr *E = dyn_cast<ParenListExpr>(OrigExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4380 | if (!E) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4381 | return Owned(OrigExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4382 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4383 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4384 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4385 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4386 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4387 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4388 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4389 | if (Result.isInvalid()) return ExprError(); |
| 4390 | |
| 4391 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4394 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4395 | SourceLocation R, |
| 4396 | MultiExprArg Val) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4397 | unsigned nexprs = Val.size(); |
| 4398 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4399 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 4400 | Expr *expr; |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4401 | if (nexprs == 1) |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4402 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 4403 | else |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4404 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R, |
| 4405 | exprs[nexprs-1]->getType()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4406 | return Owned(expr); |
| 4407 | } |
| 4408 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4409 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4410 | /// constant and the other is not a pointer. Returns true if a diagnostic is |
| 4411 | /// emitted. |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4412 | bool Sema::DiagnoseConditionalForNull(Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4413 | SourceLocation QuestionLoc) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4414 | Expr *NullExpr = LHSExpr; |
| 4415 | Expr *NonPointerExpr = RHSExpr; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4416 | Expr::NullPointerConstantKind NullKind = |
| 4417 | NullExpr->isNullPointerConstant(Context, |
| 4418 | Expr::NPC_ValueDependentIsNotNull); |
| 4419 | |
| 4420 | if (NullKind == Expr::NPCK_NotNull) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4421 | NullExpr = RHSExpr; |
| 4422 | NonPointerExpr = LHSExpr; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4423 | NullKind = |
| 4424 | NullExpr->isNullPointerConstant(Context, |
| 4425 | Expr::NPC_ValueDependentIsNotNull); |
| 4426 | } |
| 4427 | |
| 4428 | if (NullKind == Expr::NPCK_NotNull) |
| 4429 | return false; |
| 4430 | |
| 4431 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 4432 | // In this case, check to make sure that we got here from a "NULL" |
| 4433 | // string in the source code. |
| 4434 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 4435 | SourceLocation loc = NullExpr->getExprLoc(); |
| 4436 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4437 | return false; |
| 4438 | } |
| 4439 | |
| 4440 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 4441 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 4442 | << NonPointerExpr->getType() << DiagType |
| 4443 | << NonPointerExpr->getSourceRange(); |
| 4444 | return true; |
| 4445 | } |
| 4446 | |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4447 | /// \brief Return false if the condition expression is valid, true otherwise. |
| 4448 | static bool checkCondition(Sema &S, Expr *Cond) { |
| 4449 | QualType CondTy = Cond->getType(); |
| 4450 | |
| 4451 | // C99 6.5.15p2 |
| 4452 | if (CondTy->isScalarType()) return false; |
| 4453 | |
| 4454 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 4455 | if (S.getLangOptions().OpenCL && CondTy->isVectorType()) |
| 4456 | return false; |
| 4457 | |
| 4458 | // Emit the proper error message. |
| 4459 | S.Diag(Cond->getLocStart(), S.getLangOptions().OpenCL ? |
| 4460 | diag::err_typecheck_cond_expect_scalar : |
| 4461 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 4462 | << CondTy; |
| 4463 | return true; |
| 4464 | } |
| 4465 | |
| 4466 | /// \brief Return false if the two expressions can be converted to a vector, |
| 4467 | /// true otherwise |
| 4468 | static bool checkConditionalConvertScalarsToVectors(Sema &S, ExprResult &LHS, |
| 4469 | ExprResult &RHS, |
| 4470 | QualType CondTy) { |
| 4471 | // Both operands should be of scalar type. |
| 4472 | if (!LHS.get()->getType()->isScalarType()) { |
| 4473 | S.Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4474 | << CondTy; |
| 4475 | return true; |
| 4476 | } |
| 4477 | if (!RHS.get()->getType()->isScalarType()) { |
| 4478 | S.Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4479 | << CondTy; |
| 4480 | return true; |
| 4481 | } |
| 4482 | |
| 4483 | // Implicity convert these scalars to the type of the condition. |
| 4484 | LHS = S.ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 4485 | RHS = S.ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
| 4486 | return false; |
| 4487 | } |
| 4488 | |
| 4489 | /// \brief Handle when one or both operands are void type. |
| 4490 | static QualType checkConditionalVoidType(Sema &S, ExprResult &LHS, |
| 4491 | ExprResult &RHS) { |
| 4492 | Expr *LHSExpr = LHS.get(); |
| 4493 | Expr *RHSExpr = RHS.get(); |
| 4494 | |
| 4495 | if (!LHSExpr->getType()->isVoidType()) |
| 4496 | S.Diag(RHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4497 | << RHSExpr->getSourceRange(); |
| 4498 | if (!RHSExpr->getType()->isVoidType()) |
| 4499 | S.Diag(LHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4500 | << LHSExpr->getSourceRange(); |
| 4501 | LHS = S.ImpCastExprToType(LHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4502 | RHS = S.ImpCastExprToType(RHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4503 | return S.Context.VoidTy; |
| 4504 | } |
| 4505 | |
| 4506 | /// \brief Return false if the NullExpr can be promoted to PointerTy, |
| 4507 | /// true otherwise. |
| 4508 | static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr, |
| 4509 | QualType PointerTy) { |
| 4510 | if ((!PointerTy->isAnyPointerType() && !PointerTy->isBlockPointerType()) || |
| 4511 | !NullExpr.get()->isNullPointerConstant(S.Context, |
| 4512 | Expr::NPC_ValueDependentIsNull)) |
| 4513 | return true; |
| 4514 | |
| 4515 | NullExpr = S.ImpCastExprToType(NullExpr.take(), PointerTy, CK_NullToPointer); |
| 4516 | return false; |
| 4517 | } |
| 4518 | |
| 4519 | /// \brief Checks compatibility between two pointers and return the resulting |
| 4520 | /// type. |
| 4521 | static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, |
| 4522 | ExprResult &RHS, |
| 4523 | SourceLocation Loc) { |
| 4524 | QualType LHSTy = LHS.get()->getType(); |
| 4525 | QualType RHSTy = RHS.get()->getType(); |
| 4526 | |
| 4527 | if (S.Context.hasSameType(LHSTy, RHSTy)) { |
| 4528 | // Two identical pointers types are always compatible. |
| 4529 | return LHSTy; |
| 4530 | } |
| 4531 | |
| 4532 | QualType lhptee, rhptee; |
| 4533 | |
| 4534 | // Get the pointee types. |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4535 | if (const BlockPointerType *LHSBTy = LHSTy->getAs<BlockPointerType>()) { |
| 4536 | lhptee = LHSBTy->getPointeeType(); |
| 4537 | rhptee = RHSTy->castAs<BlockPointerType>()->getPointeeType(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4538 | } else { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4539 | lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); |
| 4540 | rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
| 4543 | if (!S.Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4544 | rhptee.getUnqualifiedType())) { |
| 4545 | S.Diag(Loc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4546 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4547 | << RHS.get()->getSourceRange(); |
| 4548 | // In this situation, we assume void* type. No especially good |
| 4549 | // reason, but this is what gcc does, and we do have to pick |
| 4550 | // to get a consistent AST. |
| 4551 | QualType incompatTy = S.Context.getPointerType(S.Context.VoidTy); |
| 4552 | LHS = S.ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4553 | RHS = S.ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
| 4554 | return incompatTy; |
| 4555 | } |
| 4556 | |
| 4557 | // The pointer types are compatible. |
| 4558 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4559 | // differently qualified versions of compatible types, the result type is |
| 4560 | // a pointer to an appropriately qualified version of the *composite* |
| 4561 | // type. |
| 4562 | // FIXME: Need to calculate the composite type. |
| 4563 | // FIXME: Need to add qualifiers |
| 4564 | |
| 4565 | LHS = S.ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 4566 | RHS = S.ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
| 4567 | return LHSTy; |
| 4568 | } |
| 4569 | |
| 4570 | /// \brief Return the resulting type when the operands are both block pointers. |
| 4571 | static QualType checkConditionalBlockPointerCompatibility(Sema &S, |
| 4572 | ExprResult &LHS, |
| 4573 | ExprResult &RHS, |
| 4574 | SourceLocation Loc) { |
| 4575 | QualType LHSTy = LHS.get()->getType(); |
| 4576 | QualType RHSTy = RHS.get()->getType(); |
| 4577 | |
| 4578 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4579 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4580 | QualType destType = S.Context.getPointerType(S.Context.VoidTy); |
| 4581 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4582 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4583 | return destType; |
| 4584 | } |
| 4585 | S.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 4586 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4587 | << RHS.get()->getSourceRange(); |
| 4588 | return QualType(); |
| 4589 | } |
| 4590 | |
| 4591 | // We have 2 block pointer types. |
| 4592 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4593 | } |
| 4594 | |
| 4595 | /// \brief Return the resulting type when the operands are both pointers. |
| 4596 | static QualType |
| 4597 | checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, |
| 4598 | ExprResult &RHS, |
| 4599 | SourceLocation Loc) { |
| 4600 | // get the pointer types |
| 4601 | QualType LHSTy = LHS.get()->getType(); |
| 4602 | QualType RHSTy = RHS.get()->getType(); |
| 4603 | |
| 4604 | // get the "pointed to" types |
| 4605 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4606 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4607 | |
| 4608 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4609 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4610 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 4611 | QualType destPointee |
| 4612 | = S.Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4613 | QualType destType = S.Context.getPointerType(destPointee); |
| 4614 | // Add qualifiers if necessary. |
| 4615 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
| 4616 | // Promote to void*. |
| 4617 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4618 | return destType; |
| 4619 | } |
| 4620 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
| 4621 | QualType destPointee |
| 4622 | = S.Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4623 | QualType destType = S.Context.getPointerType(destPointee); |
| 4624 | // Add qualifiers if necessary. |
| 4625 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
| 4626 | // Promote to void*. |
| 4627 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4628 | return destType; |
| 4629 | } |
| 4630 | |
| 4631 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4632 | } |
| 4633 | |
| 4634 | /// \brief Return false if the first expression is not an integer and the second |
| 4635 | /// expression is not a pointer, true otherwise. |
| 4636 | static bool checkPointerIntegerMismatch(Sema &S, ExprResult &Int, |
| 4637 | Expr* PointerExpr, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4638 | bool IsIntFirstExpr) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4639 | if (!PointerExpr->getType()->isPointerType() || |
| 4640 | !Int.get()->getType()->isIntegerType()) |
| 4641 | return false; |
| 4642 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4643 | Expr *Expr1 = IsIntFirstExpr ? Int.get() : PointerExpr; |
| 4644 | Expr *Expr2 = IsIntFirstExpr ? PointerExpr : Int.get(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4645 | |
| 4646 | S.Diag(Loc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4647 | << Expr1->getType() << Expr2->getType() |
| 4648 | << Expr1->getSourceRange() << Expr2->getSourceRange(); |
| 4649 | Int = S.ImpCastExprToType(Int.take(), PointerExpr->getType(), |
| 4650 | CK_IntegralToPointer); |
| 4651 | return true; |
| 4652 | } |
| 4653 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4654 | /// Note that LHS is not null here, even if this is the gnu "x ?: y" extension. |
| 4655 | /// In that case, LHS = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4656 | /// C99 6.5.15 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4657 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 4658 | ExprResult &RHS, ExprValueKind &VK, |
| 4659 | ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4660 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 4661 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4662 | ExprResult LHSResult = CheckPlaceholderExpr(LHS.get()); |
| 4663 | if (!LHSResult.isUsable()) return QualType(); |
| 4664 | LHS = move(LHSResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4665 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4666 | ExprResult RHSResult = CheckPlaceholderExpr(RHS.get()); |
| 4667 | if (!RHSResult.isUsable()) return QualType(); |
| 4668 | RHS = move(RHSResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4669 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4670 | // C++ is sufficiently different to merit its own checker. |
| 4671 | if (getLangOptions().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4672 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4673 | |
| 4674 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4675 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4676 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4677 | Cond = UsualUnaryConversions(Cond.take()); |
| 4678 | if (Cond.isInvalid()) |
| 4679 | return QualType(); |
| 4680 | LHS = UsualUnaryConversions(LHS.take()); |
| 4681 | if (LHS.isInvalid()) |
| 4682 | return QualType(); |
| 4683 | RHS = UsualUnaryConversions(RHS.take()); |
| 4684 | if (RHS.isInvalid()) |
| 4685 | return QualType(); |
| 4686 | |
| 4687 | QualType CondTy = Cond.get()->getType(); |
| 4688 | QualType LHSTy = LHS.get()->getType(); |
| 4689 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4690 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4691 | // first, check the condition. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4692 | if (checkCondition(*this, Cond.get())) |
| 4693 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4694 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4695 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4696 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 4697 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4698 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4699 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 4700 | // attempt to implicity convert them to the vector type to act like the |
| 4701 | // built in select. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4702 | if (getLangOptions().OpenCL && CondTy->isVectorType()) |
| 4703 | if (checkConditionalConvertScalarsToVectors(*this, LHS, RHS, CondTy)) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4704 | return QualType(); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4705 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4706 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4707 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4708 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4709 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4710 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4711 | return QualType(); |
| 4712 | return LHS.get()->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4713 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4714 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4715 | // If both operands are the same structure or union type, the result is that |
| 4716 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4717 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4718 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4719 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4720 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4721 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4722 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4723 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4724 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4725 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4726 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4727 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4728 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4729 | return checkConditionalVoidType(*this, LHS, RHS); |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4730 | } |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4731 | |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4732 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4733 | // the type of the other operand." |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4734 | if (!checkConditionalNullPointer(*this, RHS, LHSTy)) return LHSTy; |
| 4735 | if (!checkConditionalNullPointer(*this, LHS, RHSTy)) return RHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4736 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4737 | // All objective-c pointer type analysis is done here. |
| 4738 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4739 | QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4740 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4741 | return QualType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4742 | if (!compositeType.isNull()) |
| 4743 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4744 | |
| 4745 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4746 | // Handle block pointer types. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4747 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) |
| 4748 | return checkConditionalBlockPointerCompatibility(*this, LHS, RHS, |
| 4749 | QuestionLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4750 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4751 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4752 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) |
| 4753 | return checkConditionalObjectPointersCompatibility(*this, LHS, RHS, |
| 4754 | QuestionLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4755 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4756 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 4757 | // null pointers have been filtered out by this point. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4758 | if (checkPointerIntegerMismatch(*this, LHS, RHS.get(), QuestionLoc, |
| 4759 | /*isIntFirstExpr=*/true)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4760 | return RHSTy; |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4761 | if (checkPointerIntegerMismatch(*this, RHS, LHS.get(), QuestionLoc, |
| 4762 | /*isIntFirstExpr=*/false)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4763 | return LHSTy; |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4764 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4765 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 4766 | // constant and the other is not a pointer type. In this case, the user most |
| 4767 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4768 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4769 | return QualType(); |
| 4770 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4771 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4772 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4773 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4774 | << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4775 | return QualType(); |
| 4776 | } |
| 4777 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4778 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4779 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4780 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4781 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4782 | QualType LHSTy = LHS.get()->getType(); |
| 4783 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4784 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4785 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4786 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4787 | // redefinition type if an attempt is made to access its fields. |
| 4788 | if (LHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4789 | (Context.hasSameType(RHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4790 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4791 | return LHSTy; |
| 4792 | } |
| 4793 | if (RHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4794 | (Context.hasSameType(LHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4795 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4796 | return RHSTy; |
| 4797 | } |
| 4798 | // And the same for struct objc_object* / id |
| 4799 | if (LHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4800 | (Context.hasSameType(RHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4801 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4802 | return LHSTy; |
| 4803 | } |
| 4804 | if (RHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4805 | (Context.hasSameType(LHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4806 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4807 | return RHSTy; |
| 4808 | } |
| 4809 | // And the same for struct objc_selector* / SEL |
| 4810 | if (Context.isObjCSelType(LHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4811 | (Context.hasSameType(RHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4812 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4813 | return LHSTy; |
| 4814 | } |
| 4815 | if (Context.isObjCSelType(RHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4816 | (Context.hasSameType(LHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4817 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4818 | return RHSTy; |
| 4819 | } |
| 4820 | // Check constraints for Objective-C object pointers types. |
| 4821 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4822 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4823 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4824 | // Two identical object pointer types are always compatible. |
| 4825 | return LHSTy; |
| 4826 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4827 | const ObjCObjectPointerType *LHSOPT = LHSTy->castAs<ObjCObjectPointerType>(); |
| 4828 | const ObjCObjectPointerType *RHSOPT = RHSTy->castAs<ObjCObjectPointerType>(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4829 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4830 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4831 | // If both operands are interfaces and either operand can be |
| 4832 | // assigned to the other, use that type as the composite |
| 4833 | // type. This allows |
| 4834 | // xxx ? (A*) a : (B*) b |
| 4835 | // where B is a subclass of A. |
| 4836 | // |
| 4837 | // Additionally, as for assignment, if either type is 'id' |
| 4838 | // allow silent coercion. Finally, if the types are |
| 4839 | // incompatible then make sure to use 'id' as the composite |
| 4840 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4841 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4842 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4843 | // It could return the composite type. |
| 4844 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4845 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4846 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4847 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4848 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4849 | RHSTy->isObjCQualifiedIdType()) && |
| 4850 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4851 | // Need to handle "id<xx>" explicitly. |
| 4852 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4853 | // id. Currently localizing to here until clear this should be |
| 4854 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4855 | compositeType = Context.getObjCIdType(); |
| 4856 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4857 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4858 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4859 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4860 | ; |
| 4861 | else { |
| 4862 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4863 | << LHSTy << RHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4864 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4865 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4866 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4867 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4868 | return incompatTy; |
| 4869 | } |
| 4870 | // The object pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4871 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 4872 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4873 | return compositeType; |
| 4874 | } |
| 4875 | // Check Objective-C object pointer types and 'void *' |
| 4876 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4877 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4878 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4879 | QualType destPointee |
| 4880 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4881 | QualType destType = Context.getPointerType(destPointee); |
| 4882 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4883 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4884 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4885 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4886 | return destType; |
| 4887 | } |
| 4888 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4889 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4890 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4891 | QualType destPointee |
| 4892 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4893 | QualType destType = Context.getPointerType(destPointee); |
| 4894 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4895 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4896 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4897 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4898 | return destType; |
| 4899 | } |
| 4900 | return QualType(); |
| 4901 | } |
| 4902 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4903 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4904 | /// ParenRange in parentheses. |
| 4905 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4906 | const PartialDiagnostic &Note, |
| 4907 | SourceRange ParenRange) { |
| 4908 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 4909 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 4910 | EndLoc.isValid()) { |
| 4911 | Self.Diag(Loc, Note) |
| 4912 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 4913 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 4914 | } else { |
| 4915 | // We can't display the parentheses, so just show the bare note. |
| 4916 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4917 | } |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
| 4920 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 4921 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 4922 | } |
| 4923 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4924 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 4925 | /// expression, either using a built-in or overloaded operator, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4926 | /// and sets *OpCode to the opcode and *RHSExprs to the right-hand side |
| 4927 | /// expression. |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4928 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4929 | Expr **RHSExprs) { |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 4930 | // Don't strip parenthesis: we should not warn if E is in parenthesis. |
| 4931 | E = E->IgnoreImpCasts(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4932 | E = E->IgnoreConversionOperator(); |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 4933 | E = E->IgnoreImpCasts(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4934 | |
| 4935 | // Built-in binary operator. |
| 4936 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 4937 | if (IsArithmeticOp(OP->getOpcode())) { |
| 4938 | *Opcode = OP->getOpcode(); |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4939 | *RHSExprs = OP->getRHS(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4940 | return true; |
| 4941 | } |
| 4942 | } |
| 4943 | |
| 4944 | // Overloaded operator. |
| 4945 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 4946 | if (Call->getNumArgs() != 2) |
| 4947 | return false; |
| 4948 | |
| 4949 | // Make sure this is really a binary operator that is safe to pass into |
| 4950 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 4951 | OverloadedOperatorKind OO = Call->getOperator(); |
| 4952 | if (OO < OO_Plus || OO > OO_Arrow) |
| 4953 | return false; |
| 4954 | |
| 4955 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 4956 | if (IsArithmeticOp(OpKind)) { |
| 4957 | *Opcode = OpKind; |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4958 | *RHSExprs = Call->getArg(1); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4959 | return true; |
| 4960 | } |
| 4961 | } |
| 4962 | |
| 4963 | return false; |
| 4964 | } |
| 4965 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4966 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 4967 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 4968 | } |
| 4969 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4970 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 4971 | /// or is a logical expression such as (x==y) which has int type, but is |
| 4972 | /// commonly interpreted as boolean. |
| 4973 | static bool ExprLooksBoolean(Expr *E) { |
| 4974 | E = E->IgnoreParenImpCasts(); |
| 4975 | |
| 4976 | if (E->getType()->isBooleanType()) |
| 4977 | return true; |
| 4978 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 4979 | return IsLogicOp(OP->getOpcode()); |
| 4980 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 4981 | return OP->getOpcode() == UO_LNot; |
| 4982 | |
| 4983 | return false; |
| 4984 | } |
| 4985 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4986 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 4987 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 4988 | /// the conditional operator has higher precedence, for example: |
| 4989 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 4990 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 4991 | SourceLocation OpLoc, |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4992 | Expr *Condition, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4993 | Expr *LHSExpr, |
| 4994 | Expr *RHSExpr) { |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4995 | BinaryOperatorKind CondOpcode; |
| 4996 | Expr *CondRHS; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4997 | |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4998 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4999 | return; |
| 5000 | if (!ExprLooksBoolean(CondRHS)) |
| 5001 | return; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5002 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5003 | // The condition is an arithmetic binary expression, with a right- |
| 5004 | // hand side that looks boolean, so warn. |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5005 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5006 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5007 | << Condition->getSourceRange() |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5008 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5009 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5010 | SuggestParentheses(Self, OpLoc, |
| 5011 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 5012 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 5013 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Chandler Carruth | 9d5353c | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 5014 | |
| 5015 | SuggestParentheses(Self, OpLoc, |
| 5016 | Self.PDiag(diag::note_precedence_conditional_first), |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5017 | SourceRange(CondRHS->getLocStart(), RHSExpr->getLocEnd())); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5020 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5021 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5022 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5023 | SourceLocation ColonLoc, |
| 5024 | Expr *CondExpr, Expr *LHSExpr, |
| 5025 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5026 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 5027 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5028 | OpaqueValueExpr *opaqueValue = 0; |
| 5029 | Expr *commonExpr = 0; |
| 5030 | if (LHSExpr == 0) { |
| 5031 | commonExpr = CondExpr; |
| 5032 | |
| 5033 | // We usually want to apply unary conversions *before* saving, except |
| 5034 | // in the special case of a C++ l-value conditional. |
| 5035 | if (!(getLangOptions().CPlusPlus |
| 5036 | && !commonExpr->isTypeDependent() |
| 5037 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 5038 | && commonExpr->isGLValue() |
| 5039 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 5040 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 5041 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5042 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 5043 | if (commonRes.isInvalid()) |
| 5044 | return ExprError(); |
| 5045 | commonExpr = commonRes.take(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5046 | } |
| 5047 | |
| 5048 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 5049 | commonExpr->getType(), |
| 5050 | commonExpr->getValueKind(), |
| 5051 | commonExpr->getObjectKind()); |
| 5052 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5053 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5054 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5055 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5056 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5057 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 5058 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5059 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5060 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 5061 | RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5062 | return ExprError(); |
| 5063 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5064 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 5065 | RHS.get()); |
| 5066 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5067 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5068 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 5069 | LHS.take(), ColonLoc, |
| 5070 | RHS.take(), result, VK, OK)); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5071 | |
| 5072 | return Owned(new (Context) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5073 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5074 | RHS.take(), QuestionLoc, ColonLoc, result, VK, |
| 5075 | OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
Fariborz Jahanian | 71ac1e0 | 2011-09-19 18:06:07 +0000 | [diff] [blame] | 5078 | /// ConvertObjCSelfToClassRootType - convet type of 'self' in class method |
Fariborz Jahanian | 1d4e8e9 | 2011-09-17 19:23:40 +0000 | [diff] [blame] | 5079 | /// to pointer to root of method's class. |
Fariborz Jahanian | 71ac1e0 | 2011-09-19 18:06:07 +0000 | [diff] [blame] | 5080 | static QualType |
| 5081 | ConvertObjCSelfToClassRootType(Sema &S, Expr *selfExpr) { |
| 5082 | QualType SelfType; |
Fariborz Jahanian | 1d4e8e9 | 2011-09-17 19:23:40 +0000 | [diff] [blame] | 5083 | if (const ObjCMethodDecl *MD = S.GetMethodIfSelfExpr(selfExpr)) |
| 5084 | if (MD->isClassMethod()) { |
| 5085 | const ObjCInterfaceDecl *Root = 0; |
| 5086 | if (const ObjCInterfaceDecl * IDecl = MD->getClassInterface()) |
| 5087 | do { |
| 5088 | Root = IDecl; |
| 5089 | } while ((IDecl = IDecl->getSuperClass())); |
| 5090 | if (Root) |
Fariborz Jahanian | 71ac1e0 | 2011-09-19 18:06:07 +0000 | [diff] [blame] | 5091 | SelfType = S.Context.getObjCObjectPointerType( |
| 5092 | S.Context.getObjCInterfaceType(Root)); |
Fariborz Jahanian | 1d4e8e9 | 2011-09-17 19:23:40 +0000 | [diff] [blame] | 5093 | } |
Fariborz Jahanian | 71ac1e0 | 2011-09-19 18:06:07 +0000 | [diff] [blame] | 5094 | return SelfType; |
Fariborz Jahanian | 1d4e8e9 | 2011-09-17 19:23:40 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5097 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5098 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5099 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 5100 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 5101 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5102 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5103 | checkPointerTypesForAssignment(Sema &S, QualType LHSType, QualType RHSType) { |
| 5104 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5105 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5106 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5107 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5108 | const Type *lhptee, *rhptee; |
| 5109 | Qualifiers lhq, rhq; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5110 | llvm::tie(lhptee, lhq) = cast<PointerType>(LHSType)->getPointeeType().split(); |
| 5111 | llvm::tie(rhptee, rhq) = cast<PointerType>(RHSType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5112 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5113 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5114 | |
| 5115 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 5116 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 5117 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5118 | Qualifiers lq; |
| 5119 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5120 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 5121 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 5122 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 5123 | // Ignore lifetime for further calculation. |
| 5124 | lhq.removeObjCLifetime(); |
| 5125 | rhq.removeObjCLifetime(); |
| 5126 | } |
| 5127 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5128 | if (!lhq.compatiblyIncludes(rhq)) { |
| 5129 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 5130 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 5131 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5132 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5133 | // It's okay to add or remove GC or lifetime qualifiers when converting to |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5134 | // and from void*. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5135 | else if (lhq.withoutObjCGCAttr().withoutObjCGLifetime() |
| 5136 | .compatiblyIncludes( |
| 5137 | rhq.withoutObjCGCAttr().withoutObjCGLifetime()) |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5138 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 5139 | ; // keep old |
| 5140 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5141 | // Treat lifetime mismatches as fatal. |
| 5142 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 5143 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5144 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5145 | // For GCC compatibility, other qualifier mismatches are treated |
| 5146 | // as still compatible in C. |
| 5147 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 5148 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5149 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5150 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5151 | // incomplete type and the other is a pointer to a qualified or unqualified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5152 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5153 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5154 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5155 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5156 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5157 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5158 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5159 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5160 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5161 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5162 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5163 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5164 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5165 | |
| 5166 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5167 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5168 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5169 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5170 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5171 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5172 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5173 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 5174 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5175 | // Check if the pointee types are compatible ignoring the sign. |
| 5176 | // We explicitly check for char so that we catch "char" vs |
| 5177 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5178 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5179 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5180 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5181 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5182 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5183 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5184 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5185 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5186 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5187 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5188 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5189 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5190 | // takes priority over sign incompatibility because the sign |
| 5191 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5192 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5193 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5194 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5195 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5196 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5197 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5198 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5199 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5200 | // the eventual target type is the same and the pointers have the same |
| 5201 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5202 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5203 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5204 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5205 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5206 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5207 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5208 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5209 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5210 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5211 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5212 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5213 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5214 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5215 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5216 | } |
| 5217 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5218 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5219 | /// block pointer types are compatible or whether a block and normal pointer |
| 5220 | /// are compatible. It is more restrict than comparing two function pointer |
| 5221 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5222 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5223 | checkBlockPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5224 | QualType RHSType) { |
| 5225 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5226 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5227 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5228 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5229 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5230 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5231 | lhptee = cast<BlockPointerType>(LHSType)->getPointeeType(); |
| 5232 | rhptee = cast<BlockPointerType>(RHSType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5233 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5234 | // In C++, the types have to match exactly. |
| 5235 | if (S.getLangOptions().CPlusPlus) |
| 5236 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5237 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5238 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5239 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5240 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5241 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5242 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5243 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5244 | if (!S.Context.typesAreBlockPointerCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5245 | return Sema::IncompatibleBlockPointer; |
| 5246 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5247 | return ConvTy; |
| 5248 | } |
| 5249 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5250 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5251 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5252 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5253 | checkObjCPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5254 | QualType RHSType) { |
| 5255 | assert(LHSType.isCanonical() && "LHS was not canonicalized!"); |
| 5256 | assert(RHSType.isCanonical() && "RHS was not canonicalized!"); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5257 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5258 | if (LHSType->isObjCBuiltinType()) { |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5259 | // Class is not compatible with ObjC object pointers. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5260 | if (LHSType->isObjCClassType() && !RHSType->isObjCBuiltinType() && |
| 5261 | !RHSType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5262 | return Sema::IncompatiblePointer; |
| 5263 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5264 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5265 | if (RHSType->isObjCBuiltinType()) { |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5266 | if (RHSType->isObjCClassType() && !LHSType->isObjCBuiltinType() && |
| 5267 | !LHSType->isObjCQualifiedClassType()) |
Fariborz Jahanian | 412a496 | 2011-09-15 20:40:18 +0000 | [diff] [blame] | 5268 | return Sema::IncompatiblePointer; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5269 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5270 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5271 | QualType lhptee = LHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5272 | QualType rhptee = RHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5273 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5274 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 5275 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5276 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5277 | if (S.Context.typesAreCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5278 | return Sema::Compatible; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5279 | if (LHSType->isObjCQualifiedIdType() || RHSType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5280 | return Sema::IncompatibleObjCQualifiedId; |
| 5281 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5282 | } |
| 5283 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5284 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5285 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5286 | QualType LHSType, QualType RHSType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5287 | // Fake up an opaque expression. We don't actually care about what |
| 5288 | // cast operations are required, so if CheckAssignmentConstraints |
| 5289 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5290 | // usually happen on valid code. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5291 | OpaqueValueExpr RHSExpr(Loc, RHSType, VK_RValue); |
| 5292 | ExprResult RHSPtr = &RHSExpr; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5293 | CastKind K = CK_Invalid; |
| 5294 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5295 | return CheckAssignmentConstraints(LHSType, RHSPtr, K); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5296 | } |
| 5297 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5298 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5299 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5300 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5301 | /// |
| 5302 | /// int a, *pint; |
| 5303 | /// short *pshort; |
| 5304 | /// struct foo *pfoo; |
| 5305 | /// |
| 5306 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5307 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5308 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5309 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5310 | /// |
| 5311 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5312 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5313 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5314 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5315 | Sema::AssignConvertType |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5316 | Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5317 | CastKind &Kind) { |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5318 | QualType RHSType = RHS.get()->getType(); |
| 5319 | QualType OrigLHSType = LHSType; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5320 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5321 | // Get canonical types. We're not formatting these types, just comparing |
| 5322 | // them. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5323 | LHSType = Context.getCanonicalType(LHSType).getUnqualifiedType(); |
| 5324 | RHSType = Context.getCanonicalType(RHSType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5325 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5326 | // Common case: no conversion required. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5327 | if (LHSType == RHSType) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5328 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5329 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5330 | } |
| 5331 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5332 | // If the left-hand side is a reference type, then we are in a |
| 5333 | // (rare!) case where we've allowed the use of references in C, |
| 5334 | // e.g., as a parameter type in a built-in function. In this case, |
| 5335 | // just make sure that the type referenced is compatible with the |
| 5336 | // right-hand side type. The caller is responsible for adjusting |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5337 | // LHSType so that the resulting expression does not have reference |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5338 | // type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5339 | if (const ReferenceType *LHSTypeRef = LHSType->getAs<ReferenceType>()) { |
| 5340 | if (Context.typesAreCompatible(LHSTypeRef->getPointeeType(), RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5341 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5342 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5343 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5344 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5345 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5346 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5347 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5348 | // to the same ExtVector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5349 | if (LHSType->isExtVectorType()) { |
| 5350 | if (RHSType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5351 | return Incompatible; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5352 | if (RHSType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5353 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5354 | // element type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5355 | QualType elType = cast<ExtVectorType>(LHSType)->getElementType(); |
| 5356 | if (elType != RHSType) { |
| 5357 | Kind = PrepareScalarCast(*this, RHS, elType); |
| 5358 | RHS = ImpCastExprToType(RHS.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5359 | } |
| 5360 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5361 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5362 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5363 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5364 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5365 | // Conversions to or from vector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5366 | if (LHSType->isVectorType() || RHSType->isVectorType()) { |
| 5367 | if (LHSType->isVectorType() && RHSType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5368 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5369 | // vector type and vice versa |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5370 | if (Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5371 | Kind = CK_BitCast; |
| 5372 | return Compatible; |
| 5373 | } |
| 5374 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5375 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5376 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5377 | // no bits are changed but the result type is different. |
| 5378 | if (getLangOptions().LaxVectorConversions && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5379 | (Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5380 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5381 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5382 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5383 | } |
| 5384 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5385 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5386 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5387 | // Arithmetic conversions. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5388 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType() && |
| 5389 | !(getLangOptions().CPlusPlus && LHSType->isEnumeralType())) { |
| 5390 | Kind = PrepareScalarCast(*this, RHS, LHSType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5391 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5392 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5393 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5394 | // Conversions to normal pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5395 | if (const PointerType *LHSPointer = dyn_cast<PointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5396 | // U* -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5397 | if (isa<PointerType>(RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5398 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5399 | return checkPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5400 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5401 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5402 | // int -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5403 | if (RHSType->isIntegerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5404 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 5405 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5406 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5407 | |
| 5408 | // C pointers are not compatible with ObjC object pointers, |
| 5409 | // with two exceptions: |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5410 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5411 | // - conversions to void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5412 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5413 | Kind = CK_BitCast; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5414 | return Compatible; |
| 5415 | } |
| 5416 | |
| 5417 | // - conversions from 'Class' to the redefinition type |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5418 | if (RHSType->isObjCClassType() && |
| 5419 | Context.hasSameType(LHSType, |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5420 | Context.getObjCClassRedefinitionType())) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5421 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5422 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5423 | } |
Fariborz Jahanian | 71ac1e0 | 2011-09-19 18:06:07 +0000 | [diff] [blame] | 5424 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5425 | Kind = CK_BitCast; |
| 5426 | return IncompatiblePointer; |
| 5427 | } |
| 5428 | |
| 5429 | // U^ -> void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5430 | if (RHSType->getAs<BlockPointerType>()) { |
| 5431 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5432 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5433 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5434 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5435 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5436 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5437 | return Incompatible; |
| 5438 | } |
| 5439 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5440 | // Conversions to block pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5441 | if (isa<BlockPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5442 | // U^ -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5443 | if (RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5444 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5445 | return checkBlockPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | // int or null -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5449 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5450 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5451 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5454 | // id -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5455 | if (getLangOptions().ObjC1 && RHSType->isObjCIdType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5456 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5457 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5458 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5459 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5460 | // void* -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5461 | if (const PointerType *RHSPT = RHSType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5462 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 5463 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5464 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5465 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5466 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5467 | return Incompatible; |
| 5468 | } |
| 5469 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5470 | // Conversions to Objective-C pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5471 | if (isa<ObjCObjectPointerType>(LHSType)) { |
Fariborz Jahanian | 71ac1e0 | 2011-09-19 18:06:07 +0000 | [diff] [blame] | 5472 | QualType RHSQT = ConvertObjCSelfToClassRootType(*this, RHS.get()); |
| 5473 | if (!RHSQT.isNull()) |
| 5474 | RHSType = RHSQT; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5475 | // A* -> B* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5476 | if (RHSType->isObjCObjectPointerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5477 | Kind = CK_BitCast; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5478 | Sema::AssignConvertType result = |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5479 | checkObjCPointerTypesForAssignment(*this, LHSType, RHSType); |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5480 | if (getLangOptions().ObjCAutoRefCount && |
| 5481 | result == Compatible && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5482 | !CheckObjCARCUnavailableWeakConversion(OrigLHSType, RHSType)) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5483 | result = IncompatibleObjCWeakRef; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5484 | return result; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5485 | } |
| 5486 | |
| 5487 | // int or null -> A* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5488 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5489 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5490 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5491 | } |
| 5492 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5493 | // In general, C pointers are not compatible with ObjC object pointers, |
| 5494 | // with two exceptions: |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5495 | if (isa<PointerType>(RHSType)) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5496 | Kind = CK_CPointerToObjCPointerCast; |
| 5497 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5498 | // - conversions from 'void*' |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5499 | if (RHSType->isVoidPointerType()) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5500 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | // - conversions to 'Class' from its redefinition type |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5504 | if (LHSType->isObjCClassType() && |
| 5505 | Context.hasSameType(RHSType, |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5506 | Context.getObjCClassRedefinitionType())) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5507 | return Compatible; |
| 5508 | } |
| 5509 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5510 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5511 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5512 | |
| 5513 | // T^ -> A* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5514 | if (RHSType->isBlockPointerType()) { |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 5515 | maybeExtendBlockObject(*this, RHS); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5516 | Kind = CK_BlockPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5517 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5518 | } |
| 5519 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5520 | return Incompatible; |
| 5521 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5522 | |
| 5523 | // Conversions from pointers that are not covered by the above. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5524 | if (isa<PointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5525 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5526 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5527 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5528 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5529 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5530 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5531 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5532 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5533 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5534 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5535 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5536 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5537 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5538 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5539 | |
| 5540 | // Conversions from Objective-C pointers that are not covered by the above. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5541 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5542 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5543 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5544 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5545 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5546 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5547 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5548 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5549 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5550 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5551 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5552 | } |
| 5553 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5554 | return Incompatible; |
| 5555 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5556 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5557 | // struct A -> struct B |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5558 | if (isa<TagType>(LHSType) && isa<TagType>(RHSType)) { |
| 5559 | if (Context.typesAreCompatible(LHSType, RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5560 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5561 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5562 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5563 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5564 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5565 | return Incompatible; |
| 5566 | } |
| 5567 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5568 | /// \brief Constructs a transparent union from an expression that is |
| 5569 | /// used to initialize the transparent union. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5570 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, |
| 5571 | ExprResult &EResult, QualType UnionType, |
| 5572 | FieldDecl *Field) { |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5573 | // Build an initializer list that designates the appropriate member |
| 5574 | // of the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5575 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5576 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 5577 | &E, 1, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5578 | SourceLocation()); |
| 5579 | Initializer->setType(UnionType); |
| 5580 | Initializer->setInitializedFieldInUnion(Field); |
| 5581 | |
| 5582 | // Build a compound literal constructing a value of the transparent |
| 5583 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5584 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5585 | EResult = S.Owned( |
| 5586 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 5587 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | Sema::AssignConvertType |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5591 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5592 | ExprResult &RHS) { |
| 5593 | QualType RHSType = RHS.get()->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5594 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5595 | // If the ArgType is a Union type, we want to handle a potential |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5596 | // transparent_union GCC extension. |
| 5597 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5598 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5599 | return Incompatible; |
| 5600 | |
| 5601 | // The field to initialize within the transparent union. |
| 5602 | RecordDecl *UD = UT->getDecl(); |
| 5603 | FieldDecl *InitField = 0; |
| 5604 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5605 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5606 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5607 | it != itend; ++it) { |
| 5608 | if (it->getType()->isPointerType()) { |
| 5609 | // If the transparent union contains a pointer type, we allow: |
| 5610 | // 1) void pointer |
| 5611 | // 2) null pointer constant |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5612 | if (RHSType->isPointerType()) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5613 | if (RHSType->castAs<PointerType>()->getPointeeType()->isVoidType()) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5614 | RHS = ImpCastExprToType(RHS.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5615 | InitField = *it; |
| 5616 | break; |
| 5617 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5618 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5619 | if (RHS.get()->isNullPointerConstant(Context, |
| 5620 | Expr::NPC_ValueDependentIsNull)) { |
| 5621 | RHS = ImpCastExprToType(RHS.take(), it->getType(), |
| 5622 | CK_NullToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5623 | InitField = *it; |
| 5624 | break; |
| 5625 | } |
| 5626 | } |
| 5627 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5628 | CastKind Kind = CK_Invalid; |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5629 | if (CheckAssignmentConstraints(it->getType(), RHS, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5630 | == Compatible) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5631 | RHS = ImpCastExprToType(RHS.take(), it->getType(), Kind); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5632 | InitField = *it; |
| 5633 | break; |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | if (!InitField) |
| 5638 | return Incompatible; |
| 5639 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5640 | ConstructTransparentUnion(*this, Context, RHS, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5641 | return Compatible; |
| 5642 | } |
| 5643 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5644 | Sema::AssignConvertType |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5645 | Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &RHS) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5646 | if (getLangOptions().CPlusPlus) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5647 | if (!LHSType->isRecordType()) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5648 | // C++ 5.17p3: If the left operand is not of class type, the |
| 5649 | // expression is implicitly converted (C++ 4) to the |
| 5650 | // cv-unqualified type of the left operand. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5651 | ExprResult Res = PerformImplicitConversion(RHS.get(), |
| 5652 | LHSType.getUnqualifiedType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5653 | AA_Assigning); |
| 5654 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5655 | return Incompatible; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5656 | Sema::AssignConvertType result = Compatible; |
| 5657 | if (getLangOptions().ObjCAutoRefCount && |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5658 | !CheckObjCARCUnavailableWeakConversion(LHSType, |
| 5659 | RHS.get()->getType())) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5660 | result = IncompatibleObjCWeakRef; |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5661 | RHS = move(Res); |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5662 | return result; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5663 | } |
| 5664 | |
| 5665 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 5666 | // structures. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5667 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5668 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5669 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 5670 | // a null pointer constant. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5671 | if ((LHSType->isPointerType() || |
| 5672 | LHSType->isObjCObjectPointerType() || |
| 5673 | LHSType->isBlockPointerType()) |
| 5674 | && RHS.get()->isNullPointerConstant(Context, |
| 5675 | Expr::NPC_ValueDependentIsNull)) { |
| 5676 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5677 | return Compatible; |
| 5678 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5679 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5680 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5681 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 5682 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 5683 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5684 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5685 | // Suppress this for references: C++ 8.5.3p5. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5686 | if (!LHSType->isReferenceType()) { |
| 5687 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 5688 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5689 | return Incompatible; |
| 5690 | } |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5691 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5692 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5693 | Sema::AssignConvertType result = |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5694 | CheckAssignmentConstraints(LHSType, RHS, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5695 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5696 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 5697 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5698 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 5699 | // so that we can use references in built-in functions even in C. |
| 5700 | // The getNonReferenceType() call makes sure that the resulting expression |
| 5701 | // does not have reference type. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5702 | if (result != Incompatible && RHS.get()->getType() != LHSType) |
| 5703 | RHS = ImpCastExprToType(RHS.take(), |
| 5704 | LHSType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5705 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5706 | } |
| 5707 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5708 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &LHS, |
| 5709 | ExprResult &RHS) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5710 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5711 | << LHS.get()->getType() << RHS.get()->getType() |
| 5712 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5713 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5714 | } |
| 5715 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5716 | QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5717 | SourceLocation Loc, bool IsCompAssign) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5718 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5719 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5720 | QualType LHSType = |
| 5721 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 5722 | QualType RHSType = |
| 5723 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5724 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5725 | // If the vector types are identical, return. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5726 | if (LHSType == RHSType) |
| 5727 | return LHSType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5728 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5729 | // Handle the case of equivalent AltiVec and GCC vector types |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5730 | if (LHSType->isVectorType() && RHSType->isVectorType() && |
| 5731 | Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
| 5732 | if (LHSType->isExtVectorType()) { |
| 5733 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 5734 | return LHSType; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5735 | } |
| 5736 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5737 | if (!IsCompAssign) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5738 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
| 5739 | return RHSType; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5742 | if (getLangOptions().LaxVectorConversions && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5743 | Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType)) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5744 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5745 | // vectors, the total size only needs to be the same. This is a |
| 5746 | // bitcast; no bits are changed but the result type is different. |
| 5747 | // FIXME: Should we really be allowing this? |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5748 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 5749 | return LHSType; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5750 | } |
| 5751 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5752 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 5753 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 5754 | bool swapped = false; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5755 | if (RHSType->isExtVectorType() && !IsCompAssign) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5756 | swapped = true; |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5757 | std::swap(RHS, LHS); |
| 5758 | std::swap(RHSType, LHSType); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5759 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5760 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5761 | // Handle the case of an ext vector and scalar. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5762 | if (const ExtVectorType *LV = LHSType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5763 | QualType EltTy = LV->getElementType(); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5764 | if (EltTy->isIntegralType(Context) && RHSType->isIntegralType(Context)) { |
| 5765 | int order = Context.getIntegerTypeOrder(EltTy, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5766 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5767 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5768 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5769 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 5770 | if (swapped) std::swap(RHS, LHS); |
| 5771 | return LHSType; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5772 | } |
| 5773 | } |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5774 | if (EltTy->isRealFloatingType() && RHSType->isScalarType() && |
| 5775 | RHSType->isRealFloatingType()) { |
| 5776 | int order = Context.getFloatingTypeOrder(EltTy, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5777 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5778 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5779 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5780 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 5781 | if (swapped) std::swap(RHS, LHS); |
| 5782 | return LHSType; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5783 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5784 | } |
| 5785 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5786 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5787 | // Vectors of different size or scalar and non-ext-vector are errors. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5788 | if (swapped) std::swap(RHS, LHS); |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5789 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5790 | << LHS.get()->getType() << RHS.get()->getType() |
| 5791 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5792 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5793 | } |
| 5794 | |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 5795 | // checkArithmeticNull - Detect when a NULL constant is used improperly in an |
| 5796 | // expression. These are mainly cases where the null pointer is used as an |
| 5797 | // integer instead of a pointer. |
| 5798 | static void checkArithmeticNull(Sema &S, ExprResult &LHS, ExprResult &RHS, |
| 5799 | SourceLocation Loc, bool IsCompare) { |
| 5800 | // The canonical way to check for a GNU null is with isNullPointerConstant, |
| 5801 | // but we use a bit of a hack here for speed; this is a relatively |
| 5802 | // hot path, and isNullPointerConstant is slow. |
| 5803 | bool LHSNull = isa<GNUNullExpr>(LHS.get()->IgnoreParenImpCasts()); |
| 5804 | bool RHSNull = isa<GNUNullExpr>(RHS.get()->IgnoreParenImpCasts()); |
| 5805 | |
| 5806 | QualType NonNullType = LHSNull ? RHS.get()->getType() : LHS.get()->getType(); |
| 5807 | |
| 5808 | // Avoid analyzing cases where the result will either be invalid (and |
| 5809 | // diagnosed as such) or entirely valid and not something to warn about. |
| 5810 | if ((!LHSNull && !RHSNull) || NonNullType->isBlockPointerType() || |
| 5811 | NonNullType->isMemberPointerType() || NonNullType->isFunctionType()) |
| 5812 | return; |
| 5813 | |
| 5814 | // Comparison operations would not make sense with a null pointer no matter |
| 5815 | // what the other expression is. |
| 5816 | if (!IsCompare) { |
| 5817 | S.Diag(Loc, diag::warn_null_in_arithmetic_operation) |
| 5818 | << (LHSNull ? LHS.get()->getSourceRange() : SourceRange()) |
| 5819 | << (RHSNull ? RHS.get()->getSourceRange() : SourceRange()); |
| 5820 | return; |
| 5821 | } |
| 5822 | |
| 5823 | // The rest of the operations only make sense with a null pointer |
| 5824 | // if the other expression is a pointer. |
| 5825 | if (LHSNull == RHSNull || NonNullType->isAnyPointerType() || |
| 5826 | NonNullType->canDecayToPointerType()) |
| 5827 | return; |
| 5828 | |
| 5829 | S.Diag(Loc, diag::warn_null_in_comparison_operation) |
| 5830 | << LHSNull /* LHS is NULL */ << NonNullType |
| 5831 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5832 | } |
| 5833 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5834 | QualType Sema::CheckMultiplyDivideOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5835 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5836 | bool IsCompAssign, bool IsDiv) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 5837 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 5838 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5839 | if (LHS.get()->getType()->isVectorType() || |
| 5840 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5841 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5842 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5843 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5844 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5845 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5846 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5847 | if (!LHS.get()->getType()->isArithmeticType() || |
| 5848 | !RHS.get()->getType()->isArithmeticType()) |
| 5849 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5850 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5851 | // Check for division by zero. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5852 | if (IsDiv && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5853 | RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5854 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5855 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_division_by_zero) |
| 5856 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5857 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5858 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5859 | } |
| 5860 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5861 | QualType Sema::CheckRemainderOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5862 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 5863 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 5864 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5865 | if (LHS.get()->getType()->isVectorType() || |
| 5866 | RHS.get()->getType()->isVectorType()) { |
| 5867 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 5868 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5869 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5870 | return InvalidOperands(Loc, LHS, RHS); |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 5871 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5872 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5873 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5874 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5875 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5876 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5877 | if (!LHS.get()->getType()->isIntegerType() || |
| 5878 | !RHS.get()->getType()->isIntegerType()) |
| 5879 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5880 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5881 | // Check for remainder by zero. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5882 | if (RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5883 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5884 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_remainder_by_zero) |
| 5885 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5886 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5887 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5888 | } |
| 5889 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5890 | /// \brief Diagnose invalid arithmetic on two void pointers. |
| 5891 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5892 | Expr *LHSExpr, Expr *RHSExpr) { |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5893 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5894 | ? diag::err_typecheck_pointer_arith_void_type |
| 5895 | : diag::ext_gnu_void_ptr) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5896 | << 1 /* two pointers */ << LHSExpr->getSourceRange() |
| 5897 | << RHSExpr->getSourceRange(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
| 5900 | /// \brief Diagnose invalid arithmetic on a void pointer. |
| 5901 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, |
| 5902 | Expr *Pointer) { |
| 5903 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5904 | ? diag::err_typecheck_pointer_arith_void_type |
| 5905 | : diag::ext_gnu_void_ptr) |
| 5906 | << 0 /* one pointer */ << Pointer->getSourceRange(); |
| 5907 | } |
| 5908 | |
| 5909 | /// \brief Diagnose invalid arithmetic on two function pointers. |
| 5910 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, |
| 5911 | Expr *LHS, Expr *RHS) { |
| 5912 | assert(LHS->getType()->isAnyPointerType()); |
| 5913 | assert(RHS->getType()->isAnyPointerType()); |
| 5914 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5915 | ? diag::err_typecheck_pointer_arith_function_type |
| 5916 | : diag::ext_gnu_ptr_func_arith) |
| 5917 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() |
| 5918 | // We only show the second type if it differs from the first. |
| 5919 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), |
| 5920 | RHS->getType()) |
| 5921 | << RHS->getType()->getPointeeType() |
| 5922 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5923 | } |
| 5924 | |
| 5925 | /// \brief Diagnose invalid arithmetic on a function pointer. |
| 5926 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, |
| 5927 | Expr *Pointer) { |
| 5928 | assert(Pointer->getType()->isAnyPointerType()); |
| 5929 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5930 | ? diag::err_typecheck_pointer_arith_function_type |
| 5931 | : diag::ext_gnu_ptr_func_arith) |
| 5932 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() |
| 5933 | << 0 /* one pointer, so only one type */ |
| 5934 | << Pointer->getSourceRange(); |
| 5935 | } |
| 5936 | |
Richard Trieu | d9f1934 | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 5937 | /// \brief Emit error if Operand is incomplete pointer type |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5938 | /// |
| 5939 | /// \returns True if pointer has incomplete type |
| 5940 | static bool checkArithmeticIncompletePointerType(Sema &S, SourceLocation Loc, |
| 5941 | Expr *Operand) { |
| 5942 | if ((Operand->getType()->isPointerType() && |
| 5943 | !Operand->getType()->isDependentType()) || |
| 5944 | Operand->getType()->isObjCObjectPointerType()) { |
| 5945 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5946 | if (S.RequireCompleteType( |
| 5947 | Loc, PointeeTy, |
| 5948 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 5949 | << PointeeTy << Operand->getSourceRange())) |
| 5950 | return true; |
| 5951 | } |
| 5952 | return false; |
| 5953 | } |
| 5954 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5955 | /// \brief Check the validity of an arithmetic pointer operand. |
| 5956 | /// |
| 5957 | /// If the operand has pointer type, this code will check for pointer types |
| 5958 | /// which are invalid in arithmetic operations. These will be diagnosed |
| 5959 | /// appropriately, including whether or not the use is supported as an |
| 5960 | /// extension. |
| 5961 | /// |
| 5962 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5963 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, |
| 5964 | Expr *Operand) { |
| 5965 | if (!Operand->getType()->isAnyPointerType()) return true; |
| 5966 | |
| 5967 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5968 | if (PointeeTy->isVoidType()) { |
| 5969 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); |
| 5970 | return !S.getLangOptions().CPlusPlus; |
| 5971 | } |
| 5972 | if (PointeeTy->isFunctionType()) { |
| 5973 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); |
| 5974 | return !S.getLangOptions().CPlusPlus; |
| 5975 | } |
| 5976 | |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5977 | if (checkArithmeticIncompletePointerType(S, Loc, Operand)) return false; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5978 | |
| 5979 | return true; |
| 5980 | } |
| 5981 | |
| 5982 | /// \brief Check the validity of a binary arithmetic operation w.r.t. pointer |
| 5983 | /// operands. |
| 5984 | /// |
| 5985 | /// This routine will diagnose any invalid arithmetic on pointer operands much |
| 5986 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic |
| 5987 | /// for emitting a single diagnostic even for operations where both LHS and RHS |
| 5988 | /// are (potentially problematic) pointers. |
| 5989 | /// |
| 5990 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5991 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5992 | Expr *LHSExpr, Expr *RHSExpr) { |
| 5993 | bool isLHSPointer = LHSExpr->getType()->isAnyPointerType(); |
| 5994 | bool isRHSPointer = RHSExpr->getType()->isAnyPointerType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5995 | if (!isLHSPointer && !isRHSPointer) return true; |
| 5996 | |
| 5997 | QualType LHSPointeeTy, RHSPointeeTy; |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5998 | if (isLHSPointer) LHSPointeeTy = LHSExpr->getType()->getPointeeType(); |
| 5999 | if (isRHSPointer) RHSPointeeTy = RHSExpr->getType()->getPointeeType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6000 | |
| 6001 | // Check for arithmetic on pointers to incomplete types. |
| 6002 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); |
| 6003 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); |
| 6004 | if (isLHSVoidPtr || isRHSVoidPtr) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6005 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHSExpr); |
| 6006 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHSExpr); |
| 6007 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6008 | |
| 6009 | return !S.getLangOptions().CPlusPlus; |
| 6010 | } |
| 6011 | |
| 6012 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); |
| 6013 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); |
| 6014 | if (isLHSFuncPtr || isRHSFuncPtr) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6015 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHSExpr); |
| 6016 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, |
| 6017 | RHSExpr); |
| 6018 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6019 | |
| 6020 | return !S.getLangOptions().CPlusPlus; |
| 6021 | } |
| 6022 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6023 | if (checkArithmeticIncompletePointerType(S, Loc, LHSExpr)) return false; |
| 6024 | if (checkArithmeticIncompletePointerType(S, Loc, RHSExpr)) return false; |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6025 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6026 | return true; |
| 6027 | } |
| 6028 | |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6029 | /// \brief Check bad cases where we step over interface counts. |
| 6030 | static bool checkArithmethicPointerOnNonFragileABI(Sema &S, |
| 6031 | SourceLocation OpLoc, |
| 6032 | Expr *Op) { |
| 6033 | assert(Op->getType()->isAnyPointerType()); |
| 6034 | QualType PointeeTy = Op->getType()->getPointeeType(); |
| 6035 | if (!PointeeTy->isObjCObjectType() || !S.LangOpts.ObjCNonFragileABI) |
| 6036 | return true; |
| 6037 | |
| 6038 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 6039 | << PointeeTy << Op->getSourceRange(); |
| 6040 | return false; |
| 6041 | } |
| 6042 | |
Richard Trieu | d9f1934 | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 6043 | /// \brief Emit error when two pointers are incompatible. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6044 | static void diagnosePointerIncompatibility(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6045 | Expr *LHSExpr, Expr *RHSExpr) { |
| 6046 | assert(LHSExpr->getType()->isAnyPointerType()); |
| 6047 | assert(RHSExpr->getType()->isAnyPointerType()); |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6048 | S.Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6049 | << LHSExpr->getType() << RHSExpr->getType() << LHSExpr->getSourceRange() |
| 6050 | << RHSExpr->getSourceRange(); |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6051 | } |
| 6052 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6053 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6054 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, QualType* CompLHSTy) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6055 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6056 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6057 | if (LHS.get()->getType()->isVectorType() || |
| 6058 | RHS.get()->getType()->isVectorType()) { |
| 6059 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6060 | if (CompLHSTy) *CompLHSTy = compType; |
| 6061 | return compType; |
| 6062 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6063 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6064 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 6065 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6066 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6067 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6068 | // handle the common case first (both operands are arithmetic). |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6069 | if (LHS.get()->getType()->isArithmeticType() && |
| 6070 | RHS.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6071 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6072 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6073 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6074 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6075 | // Put any potential pointer into PExp |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6076 | Expr* PExp = LHS.get(), *IExp = RHS.get(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6077 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6078 | std::swap(PExp, IExp); |
| 6079 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6080 | if (!PExp->getType()->isAnyPointerType()) |
| 6081 | return InvalidOperands(Loc, LHS, RHS); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6082 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6083 | if (!IExp->getType()->isIntegerType()) |
| 6084 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6085 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6086 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) |
| 6087 | return QualType(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6088 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6089 | // Diagnose bad cases where we step over interface counts. |
| 6090 | if (!checkArithmethicPointerOnNonFragileABI(*this, Loc, PExp)) |
| 6091 | return QualType(); |
| 6092 | |
| 6093 | // Check array bounds for pointer arithemtic |
| 6094 | CheckArrayAccess(PExp, IExp); |
| 6095 | |
| 6096 | if (CompLHSTy) { |
| 6097 | QualType LHSTy = Context.isPromotableBitField(LHS.get()); |
| 6098 | if (LHSTy.isNull()) { |
| 6099 | LHSTy = LHS.get()->getType(); |
| 6100 | if (LHSTy->isPromotableIntegerType()) |
| 6101 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6102 | } |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6103 | *CompLHSTy = LHSTy; |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6104 | } |
| 6105 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6106 | return PExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6107 | } |
| 6108 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6109 | // C99 6.5.6 |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6110 | QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6111 | SourceLocation Loc, |
| 6112 | QualType* CompLHSTy) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6113 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6114 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6115 | if (LHS.get()->getType()->isVectorType() || |
| 6116 | RHS.get()->getType()->isVectorType()) { |
| 6117 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6118 | if (CompLHSTy) *CompLHSTy = compType; |
| 6119 | return compType; |
| 6120 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6121 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6122 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 6123 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6124 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6125 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6126 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6127 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6128 | // Handle the common case first (both operands are arithmetic). |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6129 | if (LHS.get()->getType()->isArithmeticType() && |
| 6130 | RHS.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6131 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6132 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6133 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6134 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6135 | // Either ptr - int or ptr - ptr. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6136 | if (LHS.get()->getType()->isAnyPointerType()) { |
| 6137 | QualType lpointee = LHS.get()->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6138 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6139 | // Diagnose bad cases where we step over interface counts. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6140 | if (!checkArithmethicPointerOnNonFragileABI(*this, Loc, LHS.get())) |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6141 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6142 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6143 | // The result type of a pointer-int computation is the pointer type. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6144 | if (RHS.get()->getType()->isIntegerType()) { |
| 6145 | if (!checkArithmeticOpPointerOperand(*this, Loc, LHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6146 | return QualType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6147 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6148 | Expr *IExpr = RHS.get()->IgnoreParenCasts(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6149 | UnaryOperator negRex(IExpr, UO_Minus, IExpr->getType(), VK_RValue, |
| 6150 | OK_Ordinary, IExpr->getExprLoc()); |
| 6151 | // Check array bounds for pointer arithemtic |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6152 | CheckArrayAccess(LHS.get()->IgnoreParenCasts(), &negRex); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6153 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6154 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
| 6155 | return LHS.get()->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6156 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6157 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6158 | // Handle pointer-pointer subtractions. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6159 | if (const PointerType *RHSPTy |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6160 | = RHS.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6161 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6162 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6163 | if (getLangOptions().CPlusPlus) { |
| 6164 | // Pointee types must be the same: C++ [expr.add] |
| 6165 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6166 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6167 | } |
| 6168 | } else { |
| 6169 | // Pointee types must be compatible C99 6.5.6p3 |
| 6170 | if (!Context.typesAreCompatible( |
| 6171 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6172 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6173 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6174 | return QualType(); |
| 6175 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6176 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6177 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6178 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6179 | LHS.get(), RHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6180 | return QualType(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6181 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6182 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6183 | return Context.getPointerDiffType(); |
| 6184 | } |
| 6185 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6186 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6187 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6188 | } |
| 6189 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6190 | static bool isScopedEnumerationType(QualType T) { |
| 6191 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6192 | return ET->getDecl()->isScoped(); |
| 6193 | return false; |
| 6194 | } |
| 6195 | |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6196 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6197 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6198 | QualType LHSType) { |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6199 | llvm::APSInt Right; |
| 6200 | // Check right/shifter operand |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6201 | if (RHS.get()->isValueDependent() || |
| 6202 | !RHS.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6203 | return; |
| 6204 | |
| 6205 | if (Right.isNegative()) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6206 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6207 | S.PDiag(diag::warn_shift_negative) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6208 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6209 | return; |
| 6210 | } |
| 6211 | llvm::APInt LeftBits(Right.getBitWidth(), |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6212 | S.Context.getTypeSize(LHS.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6213 | if (Right.uge(LeftBits)) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6214 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6215 | S.PDiag(diag::warn_shift_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6216 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6217 | return; |
| 6218 | } |
| 6219 | if (Opc != BO_Shl) |
| 6220 | return; |
| 6221 | |
| 6222 | // When left shifting an ICE which is signed, we can check for overflow which |
| 6223 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 6224 | // integers have defined behavior modulo one more than the maximum value |
| 6225 | // representable in the result type, so never warn for those. |
| 6226 | llvm::APSInt Left; |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6227 | if (LHS.get()->isValueDependent() || |
| 6228 | !LHS.get()->isIntegerConstantExpr(Left, S.Context) || |
| 6229 | LHSType->hasUnsignedIntegerRepresentation()) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6230 | return; |
| 6231 | llvm::APInt ResultBits = |
| 6232 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 6233 | if (LeftBits.uge(ResultBits)) |
| 6234 | return; |
| 6235 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 6236 | Result = Result.shl(Right); |
| 6237 | |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6238 | // Print the bit representation of the signed integer as an unsigned |
| 6239 | // hexadecimal number. |
| 6240 | llvm::SmallString<40> HexResult; |
| 6241 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 6242 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6243 | // If we are only missing a sign bit, this is less likely to result in actual |
| 6244 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 6245 | // expected value. Thus we place this behind a different warning that can be |
| 6246 | // turned off separately if needed. |
| 6247 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6248 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6249 | << HexResult.str() << LHSType |
| 6250 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6251 | return; |
| 6252 | } |
| 6253 | |
| 6254 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6255 | << HexResult.str() << Result.getMinSignedBits() << LHSType |
| 6256 | << Left.getBitWidth() << LHS.get()->getSourceRange() |
| 6257 | << RHS.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6258 | } |
| 6259 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6260 | // C99 6.5.7 |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6261 | QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6262 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6263 | bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6264 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6265 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6266 | // C99 6.5.7p2: Each of the operands shall have integer type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6267 | if (!LHS.get()->getType()->hasIntegerRepresentation() || |
| 6268 | !RHS.get()->getType()->hasIntegerRepresentation()) |
| 6269 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6270 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6271 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6272 | // hasIntegerRepresentation() above instead of this. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6273 | if (isScopedEnumerationType(LHS.get()->getType()) || |
| 6274 | isScopedEnumerationType(RHS.get()->getType())) { |
| 6275 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6276 | } |
| 6277 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6278 | // Vector shifts promote their scalar inputs to vector type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6279 | if (LHS.get()->getType()->isVectorType() || |
| 6280 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6281 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6282 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6283 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6284 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6285 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6286 | // For the LHS, do usual unary conversions, but then reset them away |
| 6287 | // if this is a compound assignment. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6288 | ExprResult OldLHS = LHS; |
| 6289 | LHS = UsualUnaryConversions(LHS.take()); |
| 6290 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6291 | return QualType(); |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6292 | QualType LHSType = LHS.get()->getType(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6293 | if (IsCompAssign) LHS = OldLHS; |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6294 | |
| 6295 | // The RHS is simpler. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6296 | RHS = UsualUnaryConversions(RHS.take()); |
| 6297 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6298 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6299 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6300 | // Sanity-check shift operands |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6301 | DiagnoseBadShiftValues(*this, LHS, RHS, Loc, Opc, LHSType); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6302 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6303 | // "The type of the result is that of the promoted left operand." |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6304 | return LHSType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6305 | } |
| 6306 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6307 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6308 | if (DeclContext *DC = D->getDeclContext()) { |
| 6309 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6310 | return true; |
| 6311 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6312 | return FD->isFunctionTemplateSpecialization(); |
| 6313 | } |
| 6314 | return false; |
| 6315 | } |
| 6316 | |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6317 | /// If two different enums are compared, raise a warning. |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6318 | static void checkEnumComparison(Sema &S, SourceLocation Loc, ExprResult &LHS, |
| 6319 | ExprResult &RHS) { |
| 6320 | QualType LHSStrippedType = LHS.get()->IgnoreParenImpCasts()->getType(); |
| 6321 | QualType RHSStrippedType = RHS.get()->IgnoreParenImpCasts()->getType(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6322 | |
| 6323 | const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>(); |
| 6324 | if (!LHSEnumType) |
| 6325 | return; |
| 6326 | const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>(); |
| 6327 | if (!RHSEnumType) |
| 6328 | return; |
| 6329 | |
| 6330 | // Ignore anonymous enums. |
| 6331 | if (!LHSEnumType->getDecl()->getIdentifier()) |
| 6332 | return; |
| 6333 | if (!RHSEnumType->getDecl()->getIdentifier()) |
| 6334 | return; |
| 6335 | |
| 6336 | if (S.Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) |
| 6337 | return; |
| 6338 | |
| 6339 | S.Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6340 | << LHSStrippedType << RHSStrippedType |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6341 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6344 | /// \brief Diagnose bad pointer comparisons. |
| 6345 | static void diagnoseDistinctPointerComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6346 | ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6347 | bool IsError) { |
| 6348 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_distinct_pointers |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6349 | : diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6350 | << LHS.get()->getType() << RHS.get()->getType() |
| 6351 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6352 | } |
| 6353 | |
| 6354 | /// \brief Returns false if the pointers are converted to a composite type, |
| 6355 | /// true otherwise. |
| 6356 | static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6357 | ExprResult &LHS, ExprResult &RHS) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6358 | // C++ [expr.rel]p2: |
| 6359 | // [...] Pointer conversions (4.10) and qualification |
| 6360 | // conversions (4.4) are performed on pointer operands (or on |
| 6361 | // a pointer operand and a null pointer constant) to bring |
| 6362 | // them to their composite pointer type. [...] |
| 6363 | // |
| 6364 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
| 6365 | // comparisons of pointers. |
| 6366 | |
| 6367 | // C++ [expr.eq]p2: |
| 6368 | // In addition, pointers to members can be compared, or a pointer to |
| 6369 | // member and a null pointer constant. Pointer to member conversions |
| 6370 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6371 | // them to a common type. If one operand is a null pointer constant, |
| 6372 | // the common type is the type of the other operand. Otherwise, the |
| 6373 | // common type is a pointer to member type similar (4.4) to the type |
| 6374 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6375 | // that is the union of the cv-qualification signatures of the operand |
| 6376 | // types. |
| 6377 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6378 | QualType LHSType = LHS.get()->getType(); |
| 6379 | QualType RHSType = RHS.get()->getType(); |
| 6380 | assert((LHSType->isPointerType() && RHSType->isPointerType()) || |
| 6381 | (LHSType->isMemberPointerType() && RHSType->isMemberPointerType())); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6382 | |
| 6383 | bool NonStandardCompositeType = false; |
Richard Trieu | 43dff1b | 2011-09-02 21:44:27 +0000 | [diff] [blame] | 6384 | bool *BoolPtr = S.isSFINAEContext() ? 0 : &NonStandardCompositeType; |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6385 | QualType T = S.FindCompositePointerType(Loc, LHS, RHS, BoolPtr); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6386 | if (T.isNull()) { |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6387 | diagnoseDistinctPointerComparison(S, Loc, LHS, RHS, /*isError*/true); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6388 | return true; |
| 6389 | } |
| 6390 | |
| 6391 | if (NonStandardCompositeType) |
| 6392 | S.Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6393 | << LHSType << RHSType << T << LHS.get()->getSourceRange() |
| 6394 | << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6395 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6396 | LHS = S.ImpCastExprToType(LHS.take(), T, CK_BitCast); |
| 6397 | RHS = S.ImpCastExprToType(RHS.take(), T, CK_BitCast); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6398 | return false; |
| 6399 | } |
| 6400 | |
| 6401 | static void diagnoseFunctionPointerToVoidComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6402 | ExprResult &LHS, |
| 6403 | ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6404 | bool IsError) { |
| 6405 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_fptr_to_void |
| 6406 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6407 | << LHS.get()->getType() << RHS.get()->getType() |
| 6408 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6409 | } |
| 6410 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6411 | // C99 6.5.8, C++ [expr.rel] |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6412 | QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6413 | SourceLocation Loc, unsigned OpaqueOpc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6414 | bool IsRelational) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6415 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/true); |
| 6416 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6417 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6418 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6419 | // Handle vector comparisons separately. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6420 | if (LHS.get()->getType()->isVectorType() || |
| 6421 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6422 | return CheckVectorCompareOperands(LHS, RHS, Loc, IsRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6423 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6424 | QualType LHSType = LHS.get()->getType(); |
| 6425 | QualType RHSType = RHS.get()->getType(); |
Benjamin Kramer | fec0959 | 2011-09-03 08:46:20 +0000 | [diff] [blame] | 6426 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6427 | Expr *LHSStripped = LHS.get()->IgnoreParenImpCasts(); |
| 6428 | Expr *RHSStripped = RHS.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6429 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6430 | checkEnumComparison(*this, Loc, LHS, RHS); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6431 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6432 | if (!LHSType->hasFloatingRepresentation() && |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6433 | !(LHSType->isBlockPointerType() && IsRelational) && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6434 | !LHS.get()->getLocStart().isMacroID() && |
| 6435 | !RHS.get()->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6436 | // For non-floating point types, check for self-comparisons of the form |
| 6437 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6438 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6439 | // |
| 6440 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6441 | // expansion. Also don't warn about comparisons which are only self |
| 6442 | // comparisons within a template specialization. The warnings should catch |
| 6443 | // obvious cases in the definition of the template anyways. The idea is to |
| 6444 | // warn when the typed comparison operator will always evaluate to the same |
| 6445 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6446 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6447 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6448 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6449 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6450 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6451 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6452 | << (Opc == BO_EQ |
| 6453 | || Opc == BO_LE |
| 6454 | || Opc == BO_GE)); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6455 | } else if (LHSType->isArrayType() && RHSType->isArrayType() && |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6456 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6457 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6458 | // what is it always going to eval to? |
| 6459 | char always_evals_to; |
| 6460 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6461 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6462 | always_evals_to = 0; // false |
| 6463 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6464 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6465 | always_evals_to = 1; // true |
| 6466 | break; |
| 6467 | default: |
| 6468 | // best we can say is 'a constant' |
| 6469 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6470 | break; |
| 6471 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6472 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6473 | << 1 // array |
| 6474 | << always_evals_to); |
| 6475 | } |
| 6476 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6477 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6478 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6479 | if (isa<CastExpr>(LHSStripped)) |
| 6480 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6481 | if (isa<CastExpr>(RHSStripped)) |
| 6482 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6483 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6484 | // Warn about comparisons against a string constant (unless the other |
| 6485 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6486 | Expr *literalString = 0; |
| 6487 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6488 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6489 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6490 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6491 | literalString = LHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6492 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6493 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 6494 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6495 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6496 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6497 | literalString = RHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6498 | literalStringStripped = RHSStripped; |
| 6499 | } |
| 6500 | |
| 6501 | if (literalString) { |
| 6502 | std::string resultComparison; |
| 6503 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6504 | case BO_LT: resultComparison = ") < 0"; break; |
| 6505 | case BO_GT: resultComparison = ") > 0"; break; |
| 6506 | case BO_LE: resultComparison = ") <= 0"; break; |
| 6507 | case BO_GE: resultComparison = ") >= 0"; break; |
| 6508 | case BO_EQ: resultComparison = ") == 0"; break; |
| 6509 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6510 | default: assert(false && "Invalid comparison operator"); |
| 6511 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6512 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6513 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 6514 | PDiag(diag::warn_stringcompare) |
| 6515 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 6516 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6517 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 6518 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6519 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6520 | // C99 6.5.8p3 / C99 6.5.9p4 |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6521 | if (LHS.get()->getType()->isArithmeticType() && |
| 6522 | RHS.get()->getType()->isArithmeticType()) { |
| 6523 | UsualArithmeticConversions(LHS, RHS); |
| 6524 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6525 | return QualType(); |
| 6526 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6527 | else { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6528 | LHS = UsualUnaryConversions(LHS.take()); |
| 6529 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6530 | return QualType(); |
| 6531 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6532 | RHS = UsualUnaryConversions(RHS.take()); |
| 6533 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6534 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6535 | } |
| 6536 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6537 | LHSType = LHS.get()->getType(); |
| 6538 | RHSType = RHS.get()->getType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6539 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6540 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 6541 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6542 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6543 | if (IsRelational) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6544 | if (LHSType->isRealType() && RHSType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6545 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6546 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 6547 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6548 | if (LHSType->hasFloatingRepresentation()) |
| 6549 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6550 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6551 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6552 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6553 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6554 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6555 | bool LHSIsNull = LHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6556 | Expr::NPC_ValueDependentIsNull); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6557 | bool RHSIsNull = RHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6558 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6559 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6560 | // All of the following pointer-related warnings are GCC extensions, except |
| 6561 | // when handling null pointer constants. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6562 | if (LHSType->isPointerType() && RHSType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6563 | QualType LCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6564 | LHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6565 | QualType RCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6566 | RHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6567 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6568 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6569 | if (LCanPointeeTy == RCanPointeeTy) |
| 6570 | return ResultTy; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6571 | if (!IsRelational && |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6572 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6573 | // Valid unless comparison between non-null pointer and function pointer |
| 6574 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6575 | // In a SFINAE context, we treat this as a hard error to maintain |
| 6576 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6577 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6578 | && !LHSIsNull && !RHSIsNull) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6579 | diagnoseFunctionPointerToVoidComparison( |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6580 | *this, Loc, LHS, RHS, /*isError*/ isSFINAEContext()); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6581 | |
| 6582 | if (isSFINAEContext()) |
| 6583 | return QualType(); |
| 6584 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6585 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6586 | return ResultTy; |
| 6587 | } |
| 6588 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6589 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6590 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6591 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6592 | else |
| 6593 | return ResultTy; |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6594 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6595 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 6596 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 6597 | RCanPointeeTy.getUnqualifiedType())) { |
| 6598 | // Valid unless a relational comparison of function pointers |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6599 | if (IsRelational && LCanPointeeTy->isFunctionType()) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6600 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6601 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6602 | << RHS.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6603 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6604 | } else if (!IsRelational && |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6605 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6606 | // Valid unless comparison between non-null pointer and function pointer |
| 6607 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6608 | && !LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6609 | diagnoseFunctionPointerToVoidComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6610 | /*isError*/false); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6611 | } else { |
| 6612 | // Invalid |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6613 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6614 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6615 | if (LCanPointeeTy != RCanPointeeTy) { |
| 6616 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6617 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6618 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6619 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6620 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6621 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 6622 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6623 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6624 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6625 | // Comparison of nullptr_t with itself. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6626 | if (LHSType->isNullPtrType() && RHSType->isNullPtrType()) |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6627 | return ResultTy; |
| 6628 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6629 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6630 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6631 | if (RHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6632 | ((LHSType->isAnyPointerType() || LHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6633 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6634 | (LHSType->isMemberPointerType() || LHSType->isBlockPointerType())))) { |
| 6635 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 6636 | LHSType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6637 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6638 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6639 | return ResultTy; |
| 6640 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6641 | if (LHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6642 | ((RHSType->isAnyPointerType() || RHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6643 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6644 | (RHSType->isMemberPointerType() || RHSType->isBlockPointerType())))) { |
| 6645 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 6646 | RHSType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6647 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6648 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6649 | return ResultTy; |
| 6650 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6651 | |
| 6652 | // Comparison of member pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6653 | if (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6654 | LHSType->isMemberPointerType() && RHSType->isMemberPointerType()) { |
| 6655 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6656 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6657 | else |
| 6658 | return ResultTy; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6659 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6660 | |
| 6661 | // Handle scoped enumeration types specifically, since they don't promote |
| 6662 | // to integers. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6663 | if (LHS.get()->getType()->isEnumeralType() && |
| 6664 | Context.hasSameUnqualifiedType(LHS.get()->getType(), |
| 6665 | RHS.get()->getType())) |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6666 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6667 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6668 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6669 | // Handle block pointer types. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6670 | if (!IsRelational && LHSType->isBlockPointerType() && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6671 | RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6672 | QualType lpointee = LHSType->castAs<BlockPointerType>()->getPointeeType(); |
| 6673 | QualType rpointee = RHSType->castAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6674 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6675 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 6676 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6677 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6678 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6679 | << RHS.get()->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6680 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6681 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6682 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6683 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6684 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6685 | // Allow block pointers to be compared with null pointer constants. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6686 | if (!IsRelational |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6687 | && ((LHSType->isBlockPointerType() && RHSType->isPointerType()) |
| 6688 | || (LHSType->isPointerType() && RHSType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6689 | if (!LHSIsNull && !RHSIsNull) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6690 | if (!((RHSType->isPointerType() && RHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6691 | ->getPointeeType()->isVoidType()) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6692 | || (LHSType->isPointerType() && LHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6693 | ->getPointeeType()->isVoidType()))) |
| 6694 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6695 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6696 | << RHS.get()->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6697 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6698 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6699 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 6700 | RHSType->isPointerType() ? CK_BitCast |
| 6701 | : CK_AnyPointerToBlockPointerCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6702 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6703 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 6704 | LHSType->isPointerType() ? CK_BitCast |
| 6705 | : CK_AnyPointerToBlockPointerCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6706 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6707 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6708 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6709 | if (LHSType->isObjCObjectPointerType() || |
| 6710 | RHSType->isObjCObjectPointerType()) { |
| 6711 | const PointerType *LPT = LHSType->getAs<PointerType>(); |
| 6712 | const PointerType *RPT = RHSType->getAs<PointerType>(); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6713 | if (LPT || RPT) { |
| 6714 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 6715 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6716 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 6717 | if (!LPtrToVoid && !RPtrToVoid && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6718 | !Context.typesAreCompatible(LHSType, RHSType)) { |
| 6719 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6720 | /*isError*/false); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 6721 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6722 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6723 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 6724 | RPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6725 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6726 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 6727 | LPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6728 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 6729 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6730 | if (LHSType->isObjCObjectPointerType() && |
| 6731 | RHSType->isObjCObjectPointerType()) { |
| 6732 | if (!Context.areComparableObjCPointerTypes(LHSType, RHSType)) |
| 6733 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6734 | /*isError*/false); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6735 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6736 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6737 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6738 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6739 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 6740 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 6741 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6742 | if ((LHSType->isAnyPointerType() && RHSType->isIntegerType()) || |
| 6743 | (LHSType->isIntegerType() && RHSType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6744 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6745 | bool isError = false; |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6746 | if ((LHSIsNull && LHSType->isIntegerType()) || |
| 6747 | (RHSIsNull && RHSType->isIntegerType())) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6748 | if (IsRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6749 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6750 | } else if (IsRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6751 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6752 | else if (getLangOptions().CPlusPlus) { |
| 6753 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 6754 | isError = true; |
| 6755 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6756 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6757 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6758 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6759 | Diag(Loc, DiagID) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6760 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6761 | << RHS.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6762 | if (isError) |
| 6763 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6764 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6765 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6766 | if (LHSType->isIntegerType()) |
| 6767 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6768 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6769 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6770 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6771 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6772 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6773 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6774 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6775 | // Handle block pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6776 | if (!IsRelational && RHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6777 | && LHSType->isBlockPointerType() && RHSType->isIntegerType()) { |
| 6778 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6779 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6780 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6781 | if (!IsRelational && LHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6782 | && LHSType->isIntegerType() && RHSType->isBlockPointerType()) { |
| 6783 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6784 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6785 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6786 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6787 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6788 | } |
| 6789 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6790 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6791 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6792 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 6793 | /// types. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6794 | QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6795 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6796 | bool IsRelational) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6797 | // Check to make sure we're operating on vectors of the same type and width, |
| 6798 | // Allowing one side to be a scalar of element type. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6799 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, /*isCompAssign*/false); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6800 | if (vType.isNull()) |
| 6801 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6802 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6803 | QualType LHSType = LHS.get()->getType(); |
| 6804 | QualType RHSType = RHS.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6805 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6806 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 6807 | // bool for C++, int for C |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 6808 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6809 | return Context.getLogicalOperationType(); |
| 6810 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6811 | // For non-floating point types, check for self-comparisons of the form |
| 6812 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6813 | // often indicate logic errors in the program. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6814 | if (!LHSType->hasFloatingRepresentation()) { |
| 6815 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHS.get()->IgnoreParens())) |
| 6816 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHS.get()->IgnoreParens())) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6817 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6818 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6819 | PDiag(diag::warn_comparison_always) |
| 6820 | << 0 // self- |
| 6821 | << 2 // "a constant" |
| 6822 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6823 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6824 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6825 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6826 | if (!IsRelational && LHSType->hasFloatingRepresentation()) { |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6827 | assert (RHSType->hasFloatingRepresentation()); |
| 6828 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6829 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6830 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6831 | // Return the type for the comparison, which is the same as vector type for |
| 6832 | // integer vectors, or an integer type of identical size and number of |
| 6833 | // elements for floating point vectors. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6834 | if (LHSType->hasIntegerRepresentation()) |
| 6835 | return LHSType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6836 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6837 | const VectorType *VTy = LHSType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6838 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6839 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6840 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 6841 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6842 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 6843 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6844 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6845 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6846 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 6847 | } |
| 6848 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6849 | inline QualType Sema::CheckBitwiseOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6850 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6851 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6852 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6853 | if (LHS.get()->getType()->isVectorType() || |
| 6854 | RHS.get()->getType()->isVectorType()) { |
| 6855 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 6856 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6857 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6858 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6859 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6860 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6861 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6862 | ExprResult LHSResult = Owned(LHS), RHSResult = Owned(RHS); |
| 6863 | QualType compType = UsualArithmeticConversions(LHSResult, RHSResult, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6864 | IsCompAssign); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6865 | if (LHSResult.isInvalid() || RHSResult.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6866 | return QualType(); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6867 | LHS = LHSResult.take(); |
| 6868 | RHS = RHSResult.take(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6869 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6870 | if (LHS.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 6871 | RHS.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6872 | return compType; |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6873 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6874 | } |
| 6875 | |
| 6876 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6877 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6878 | |
| 6879 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 6880 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 6881 | // is a constant. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6882 | if (LHS.get()->getType()->isIntegerType() && |
| 6883 | !LHS.get()->getType()->isBooleanType() && |
| 6884 | RHS.get()->getType()->isIntegerType() && !RHS.get()->isValueDependent() && |
Richard Trieu | e5adf59 | 2011-07-15 00:00:51 +0000 | [diff] [blame] | 6885 | // Don't warn in macros or template instantiations. |
| 6886 | !Loc.isMacroID() && ActiveTemplateInstantiations.empty()) { |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6887 | // If the RHS can be constant folded, and if it constant folds to something |
| 6888 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 6889 | // happened to fold to true/false) then warn. |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6890 | // Parens on the RHS are ignored. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6891 | Expr::EvalResult Result; |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6892 | if (RHS.get()->Evaluate(Result, Context) && !Result.HasSideEffects) |
| 6893 | if ((getLangOptions().Bool && !RHS.get()->getType()->isBooleanType()) || |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6894 | (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) { |
| 6895 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6896 | << RHS.get()->getSourceRange() |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6897 | << (Opc == BO_LAnd ? "&&" : "||"); |
| 6898 | // Suggest replacing the logical operator with the bitwise version |
| 6899 | Diag(Loc, diag::note_logical_instead_of_bitwise_change_operator) |
| 6900 | << (Opc == BO_LAnd ? "&" : "|") |
| 6901 | << FixItHint::CreateReplacement(SourceRange( |
| 6902 | Loc, Lexer::getLocForEndOfToken(Loc, 0, getSourceManager(), |
| 6903 | getLangOptions())), |
| 6904 | Opc == BO_LAnd ? "&" : "|"); |
| 6905 | if (Opc == BO_LAnd) |
| 6906 | // Suggest replacing "Foo() && kNonZero" with "Foo()" |
| 6907 | Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant) |
| 6908 | << FixItHint::CreateRemoval( |
| 6909 | SourceRange( |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6910 | Lexer::getLocForEndOfToken(LHS.get()->getLocEnd(), |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6911 | 0, getSourceManager(), |
| 6912 | getLangOptions()), |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6913 | RHS.get()->getLocEnd())); |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6914 | } |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6915 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6916 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6917 | if (!Context.getLangOptions().CPlusPlus) { |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6918 | LHS = UsualUnaryConversions(LHS.take()); |
| 6919 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6920 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6921 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6922 | RHS = UsualUnaryConversions(RHS.take()); |
| 6923 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6924 | return QualType(); |
| 6925 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6926 | if (!LHS.get()->getType()->isScalarType() || |
| 6927 | !RHS.get()->getType()->isScalarType()) |
| 6928 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6929 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6930 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 6931 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6932 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6933 | // The following is safe because we only use this method for |
| 6934 | // non-overloadable operands. |
| 6935 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6936 | // C++ [expr.log.and]p1 |
| 6937 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6938 | // The operands are both contextually converted to type bool. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6939 | ExprResult LHSRes = PerformContextuallyConvertToBool(LHS.get()); |
| 6940 | if (LHSRes.isInvalid()) |
| 6941 | return InvalidOperands(Loc, LHS, RHS); |
| 6942 | LHS = move(LHSRes); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6943 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6944 | ExprResult RHSRes = PerformContextuallyConvertToBool(RHS.get()); |
| 6945 | if (RHSRes.isInvalid()) |
| 6946 | return InvalidOperands(Loc, LHS, RHS); |
| 6947 | RHS = move(RHSRes); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6948 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6949 | // C++ [expr.log.and]p2 |
| 6950 | // C++ [expr.log.or]p2 |
| 6951 | // The result is a bool. |
| 6952 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6953 | } |
| 6954 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6955 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 6956 | /// is a read-only property; return true if so. A readonly property expression |
| 6957 | /// depends on various declarations and thus must be treated specially. |
| 6958 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6959 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6960 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6961 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6962 | if (PropExpr->isImplicitProperty()) return false; |
| 6963 | |
| 6964 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6965 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 6966 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 6967 | PropExpr->getBase()->getType(); |
| 6968 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6969 | if (const ObjCObjectPointerType *OPT = |
| 6970 | BaseType->getAsObjCInterfacePointerType()) |
| 6971 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 6972 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 6973 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6974 | } |
| 6975 | return false; |
| 6976 | } |
| 6977 | |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6978 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 6979 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6980 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 6981 | if (PropExpr->isImplicitProperty()) return false; |
| 6982 | |
| 6983 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6984 | QualType T = PDecl->getType(); |
| 6985 | if (T->isReferenceType()) |
Fariborz Jahanian | 61750f2 | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 6986 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6987 | CanQualType CT = S.Context.getCanonicalType(T); |
| 6988 | return CT.isConstQualified(); |
| 6989 | } |
| 6990 | return false; |
| 6991 | } |
| 6992 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6993 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 6994 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 6995 | return false; |
| 6996 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 6997 | NamedDecl *Member = ME->getMemberDecl(); |
| 6998 | if (isa<FieldDecl>(Member)) { |
| 6999 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 7000 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 7001 | return false; |
| 7002 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 7003 | } |
| 7004 | return false; |
| 7005 | } |
| 7006 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7007 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 7008 | /// emit an error and return true. If so, return false. |
| 7009 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7010 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7011 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7012 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7013 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 7014 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 7015 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 7016 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7017 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 7018 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7019 | if (IsLV == Expr::MLV_Valid) |
| 7020 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7021 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7022 | unsigned Diag = 0; |
| 7023 | bool NeedType = false; |
| 7024 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7025 | case Expr::MLV_ConstQualified: |
| 7026 | Diag = diag::err_typecheck_assign_const; |
| 7027 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7028 | // In ARC, use some specialized diagnostics for occasions where we |
| 7029 | // infer 'const'. These are always pseudo-strong variables. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7030 | if (S.getLangOptions().ObjCAutoRefCount) { |
| 7031 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 7032 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 7033 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 7034 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7035 | // Use the normal diagnostic if it's pseudo-__strong but the |
| 7036 | // user actually wrote 'const'. |
| 7037 | if (var->isARCPseudoStrong() && |
| 7038 | (!var->getTypeSourceInfo() || |
| 7039 | !var->getTypeSourceInfo()->getType().isConstQualified())) { |
| 7040 | // There are two pseudo-strong cases: |
| 7041 | // - self |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7042 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 7043 | if (method && var == method->getSelfDecl()) |
| 7044 | Diag = diag::err_typecheck_arr_assign_self; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7045 | |
| 7046 | // - fast enumeration variables |
| 7047 | else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7048 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7049 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7050 | SourceRange Assign; |
| 7051 | if (Loc != OrigLoc) |
| 7052 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 7053 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 7054 | // We need to preserve the AST regardless, so migration tool |
| 7055 | // can do its job. |
| 7056 | return false; |
| 7057 | } |
| 7058 | } |
| 7059 | } |
| 7060 | |
| 7061 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7062 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7063 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 7064 | NeedType = true; |
| 7065 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7066 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7067 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 7068 | NeedType = true; |
| 7069 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 7070 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7071 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 7072 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7073 | case Expr::MLV_Valid: |
| 7074 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7075 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7076 | case Expr::MLV_MemberFunction: |
| 7077 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7078 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 7079 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7080 | case Expr::MLV_IncompleteType: |
| 7081 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 7082 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7083 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 7084 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7085 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7086 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 7087 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 7088 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7089 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 7090 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 7091 | case Expr::MLV_ReadonlyProperty: |
| 7092 | Diag = diag::error_readonly_property_assignment; |
| 7093 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 7094 | case Expr::MLV_NoSetterProperty: |
| 7095 | Diag = diag::error_nosetter_property_assignment; |
| 7096 | break; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7097 | case Expr::MLV_InvalidMessageExpression: |
| 7098 | Diag = diag::error_readonly_message_assignment; |
| 7099 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 7100 | case Expr::MLV_SubObjCPropertySetting: |
| 7101 | Diag = diag::error_no_subobject_property_setting; |
| 7102 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7103 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7104 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7105 | SourceRange Assign; |
| 7106 | if (Loc != OrigLoc) |
| 7107 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7108 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7109 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7110 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7111 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7112 | return true; |
| 7113 | } |
| 7114 | |
| 7115 | |
| 7116 | |
| 7117 | // C99 6.5.16.1 |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7118 | QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7119 | SourceLocation Loc, |
| 7120 | QualType CompoundType) { |
| 7121 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7122 | if (CheckForModifiableLvalue(LHSExpr, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7123 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7124 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7125 | QualType LHSType = LHSExpr->getType(); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7126 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : |
| 7127 | CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7128 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7129 | if (CompoundType.isNull()) { |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7130 | QualType LHSTy(LHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7131 | // Simple assignment "x = y". |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7132 | if (LHSExpr->getObjectKind() == OK_ObjCProperty) { |
| 7133 | ExprResult LHSResult = Owned(LHSExpr); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7134 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 7135 | if (LHSResult.isInvalid()) |
| 7136 | return QualType(); |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7137 | LHSExpr = LHSResult.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7138 | } |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7139 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7140 | if (RHS.isInvalid()) |
| 7141 | return QualType(); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7142 | // Special case of NSObject attributes on c-style pointer types. |
| 7143 | if (ConvTy == IncompatiblePointer && |
| 7144 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7145 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7146 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7147 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7148 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7149 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7150 | if (ConvTy == Compatible && |
| 7151 | getLangOptions().ObjCNonFragileABI && |
| 7152 | LHSType->isObjCObjectType()) |
| 7153 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 7154 | << LHSType; |
| 7155 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7156 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 7157 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 7158 | // instead of "x += 4". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7159 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7160 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 7161 | RHSCheck = ICE->getSubExpr(); |
| 7162 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7163 | if ((UO->getOpcode() == UO_Plus || |
| 7164 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7165 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7166 | // Only if the two operators are exactly adjacent. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 7167 | Loc.getLocWithOffset(1) == UO->getOperatorLoc() && |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7168 | // And there is a space or other character before the subexpr of the |
| 7169 | // unary +/-. We don't want to warn on "x=-1". |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 7170 | Loc.getLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7171 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7172 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7173 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7174 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7175 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7176 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7177 | |
| 7178 | if (ConvTy == Compatible) { |
| 7179 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7180 | checkRetainCycles(LHSExpr, RHS.get()); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 7181 | else if (getLangOptions().ObjCAutoRefCount) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7182 | checkUnsafeExprAssigns(Loc, LHSExpr, RHS.get()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7183 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7184 | } else { |
| 7185 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7186 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7187 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7188 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7189 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7190 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7191 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7192 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7193 | CheckForNullPointerDereference(*this, LHSExpr); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7194 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7195 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7196 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7197 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7198 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7199 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7200 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 7201 | // operand. |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7202 | return (getLangOptions().CPlusPlus |
| 7203 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7204 | } |
| 7205 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7206 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7207 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7208 | SourceLocation Loc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7209 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 7210 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7211 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 7212 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7213 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7214 | return QualType(); |
| 7215 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7216 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7217 | // operands, but not unary promotions. |
| 7218 | // C++'s comma does not do any conversions at all (C++ [expr.comma]p1). |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7219 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7220 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7221 | // containing site to determine what should be done with the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7222 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 7223 | if (LHS.isInvalid()) |
| 7224 | return QualType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7225 | |
| 7226 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7227 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 7228 | if (RHS.isInvalid()) |
| 7229 | return QualType(); |
| 7230 | if (!RHS.get()->getType()->isVoidType()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7231 | S.RequireCompleteType(Loc, RHS.get()->getType(), |
| 7232 | diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7233 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7234 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7235 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7236 | } |
| 7237 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7238 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7239 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7240 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7241 | ExprValueKind &VK, |
| 7242 | SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7243 | bool IsInc, bool IsPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7244 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7245 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7246 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7247 | QualType ResType = Op->getType(); |
| 7248 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7249 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7250 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7251 | // Decrement of bool is not allowed. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7252 | if (!IsInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7253 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7254 | return QualType(); |
| 7255 | } |
| 7256 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7257 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7258 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7259 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7260 | } else if (ResType->isAnyPointerType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7261 | // C99 6.5.2.4p2, 6.5.6p2 |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7262 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7263 | return QualType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7264 | |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7265 | // Diagnose bad cases where we step over interface counts. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 7266 | else if (!checkArithmethicPointerOnNonFragileABI(S, OpLoc, Op)) |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7267 | return QualType(); |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7268 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7269 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7270 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7271 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7272 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7273 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7274 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7275 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7276 | IsInc, IsPrefix); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7277 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 7278 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7279 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7280 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7281 | << ResType << int(IsInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7282 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7283 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7284 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 7285 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7286 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7287 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7288 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7289 | // (in C or with postfix), the increment is the unqualified type of the |
| 7290 | // operand. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7291 | if (IsPrefix && S.getLangOptions().CPlusPlus) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7292 | VK = VK_LValue; |
| 7293 | return ResType; |
| 7294 | } else { |
| 7295 | VK = VK_RValue; |
| 7296 | return ResType.getUnqualifiedType(); |
| 7297 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7298 | } |
| 7299 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7300 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7301 | assert(E->getValueKind() == VK_LValue && |
| 7302 | E->getObjectKind() == OK_ObjCProperty); |
| 7303 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 7304 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7305 | QualType T = E->getType(); |
| 7306 | QualType ReceiverType; |
| 7307 | if (PRE->isObjectReceiver()) |
| 7308 | ReceiverType = PRE->getBase()->getType(); |
| 7309 | else if (PRE->isSuperReceiver()) |
| 7310 | ReceiverType = PRE->getSuperReceiverType(); |
| 7311 | else |
| 7312 | ReceiverType = Context.getObjCInterfaceType(PRE->getClassReceiver()); |
| 7313 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7314 | ExprValueKind VK = VK_RValue; |
| 7315 | if (PRE->isImplicitProperty()) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7316 | if (ObjCMethodDecl *GetterMethod = |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7317 | PRE->getImplicitPropertyGetter()) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7318 | T = getMessageSendResultType(ReceiverType, GetterMethod, |
| 7319 | PRE->isClassReceiver(), |
| 7320 | PRE->isSuperReceiver()); |
| 7321 | VK = Expr::getValueKindForType(GetterMethod->getResultType()); |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7322 | } |
| 7323 | else { |
| 7324 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 7325 | << PRE->getBase()->getType(); |
| 7326 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7327 | } |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7328 | |
| 7329 | E = ImplicitCastExpr::Create(Context, T, CK_GetObjCProperty, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7330 | E, 0, VK); |
John McCall | db67e2f | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 7331 | |
| 7332 | ExprResult Result = MaybeBindToTemporary(E); |
| 7333 | if (!Result.isInvalid()) |
| 7334 | E = Result.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7335 | |
| 7336 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7337 | } |
| 7338 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7339 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, |
| 7340 | QualType &LHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7341 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 7342 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 7343 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7344 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7345 | bool Consumed = false; |
| 7346 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7347 | if (PropRef->isImplicitProperty()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7348 | // If using property-dot syntax notation for assignment, and there is a |
| 7349 | // setter, RHS expression is being passed to the setter argument. So, |
| 7350 | // type conversion (and comparison) is RHS to setter's argument type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7351 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7352 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 7353 | LHSTy = (*P)->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7354 | Consumed = (getLangOptions().ObjCAutoRefCount && |
| 7355 | (*P)->hasAttr<NSConsumedAttr>()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7356 | |
| 7357 | // Otherwise, if the getter returns an l-value, just call that. |
| 7358 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7359 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7360 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 7361 | if (VK == VK_LValue) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7362 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 7363 | CK_GetObjCProperty, LHS.take(), 0, VK); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7364 | return; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7365 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7366 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7367 | } else if (getLangOptions().ObjCAutoRefCount) { |
| 7368 | const ObjCMethodDecl *setter |
| 7369 | = PropRef->getExplicitProperty()->getSetterMethodDecl(); |
| 7370 | if (setter) { |
| 7371 | ObjCMethodDecl::param_iterator P = setter->param_begin(); |
| 7372 | LHSTy = (*P)->getType(); |
| 7373 | Consumed = (*P)->hasAttr<NSConsumedAttr>(); |
| 7374 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7375 | } |
| 7376 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7377 | if ((getLangOptions().CPlusPlus && LHSTy->isRecordType()) || |
| 7378 | getLangOptions().ObjCAutoRefCount) { |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7379 | InitializedEntity Entity = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7380 | InitializedEntity::InitializeParameter(Context, LHSTy, Consumed); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7381 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7382 | if (!ArgE.isInvalid()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7383 | RHS = ArgE; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7384 | if (getLangOptions().ObjCAutoRefCount && !PropRef->isSuperReceiver()) |
| 7385 | checkRetainCycles(const_cast<Expr*>(PropRef->getBase()), RHS.get()); |
| 7386 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7387 | } |
| 7388 | } |
| 7389 | |
| 7390 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7391 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7392 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7393 | /// where the declaration is needed for type checking. We only need to |
| 7394 | /// handle cases when the expression references a function designator |
| 7395 | /// or is an lvalue. Here are some examples: |
| 7396 | /// - &(x) => x |
| 7397 | /// - &*****f => f for f a function designator. |
| 7398 | /// - &s.xx => s |
| 7399 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7400 | /// - *(x + 1) -> x, if x is an array |
| 7401 | /// - &"123"[2] -> 0 |
| 7402 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7403 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7404 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7405 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7406 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7407 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7408 | // If this is an arrow operator, the address is an offset from |
| 7409 | // the base's value, so the object the base refers to is |
| 7410 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7411 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7412 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7413 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7414 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7415 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7416 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7417 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7418 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7419 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7420 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7421 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7422 | } |
| 7423 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7424 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7425 | case Stmt::UnaryOperatorClass: { |
| 7426 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7427 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7428 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7429 | case UO_Real: |
| 7430 | case UO_Imag: |
| 7431 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7432 | return getPrimaryDecl(UO->getSubExpr()); |
| 7433 | default: |
| 7434 | return 0; |
| 7435 | } |
| 7436 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7437 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7438 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7439 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7440 | // If the result of an implicit cast is an l-value, we care about |
| 7441 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7442 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7443 | default: |
| 7444 | return 0; |
| 7445 | } |
| 7446 | } |
| 7447 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7448 | namespace { |
| 7449 | enum { |
| 7450 | AO_Bit_Field = 0, |
| 7451 | AO_Vector_Element = 1, |
| 7452 | AO_Property_Expansion = 2, |
| 7453 | AO_Register_Variable = 3, |
| 7454 | AO_No_Error = 4 |
| 7455 | }; |
| 7456 | } |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7457 | /// \brief Diagnose invalid operand for address of operations. |
| 7458 | /// |
| 7459 | /// \param Type The type of operand which cannot have its address taken. |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7460 | static void diagnoseAddressOfInvalidType(Sema &S, SourceLocation Loc, |
| 7461 | Expr *E, unsigned Type) { |
| 7462 | S.Diag(Loc, diag::err_typecheck_address_of) << Type << E->getSourceRange(); |
| 7463 | } |
| 7464 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7465 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7466 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7467 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7468 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7469 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7470 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7471 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7472 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 7473 | SourceLocation OpLoc) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7474 | if (OrigOp->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7475 | return S.Context.DependentTy; |
| 7476 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 7477 | return S.Context.OverloadTy; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7478 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 7479 | return S.Context.UnknownAnyTy; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7480 | if (OrigOp->getType() == S.Context.BoundMemberTy) { |
| 7481 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 7482 | << OrigOp->getSourceRange(); |
| 7483 | return QualType(); |
| 7484 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7485 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7486 | assert(!OrigOp->getType()->isPlaceholderType()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7487 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7488 | // Make sure to ignore parentheses in subsequent checks |
| 7489 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 7490 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7491 | if (S.getLangOptions().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7492 | // Implement C99-only parts of addressof rules. |
| 7493 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7494 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7495 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 7496 | // (assuming the deref expression is valid). |
| 7497 | return uOp->getSubExpr()->getType(); |
| 7498 | } |
| 7499 | // Technically, there should be a check for array subscript |
| 7500 | // expressions here, but the result of one is always an lvalue anyway. |
| 7501 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7502 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7503 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7504 | unsigned AddressOfError = AO_No_Error; |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 7505 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7506 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7507 | bool sfinae = S.isSFINAEContext(); |
| 7508 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 7509 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7510 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7511 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7512 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7513 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7514 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7515 | } else if (lval == Expr::LV_MemberFunction) { |
| 7516 | // If it's an instance method, make a member pointer. |
| 7517 | // The expression must have exactly the form &A::foo. |
| 7518 | |
| 7519 | // If the underlying expression isn't a decl ref, give up. |
| 7520 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7521 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7522 | << OrigOp->getSourceRange(); |
| 7523 | return QualType(); |
| 7524 | } |
| 7525 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 7526 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 7527 | |
| 7528 | // The id-expression was parenthesized. |
| 7529 | if (OrigOp != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7530 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7531 | << OrigOp->getSourceRange(); |
| 7532 | |
| 7533 | // The method was named without a qualifier. |
| 7534 | } else if (!DRE->getQualifier()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7535 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7536 | << op->getSourceRange(); |
| 7537 | } |
| 7538 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7539 | return S.Context.getMemberPointerType(op->getType(), |
| 7540 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7541 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7542 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7543 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7544 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7545 | // FIXME: emit more specific diag... |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7546 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 7547 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7548 | return QualType(); |
| 7549 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7550 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7551 | // The operand cannot be a bit-field |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7552 | AddressOfError = AO_Bit_Field; |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7553 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7554 | // The operand cannot be an element of a vector |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7555 | AddressOfError = AO_Vector_Element; |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7556 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7557 | // cannot take address of a property expression. |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7558 | AddressOfError = AO_Property_Expansion; |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7559 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7560 | // We have an lvalue with a decl. Make sure the decl is not declared |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7561 | // with the register storage-class specifier. |
| 7562 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 7563 | // in C++ it is not error to take address of a register |
| 7564 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7565 | if (vd->getStorageClass() == SC_Register && |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7566 | !S.getLangOptions().CPlusPlus) { |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7567 | AddressOfError = AO_Register_Variable; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7568 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7569 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7570 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7571 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 7572 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7573 | // Could be a pointer to member, though, if there is an explicit |
| 7574 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 7575 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7576 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7577 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7578 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7579 | S.Diag(OpLoc, |
| 7580 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7581 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7582 | return QualType(); |
| 7583 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7584 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 7585 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 7586 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7587 | return S.Context.getMemberPointerType(op->getType(), |
| 7588 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7589 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7590 | } |
Eli Friedman | 7b2f51c | 2011-08-26 20:28:17 +0000 | [diff] [blame] | 7591 | } else if (!isa<FunctionDecl>(dcl) && !isa<NonTypeTemplateParmDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7592 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7593 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7594 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7595 | if (AddressOfError != AO_No_Error) { |
| 7596 | diagnoseAddressOfInvalidType(S, OpLoc, op, AddressOfError); |
| 7597 | return QualType(); |
| 7598 | } |
| 7599 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7600 | if (lval == Expr::LV_IncompleteVoidType) { |
| 7601 | // Taking the address of a void variable is technically illegal, but we |
| 7602 | // allow it in cases which are otherwise valid. |
| 7603 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7604 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7605 | } |
| 7606 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7607 | // If the operand has type "type", the result has type "pointer to type". |
Douglas Gregor | 8f70ddb | 2010-07-29 16:05:45 +0000 | [diff] [blame] | 7608 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7609 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 7610 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7611 | } |
| 7612 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7613 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7614 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 7615 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7616 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7617 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7618 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7619 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 7620 | if (ConvResult.isInvalid()) |
| 7621 | return QualType(); |
| 7622 | Op = ConvResult.take(); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7623 | QualType OpTy = Op->getType(); |
| 7624 | QualType Result; |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 7625 | |
| 7626 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 7627 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 7628 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 7629 | Op->getSourceRange()); |
| 7630 | } |
| 7631 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7632 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 7633 | // is an incomplete type or void. It would be possible to warn about |
| 7634 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 7635 | // warning is unlikely to catch any mistakes. |
| 7636 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 7637 | Result = PT->getPointeeType(); |
| 7638 | else if (const ObjCObjectPointerType *OPT = |
| 7639 | OpTy->getAs<ObjCObjectPointerType>()) |
| 7640 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7641 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7642 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7643 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7644 | if (PR.take() != Op) |
| 7645 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7646 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7647 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7648 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7649 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7650 | << OpTy << Op->getSourceRange(); |
| 7651 | return QualType(); |
| 7652 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7653 | |
| 7654 | // Dereferences are usually l-values... |
| 7655 | VK = VK_LValue; |
| 7656 | |
| 7657 | // ...except that certain expressions are never l-values in C. |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 7658 | if (!S.getLangOptions().CPlusPlus && Result.isCForbiddenLValueType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7659 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7660 | |
| 7661 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7662 | } |
| 7663 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7664 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7665 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7666 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7667 | switch (Kind) { |
| 7668 | default: assert(0 && "Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7669 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 7670 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 7671 | case tok::star: Opc = BO_Mul; break; |
| 7672 | case tok::slash: Opc = BO_Div; break; |
| 7673 | case tok::percent: Opc = BO_Rem; break; |
| 7674 | case tok::plus: Opc = BO_Add; break; |
| 7675 | case tok::minus: Opc = BO_Sub; break; |
| 7676 | case tok::lessless: Opc = BO_Shl; break; |
| 7677 | case tok::greatergreater: Opc = BO_Shr; break; |
| 7678 | case tok::lessequal: Opc = BO_LE; break; |
| 7679 | case tok::less: Opc = BO_LT; break; |
| 7680 | case tok::greaterequal: Opc = BO_GE; break; |
| 7681 | case tok::greater: Opc = BO_GT; break; |
| 7682 | case tok::exclaimequal: Opc = BO_NE; break; |
| 7683 | case tok::equalequal: Opc = BO_EQ; break; |
| 7684 | case tok::amp: Opc = BO_And; break; |
| 7685 | case tok::caret: Opc = BO_Xor; break; |
| 7686 | case tok::pipe: Opc = BO_Or; break; |
| 7687 | case tok::ampamp: Opc = BO_LAnd; break; |
| 7688 | case tok::pipepipe: Opc = BO_LOr; break; |
| 7689 | case tok::equal: Opc = BO_Assign; break; |
| 7690 | case tok::starequal: Opc = BO_MulAssign; break; |
| 7691 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 7692 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 7693 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 7694 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 7695 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 7696 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 7697 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 7698 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 7699 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 7700 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7701 | } |
| 7702 | return Opc; |
| 7703 | } |
| 7704 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7705 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7706 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7707 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7708 | switch (Kind) { |
| 7709 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7710 | case tok::plusplus: Opc = UO_PreInc; break; |
| 7711 | case tok::minusminus: Opc = UO_PreDec; break; |
| 7712 | case tok::amp: Opc = UO_AddrOf; break; |
| 7713 | case tok::star: Opc = UO_Deref; break; |
| 7714 | case tok::plus: Opc = UO_Plus; break; |
| 7715 | case tok::minus: Opc = UO_Minus; break; |
| 7716 | case tok::tilde: Opc = UO_Not; break; |
| 7717 | case tok::exclaim: Opc = UO_LNot; break; |
| 7718 | case tok::kw___real: Opc = UO_Real; break; |
| 7719 | case tok::kw___imag: Opc = UO_Imag; break; |
| 7720 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7721 | } |
| 7722 | return Opc; |
| 7723 | } |
| 7724 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7725 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 7726 | /// This warning is only emitted for builtin assignment operations. It is also |
| 7727 | /// suppressed in the event of macro expansions. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7728 | static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7729 | SourceLocation OpLoc) { |
| 7730 | if (!S.ActiveTemplateInstantiations.empty()) |
| 7731 | return; |
| 7732 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 7733 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7734 | LHSExpr = LHSExpr->IgnoreParenImpCasts(); |
| 7735 | RHSExpr = RHSExpr->IgnoreParenImpCasts(); |
| 7736 | const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); |
| 7737 | const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); |
| 7738 | if (!LHSDeclRef || !RHSDeclRef || |
| 7739 | LHSDeclRef->getLocation().isMacroID() || |
| 7740 | RHSDeclRef->getLocation().isMacroID()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7741 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7742 | const ValueDecl *LHSDecl = |
| 7743 | cast<ValueDecl>(LHSDeclRef->getDecl()->getCanonicalDecl()); |
| 7744 | const ValueDecl *RHSDecl = |
| 7745 | cast<ValueDecl>(RHSDeclRef->getDecl()->getCanonicalDecl()); |
| 7746 | if (LHSDecl != RHSDecl) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7747 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7748 | if (LHSDecl->getType().isVolatileQualified()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7749 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7750 | if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7751 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 7752 | return; |
| 7753 | |
| 7754 | S.Diag(OpLoc, diag::warn_self_assignment) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7755 | << LHSDeclRef->getType() |
| 7756 | << LHSExpr->getSourceRange() << RHSExpr->getSourceRange(); |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7757 | } |
| 7758 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7759 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 7760 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 7761 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7762 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7763 | BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7764 | Expr *LHSExpr, Expr *RHSExpr) { |
| 7765 | ExprResult LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7766 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7767 | // The following two variables are used for compound assignment operators |
| 7768 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 7769 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7770 | ExprValueKind VK = VK_RValue; |
| 7771 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7772 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7773 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 7774 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 7775 | // But since an assignment can trigger target based overload, exclude it in |
| 7776 | // our blind search. i.e: |
| 7777 | // template<class T> void f(); template<class T, class U> void f(U); |
| 7778 | // f<int> == 0; // resolve f<int> blindly |
| 7779 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 7780 | if (Opc != BO_Assign) { |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7781 | ExprResult resolvedLHS = CheckPlaceholderExpr(LHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7782 | if (!resolvedLHS.isUsable()) return ExprError(); |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7783 | LHS = move(resolvedLHS); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7784 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7785 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7786 | if (!resolvedRHS.isUsable()) return ExprError(); |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7787 | RHS = move(resolvedRHS); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7788 | } |
| 7789 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7790 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7791 | case BO_Assign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7792 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, QualType()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7793 | if (getLangOptions().CPlusPlus && |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7794 | LHS.get()->getObjectKind() != OK_ObjCProperty) { |
| 7795 | VK = LHS.get()->getValueKind(); |
| 7796 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7797 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7798 | if (!ResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7799 | DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7800 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7801 | case BO_PtrMemD: |
| 7802 | case BO_PtrMemI: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7803 | ResultTy = CheckPointerToMemberOperands(LHS, RHS, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7804 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7805 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7806 | case BO_Mul: |
| 7807 | case BO_Div: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7808 | ResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7809 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7810 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7811 | case BO_Rem: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7812 | ResultTy = CheckRemainderOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7813 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7814 | case BO_Add: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7815 | ResultTy = CheckAdditionOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7816 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7817 | case BO_Sub: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7818 | ResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7819 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7820 | case BO_Shl: |
| 7821 | case BO_Shr: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7822 | ResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7823 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7824 | case BO_LE: |
| 7825 | case BO_LT: |
| 7826 | case BO_GE: |
| 7827 | case BO_GT: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7828 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7829 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7830 | case BO_EQ: |
| 7831 | case BO_NE: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7832 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7833 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7834 | case BO_And: |
| 7835 | case BO_Xor: |
| 7836 | case BO_Or: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7837 | ResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7838 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7839 | case BO_LAnd: |
| 7840 | case BO_LOr: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7841 | ResultTy = CheckLogicalOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7842 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7843 | case BO_MulAssign: |
| 7844 | case BO_DivAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7845 | CompResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7846 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7847 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7848 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7849 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7850 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7851 | case BO_RemAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7852 | CompResultTy = CheckRemainderOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7853 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7854 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7855 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7856 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7857 | case BO_AddAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7858 | CompResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, &CompLHSTy); |
| 7859 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7860 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7861 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7862 | case BO_SubAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7863 | CompResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc, &CompLHSTy); |
| 7864 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7865 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7866 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7867 | case BO_ShlAssign: |
| 7868 | case BO_ShrAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7869 | CompResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7870 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7871 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7872 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7873 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7874 | case BO_AndAssign: |
| 7875 | case BO_XorAssign: |
| 7876 | case BO_OrAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7877 | CompResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7878 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7879 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7880 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7881 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7882 | case BO_Comma: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7883 | ResultTy = CheckCommaOperands(*this, LHS, RHS, OpLoc); |
| 7884 | if (getLangOptions().CPlusPlus && !RHS.isInvalid()) { |
| 7885 | VK = RHS.get()->getValueKind(); |
| 7886 | OK = RHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7887 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7888 | break; |
| 7889 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7890 | if (ResultTy.isNull() || LHS.isInvalid() || RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7891 | return ExprError(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7892 | |
| 7893 | // Check for array bounds violations for both sides of the BinaryOperator |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7894 | CheckArrayAccess(LHS.get()); |
| 7895 | CheckArrayAccess(RHS.get()); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7896 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7897 | if (CompResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7898 | return Owned(new (Context) BinaryOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7899 | ResultTy, VK, OK, OpLoc)); |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7900 | if (getLangOptions().CPlusPlus && LHS.get()->getObjectKind() != |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7901 | OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7902 | VK = VK_LValue; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7903 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7904 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7905 | return Owned(new (Context) CompoundAssignOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7906 | ResultTy, VK, OK, CompLHSTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7907 | CompResultTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7908 | } |
| 7909 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7910 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 7911 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 7912 | /// comparison operators have higher precedence. The most typical example of |
| 7913 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7914 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7915 | SourceLocation OpLoc, Expr *LHSExpr, |
| 7916 | Expr *RHSExpr) { |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7917 | typedef BinaryOperator BinOp; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7918 | BinOp::Opcode LHSopc = static_cast<BinOp::Opcode>(-1), |
| 7919 | RHSopc = static_cast<BinOp::Opcode>(-1); |
| 7920 | if (BinOp *BO = dyn_cast<BinOp>(LHSExpr)) |
| 7921 | LHSopc = BO->getOpcode(); |
| 7922 | if (BinOp *BO = dyn_cast<BinOp>(RHSExpr)) |
| 7923 | RHSopc = BO->getOpcode(); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7924 | |
| 7925 | // Subs are not binary operators. |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7926 | if (LHSopc == -1 && RHSopc == -1) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7927 | return; |
| 7928 | |
| 7929 | // Bitwise operations are sometimes used as eager logical ops. |
| 7930 | // Don't diagnose this. |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7931 | if ((BinOp::isComparisonOp(LHSopc) || BinOp::isBitwiseOp(LHSopc)) && |
| 7932 | (BinOp::isComparisonOp(RHSopc) || BinOp::isBitwiseOp(RHSopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7933 | return; |
| 7934 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7935 | bool isLeftComp = BinOp::isComparisonOp(LHSopc); |
| 7936 | bool isRightComp = BinOp::isComparisonOp(RHSopc); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7937 | if (!isLeftComp && !isRightComp) return; |
| 7938 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7939 | SourceRange DiagRange = isLeftComp ? SourceRange(LHSExpr->getLocStart(), |
| 7940 | OpLoc) |
| 7941 | : SourceRange(OpLoc, RHSExpr->getLocEnd()); |
| 7942 | std::string OpStr = isLeftComp ? BinOp::getOpcodeStr(LHSopc) |
| 7943 | : BinOp::getOpcodeStr(RHSopc); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7944 | SourceRange ParensRange = isLeftComp ? |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7945 | SourceRange(cast<BinOp>(LHSExpr)->getRHS()->getLocStart(), |
| 7946 | RHSExpr->getLocEnd()) |
| 7947 | : SourceRange(LHSExpr->getLocStart(), |
| 7948 | cast<BinOp>(RHSExpr)->getLHS()->getLocStart()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7949 | |
| 7950 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 7951 | << DiagRange << BinOp::getOpcodeStr(Opc) << OpStr; |
| 7952 | SuggestParentheses(Self, OpLoc, |
| 7953 | Self.PDiag(diag::note_precedence_bitwise_silence) << OpStr, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7954 | RHSExpr->getSourceRange()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7955 | SuggestParentheses(Self, OpLoc, |
| 7956 | Self.PDiag(diag::note_precedence_bitwise_first) << BinOp::getOpcodeStr(Opc), |
| 7957 | ParensRange); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7958 | } |
| 7959 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7960 | /// \brief It accepts a '&' expr that is inside a '|' one. |
| 7961 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression |
| 7962 | /// in parentheses. |
| 7963 | static void |
| 7964 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, |
| 7965 | BinaryOperator *Bop) { |
| 7966 | assert(Bop->getOpcode() == BO_And); |
| 7967 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) |
| 7968 | << Bop->getSourceRange() << OpLoc; |
| 7969 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
| 7970 | Self.PDiag(diag::note_bitwise_and_in_bitwise_or_silence), |
| 7971 | Bop->getSourceRange()); |
| 7972 | } |
| 7973 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7974 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 7975 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 7976 | /// in parentheses. |
| 7977 | static void |
| 7978 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7979 | BinaryOperator *Bop) { |
| 7980 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7981 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 7982 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7983 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7984 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7985 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7986 | } |
| 7987 | |
| 7988 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7989 | /// 'true'. |
| 7990 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 7991 | bool Res; |
| 7992 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 7993 | } |
| 7994 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7995 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7996 | /// 'false'. |
| 7997 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 7998 | bool Res; |
| 7999 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 8000 | } |
| 8001 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8002 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 8003 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8004 | Expr *LHSExpr, Expr *RHSExpr) { |
| 8005 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(LHSExpr)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8006 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8007 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8008 | if (EvaluatesAsFalse(S, RHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8009 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8010 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 8011 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 8012 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 8013 | } else if (Bop->getOpcode() == BO_LOr) { |
| 8014 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 8015 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 8016 | // "a || b && 1", but warn now. |
| 8017 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 8018 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 8019 | } |
| 8020 | } |
| 8021 | } |
| 8022 | } |
| 8023 | |
| 8024 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 8025 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8026 | Expr *LHSExpr, Expr *RHSExpr) { |
| 8027 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(RHSExpr)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8028 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8029 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8030 | if (EvaluatesAsFalse(S, LHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8031 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8032 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 8033 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 8034 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8035 | } |
| 8036 | } |
| 8037 | } |
| 8038 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8039 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
| 8040 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, |
| 8041 | Expr *OrArg) { |
| 8042 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { |
| 8043 | if (Bop->getOpcode() == BO_And) |
| 8044 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); |
| 8045 | } |
| 8046 | } |
| 8047 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8048 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8049 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8050 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8051 | SourceLocation OpLoc, Expr *LHSExpr, |
| 8052 | Expr *RHSExpr){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8053 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8054 | if (BinaryOperator::isBitwiseOp(Opc)) |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8055 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8056 | |
| 8057 | // Diagnose "arg1 & arg2 | arg3" |
| 8058 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8059 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, LHSExpr); |
| 8060 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8061 | } |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8062 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8063 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 8064 | // We don't warn for 'assert(a || b && "bad")' since this is safe. |
Argyrios Kyrtzidis | d92ccaa | 2010-11-17 18:54:22 +0000 | [diff] [blame] | 8065 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8066 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, LHSExpr, RHSExpr); |
| 8067 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8068 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8069 | } |
| 8070 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8071 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8072 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8073 | tok::TokenKind Kind, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8074 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8075 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8076 | assert((LHSExpr != 0) && "ActOnBinOp(): missing left expression"); |
| 8077 | assert((RHSExpr != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8078 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8079 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8080 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, LHSExpr, RHSExpr); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8081 | |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8082 | return BuildBinOp(S, TokLoc, Opc, LHSExpr, RHSExpr); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8083 | } |
| 8084 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8085 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8086 | BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8087 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8088 | if (getLangOptions().CPlusPlus) { |
| 8089 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8090 | |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8091 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8092 | UseBuiltinOperator = false; |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8093 | } else if (Opc == BO_Assign && |
| 8094 | LHSExpr->getObjectKind() == OK_ObjCProperty) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8095 | UseBuiltinOperator = true; |
| 8096 | } else { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8097 | UseBuiltinOperator = !LHSExpr->getType()->isOverloadableType() && |
| 8098 | !RHSExpr->getType()->isOverloadableType(); |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8099 | } |
| 8100 | |
| 8101 | if (!UseBuiltinOperator) { |
| 8102 | // Find all of the overloaded operators visible from this |
| 8103 | // point. We perform both an operator-name lookup from the local |
| 8104 | // scope and an argument-dependent lookup based on the types of |
| 8105 | // the arguments. |
| 8106 | UnresolvedSet<16> Functions; |
| 8107 | OverloadedOperatorKind OverOp |
| 8108 | = BinaryOperator::getOverloadedOperator(Opc); |
| 8109 | if (S && OverOp != OO_None) |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8110 | LookupOverloadedOperatorName(OverOp, S, LHSExpr->getType(), |
| 8111 | RHSExpr->getType(), Functions); |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8112 | |
| 8113 | // Build the (potentially-overloaded, potentially-dependent) |
| 8114 | // binary operation. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8115 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, LHSExpr, RHSExpr); |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8116 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8117 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8118 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8119 | // Build a built-in binary operation. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8120 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8121 | } |
| 8122 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8123 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8124 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8125 | Expr *InputExpr) { |
| 8126 | ExprResult Input = Owned(InputExpr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8127 | ExprValueKind VK = VK_RValue; |
| 8128 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8129 | QualType resultType; |
| 8130 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8131 | case UO_PreInc: |
| 8132 | case UO_PreDec: |
| 8133 | case UO_PostInc: |
| 8134 | case UO_PostDec: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8135 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8136 | Opc == UO_PreInc || |
| 8137 | Opc == UO_PostInc, |
| 8138 | Opc == UO_PreInc || |
| 8139 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8140 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8141 | case UO_AddrOf: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8142 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8143 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8144 | case UO_Deref: { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8145 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8146 | if (!resolved.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8147 | Input = move(resolved); |
| 8148 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8149 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8150 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8151 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8152 | case UO_Plus: |
| 8153 | case UO_Minus: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8154 | Input = UsualUnaryConversions(Input.take()); |
| 8155 | if (Input.isInvalid()) return ExprError(); |
| 8156 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8157 | if (resultType->isDependentType()) |
| 8158 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8159 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8160 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8161 | break; |
| 8162 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 8163 | resultType->isEnumeralType()) |
| 8164 | break; |
| 8165 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8166 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8167 | resultType->isPointerType()) |
| 8168 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8169 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8170 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8171 | if (Input.isInvalid()) return ExprError(); |
| 8172 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8173 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8174 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8175 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8176 | << resultType << Input.get()->getSourceRange()); |
| 8177 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8178 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8179 | Input = UsualUnaryConversions(Input.take()); |
| 8180 | if (Input.isInvalid()) return ExprError(); |
| 8181 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8182 | if (resultType->isDependentType()) |
| 8183 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8184 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8185 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8186 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8187 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8188 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8189 | else if (resultType->hasIntegerRepresentation()) |
| 8190 | break; |
| 8191 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8192 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8193 | if (Input.isInvalid()) return ExprError(); |
| 8194 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8195 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8196 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8197 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8198 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8199 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8200 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8201 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8202 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8203 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8204 | if (Input.isInvalid()) return ExprError(); |
| 8205 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8206 | if (resultType->isDependentType()) |
| 8207 | break; |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8208 | if (resultType->isScalarType()) { |
| 8209 | // C99 6.5.3.3p1: ok, fallthrough; |
| 8210 | if (Context.getLangOptions().CPlusPlus) { |
| 8211 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 8212 | // operand contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8213 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 8214 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8215 | } |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8216 | } else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8217 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8218 | if (Input.isInvalid()) return ExprError(); |
| 8219 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8220 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8221 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8222 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8223 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8224 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8225 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8226 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8227 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8228 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8229 | case UO_Real: |
| 8230 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8231 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8232 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8233 | if (Input.isInvalid()) return ExprError(); |
| 8234 | if (Input.get()->getValueKind() != VK_RValue && |
| 8235 | Input.get()->getObjectKind() == OK_Ordinary) |
| 8236 | VK = Input.get()->getValueKind(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8237 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8238 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8239 | resultType = Input.get()->getType(); |
| 8240 | VK = Input.get()->getValueKind(); |
| 8241 | OK = Input.get()->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8242 | break; |
| 8243 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8244 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8245 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8246 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8247 | // Check for array bounds violations in the operand of the UnaryOperator, |
| 8248 | // except for the '*' and '&' operators that have to be handled specially |
| 8249 | // by CheckArrayAccess (as there are special cases like &array[arraysize] |
| 8250 | // that are explicitly defined as valid by the standard). |
| 8251 | if (Opc != UO_AddrOf && Opc != UO_Deref) |
| 8252 | CheckArrayAccess(Input.get()); |
| 8253 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8254 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8255 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8256 | } |
| 8257 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8258 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8259 | UnaryOperatorKind Opc, Expr *Input) { |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 8260 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 957c094 | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 8261 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8262 | // Find all of the overloaded operators visible from this |
| 8263 | // point. We perform both an operator-name lookup from the local |
| 8264 | // scope and an argument-dependent lookup based on the types of |
| 8265 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8266 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8267 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8268 | if (S && OverOp != OO_None) |
| 8269 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 8270 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8271 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8272 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8273 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8274 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8275 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8276 | } |
| 8277 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8278 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8279 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8280 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8281 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8282 | } |
| 8283 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8284 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8285 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8286 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8287 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8288 | // Create the AST node. The address of a label always has type 'void*'. |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8289 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8290 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8291 | } |
| 8292 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8293 | /// Given the last statement in a statement-expression, check whether |
| 8294 | /// the result is a producing expression (like a call to an |
| 8295 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 8296 | /// release out of the full-expression. Otherwise, return null. |
| 8297 | /// Cannot fail. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8298 | static Expr *maybeRebuildARCConsumingStmt(Stmt *Statement) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8299 | // Should always be wrapped with one of these. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8300 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(Statement); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8301 | if (!cleanups) return 0; |
| 8302 | |
| 8303 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 8304 | if (!cast || cast->getCastKind() != CK_ARCConsumeObject) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8305 | return 0; |
| 8306 | |
| 8307 | // Splice out the cast. This shouldn't modify any interesting |
| 8308 | // features of the statement. |
| 8309 | Expr *producer = cast->getSubExpr(); |
| 8310 | assert(producer->getType() == cast->getType()); |
| 8311 | assert(producer->getValueKind() == cast->getValueKind()); |
| 8312 | cleanups->setSubExpr(producer); |
| 8313 | return cleanups; |
| 8314 | } |
| 8315 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8316 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8317 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8318 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8319 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8320 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8321 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8322 | bool isFileScope |
| 8323 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8324 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8325 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8326 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8327 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8328 | // example, it is not possible to goto into a stmt expression apparently. |
| 8329 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8330 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8331 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8332 | // as the type of the stmtexpr. |
| 8333 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8334 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8335 | if (!Compound->body_empty()) { |
| 8336 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8337 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8338 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8339 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8340 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8341 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8342 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8343 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8344 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8345 | // Do function/array conversion on the last expression, but not |
| 8346 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8347 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 8348 | if (LastExpr.isInvalid()) |
| 8349 | return ExprError(); |
| 8350 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8351 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8352 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8353 | // In ARC, if the final expression ends in a consume, splice |
| 8354 | // the consume out and bind it later. In the alternate case |
| 8355 | // (when dealing with a retainable type), the result |
| 8356 | // initialization will create a produce. In both cases the |
| 8357 | // result will be +1, and we'll need to balance that out with |
| 8358 | // a bind. |
| 8359 | if (Expr *rebuiltLastStmt |
| 8360 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 8361 | LastExpr = rebuiltLastStmt; |
| 8362 | } else { |
| 8363 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8364 | InitializedEntity::InitializeResult(LPLoc, |
| 8365 | Ty, |
| 8366 | false), |
| 8367 | SourceLocation(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8368 | LastExpr); |
| 8369 | } |
| 8370 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8371 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8372 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8373 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8374 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8375 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8376 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8377 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8378 | StmtExprMayBindToTemp = true; |
| 8379 | } |
| 8380 | } |
| 8381 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8382 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8383 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8384 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8385 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8386 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8387 | if (StmtExprMayBindToTemp) |
| 8388 | return MaybeBindToTemporary(ResStmtExpr); |
| 8389 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8390 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8391 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8392 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8393 | TypeSourceInfo *TInfo, |
| 8394 | OffsetOfComponent *CompPtr, |
| 8395 | unsigned NumComponents, |
| 8396 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8397 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8398 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8399 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8400 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8401 | // We must have at least one component that refers to the type, and the first |
| 8402 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8403 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8404 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8405 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 8406 | << ArgTy << TypeRange); |
| 8407 | |
| 8408 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 8409 | // with an incomplete type would be ill-formed. |
| 8410 | if (!Dependent |
| 8411 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 8412 | PDiag(diag::err_offsetof_incomplete_type) |
| 8413 | << TypeRange)) |
| 8414 | return ExprError(); |
| 8415 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8416 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 8417 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 8418 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 8419 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8420 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8421 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 8422 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8423 | |
| 8424 | bool DidWarnAboutNonPOD = false; |
| 8425 | QualType CurrentType = ArgTy; |
| 8426 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8427 | SmallVector<OffsetOfNode, 4> Comps; |
| 8428 | SmallVector<Expr*, 4> Exprs; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8429 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 8430 | const OffsetOfComponent &OC = CompPtr[i]; |
| 8431 | if (OC.isBrackets) { |
| 8432 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 8433 | if (!CurrentType->isDependentType()) { |
| 8434 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 8435 | if(!AT) |
| 8436 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 8437 | << CurrentType); |
| 8438 | CurrentType = AT->getElementType(); |
| 8439 | } else |
| 8440 | CurrentType = Context.DependentTy; |
| 8441 | |
| 8442 | // The expression must be an integral expression. |
| 8443 | // FIXME: An integral constant expression? |
| 8444 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 8445 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 8446 | !Idx->getType()->isIntegerType()) |
| 8447 | return ExprError(Diag(Idx->getLocStart(), |
| 8448 | diag::err_typecheck_subscript_not_integer) |
| 8449 | << Idx->getSourceRange()); |
| 8450 | |
| 8451 | // Record this array index. |
| 8452 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 8453 | Exprs.push_back(Idx); |
| 8454 | continue; |
| 8455 | } |
| 8456 | |
| 8457 | // Offset of a field. |
| 8458 | if (CurrentType->isDependentType()) { |
| 8459 | // We have the offset of a field, but we can't look into the dependent |
| 8460 | // type. Just record the identifier of the field. |
| 8461 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 8462 | CurrentType = Context.DependentTy; |
| 8463 | continue; |
| 8464 | } |
| 8465 | |
| 8466 | // We need to have a complete type to look into. |
| 8467 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 8468 | diag::err_offsetof_incomplete_type)) |
| 8469 | return ExprError(); |
| 8470 | |
| 8471 | // Look for the designated field. |
| 8472 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 8473 | if (!RC) |
| 8474 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 8475 | << CurrentType); |
| 8476 | RecordDecl *RD = RC->getDecl(); |
| 8477 | |
| 8478 | // C++ [lib.support.types]p5: |
| 8479 | // The macro offsetof accepts a restricted set of type arguments in this |
| 8480 | // International Standard. type shall be a POD structure or a POD union |
| 8481 | // (clause 9). |
| 8482 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 8483 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 8484 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8485 | PDiag(diag::warn_offsetof_non_pod_type) |
| 8486 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 8487 | << CurrentType)) |
| 8488 | DidWarnAboutNonPOD = true; |
| 8489 | } |
| 8490 | |
| 8491 | // Look for the field. |
| 8492 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 8493 | LookupQualifiedName(R, RD); |
| 8494 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8495 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 8496 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 8497 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8498 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 8499 | } |
| 8500 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8501 | if (!MemberDecl) |
| 8502 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 8503 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 8504 | OC.LocEnd)); |
| 8505 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 8506 | // C99 7.17p3: |
| 8507 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 8508 | // |
| 8509 | // We diagnose this as an error. |
| 8510 | if (MemberDecl->getBitWidth()) { |
| 8511 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 8512 | << MemberDecl->getDeclName() |
| 8513 | << SourceRange(BuiltinLoc, RParenLoc); |
| 8514 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 8515 | return ExprError(); |
| 8516 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8517 | |
| 8518 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8519 | if (IndirectMemberDecl) |
| 8520 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8521 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8522 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 8523 | // the base class indirections. |
| 8524 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8525 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8526 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8527 | CXXBasePath &Path = Paths.front(); |
| 8528 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 8529 | B != BEnd; ++B) |
| 8530 | Comps.push_back(OffsetOfNode(B->Base)); |
| 8531 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8532 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8533 | if (IndirectMemberDecl) { |
| 8534 | for (IndirectFieldDecl::chain_iterator FI = |
| 8535 | IndirectMemberDecl->chain_begin(), |
| 8536 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 8537 | assert(isa<FieldDecl>(*FI)); |
| 8538 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 8539 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 8540 | } |
| 8541 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8542 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8543 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8544 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 8545 | } |
| 8546 | |
| 8547 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 8548 | TInfo, Comps.data(), Comps.size(), |
| 8549 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 8550 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8551 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8552 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8553 | SourceLocation BuiltinLoc, |
| 8554 | SourceLocation TypeLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8555 | ParsedType ParsedArgTy, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8556 | OffsetOfComponent *CompPtr, |
| 8557 | unsigned NumComponents, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8558 | SourceLocation RParenLoc) { |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8559 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8560 | TypeSourceInfo *ArgTInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8561 | QualType ArgTy = GetTypeFromParser(ParsedArgTy, &ArgTInfo); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8562 | if (ArgTy.isNull()) |
| 8563 | return ExprError(); |
| 8564 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 8565 | if (!ArgTInfo) |
| 8566 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 8567 | |
| 8568 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8569 | RParenLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8570 | } |
| 8571 | |
| 8572 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8573 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8574 | Expr *CondExpr, |
| 8575 | Expr *LHSExpr, Expr *RHSExpr, |
| 8576 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8577 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 8578 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8579 | ExprValueKind VK = VK_RValue; |
| 8580 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8581 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8582 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 8583 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8584 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8585 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8586 | } else { |
| 8587 | // The conditional expression is required to be a constant expression. |
| 8588 | llvm::APSInt condEval(32); |
| 8589 | SourceLocation ExpLoc; |
| 8590 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8591 | return ExprError(Diag(ExpLoc, |
| 8592 | diag::err_typecheck_choose_expr_requires_constant) |
| 8593 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8594 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8595 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8596 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 8597 | |
| 8598 | resType = ActiveExpr->getType(); |
| 8599 | ValueDependent = ActiveExpr->isValueDependent(); |
| 8600 | VK = ActiveExpr->getValueKind(); |
| 8601 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8602 | } |
| 8603 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8604 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8605 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8606 | resType->isDependentType(), |
| 8607 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8608 | } |
| 8609 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8610 | //===----------------------------------------------------------------------===// |
| 8611 | // Clang Extensions. |
| 8612 | //===----------------------------------------------------------------------===// |
| 8613 | |
| 8614 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8615 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *CurScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8616 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8617 | PushBlockScope(CurScope, Block); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8618 | CurContext->addDecl(Block); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8619 | if (CurScope) |
| 8620 | PushDeclContext(CurScope, Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8621 | else |
| 8622 | CurContext = Block; |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8623 | } |
| 8624 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8625 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 8626 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8627 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8628 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8629 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8630 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8631 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8632 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8633 | // GetTypeForDeclarator always produces a function type for a block |
| 8634 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 8635 | // unless the function was written with a typedef. |
| 8636 | assert(T->isFunctionType() && |
| 8637 | "GetTypeForDeclarator made a non-function block signature"); |
| 8638 | |
| 8639 | // Look for an explicit signature in that function type. |
| 8640 | FunctionProtoTypeLoc ExplicitSignature; |
| 8641 | |
| 8642 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 8643 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 8644 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 8645 | |
| 8646 | // Check whether that explicit signature was synthesized by |
| 8647 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 8648 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8649 | if (ExplicitSignature.getLocalRangeBegin() == |
| 8650 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8651 | // This would be much cheaper if we stored TypeLocs instead of |
| 8652 | // TypeSourceInfos. |
| 8653 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 8654 | unsigned Size = Result.getFullDataSize(); |
| 8655 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 8656 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 8657 | |
| 8658 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 8659 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8660 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8661 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8662 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 8663 | CurBlock->FunctionType = T; |
| 8664 | |
| 8665 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 8666 | QualType RetTy = Fn->getResultType(); |
| 8667 | bool isVariadic = |
| 8668 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 8669 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8670 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8671 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8672 | // Don't allow returning a objc interface by value. |
| 8673 | if (RetTy->isObjCObjectType()) { |
| 8674 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 8675 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 8676 | return; |
| 8677 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8678 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8679 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8680 | // return type. TODO: what should we do with declarators like: |
| 8681 | // ^ * { ... } |
| 8682 | // If the answer is "apply template argument deduction".... |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8683 | if (RetTy != Context.DependentTy) |
| 8684 | CurBlock->ReturnType = RetTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8685 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8686 | // Push block parameters from the declarator if we had them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8687 | SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8688 | if (ExplicitSignature) { |
| 8689 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 8690 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8691 | if (Param->getIdentifier() == 0 && |
| 8692 | !Param->isImplicit() && |
| 8693 | !Param->isInvalidDecl() && |
| 8694 | !getLangOptions().CPlusPlus) |
| 8695 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8696 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8697 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8698 | |
| 8699 | // Fake up parameter variables if we have a typedef, like |
| 8700 | // ^ fntype { ... } |
| 8701 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 8702 | for (FunctionProtoType::arg_type_iterator |
| 8703 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 8704 | ParmVarDecl *Param = |
| 8705 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 8706 | ParamInfo.getSourceRange().getBegin(), |
| 8707 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8708 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8709 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8710 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8711 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8712 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8713 | if (!Params.empty()) { |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8714 | CurBlock->TheDecl->setParams(Params); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8715 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 8716 | CurBlock->TheDecl->param_end(), |
| 8717 | /*CheckParameterNames=*/false); |
| 8718 | } |
| 8719 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8720 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 8721 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8722 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8723 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8724 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 8725 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 8726 | // FIXME: remove the attribute. |
| 8727 | } |
| 8728 | |
| 8729 | // Put the parameter variables in scope. We can bail out immediately |
| 8730 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8731 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8732 | return; |
| 8733 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8734 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8735 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 8736 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 8737 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8738 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8739 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8740 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8741 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8742 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8743 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8744 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8745 | } |
| 8746 | |
| 8747 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 8748 | /// is invoked to pop the information about the block from the action impl. |
| 8749 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8750 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 8751 | PopDeclContext(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8752 | PopFunctionOrBlockScope(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8753 | } |
| 8754 | |
| 8755 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 8756 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8757 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8758 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 8759 | // If blocks are disabled, emit an error. |
| 8760 | if (!LangOpts.Blocks) |
| 8761 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8762 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8763 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8764 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8765 | PopDeclContext(); |
| 8766 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8767 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 8768 | if (!BSI->ReturnType.isNull()) |
| 8769 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8770 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 8771 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8772 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8773 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8774 | // Set the captured variables on the block. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 8775 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 8776 | BSI->CapturesCXXThis); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8777 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8778 | // If the user wrote a function type in some form, try to use that. |
| 8779 | if (!BSI->FunctionType.isNull()) { |
| 8780 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 8781 | |
| 8782 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 8783 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 8784 | |
| 8785 | // Turn protoless block types into nullary block types. |
| 8786 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8787 | FunctionProtoType::ExtProtoInfo EPI; |
| 8788 | EPI.ExtInfo = Ext; |
| 8789 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8790 | |
| 8791 | // Otherwise, if we don't need to change anything about the function type, |
| 8792 | // preserve its sugar structure. |
| 8793 | } else if (FTy->getResultType() == RetTy && |
| 8794 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 8795 | BlockTy = BSI->FunctionType; |
| 8796 | |
| 8797 | // Otherwise, make the minimal modifications to the function type. |
| 8798 | } else { |
| 8799 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8800 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8801 | EPI.TypeQuals = 0; // FIXME: silently? |
| 8802 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8803 | BlockTy = Context.getFunctionType(RetTy, |
| 8804 | FPT->arg_type_begin(), |
| 8805 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8806 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8807 | } |
| 8808 | |
| 8809 | // If we don't have a function type, just build one from nothing. |
| 8810 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8811 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8812 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8813 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8814 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8815 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8816 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 8817 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8818 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8819 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 8820 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8821 | if (getCurFunction()->NeedsScopeChecking() && |
| 8822 | !hasAnyUnrecoverableErrorsInThisFunction()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8823 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8824 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8825 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8826 | |
Fariborz Jahanian | 4e7c7f2 | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 8827 | for (BlockDecl::capture_const_iterator ci = BSI->TheDecl->capture_begin(), |
| 8828 | ce = BSI->TheDecl->capture_end(); ci != ce; ++ci) { |
| 8829 | const VarDecl *variable = ci->getVariable(); |
| 8830 | QualType T = variable->getType(); |
| 8831 | QualType::DestructionKind destructKind = T.isDestructedType(); |
| 8832 | if (destructKind != QualType::DK_none) |
| 8833 | getCurFunction()->setHasBranchProtectedScope(); |
| 8834 | } |
| 8835 | |
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 8836 | computeNRVO(Body, getCurBlock()); |
| 8837 | |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 8838 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
| 8839 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 8840 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
| 8841 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8842 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8843 | } |
| 8844 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8845 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8846 | Expr *E, ParsedType Ty, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8847 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8848 | TypeSourceInfo *TInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8849 | GetTypeFromParser(Ty, &TInfo); |
| 8850 | return BuildVAArgExpr(BuiltinLoc, E, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8851 | } |
| 8852 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8853 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8854 | Expr *E, TypeSourceInfo *TInfo, |
| 8855 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8856 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8857 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8858 | // Get the va_list type |
| 8859 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8860 | if (VaListType->isArrayType()) { |
| 8861 | // Deal with implicit array decay; for example, on x86-64, |
| 8862 | // va_list is an array, but it's supposed to decay to |
| 8863 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8864 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8865 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8866 | ExprResult Result = UsualUnaryConversions(E); |
| 8867 | if (Result.isInvalid()) |
| 8868 | return ExprError(); |
| 8869 | E = Result.take(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8870 | } else { |
| 8871 | // Otherwise, the va_list argument must be an l-value because |
| 8872 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8873 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8874 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8875 | return ExprError(); |
| 8876 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8877 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8878 | if (!E->isTypeDependent() && |
| 8879 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8880 | return ExprError(Diag(E->getLocStart(), |
| 8881 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8882 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 8883 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8884 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8885 | if (!TInfo->getType()->isDependentType()) { |
| 8886 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
| 8887 | PDiag(diag::err_second_parameter_to_va_arg_incomplete) |
| 8888 | << TInfo->getTypeLoc().getSourceRange())) |
| 8889 | return ExprError(); |
David Majnemer | db11b01 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 8890 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8891 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
| 8892 | TInfo->getType(), |
| 8893 | PDiag(diag::err_second_parameter_to_va_arg_abstract) |
| 8894 | << TInfo->getTypeLoc().getSourceRange())) |
| 8895 | return ExprError(); |
| 8896 | |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8897 | if (!TInfo->getType().isPODType(Context)) { |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8898 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8899 | TInfo->getType()->isObjCLifetimeType() |
| 8900 | ? diag::warn_second_parameter_to_va_arg_ownership_qualified |
| 8901 | : diag::warn_second_parameter_to_va_arg_not_pod) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8902 | << TInfo->getType() |
| 8903 | << TInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8904 | } |
Eli Friedman | 46d37c1 | 2011-07-11 21:45:59 +0000 | [diff] [blame] | 8905 | |
| 8906 | // Check for va_arg where arguments of the given type will be promoted |
| 8907 | // (i.e. this va_arg is guaranteed to have undefined behavior). |
| 8908 | QualType PromoteType; |
| 8909 | if (TInfo->getType()->isPromotableIntegerType()) { |
| 8910 | PromoteType = Context.getPromotedIntegerType(TInfo->getType()); |
| 8911 | if (Context.typesAreCompatible(PromoteType, TInfo->getType())) |
| 8912 | PromoteType = QualType(); |
| 8913 | } |
| 8914 | if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) |
| 8915 | PromoteType = Context.DoubleTy; |
| 8916 | if (!PromoteType.isNull()) |
| 8917 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 8918 | diag::warn_second_parameter_to_va_arg_never_compatible) |
| 8919 | << TInfo->getType() |
| 8920 | << PromoteType |
| 8921 | << TInfo->getTypeLoc().getSourceRange(); |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8922 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8923 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8924 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 8925 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 8926 | } |
| 8927 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8928 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8929 | // The type of __null will be int or long, depending on the size of |
| 8930 | // pointers on the target. |
| 8931 | QualType Ty; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8932 | unsigned pw = Context.getTargetInfo().getPointerWidth(0); |
| 8933 | if (pw == Context.getTargetInfo().getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8934 | Ty = Context.IntTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8935 | else if (pw == Context.getTargetInfo().getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8936 | Ty = Context.LongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8937 | else if (pw == Context.getTargetInfo().getLongLongWidth()) |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8938 | Ty = Context.LongLongTy; |
| 8939 | else { |
Richard Trieu | 47456fa | 2011-09-21 02:50:14 +0000 | [diff] [blame] | 8940 | assert(0 && "I don't know size of pointer!"); |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8941 | Ty = Context.IntTy; |
| 8942 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8943 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8944 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8945 | } |
| 8946 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8947 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8948 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8949 | if (!SemaRef.getLangOptions().ObjC1) |
| 8950 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8951 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8952 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 8953 | if (!PT) |
| 8954 | return; |
| 8955 | |
| 8956 | // Check if the destination is of type 'id'. |
| 8957 | if (!PT->isObjCIdType()) { |
| 8958 | // Check if the destination is the 'NSString' interface. |
| 8959 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 8960 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 8961 | return; |
| 8962 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8963 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8964 | // Strip off any parens and casts. |
| 8965 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 8966 | if (!SL || !SL->isAscii()) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8967 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8968 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8969 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8970 | } |
| 8971 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8972 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 8973 | SourceLocation Loc, |
| 8974 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8975 | Expr *SrcExpr, AssignmentAction Action, |
| 8976 | bool *Complained) { |
| 8977 | if (Complained) |
| 8978 | *Complained = false; |
| 8979 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8980 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8981 | bool CheckInferredResultType = false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8982 | bool isInvalid = false; |
| 8983 | unsigned DiagKind; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8984 | FixItHint Hint; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8985 | ConversionFixItGenerator ConvHints; |
| 8986 | bool MayHaveConvFixit = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8987 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8988 | switch (ConvTy) { |
| 8989 | default: assert(0 && "Unknown conversion type"); |
| 8990 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8991 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8992 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8993 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8994 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8995 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8996 | case IntToPointer: |
| 8997 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8998 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8999 | MayHaveConvFixit = true; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9000 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9001 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9002 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9003 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9004 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 9005 | SrcType->isObjCObjectPointerType(); |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9006 | if (Hint.isNull() && !CheckInferredResultType) { |
| 9007 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9008 | } |
| 9009 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9010 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9011 | case IncompatiblePointerSign: |
| 9012 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 9013 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9014 | case FunctionVoidPointer: |
| 9015 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 9016 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9017 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9018 | // Perform array-to-pointer decay if necessary. |
| 9019 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9020 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9021 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9022 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9023 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9024 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9025 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9026 | |
| 9027 | |
| 9028 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 9029 | DiagKind = diag::err_typecheck_incompatible_ownership; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9030 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9031 | } |
| 9032 | |
| 9033 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9034 | // fallthrough |
| 9035 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9036 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9037 | // If the qualifiers lost were because we were applying the |
| 9038 | // (deprecated) C++ conversion from a string literal to a char* |
| 9039 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9040 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9041 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9042 | // bit of refactoring (so that the second argument is an |
| 9043 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9044 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9045 | // C++ semantics. |
| 9046 | if (getLangOptions().CPlusPlus && |
| 9047 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9048 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9049 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9050 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9051 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9052 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9053 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9054 | case IntToBlockPointer: |
| 9055 | DiagKind = diag::err_int_to_block_pointer; |
| 9056 | break; |
| 9057 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9058 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9059 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9060 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9061 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9062 | // it can give a more specific diagnostic. |
| 9063 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9064 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9065 | case IncompatibleVectors: |
| 9066 | DiagKind = diag::warn_incompatible_vectors; |
| 9067 | break; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 9068 | case IncompatibleObjCWeakRef: |
| 9069 | DiagKind = diag::err_arc_weak_unavailable_assign; |
| 9070 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9071 | case Incompatible: |
| 9072 | DiagKind = diag::err_typecheck_convert_incompatible; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9073 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9074 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9075 | isInvalid = true; |
| 9076 | break; |
| 9077 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9078 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9079 | QualType FirstType, SecondType; |
| 9080 | switch (Action) { |
| 9081 | case AA_Assigning: |
| 9082 | case AA_Initializing: |
| 9083 | // The destination type comes first. |
| 9084 | FirstType = DstType; |
| 9085 | SecondType = SrcType; |
| 9086 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9087 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9088 | case AA_Returning: |
| 9089 | case AA_Passing: |
| 9090 | case AA_Converting: |
| 9091 | case AA_Sending: |
| 9092 | case AA_Casting: |
| 9093 | // The source type comes first. |
| 9094 | FirstType = SrcType; |
| 9095 | SecondType = DstType; |
| 9096 | break; |
| 9097 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9098 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9099 | PartialDiagnostic FDiag = PDiag(DiagKind); |
| 9100 | FDiag << FirstType << SecondType << Action << SrcExpr->getSourceRange(); |
| 9101 | |
| 9102 | // If we can fix the conversion, suggest the FixIts. |
| 9103 | assert(ConvHints.isNull() || Hint.isNull()); |
| 9104 | if (!ConvHints.isNull()) { |
| 9105 | for (llvm::SmallVector<FixItHint, 1>::iterator |
| 9106 | HI = ConvHints.Hints.begin(), HE = ConvHints.Hints.end(); |
| 9107 | HI != HE; ++HI) |
| 9108 | FDiag << *HI; |
| 9109 | } else { |
| 9110 | FDiag << Hint; |
| 9111 | } |
| 9112 | if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } |
| 9113 | |
| 9114 | Diag(Loc, FDiag); |
| 9115 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9116 | if (CheckInferredResultType) |
| 9117 | EmitRelatedResultTypeNote(SrcExpr); |
| 9118 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9119 | if (Complained) |
| 9120 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9121 | return isInvalid; |
| 9122 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9123 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 9124 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9125 | llvm::APSInt ICEResult; |
| 9126 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 9127 | if (Result) |
| 9128 | *Result = ICEResult; |
| 9129 | return false; |
| 9130 | } |
| 9131 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9132 | Expr::EvalResult EvalResult; |
| 9133 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9134 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9135 | EvalResult.HasSideEffects) { |
| 9136 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 9137 | |
| 9138 | if (EvalResult.Diag) { |
| 9139 | // We only show the note if it's not the usual "invalid subexpression" |
| 9140 | // or if it's actually in a subexpression. |
| 9141 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 9142 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 9143 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 9144 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9145 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9146 | return true; |
| 9147 | } |
| 9148 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9149 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 9150 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9151 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9152 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9153 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 9154 | != Diagnostic::Ignored) |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9155 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9156 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9157 | if (Result) |
| 9158 | *Result = EvalResult.Val.getInt(); |
| 9159 | return false; |
| 9160 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9161 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9162 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9163 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9164 | ExprEvalContexts.push_back( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9165 | ExpressionEvaluationContextRecord(NewContext, |
| 9166 | ExprTemporaries.size(), |
| 9167 | ExprNeedsCleanups)); |
| 9168 | ExprNeedsCleanups = false; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9169 | } |
| 9170 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9171 | void Sema::PopExpressionEvaluationContext() { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9172 | // Pop the current expression evaluation context off the stack. |
| 9173 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 9174 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9175 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9176 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 9177 | if (Rec.PotentiallyReferenced) { |
| 9178 | // Mark any remaining declarations in the current position of the stack |
| 9179 | // as "referenced". If they were not meant to be referenced, semantic |
| 9180 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9181 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9182 | I = Rec.PotentiallyReferenced->begin(), |
| 9183 | IEnd = Rec.PotentiallyReferenced->end(); |
| 9184 | I != IEnd; ++I) |
| 9185 | MarkDeclarationReferenced(I->first, I->second); |
| 9186 | } |
| 9187 | |
| 9188 | if (Rec.PotentiallyDiagnosed) { |
| 9189 | // Emit any pending diagnostics. |
| 9190 | for (PotentiallyEmittedDiagnostics::iterator |
| 9191 | I = Rec.PotentiallyDiagnosed->begin(), |
| 9192 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 9193 | I != IEnd; ++I) |
| 9194 | Diag(I->first, I->second); |
| 9195 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9196 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9197 | |
| 9198 | // When are coming out of an unevaluated context, clear out any |
| 9199 | // temporaries that we may have created as part of the evaluation of |
| 9200 | // the expression in that context: they aren't relevant because they |
| 9201 | // will never be constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9202 | if (Rec.Context == Unevaluated) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9203 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 9204 | ExprTemporaries.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9205 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
| 9206 | |
| 9207 | // Otherwise, merge the contexts together. |
| 9208 | } else { |
| 9209 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
| 9210 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9211 | |
| 9212 | // Destroy the popped expression evaluation record. |
| 9213 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9214 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9215 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9216 | void Sema::DiscardCleanupsInEvaluationContext() { |
| 9217 | ExprTemporaries.erase( |
| 9218 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 9219 | ExprTemporaries.end()); |
| 9220 | ExprNeedsCleanups = false; |
| 9221 | } |
| 9222 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9223 | /// \brief Note that the given declaration was referenced in the source code. |
| 9224 | /// |
| 9225 | /// This routine should be invoke whenever a given declaration is referenced |
| 9226 | /// in the source code, and where that reference occurred. If this declaration |
| 9227 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 9228 | /// C99 6.9p3), then the declaration will be marked as used. |
| 9229 | /// |
| 9230 | /// \param Loc the location where the declaration was referenced. |
| 9231 | /// |
| 9232 | /// \param D the declaration that has been referenced by the source code. |
| 9233 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 9234 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9235 | |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 9236 | D->setReferenced(); |
| 9237 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9238 | if (D->isUsed(false)) |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9239 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9240 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9241 | // Mark a parameter or variable declaration "used", regardless of whether |
| 9242 | // we're in a template or not. The reason for this is that unevaluated |
| 9243 | // expressions (e.g. (void)sizeof()) constitute a use for warning purposes |
| 9244 | // (-Wunused-variables and -Wunused-parameters) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9245 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9246 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 9247 | D->setUsed(); |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9248 | return; |
| 9249 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9250 | |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9251 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 9252 | return; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9253 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9254 | // Do not mark anything as "used" within a dependent context; wait for |
| 9255 | // an instantiation. |
| 9256 | if (CurContext->isDependentContext()) |
| 9257 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9258 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9259 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9260 | case Unevaluated: |
| 9261 | // We are in an expression that is not potentially evaluated; do nothing. |
| 9262 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9263 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9264 | case PotentiallyEvaluated: |
| 9265 | // We are in a potentially-evaluated expression, so this declaration is |
| 9266 | // "used"; handle this below. |
| 9267 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9268 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9269 | case PotentiallyPotentiallyEvaluated: |
| 9270 | // We are in an expression that may be potentially evaluated; queue this |
| 9271 | // declaration reference until we know whether the expression is |
| 9272 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9273 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9274 | return; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9275 | |
| 9276 | case PotentiallyEvaluatedIfUsed: |
| 9277 | // Referenced declarations will only be used if the construct in the |
| 9278 | // containing expression is used. |
| 9279 | return; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9280 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9281 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9282 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 9283 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9284 | if (Constructor->isDefaulted()) { |
| 9285 | if (Constructor->isDefaultConstructor()) { |
| 9286 | if (Constructor->isTrivial()) |
| 9287 | return; |
| 9288 | if (!Constructor->isUsed(false)) |
| 9289 | DefineImplicitDefaultConstructor(Loc, Constructor); |
| 9290 | } else if (Constructor->isCopyConstructor()) { |
| 9291 | if (!Constructor->isUsed(false)) |
| 9292 | DefineImplicitCopyConstructor(Loc, Constructor); |
| 9293 | } else if (Constructor->isMoveConstructor()) { |
| 9294 | if (!Constructor->isUsed(false)) |
| 9295 | DefineImplicitMoveConstructor(Loc, Constructor); |
| 9296 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9297 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9298 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9299 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9300 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9301 | if (Destructor->isDefaulted() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9302 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9303 | if (Destructor->isVirtual()) |
| 9304 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9305 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 9306 | if (MethodDecl->isDefaulted() && MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9307 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9308 | if (!MethodDecl->isUsed(false)) { |
| 9309 | if (MethodDecl->isCopyAssignmentOperator()) |
| 9310 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
| 9311 | else |
| 9312 | DefineImplicitMoveAssignment(Loc, MethodDecl); |
| 9313 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9314 | } else if (MethodDecl->isVirtual()) |
| 9315 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9316 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 9317 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9318 | // Recursive functions should be marked when used from another function. |
| 9319 | if (CurContext == Function) return; |
| 9320 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9321 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 9322 | // class templates. |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 9323 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9324 | bool AlreadyInstantiated = false; |
| 9325 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 9326 | = Function->getTemplateSpecializationInfo()) { |
| 9327 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 9328 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9329 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9330 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9331 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9332 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9333 | = Function->getMemberSpecializationInfo()) { |
| 9334 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 9335 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9336 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9337 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9338 | AlreadyInstantiated = true; |
| 9339 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9340 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9341 | if (!AlreadyInstantiated) { |
| 9342 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 9343 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 9344 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 9345 | Loc)); |
| 9346 | else |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9347 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9348 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9349 | } else { |
| 9350 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9351 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 9352 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | be9ebe3 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 9353 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9354 | MarkDeclarationReferenced(Loc, *i); |
| 9355 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9356 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9357 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9358 | // Keep track of used but undefined functions. |
| 9359 | if (!Function->isPure() && !Function->hasBody() && |
| 9360 | Function->getLinkage() != ExternalLinkage) { |
| 9361 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 9362 | if (old.isInvalid()) old = Loc; |
| 9363 | } |
Argyrios Kyrtzidis | 58b5259 | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 9364 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9365 | Function->setUsed(true); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9366 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9367 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9368 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9369 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9370 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9371 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9372 | Var->getInstantiatedFromStaticDataMember()) { |
| 9373 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 9374 | assert(MSInfo && "Missing member specialization information?"); |
| 9375 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 9376 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 9377 | MSInfo->setPointOfInstantiation(Loc); |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 9378 | // This is a modification of an existing AST node. Notify listeners. |
| 9379 | if (ASTMutationListener *L = getASTMutationListener()) |
| 9380 | L->StaticDataMemberInstantiated(Var); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9381 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9382 | } |
| 9383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9384 | |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9385 | // Keep track of used but undefined variables. We make a hole in |
| 9386 | // the warning for static const data members with in-line |
| 9387 | // initializers. |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9388 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9389 | && Var->getLinkage() != ExternalLinkage |
| 9390 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9391 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 9392 | if (old.isInvalid()) old = Loc; |
| 9393 | } |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9394 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9395 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9396 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 9397 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9398 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9399 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9400 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9401 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9402 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9403 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9404 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 9405 | Sema &S; |
| 9406 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9407 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9408 | public: |
| 9409 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9410 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9411 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9412 | |
| 9413 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 9414 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9415 | }; |
| 9416 | } |
| 9417 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9418 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 9419 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9420 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 9421 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 9422 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9423 | |
| 9424 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9425 | } |
| 9426 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9427 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9428 | if (ClassTemplateSpecializationDecl *Spec |
| 9429 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 9430 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9431 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9432 | } |
| 9433 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 9434 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9435 | } |
| 9436 | |
| 9437 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 9438 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9439 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9440 | } |
| 9441 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9442 | namespace { |
| 9443 | /// \brief Helper class that marks all of the declarations referenced by |
| 9444 | /// potentially-evaluated subexpressions as "referenced". |
| 9445 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 9446 | Sema &S; |
| 9447 | |
| 9448 | public: |
| 9449 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 9450 | |
| 9451 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 9452 | |
| 9453 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 9454 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9455 | } |
| 9456 | |
| 9457 | void VisitMemberExpr(MemberExpr *E) { |
| 9458 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9459 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9460 | } |
| 9461 | |
| 9462 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 9463 | if (E->getConstructor()) |
| 9464 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 9465 | if (E->getOperatorNew()) |
| 9466 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 9467 | if (E->getOperatorDelete()) |
| 9468 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9469 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9470 | } |
| 9471 | |
| 9472 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 9473 | if (E->getOperatorDelete()) |
| 9474 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 9475 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 9476 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 9477 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 9478 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 9479 | S.LookupDestructor(Record)); |
| 9480 | } |
| 9481 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9482 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9483 | } |
| 9484 | |
| 9485 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 9486 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9487 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9488 | } |
| 9489 | |
| 9490 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 9491 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9492 | } |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 9493 | |
| 9494 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 9495 | Visit(E->getExpr()); |
| 9496 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9497 | }; |
| 9498 | } |
| 9499 | |
| 9500 | /// \brief Mark any declarations that appear within this expression or any |
| 9501 | /// potentially-evaluated subexpressions as "referenced". |
| 9502 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 9503 | EvaluatedExprMarker(*this).Visit(E); |
| 9504 | } |
| 9505 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9506 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 9507 | /// of the program being compiled. |
| 9508 | /// |
| 9509 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9510 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9511 | /// possibility that the code will actually be executable. Code in sizeof() |
| 9512 | /// expressions, code used only during overload resolution, etc., are not |
| 9513 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 9514 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9515 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9516 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9517 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9518 | /// This routine should be used for all diagnostics that describe the run-time |
| 9519 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 9520 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 9521 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9522 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *Statement, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9523 | const PartialDiagnostic &PD) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9524 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9525 | case Unevaluated: |
| 9526 | // The argument will never be evaluated, so don't complain. |
| 9527 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9528 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9529 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9530 | case PotentiallyEvaluatedIfUsed: |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9531 | if (Statement && getCurFunctionOrMethodDecl()) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 9532 | FunctionScopes.back()->PossiblyUnreachableDiags. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9533 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, Statement)); |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 9534 | } |
| 9535 | else |
| 9536 | Diag(Loc, PD); |
| 9537 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9538 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9539 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9540 | case PotentiallyPotentiallyEvaluated: |
| 9541 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 9542 | break; |
| 9543 | } |
| 9544 | |
| 9545 | return false; |
| 9546 | } |
| 9547 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9548 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 9549 | CallExpr *CE, FunctionDecl *FD) { |
| 9550 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 9551 | return false; |
| 9552 | |
| 9553 | PartialDiagnostic Note = |
| 9554 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 9555 | << FD->getDeclName() : PDiag(); |
| 9556 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9557 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9558 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9559 | FD ? |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9560 | PDiag(diag::err_call_function_incomplete_return) |
| 9561 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9562 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9563 | << CE->getSourceRange(), |
| 9564 | std::make_pair(NoteLoc, Note))) |
| 9565 | return true; |
| 9566 | |
| 9567 | return false; |
| 9568 | } |
| 9569 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9570 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9571 | // will prevent this condition from triggering, which is what we want. |
| 9572 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 9573 | SourceLocation Loc; |
| 9574 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9575 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9576 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9577 | |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 9578 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9579 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9580 | return; |
| 9581 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9582 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 9583 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9584 | // Greylist some idioms by putting them into a warning subcategory. |
| 9585 | if (ObjCMessageExpr *ME |
| 9586 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 9587 | Selector Sel = ME->getSelector(); |
| 9588 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9589 | // self = [<foo> init...] |
Fariborz Jahanian | 1d4e8e9 | 2011-09-17 19:23:40 +0000 | [diff] [blame] | 9590 | if (GetMethodIfSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9591 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9592 | |
| 9593 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9594 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9595 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9596 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9597 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9598 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 9599 | } else if (CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9600 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9601 | return; |
| 9602 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9603 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9604 | Loc = Op->getOperatorLoc(); |
| 9605 | } else { |
| 9606 | // Not an assignment. |
| 9607 | return; |
| 9608 | } |
| 9609 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9610 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9611 | |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9612 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 9613 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 9614 | Diag(Loc, diag::note_condition_assign_silence) |
| 9615 | << FixItHint::CreateInsertion(Open, "(") |
| 9616 | << FixItHint::CreateInsertion(Close, ")"); |
| 9617 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9618 | if (IsOrAssign) |
| 9619 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 9620 | << FixItHint::CreateReplacement(Loc, "!="); |
| 9621 | else |
| 9622 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 9623 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9624 | } |
| 9625 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9626 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 9627 | /// that the user intended an assignment used as condition. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9628 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *ParenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9629 | // Don't warn if the parens came from a macro. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9630 | SourceLocation parenLoc = ParenE->getLocStart(); |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9631 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 9632 | return; |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 9633 | // Don't warn for dependent expressions. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9634 | if (ParenE->isTypeDependent()) |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 9635 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9636 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9637 | Expr *E = ParenE->IgnoreParens(); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9638 | |
| 9639 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 9640 | if (opE->getOpcode() == BO_EQ && |
| 9641 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 9642 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9643 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 9644 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9645 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9646 | Diag(Loc, diag::note_equality_comparison_silence) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9647 | << FixItHint::CreateRemoval(ParenE->getSourceRange().getBegin()) |
| 9648 | << FixItHint::CreateRemoval(ParenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9649 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 9650 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9651 | } |
| 9652 | } |
| 9653 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9654 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9655 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9656 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 9657 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9658 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9659 | ExprResult result = CheckPlaceholderExpr(E); |
| 9660 | if (result.isInvalid()) return ExprError(); |
| 9661 | E = result.take(); |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 9662 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9663 | if (!E->isTypeDependent()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9664 | if (getLangOptions().CPlusPlus) |
| 9665 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 9666 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9667 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 9668 | if (ERes.isInvalid()) |
| 9669 | return ExprError(); |
| 9670 | E = ERes.take(); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 9671 | |
| 9672 | QualType T = E->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9673 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 9674 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 9675 | << T << E->getSourceRange(); |
| 9676 | return ExprError(); |
| 9677 | } |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9678 | } |
| 9679 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9680 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9681 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9682 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9683 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9684 | Expr *SubExpr) { |
| 9685 | if (!SubExpr) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9686 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9687 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9688 | return CheckBooleanCondition(SubExpr, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9689 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9690 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9691 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9692 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 9693 | /// to have an appropriate type. |
| 9694 | struct RebuildUnknownAnyFunction |
| 9695 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 9696 | |
| 9697 | Sema &S; |
| 9698 | |
| 9699 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 9700 | |
| 9701 | ExprResult VisitStmt(Stmt *S) { |
| 9702 | llvm_unreachable("unexpected statement!"); |
| 9703 | return ExprError(); |
| 9704 | } |
| 9705 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9706 | ExprResult VisitExpr(Expr *E) { |
| 9707 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 9708 | << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9709 | return ExprError(); |
| 9710 | } |
| 9711 | |
| 9712 | /// Rebuild an expression which simply semantically wraps another |
| 9713 | /// expression which it shares the type and value kind of. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9714 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 9715 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 9716 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9717 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9718 | Expr *SubExpr = SubResult.take(); |
| 9719 | E->setSubExpr(SubExpr); |
| 9720 | E->setType(SubExpr->getType()); |
| 9721 | E->setValueKind(SubExpr->getValueKind()); |
| 9722 | assert(E->getObjectKind() == OK_Ordinary); |
| 9723 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9724 | } |
| 9725 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9726 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 9727 | return rebuildSugarExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9728 | } |
| 9729 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9730 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 9731 | return rebuildSugarExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9732 | } |
| 9733 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9734 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 9735 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 9736 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9737 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9738 | Expr *SubExpr = SubResult.take(); |
| 9739 | E->setSubExpr(SubExpr); |
| 9740 | E->setType(S.Context.getPointerType(SubExpr->getType())); |
| 9741 | assert(E->getValueKind() == VK_RValue); |
| 9742 | assert(E->getObjectKind() == OK_Ordinary); |
| 9743 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9744 | } |
| 9745 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9746 | ExprResult resolveDecl(Expr *E, ValueDecl *VD) { |
| 9747 | if (!isa<FunctionDecl>(VD)) return VisitExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9748 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9749 | E->setType(VD->getType()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9750 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9751 | assert(E->getValueKind() == VK_RValue); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9752 | if (S.getLangOptions().CPlusPlus && |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9753 | !(isa<CXXMethodDecl>(VD) && |
| 9754 | cast<CXXMethodDecl>(VD)->isInstance())) |
| 9755 | E->setValueKind(VK_LValue); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9756 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9757 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9758 | } |
| 9759 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9760 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 9761 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9762 | } |
| 9763 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9764 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 9765 | return resolveDecl(E, E->getDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9766 | } |
| 9767 | }; |
| 9768 | } |
| 9769 | |
| 9770 | /// Given a function expression of unknown-any type, try to rebuild it |
| 9771 | /// to have a function type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9772 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *FunctionExpr) { |
| 9773 | ExprResult Result = RebuildUnknownAnyFunction(S).Visit(FunctionExpr); |
| 9774 | if (Result.isInvalid()) return ExprError(); |
| 9775 | return S.DefaultFunctionArrayConversion(Result.take()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9776 | } |
| 9777 | |
| 9778 | namespace { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9779 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 9780 | /// into one which resolves the type directly on the referring |
| 9781 | /// expression. Strict preservation of the original source |
| 9782 | /// structure is not a goal. |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9783 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9784 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9785 | |
| 9786 | Sema &S; |
| 9787 | |
| 9788 | /// The current destination type. |
| 9789 | QualType DestType; |
| 9790 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9791 | RebuildUnknownAnyExpr(Sema &S, QualType CastType) |
| 9792 | : S(S), DestType(CastType) {} |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9793 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9794 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9795 | llvm_unreachable("unexpected statement!"); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9796 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9797 | } |
| 9798 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9799 | ExprResult VisitExpr(Expr *E) { |
| 9800 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 9801 | << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9802 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9803 | } |
| 9804 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9805 | ExprResult VisitCallExpr(CallExpr *E); |
| 9806 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9807 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9808 | /// Rebuild an expression which simply semantically wraps another |
| 9809 | /// expression which it shares the type and value kind of. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9810 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 9811 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 9812 | if (SubResult.isInvalid()) return ExprError(); |
| 9813 | Expr *SubExpr = SubResult.take(); |
| 9814 | E->setSubExpr(SubExpr); |
| 9815 | E->setType(SubExpr->getType()); |
| 9816 | E->setValueKind(SubExpr->getValueKind()); |
| 9817 | assert(E->getObjectKind() == OK_Ordinary); |
| 9818 | return E; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9819 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9820 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9821 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 9822 | return rebuildSugarExpr(E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9823 | } |
| 9824 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9825 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 9826 | return rebuildSugarExpr(E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9827 | } |
| 9828 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9829 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 9830 | const PointerType *Ptr = DestType->getAs<PointerType>(); |
| 9831 | if (!Ptr) { |
| 9832 | S.Diag(E->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 9833 | << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9834 | return ExprError(); |
| 9835 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9836 | assert(E->getValueKind() == VK_RValue); |
| 9837 | assert(E->getObjectKind() == OK_Ordinary); |
| 9838 | E->setType(DestType); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9839 | |
| 9840 | // Build the sub-expression as if it were an object of the pointee type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9841 | DestType = Ptr->getPointeeType(); |
| 9842 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 9843 | if (SubResult.isInvalid()) return ExprError(); |
| 9844 | E->setSubExpr(SubResult.take()); |
| 9845 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9846 | } |
| 9847 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9848 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9849 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9850 | ExprResult resolveDecl(Expr *E, ValueDecl *VD); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9851 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9852 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 9853 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9854 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9855 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9856 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 9857 | return resolveDecl(E, E->getDecl()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9858 | } |
| 9859 | }; |
| 9860 | } |
| 9861 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9862 | /// Rebuilds a call expression which yielded __unknown_anytype. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9863 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) { |
| 9864 | Expr *CalleeExpr = E->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9865 | |
| 9866 | enum FnKind { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9867 | FK_MemberFunction, |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9868 | FK_FunctionPointer, |
| 9869 | FK_BlockPointer |
| 9870 | }; |
| 9871 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9872 | FnKind Kind; |
| 9873 | QualType CalleeType = CalleeExpr->getType(); |
| 9874 | if (CalleeType == S.Context.BoundMemberTy) { |
| 9875 | assert(isa<CXXMemberCallExpr>(E) || isa<CXXOperatorCallExpr>(E)); |
| 9876 | Kind = FK_MemberFunction; |
| 9877 | CalleeType = Expr::findBoundMemberType(CalleeExpr); |
| 9878 | } else if (const PointerType *Ptr = CalleeType->getAs<PointerType>()) { |
| 9879 | CalleeType = Ptr->getPointeeType(); |
| 9880 | Kind = FK_FunctionPointer; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9881 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9882 | CalleeType = CalleeType->castAs<BlockPointerType>()->getPointeeType(); |
| 9883 | Kind = FK_BlockPointer; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9884 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9885 | const FunctionType *FnType = CalleeType->castAs<FunctionType>(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9886 | |
| 9887 | // Verify that this is a legal result type of a function. |
| 9888 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 9889 | unsigned diagID = diag::err_func_returning_array_function; |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9890 | if (Kind == FK_BlockPointer) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9891 | diagID = diag::err_block_returning_array_function; |
| 9892 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9893 | S.Diag(E->getExprLoc(), diagID) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9894 | << DestType->isFunctionType() << DestType; |
| 9895 | return ExprError(); |
| 9896 | } |
| 9897 | |
| 9898 | // Otherwise, go ahead and set DestType as the call's result. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9899 | E->setType(DestType.getNonLValueExprType(S.Context)); |
| 9900 | E->setValueKind(Expr::getValueKindForType(DestType)); |
| 9901 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9902 | |
| 9903 | // Rebuild the function type, replacing the result type with DestType. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9904 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType)) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9905 | DestType = S.Context.getFunctionType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9906 | Proto->arg_type_begin(), |
| 9907 | Proto->getNumArgs(), |
| 9908 | Proto->getExtProtoInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9909 | else |
| 9910 | DestType = S.Context.getFunctionNoProtoType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9911 | FnType->getExtInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9912 | |
| 9913 | // Rebuild the appropriate pointer-to-function type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9914 | switch (Kind) { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9915 | case FK_MemberFunction: |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9916 | // Nothing to do. |
| 9917 | break; |
| 9918 | |
| 9919 | case FK_FunctionPointer: |
| 9920 | DestType = S.Context.getPointerType(DestType); |
| 9921 | break; |
| 9922 | |
| 9923 | case FK_BlockPointer: |
| 9924 | DestType = S.Context.getBlockPointerType(DestType); |
| 9925 | break; |
| 9926 | } |
| 9927 | |
| 9928 | // Finally, we can recurse. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9929 | ExprResult CalleeResult = Visit(CalleeExpr); |
| 9930 | if (!CalleeResult.isUsable()) return ExprError(); |
| 9931 | E->setCallee(CalleeResult.take()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9932 | |
| 9933 | // Bind a temporary if necessary. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9934 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9935 | } |
| 9936 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9937 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9938 | // Verify that this is a legal result type of a call. |
| 9939 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9940 | S.Diag(E->getExprLoc(), diag::err_func_returning_array_function) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9941 | << DestType->isFunctionType() << DestType; |
| 9942 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9943 | } |
| 9944 | |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 9945 | // Rewrite the method result type if available. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9946 | if (ObjCMethodDecl *Method = E->getMethodDecl()) { |
| 9947 | assert(Method->getResultType() == S.Context.UnknownAnyTy); |
| 9948 | Method->setResultType(DestType); |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 9949 | } |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9950 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9951 | // Change the type of the message. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9952 | E->setType(DestType.getNonReferenceType()); |
| 9953 | E->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9954 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9955 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9956 | } |
| 9957 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9958 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9959 | // The only case we should ever see here is a function-to-pointer decay. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9960 | assert(E->getCastKind() == CK_FunctionToPointerDecay); |
| 9961 | assert(E->getValueKind() == VK_RValue); |
| 9962 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9963 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9964 | E->setType(DestType); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9965 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9966 | // Rebuild the sub-expression as the pointee (function) type. |
| 9967 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 9968 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9969 | ExprResult Result = Visit(E->getSubExpr()); |
| 9970 | if (!Result.isUsable()) return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9971 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9972 | E->setSubExpr(Result.take()); |
| 9973 | return S.Owned(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9974 | } |
| 9975 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9976 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *E, ValueDecl *VD) { |
| 9977 | ExprValueKind ValueKind = VK_LValue; |
| 9978 | QualType Type = DestType; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9979 | |
| 9980 | // We know how to make this work for certain kinds of decls: |
| 9981 | |
| 9982 | // - functions |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9983 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(VD)) { |
| 9984 | if (const PointerType *Ptr = Type->getAs<PointerType>()) { |
| 9985 | DestType = Ptr->getPointeeType(); |
| 9986 | ExprResult Result = resolveDecl(E, VD); |
| 9987 | if (Result.isInvalid()) return ExprError(); |
| 9988 | return S.ImpCastExprToType(Result.take(), Type, |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 9989 | CK_FunctionToPointerDecay, VK_RValue); |
| 9990 | } |
| 9991 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9992 | if (!Type->isFunctionType()) { |
| 9993 | S.Diag(E->getExprLoc(), diag::err_unknown_any_function) |
| 9994 | << VD << E->getSourceRange(); |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 9995 | return ExprError(); |
| 9996 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9997 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9998 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 9999 | if (MD->isInstance()) { |
| 10000 | ValueKind = VK_RValue; |
| 10001 | Type = S.Context.BoundMemberTy; |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10002 | } |
| 10003 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10004 | // Function references aren't l-values in C. |
| 10005 | if (!S.getLangOptions().CPlusPlus) |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10006 | ValueKind = VK_RValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10007 | |
| 10008 | // - variables |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10009 | } else if (isa<VarDecl>(VD)) { |
| 10010 | if (const ReferenceType *RefTy = Type->getAs<ReferenceType>()) { |
| 10011 | Type = RefTy->getPointeeType(); |
| 10012 | } else if (Type->isFunctionType()) { |
| 10013 | S.Diag(E->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 10014 | << VD << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10015 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10016 | } |
| 10017 | |
| 10018 | // - nothing else |
| 10019 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10020 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 10021 | << VD << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10022 | return ExprError(); |
| 10023 | } |
| 10024 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10025 | VD->setType(DestType); |
| 10026 | E->setType(Type); |
| 10027 | E->setValueKind(ValueKind); |
| 10028 | return S.Owned(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10029 | } |
| 10030 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10031 | /// Check a cast of an unknown-any type. We intentionally only |
| 10032 | /// trigger this for C-style casts. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10033 | ExprResult Sema::checkUnknownAnyCast(SourceRange TypeRange, QualType CastType, |
| 10034 | Expr *CastExpr, CastKind &CastKind, |
| 10035 | ExprValueKind &VK, CXXCastPath &Path) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10036 | // Rewrite the casted expression from scratch. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10037 | ExprResult result = RebuildUnknownAnyExpr(*this, CastType).Visit(CastExpr); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10038 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10039 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10040 | CastExpr = result.take(); |
| 10041 | VK = CastExpr->getValueKind(); |
| 10042 | CastKind = CK_NoOp; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10043 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10044 | return CastExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10045 | } |
| 10046 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10047 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *E) { |
| 10048 | Expr *orig = E; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10049 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10050 | while (true) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10051 | E = E->IgnoreParenImpCasts(); |
| 10052 | if (CallExpr *call = dyn_cast<CallExpr>(E)) { |
| 10053 | E = call->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10054 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10055 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10056 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10057 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10058 | } |
| 10059 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10060 | SourceLocation loc; |
| 10061 | NamedDecl *d; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10062 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10063 | loc = ref->getLocation(); |
| 10064 | d = ref->getDecl(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10065 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10066 | loc = mem->getMemberLoc(); |
| 10067 | d = mem->getMemberDecl(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10068 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10069 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10070 | loc = msg->getSelectorLoc(); |
| 10071 | d = msg->getMethodDecl(); |
John McCall | 819e745 | 2011-08-31 20:57:36 +0000 | [diff] [blame] | 10072 | if (!d) { |
| 10073 | S.Diag(loc, diag::err_uncasted_send_to_unknown_any_method) |
| 10074 | << static_cast<unsigned>(msg->isClassMessage()) << msg->getSelector() |
| 10075 | << orig->getSourceRange(); |
| 10076 | return ExprError(); |
| 10077 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10078 | } else { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10079 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10080 | << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10081 | return ExprError(); |
| 10082 | } |
| 10083 | |
| 10084 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10085 | |
| 10086 | // Never recoverable. |
| 10087 | return ExprError(); |
| 10088 | } |
| 10089 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10090 | /// Check for operands with placeholder types and complain if found. |
| 10091 | /// Returns true if there was an error and no recovery was possible. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 10092 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10093 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 10094 | QualType type = E->getType(); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10095 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10096 | // Overloaded expressions. |
| 10097 | if (type == Context.OverloadTy) |
| 10098 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 10099 | E->getSourceRange(), |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10100 | QualType(), |
| 10101 | diag::err_ovl_unresolvable); |
| 10102 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10103 | // Bound member functions. |
| 10104 | if (type == Context.BoundMemberTy) { |
| 10105 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 10106 | << E->getSourceRange(); |
| 10107 | return ExprError(); |
| 10108 | } |
| 10109 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10110 | // Expressions of unknown type. |
| 10111 | if (type == Context.UnknownAnyTy) |
| 10112 | return diagnoseUnknownAnyExpr(*this, E); |
| 10113 | |
| 10114 | assert(!type->isPlaceholderType()); |
| 10115 | return Owned(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10116 | } |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10117 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10118 | bool Sema::CheckCaseExpression(Expr *E) { |
| 10119 | if (E->isTypeDependent()) |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10120 | return true; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10121 | if (E->isValueDependent() || E->isIntegerConstantExpr(Context)) |
| 10122 | return E->getType()->isIntegralOrEnumerationType(); |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10123 | return false; |
| 10124 | } |