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 | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 1444 | if (getLangOptions().Microsoft) |
| 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 || |
| 2604 | (getLangOptions().Microsoft && 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(), |
| 3795 | CK_ObjCExtendBlockObject, E.get(), |
| 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); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4251 | if (getLangOptions().AltiVec && castType->isVectorType() && (PE || PLE)) { |
| 4252 | if (PLE && PLE->getNumExprs() == 0) { |
| 4253 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4254 | return ExprError(); |
| 4255 | } |
| 4256 | if (PE || PLE->getNumExprs() == 1) { |
| 4257 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); |
| 4258 | if (!E->getType()->isVectorType()) |
| 4259 | isVectorLiteral = true; |
| 4260 | } |
| 4261 | else |
| 4262 | isVectorLiteral = true; |
| 4263 | } |
| 4264 | |
| 4265 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
| 4266 | // then handle it as such. |
| 4267 | if (isVectorLiteral) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4268 | return BuildVectorLiteral(LParenLoc, RParenLoc, CastExpr, castTInfo); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4269 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4270 | // If the Expr being casted is a ParenListExpr, handle it specially. |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4271 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 4272 | // sequence of BinOp comma operators. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4273 | if (isa<ParenListExpr>(CastExpr)) { |
| 4274 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, CastExpr); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4275 | if (Result.isInvalid()) return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4276 | CastExpr = Result.take(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4277 | } |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4278 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4279 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, CastExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4280 | } |
| 4281 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4282 | ExprResult |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4283 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4284 | SourceLocation RParenLoc, Expr *CastExpr) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4285 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4286 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4287 | CXXCastPath BasePath; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4288 | ExprResult CastResult = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4289 | CheckCastTypes(LParenLoc, SourceRange(LParenLoc, RParenLoc), Ty->getType(), |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4290 | CastExpr, Kind, VK, BasePath); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4291 | if (CastResult.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4292 | return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4293 | CastExpr = CastResult.take(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 4294 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4295 | return Owned(CStyleCastExpr::Create( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4296 | Context, Ty->getType().getNonLValueExprType(Context), VK, Kind, CastExpr, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4297 | &BasePath, Ty, LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4300 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
| 4301 | SourceLocation RParenLoc, Expr *E, |
| 4302 | TypeSourceInfo *TInfo) { |
| 4303 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && |
| 4304 | "Expected paren or paren list expression"); |
| 4305 | |
| 4306 | Expr **exprs; |
| 4307 | unsigned numExprs; |
| 4308 | Expr *subExpr; |
| 4309 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { |
| 4310 | exprs = PE->getExprs(); |
| 4311 | numExprs = PE->getNumExprs(); |
| 4312 | } else { |
| 4313 | subExpr = cast<ParenExpr>(E)->getSubExpr(); |
| 4314 | exprs = &subExpr; |
| 4315 | numExprs = 1; |
| 4316 | } |
| 4317 | |
| 4318 | QualType Ty = TInfo->getType(); |
| 4319 | assert(Ty->isVectorType() && "Expected vector type"); |
| 4320 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4321 | SmallVector<Expr *, 8> initExprs; |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4322 | const VectorType *VTy = Ty->getAs<VectorType>(); |
| 4323 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 4324 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4325 | // '(...)' form of vector initialization in AltiVec: the number of |
| 4326 | // initializers must be one or must match the size of the vector. |
| 4327 | // If a single value is specified in the initializer then it will be |
| 4328 | // replicated to all the components of the vector |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4329 | if (VTy->getVectorKind() == VectorType::AltiVecVector) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4330 | // The number of initializers must be one or must match the size of the |
| 4331 | // vector. If a single value is specified in the initializer then it will |
| 4332 | // be replicated to all the components of the vector |
| 4333 | if (numExprs == 1) { |
| 4334 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4335 | ExprResult Literal = Owned(exprs[0]); |
| 4336 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4337 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4338 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4339 | } |
| 4340 | else if (numExprs < numElems) { |
| 4341 | Diag(E->getExprLoc(), |
| 4342 | diag::err_incorrect_number_of_vector_initializers); |
| 4343 | return ExprError(); |
| 4344 | } |
| 4345 | else |
| 4346 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4347 | initExprs.push_back(exprs[i]); |
| 4348 | } |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4349 | else { |
| 4350 | // For OpenCL, when the number of initializers is a single value, |
| 4351 | // it will be replicated to all components of the vector. |
| 4352 | if (getLangOptions().OpenCL && |
| 4353 | VTy->getVectorKind() == VectorType::GenericVector && |
| 4354 | numExprs == 1) { |
| 4355 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4356 | ExprResult Literal = Owned(exprs[0]); |
| 4357 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4358 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4359 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4360 | } |
| 4361 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4362 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4363 | initExprs.push_back(exprs[i]); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4364 | } |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4365 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4366 | // braces instead of the original commas. |
| 4367 | InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc, |
| 4368 | &initExprs[0], |
| 4369 | initExprs.size(), RParenLoc); |
| 4370 | initE->setType(Ty); |
| 4371 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); |
| 4372 | } |
| 4373 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4374 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 4375 | /// of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4376 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4377 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *OrigExpr) { |
| 4378 | ParenListExpr *E = dyn_cast<ParenListExpr>(OrigExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4379 | if (!E) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4380 | return Owned(OrigExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4381 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4382 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4383 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4384 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4385 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4386 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4387 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4388 | if (Result.isInvalid()) return ExprError(); |
| 4389 | |
| 4390 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4393 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4394 | SourceLocation R, |
| 4395 | MultiExprArg Val) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4396 | unsigned nexprs = Val.size(); |
| 4397 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4398 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 4399 | Expr *expr; |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4400 | if (nexprs == 1) |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4401 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 4402 | else |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4403 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R, |
| 4404 | exprs[nexprs-1]->getType()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4405 | return Owned(expr); |
| 4406 | } |
| 4407 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4408 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4409 | /// constant and the other is not a pointer. Returns true if a diagnostic is |
| 4410 | /// emitted. |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4411 | bool Sema::DiagnoseConditionalForNull(Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4412 | SourceLocation QuestionLoc) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4413 | Expr *NullExpr = LHSExpr; |
| 4414 | Expr *NonPointerExpr = RHSExpr; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4415 | Expr::NullPointerConstantKind NullKind = |
| 4416 | NullExpr->isNullPointerConstant(Context, |
| 4417 | Expr::NPC_ValueDependentIsNotNull); |
| 4418 | |
| 4419 | if (NullKind == Expr::NPCK_NotNull) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4420 | NullExpr = RHSExpr; |
| 4421 | NonPointerExpr = LHSExpr; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4422 | NullKind = |
| 4423 | NullExpr->isNullPointerConstant(Context, |
| 4424 | Expr::NPC_ValueDependentIsNotNull); |
| 4425 | } |
| 4426 | |
| 4427 | if (NullKind == Expr::NPCK_NotNull) |
| 4428 | return false; |
| 4429 | |
| 4430 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 4431 | // In this case, check to make sure that we got here from a "NULL" |
| 4432 | // string in the source code. |
| 4433 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 4434 | SourceLocation loc = NullExpr->getExprLoc(); |
| 4435 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4436 | return false; |
| 4437 | } |
| 4438 | |
| 4439 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 4440 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 4441 | << NonPointerExpr->getType() << DiagType |
| 4442 | << NonPointerExpr->getSourceRange(); |
| 4443 | return true; |
| 4444 | } |
| 4445 | |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4446 | /// \brief Return false if the condition expression is valid, true otherwise. |
| 4447 | static bool checkCondition(Sema &S, Expr *Cond) { |
| 4448 | QualType CondTy = Cond->getType(); |
| 4449 | |
| 4450 | // C99 6.5.15p2 |
| 4451 | if (CondTy->isScalarType()) return false; |
| 4452 | |
| 4453 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 4454 | if (S.getLangOptions().OpenCL && CondTy->isVectorType()) |
| 4455 | return false; |
| 4456 | |
| 4457 | // Emit the proper error message. |
| 4458 | S.Diag(Cond->getLocStart(), S.getLangOptions().OpenCL ? |
| 4459 | diag::err_typecheck_cond_expect_scalar : |
| 4460 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 4461 | << CondTy; |
| 4462 | return true; |
| 4463 | } |
| 4464 | |
| 4465 | /// \brief Return false if the two expressions can be converted to a vector, |
| 4466 | /// true otherwise |
| 4467 | static bool checkConditionalConvertScalarsToVectors(Sema &S, ExprResult &LHS, |
| 4468 | ExprResult &RHS, |
| 4469 | QualType CondTy) { |
| 4470 | // Both operands should be of scalar type. |
| 4471 | if (!LHS.get()->getType()->isScalarType()) { |
| 4472 | S.Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4473 | << CondTy; |
| 4474 | return true; |
| 4475 | } |
| 4476 | if (!RHS.get()->getType()->isScalarType()) { |
| 4477 | S.Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4478 | << CondTy; |
| 4479 | return true; |
| 4480 | } |
| 4481 | |
| 4482 | // Implicity convert these scalars to the type of the condition. |
| 4483 | LHS = S.ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 4484 | RHS = S.ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
| 4485 | return false; |
| 4486 | } |
| 4487 | |
| 4488 | /// \brief Handle when one or both operands are void type. |
| 4489 | static QualType checkConditionalVoidType(Sema &S, ExprResult &LHS, |
| 4490 | ExprResult &RHS) { |
| 4491 | Expr *LHSExpr = LHS.get(); |
| 4492 | Expr *RHSExpr = RHS.get(); |
| 4493 | |
| 4494 | if (!LHSExpr->getType()->isVoidType()) |
| 4495 | S.Diag(RHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4496 | << RHSExpr->getSourceRange(); |
| 4497 | if (!RHSExpr->getType()->isVoidType()) |
| 4498 | S.Diag(LHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4499 | << LHSExpr->getSourceRange(); |
| 4500 | LHS = S.ImpCastExprToType(LHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4501 | RHS = S.ImpCastExprToType(RHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4502 | return S.Context.VoidTy; |
| 4503 | } |
| 4504 | |
| 4505 | /// \brief Return false if the NullExpr can be promoted to PointerTy, |
| 4506 | /// true otherwise. |
| 4507 | static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr, |
| 4508 | QualType PointerTy) { |
| 4509 | if ((!PointerTy->isAnyPointerType() && !PointerTy->isBlockPointerType()) || |
| 4510 | !NullExpr.get()->isNullPointerConstant(S.Context, |
| 4511 | Expr::NPC_ValueDependentIsNull)) |
| 4512 | return true; |
| 4513 | |
| 4514 | NullExpr = S.ImpCastExprToType(NullExpr.take(), PointerTy, CK_NullToPointer); |
| 4515 | return false; |
| 4516 | } |
| 4517 | |
| 4518 | /// \brief Checks compatibility between two pointers and return the resulting |
| 4519 | /// type. |
| 4520 | static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, |
| 4521 | ExprResult &RHS, |
| 4522 | SourceLocation Loc) { |
| 4523 | QualType LHSTy = LHS.get()->getType(); |
| 4524 | QualType RHSTy = RHS.get()->getType(); |
| 4525 | |
| 4526 | if (S.Context.hasSameType(LHSTy, RHSTy)) { |
| 4527 | // Two identical pointers types are always compatible. |
| 4528 | return LHSTy; |
| 4529 | } |
| 4530 | |
| 4531 | QualType lhptee, rhptee; |
| 4532 | |
| 4533 | // Get the pointee types. |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4534 | if (const BlockPointerType *LHSBTy = LHSTy->getAs<BlockPointerType>()) { |
| 4535 | lhptee = LHSBTy->getPointeeType(); |
| 4536 | rhptee = RHSTy->castAs<BlockPointerType>()->getPointeeType(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4537 | } else { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4538 | lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); |
| 4539 | rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
| 4542 | if (!S.Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4543 | rhptee.getUnqualifiedType())) { |
| 4544 | S.Diag(Loc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4545 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4546 | << RHS.get()->getSourceRange(); |
| 4547 | // In this situation, we assume void* type. No especially good |
| 4548 | // reason, but this is what gcc does, and we do have to pick |
| 4549 | // to get a consistent AST. |
| 4550 | QualType incompatTy = S.Context.getPointerType(S.Context.VoidTy); |
| 4551 | LHS = S.ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4552 | RHS = S.ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
| 4553 | return incompatTy; |
| 4554 | } |
| 4555 | |
| 4556 | // The pointer types are compatible. |
| 4557 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4558 | // differently qualified versions of compatible types, the result type is |
| 4559 | // a pointer to an appropriately qualified version of the *composite* |
| 4560 | // type. |
| 4561 | // FIXME: Need to calculate the composite type. |
| 4562 | // FIXME: Need to add qualifiers |
| 4563 | |
| 4564 | LHS = S.ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 4565 | RHS = S.ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
| 4566 | return LHSTy; |
| 4567 | } |
| 4568 | |
| 4569 | /// \brief Return the resulting type when the operands are both block pointers. |
| 4570 | static QualType checkConditionalBlockPointerCompatibility(Sema &S, |
| 4571 | ExprResult &LHS, |
| 4572 | ExprResult &RHS, |
| 4573 | SourceLocation Loc) { |
| 4574 | QualType LHSTy = LHS.get()->getType(); |
| 4575 | QualType RHSTy = RHS.get()->getType(); |
| 4576 | |
| 4577 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4578 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4579 | QualType destType = S.Context.getPointerType(S.Context.VoidTy); |
| 4580 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4581 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4582 | return destType; |
| 4583 | } |
| 4584 | S.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 4585 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4586 | << RHS.get()->getSourceRange(); |
| 4587 | return QualType(); |
| 4588 | } |
| 4589 | |
| 4590 | // We have 2 block pointer types. |
| 4591 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4592 | } |
| 4593 | |
| 4594 | /// \brief Return the resulting type when the operands are both pointers. |
| 4595 | static QualType |
| 4596 | checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, |
| 4597 | ExprResult &RHS, |
| 4598 | SourceLocation Loc) { |
| 4599 | // get the pointer types |
| 4600 | QualType LHSTy = LHS.get()->getType(); |
| 4601 | QualType RHSTy = RHS.get()->getType(); |
| 4602 | |
| 4603 | // get the "pointed to" types |
| 4604 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4605 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4606 | |
| 4607 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4608 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4609 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 4610 | QualType destPointee |
| 4611 | = S.Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4612 | QualType destType = S.Context.getPointerType(destPointee); |
| 4613 | // Add qualifiers if necessary. |
| 4614 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
| 4615 | // Promote to void*. |
| 4616 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4617 | return destType; |
| 4618 | } |
| 4619 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
| 4620 | QualType destPointee |
| 4621 | = S.Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4622 | QualType destType = S.Context.getPointerType(destPointee); |
| 4623 | // Add qualifiers if necessary. |
| 4624 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
| 4625 | // Promote to void*. |
| 4626 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4627 | return destType; |
| 4628 | } |
| 4629 | |
| 4630 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4631 | } |
| 4632 | |
| 4633 | /// \brief Return false if the first expression is not an integer and the second |
| 4634 | /// expression is not a pointer, true otherwise. |
| 4635 | static bool checkPointerIntegerMismatch(Sema &S, ExprResult &Int, |
| 4636 | Expr* PointerExpr, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4637 | bool IsIntFirstExpr) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4638 | if (!PointerExpr->getType()->isPointerType() || |
| 4639 | !Int.get()->getType()->isIntegerType()) |
| 4640 | return false; |
| 4641 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4642 | Expr *Expr1 = IsIntFirstExpr ? Int.get() : PointerExpr; |
| 4643 | Expr *Expr2 = IsIntFirstExpr ? PointerExpr : Int.get(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4644 | |
| 4645 | S.Diag(Loc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4646 | << Expr1->getType() << Expr2->getType() |
| 4647 | << Expr1->getSourceRange() << Expr2->getSourceRange(); |
| 4648 | Int = S.ImpCastExprToType(Int.take(), PointerExpr->getType(), |
| 4649 | CK_IntegralToPointer); |
| 4650 | return true; |
| 4651 | } |
| 4652 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4653 | /// Note that LHS is not null here, even if this is the gnu "x ?: y" extension. |
| 4654 | /// In that case, LHS = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4655 | /// C99 6.5.15 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4656 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 4657 | ExprResult &RHS, ExprValueKind &VK, |
| 4658 | ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4659 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 4660 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4661 | ExprResult LHSResult = CheckPlaceholderExpr(LHS.get()); |
| 4662 | if (!LHSResult.isUsable()) return QualType(); |
| 4663 | LHS = move(LHSResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4664 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4665 | ExprResult RHSResult = CheckPlaceholderExpr(RHS.get()); |
| 4666 | if (!RHSResult.isUsable()) return QualType(); |
| 4667 | RHS = move(RHSResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4668 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4669 | // C++ is sufficiently different to merit its own checker. |
| 4670 | if (getLangOptions().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4671 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4672 | |
| 4673 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4674 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4675 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4676 | Cond = UsualUnaryConversions(Cond.take()); |
| 4677 | if (Cond.isInvalid()) |
| 4678 | return QualType(); |
| 4679 | LHS = UsualUnaryConversions(LHS.take()); |
| 4680 | if (LHS.isInvalid()) |
| 4681 | return QualType(); |
| 4682 | RHS = UsualUnaryConversions(RHS.take()); |
| 4683 | if (RHS.isInvalid()) |
| 4684 | return QualType(); |
| 4685 | |
| 4686 | QualType CondTy = Cond.get()->getType(); |
| 4687 | QualType LHSTy = LHS.get()->getType(); |
| 4688 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4689 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4690 | // first, check the condition. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4691 | if (checkCondition(*this, Cond.get())) |
| 4692 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4693 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4694 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4695 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 4696 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4697 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4698 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 4699 | // attempt to implicity convert them to the vector type to act like the |
| 4700 | // built in select. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4701 | if (getLangOptions().OpenCL && CondTy->isVectorType()) |
| 4702 | if (checkConditionalConvertScalarsToVectors(*this, LHS, RHS, CondTy)) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4703 | return QualType(); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4704 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4705 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4706 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4707 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4708 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4709 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4710 | return QualType(); |
| 4711 | return LHS.get()->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4712 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4713 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4714 | // If both operands are the same structure or union type, the result is that |
| 4715 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4716 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4717 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4718 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4719 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4720 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4721 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4722 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4723 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4724 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4725 | // 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] | 4726 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4727 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4728 | return checkConditionalVoidType(*this, LHS, RHS); |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4729 | } |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4730 | |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4731 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4732 | // the type of the other operand." |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4733 | if (!checkConditionalNullPointer(*this, RHS, LHSTy)) return LHSTy; |
| 4734 | if (!checkConditionalNullPointer(*this, LHS, RHSTy)) return RHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4735 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4736 | // All objective-c pointer type analysis is done here. |
| 4737 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4738 | QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4739 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4740 | return QualType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4741 | if (!compositeType.isNull()) |
| 4742 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4743 | |
| 4744 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4745 | // Handle block pointer types. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4746 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) |
| 4747 | return checkConditionalBlockPointerCompatibility(*this, LHS, RHS, |
| 4748 | QuestionLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4749 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4750 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4751 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) |
| 4752 | return checkConditionalObjectPointersCompatibility(*this, LHS, RHS, |
| 4753 | QuestionLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4754 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4755 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 4756 | // null pointers have been filtered out by this point. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4757 | if (checkPointerIntegerMismatch(*this, LHS, RHS.get(), QuestionLoc, |
| 4758 | /*isIntFirstExpr=*/true)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4759 | return RHSTy; |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4760 | if (checkPointerIntegerMismatch(*this, RHS, LHS.get(), QuestionLoc, |
| 4761 | /*isIntFirstExpr=*/false)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4762 | return LHSTy; |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4763 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4764 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 4765 | // constant and the other is not a pointer type. In this case, the user most |
| 4766 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4767 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4768 | return QualType(); |
| 4769 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4770 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4771 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4772 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4773 | << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4774 | return QualType(); |
| 4775 | } |
| 4776 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4777 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4778 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4779 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4780 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4781 | QualType LHSTy = LHS.get()->getType(); |
| 4782 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4783 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4784 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4785 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4786 | // redefinition type if an attempt is made to access its fields. |
| 4787 | if (LHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4788 | (Context.hasSameType(RHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4789 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4790 | return LHSTy; |
| 4791 | } |
| 4792 | if (RHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4793 | (Context.hasSameType(LHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4794 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4795 | return RHSTy; |
| 4796 | } |
| 4797 | // And the same for struct objc_object* / id |
| 4798 | if (LHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4799 | (Context.hasSameType(RHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4800 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4801 | return LHSTy; |
| 4802 | } |
| 4803 | if (RHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4804 | (Context.hasSameType(LHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4805 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4806 | return RHSTy; |
| 4807 | } |
| 4808 | // And the same for struct objc_selector* / SEL |
| 4809 | if (Context.isObjCSelType(LHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4810 | (Context.hasSameType(RHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4811 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4812 | return LHSTy; |
| 4813 | } |
| 4814 | if (Context.isObjCSelType(RHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4815 | (Context.hasSameType(LHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4816 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4817 | return RHSTy; |
| 4818 | } |
| 4819 | // Check constraints for Objective-C object pointers types. |
| 4820 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4821 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4822 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4823 | // Two identical object pointer types are always compatible. |
| 4824 | return LHSTy; |
| 4825 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4826 | const ObjCObjectPointerType *LHSOPT = LHSTy->castAs<ObjCObjectPointerType>(); |
| 4827 | const ObjCObjectPointerType *RHSOPT = RHSTy->castAs<ObjCObjectPointerType>(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4828 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4829 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4830 | // If both operands are interfaces and either operand can be |
| 4831 | // assigned to the other, use that type as the composite |
| 4832 | // type. This allows |
| 4833 | // xxx ? (A*) a : (B*) b |
| 4834 | // where B is a subclass of A. |
| 4835 | // |
| 4836 | // Additionally, as for assignment, if either type is 'id' |
| 4837 | // allow silent coercion. Finally, if the types are |
| 4838 | // incompatible then make sure to use 'id' as the composite |
| 4839 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4840 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4841 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4842 | // It could return the composite type. |
| 4843 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4844 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4845 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4846 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4847 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4848 | RHSTy->isObjCQualifiedIdType()) && |
| 4849 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4850 | // Need to handle "id<xx>" explicitly. |
| 4851 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4852 | // id. Currently localizing to here until clear this should be |
| 4853 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4854 | compositeType = Context.getObjCIdType(); |
| 4855 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4856 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4857 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4858 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4859 | ; |
| 4860 | else { |
| 4861 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4862 | << LHSTy << RHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4863 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4864 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4865 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4866 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4867 | return incompatTy; |
| 4868 | } |
| 4869 | // The object pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4870 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 4871 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4872 | return compositeType; |
| 4873 | } |
| 4874 | // Check Objective-C object pointer types and 'void *' |
| 4875 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4876 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4877 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4878 | QualType destPointee |
| 4879 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4880 | QualType destType = Context.getPointerType(destPointee); |
| 4881 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4882 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4883 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4884 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4885 | return destType; |
| 4886 | } |
| 4887 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4888 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4889 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4890 | QualType destPointee |
| 4891 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4892 | QualType destType = Context.getPointerType(destPointee); |
| 4893 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4894 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4895 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4896 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4897 | return destType; |
| 4898 | } |
| 4899 | return QualType(); |
| 4900 | } |
| 4901 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4902 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4903 | /// ParenRange in parentheses. |
| 4904 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4905 | const PartialDiagnostic &Note, |
| 4906 | SourceRange ParenRange) { |
| 4907 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 4908 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 4909 | EndLoc.isValid()) { |
| 4910 | Self.Diag(Loc, Note) |
| 4911 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 4912 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 4913 | } else { |
| 4914 | // We can't display the parentheses, so just show the bare note. |
| 4915 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4916 | } |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 4920 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 4921 | } |
| 4922 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4923 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 4924 | /// expression, either using a built-in or overloaded operator, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4925 | /// and sets *OpCode to the opcode and *RHSExprs to the right-hand side |
| 4926 | /// expression. |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4927 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4928 | Expr **RHSExprs) { |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4929 | E = E->IgnoreParenImpCasts(); |
| 4930 | E = E->IgnoreConversionOperator(); |
| 4931 | E = E->IgnoreParenImpCasts(); |
| 4932 | |
| 4933 | // Built-in binary operator. |
| 4934 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 4935 | if (IsArithmeticOp(OP->getOpcode())) { |
| 4936 | *Opcode = OP->getOpcode(); |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4937 | *RHSExprs = OP->getRHS(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4938 | return true; |
| 4939 | } |
| 4940 | } |
| 4941 | |
| 4942 | // Overloaded operator. |
| 4943 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 4944 | if (Call->getNumArgs() != 2) |
| 4945 | return false; |
| 4946 | |
| 4947 | // Make sure this is really a binary operator that is safe to pass into |
| 4948 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 4949 | OverloadedOperatorKind OO = Call->getOperator(); |
| 4950 | if (OO < OO_Plus || OO > OO_Arrow) |
| 4951 | return false; |
| 4952 | |
| 4953 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 4954 | if (IsArithmeticOp(OpKind)) { |
| 4955 | *Opcode = OpKind; |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4956 | *RHSExprs = Call->getArg(1); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4957 | return true; |
| 4958 | } |
| 4959 | } |
| 4960 | |
| 4961 | return false; |
| 4962 | } |
| 4963 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4964 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 4965 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 4966 | } |
| 4967 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4968 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 4969 | /// or is a logical expression such as (x==y) which has int type, but is |
| 4970 | /// commonly interpreted as boolean. |
| 4971 | static bool ExprLooksBoolean(Expr *E) { |
| 4972 | E = E->IgnoreParenImpCasts(); |
| 4973 | |
| 4974 | if (E->getType()->isBooleanType()) |
| 4975 | return true; |
| 4976 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 4977 | return IsLogicOp(OP->getOpcode()); |
| 4978 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 4979 | return OP->getOpcode() == UO_LNot; |
| 4980 | |
| 4981 | return false; |
| 4982 | } |
| 4983 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4984 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 4985 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 4986 | /// the conditional operator has higher precedence, for example: |
| 4987 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 4988 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 4989 | SourceLocation OpLoc, |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4990 | Expr *Condition, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4991 | Expr *LHSExpr, |
| 4992 | Expr *RHSExpr) { |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4993 | BinaryOperatorKind CondOpcode; |
| 4994 | Expr *CondRHS; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4995 | |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4996 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4997 | return; |
| 4998 | if (!ExprLooksBoolean(CondRHS)) |
| 4999 | return; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5000 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5001 | // The condition is an arithmetic binary expression, with a right- |
| 5002 | // hand side that looks boolean, so warn. |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5003 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5004 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5005 | << Condition->getSourceRange() |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5006 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5007 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5008 | SuggestParentheses(Self, OpLoc, |
| 5009 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 5010 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 5011 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Chandler Carruth | 9d5353c | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 5012 | |
| 5013 | SuggestParentheses(Self, OpLoc, |
| 5014 | Self.PDiag(diag::note_precedence_conditional_first), |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5015 | SourceRange(CondRHS->getLocStart(), RHSExpr->getLocEnd())); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5018 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5019 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5020 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5021 | SourceLocation ColonLoc, |
| 5022 | Expr *CondExpr, Expr *LHSExpr, |
| 5023 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5024 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 5025 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5026 | OpaqueValueExpr *opaqueValue = 0; |
| 5027 | Expr *commonExpr = 0; |
| 5028 | if (LHSExpr == 0) { |
| 5029 | commonExpr = CondExpr; |
| 5030 | |
| 5031 | // We usually want to apply unary conversions *before* saving, except |
| 5032 | // in the special case of a C++ l-value conditional. |
| 5033 | if (!(getLangOptions().CPlusPlus |
| 5034 | && !commonExpr->isTypeDependent() |
| 5035 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 5036 | && commonExpr->isGLValue() |
| 5037 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 5038 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 5039 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5040 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 5041 | if (commonRes.isInvalid()) |
| 5042 | return ExprError(); |
| 5043 | commonExpr = commonRes.take(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 5047 | commonExpr->getType(), |
| 5048 | commonExpr->getValueKind(), |
| 5049 | commonExpr->getObjectKind()); |
| 5050 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5051 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5052 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5053 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5054 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5055 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 5056 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5057 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5058 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 5059 | RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5060 | return ExprError(); |
| 5061 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5062 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 5063 | RHS.get()); |
| 5064 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5065 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5066 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 5067 | LHS.take(), ColonLoc, |
| 5068 | RHS.take(), result, VK, OK)); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5069 | |
| 5070 | return Owned(new (Context) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5071 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5072 | RHS.take(), QuestionLoc, ColonLoc, result, VK, |
| 5073 | OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5076 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5077 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5078 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 5079 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 5080 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5081 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5082 | checkPointerTypesForAssignment(Sema &S, QualType LHSType, QualType RHSType) { |
| 5083 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5084 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5085 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5086 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5087 | const Type *lhptee, *rhptee; |
| 5088 | Qualifiers lhq, rhq; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5089 | llvm::tie(lhptee, lhq) = cast<PointerType>(LHSType)->getPointeeType().split(); |
| 5090 | llvm::tie(rhptee, rhq) = cast<PointerType>(RHSType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5091 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5092 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5093 | |
| 5094 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 5095 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 5096 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5097 | Qualifiers lq; |
| 5098 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5099 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 5100 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 5101 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 5102 | // Ignore lifetime for further calculation. |
| 5103 | lhq.removeObjCLifetime(); |
| 5104 | rhq.removeObjCLifetime(); |
| 5105 | } |
| 5106 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5107 | if (!lhq.compatiblyIncludes(rhq)) { |
| 5108 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 5109 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 5110 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5111 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5112 | // 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] | 5113 | // and from void*. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5114 | else if (lhq.withoutObjCGCAttr().withoutObjCGLifetime() |
| 5115 | .compatiblyIncludes( |
| 5116 | rhq.withoutObjCGCAttr().withoutObjCGLifetime()) |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5117 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 5118 | ; // keep old |
| 5119 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5120 | // Treat lifetime mismatches as fatal. |
| 5121 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 5122 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5123 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5124 | // For GCC compatibility, other qualifier mismatches are treated |
| 5125 | // as still compatible in C. |
| 5126 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 5127 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5128 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5129 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5130 | // 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] | 5131 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5132 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5133 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5134 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5135 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5136 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5137 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5138 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5139 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5140 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5141 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5142 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5143 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5144 | |
| 5145 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5146 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5147 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5148 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5149 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5150 | // 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] | 5151 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5152 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 5153 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5154 | // Check if the pointee types are compatible ignoring the sign. |
| 5155 | // We explicitly check for char so that we catch "char" vs |
| 5156 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5157 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5158 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5159 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5160 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5161 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5162 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5163 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5164 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5165 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5166 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5167 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5168 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5169 | // takes priority over sign incompatibility because the sign |
| 5170 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5171 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5172 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5173 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5174 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5175 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5176 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5177 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5178 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5179 | // the eventual target type is the same and the pointers have the same |
| 5180 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5181 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5182 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5183 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5184 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5185 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5186 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5187 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5188 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5189 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5190 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5191 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5192 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5193 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5194 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5195 | } |
| 5196 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5197 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5198 | /// block pointer types are compatible or whether a block and normal pointer |
| 5199 | /// are compatible. It is more restrict than comparing two function pointer |
| 5200 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5201 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5202 | checkBlockPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5203 | QualType RHSType) { |
| 5204 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5205 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5206 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5207 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5208 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5209 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5210 | lhptee = cast<BlockPointerType>(LHSType)->getPointeeType(); |
| 5211 | rhptee = cast<BlockPointerType>(RHSType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5212 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5213 | // In C++, the types have to match exactly. |
| 5214 | if (S.getLangOptions().CPlusPlus) |
| 5215 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5216 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5217 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5218 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5219 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5220 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5221 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5222 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5223 | if (!S.Context.typesAreBlockPointerCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5224 | return Sema::IncompatibleBlockPointer; |
| 5225 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5226 | return ConvTy; |
| 5227 | } |
| 5228 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5229 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5230 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5231 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5232 | checkObjCPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5233 | QualType RHSType) { |
| 5234 | assert(LHSType.isCanonical() && "LHS was not canonicalized!"); |
| 5235 | assert(RHSType.isCanonical() && "RHS was not canonicalized!"); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5236 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5237 | if (LHSType->isObjCBuiltinType()) { |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5238 | // Class is not compatible with ObjC object pointers. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5239 | if (LHSType->isObjCClassType() && !RHSType->isObjCBuiltinType() && |
| 5240 | !RHSType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5241 | return Sema::IncompatiblePointer; |
| 5242 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5243 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5244 | if (RHSType->isObjCBuiltinType()) { |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5245 | // Class is not compatible with ObjC object pointers. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5246 | if (RHSType->isObjCClassType() && !LHSType->isObjCBuiltinType() && |
| 5247 | !LHSType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5248 | return Sema::IncompatiblePointer; |
| 5249 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5250 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5251 | QualType lhptee = LHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5252 | QualType rhptee = RHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5253 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5254 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 5255 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5256 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5257 | if (S.Context.typesAreCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5258 | return Sema::Compatible; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5259 | if (LHSType->isObjCQualifiedIdType() || RHSType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5260 | return Sema::IncompatibleObjCQualifiedId; |
| 5261 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5262 | } |
| 5263 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5264 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5265 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5266 | QualType LHSType, QualType RHSType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5267 | // Fake up an opaque expression. We don't actually care about what |
| 5268 | // cast operations are required, so if CheckAssignmentConstraints |
| 5269 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5270 | // usually happen on valid code. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5271 | OpaqueValueExpr RHSExpr(Loc, RHSType, VK_RValue); |
| 5272 | ExprResult RHSPtr = &RHSExpr; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5273 | CastKind K = CK_Invalid; |
| 5274 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5275 | return CheckAssignmentConstraints(LHSType, RHSPtr, K); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5278 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5279 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5280 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5281 | /// |
| 5282 | /// int a, *pint; |
| 5283 | /// short *pshort; |
| 5284 | /// struct foo *pfoo; |
| 5285 | /// |
| 5286 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5287 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5288 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5289 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5290 | /// |
| 5291 | /// 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] | 5292 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5293 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5294 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5295 | Sema::AssignConvertType |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5296 | Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5297 | CastKind &Kind) { |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5298 | QualType RHSType = RHS.get()->getType(); |
| 5299 | QualType OrigLHSType = LHSType; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5300 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5301 | // Get canonical types. We're not formatting these types, just comparing |
| 5302 | // them. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5303 | LHSType = Context.getCanonicalType(LHSType).getUnqualifiedType(); |
| 5304 | RHSType = Context.getCanonicalType(RHSType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5305 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5306 | // Common case: no conversion required. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5307 | if (LHSType == RHSType) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5308 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5309 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5310 | } |
| 5311 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5312 | // If the left-hand side is a reference type, then we are in a |
| 5313 | // (rare!) case where we've allowed the use of references in C, |
| 5314 | // e.g., as a parameter type in a built-in function. In this case, |
| 5315 | // just make sure that the type referenced is compatible with the |
| 5316 | // right-hand side type. The caller is responsible for adjusting |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5317 | // LHSType so that the resulting expression does not have reference |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5318 | // type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5319 | if (const ReferenceType *LHSTypeRef = LHSType->getAs<ReferenceType>()) { |
| 5320 | if (Context.typesAreCompatible(LHSTypeRef->getPointeeType(), RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5321 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5322 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5323 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5324 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5325 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5326 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5327 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5328 | // to the same ExtVector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5329 | if (LHSType->isExtVectorType()) { |
| 5330 | if (RHSType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5331 | return Incompatible; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5332 | if (RHSType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5333 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5334 | // element type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5335 | QualType elType = cast<ExtVectorType>(LHSType)->getElementType(); |
| 5336 | if (elType != RHSType) { |
| 5337 | Kind = PrepareScalarCast(*this, RHS, elType); |
| 5338 | RHS = ImpCastExprToType(RHS.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5339 | } |
| 5340 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5341 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5342 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5343 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5344 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5345 | // Conversions to or from vector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5346 | if (LHSType->isVectorType() || RHSType->isVectorType()) { |
| 5347 | if (LHSType->isVectorType() && RHSType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5348 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5349 | // vector type and vice versa |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5350 | if (Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5351 | Kind = CK_BitCast; |
| 5352 | return Compatible; |
| 5353 | } |
| 5354 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5355 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5356 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5357 | // no bits are changed but the result type is different. |
| 5358 | if (getLangOptions().LaxVectorConversions && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5359 | (Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5360 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5361 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5362 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5363 | } |
| 5364 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5365 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5366 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5367 | // Arithmetic conversions. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5368 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType() && |
| 5369 | !(getLangOptions().CPlusPlus && LHSType->isEnumeralType())) { |
| 5370 | Kind = PrepareScalarCast(*this, RHS, LHSType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5371 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5372 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5373 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5374 | // Conversions to normal pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5375 | if (const PointerType *LHSPointer = dyn_cast<PointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5376 | // U* -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5377 | if (isa<PointerType>(RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5378 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5379 | return checkPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5380 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5381 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5382 | // int -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5383 | if (RHSType->isIntegerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5384 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 5385 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5386 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5387 | |
| 5388 | // C pointers are not compatible with ObjC object pointers, |
| 5389 | // with two exceptions: |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5390 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5391 | // - conversions to void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5392 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5393 | Kind = CK_BitCast; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5394 | return Compatible; |
| 5395 | } |
| 5396 | |
| 5397 | // - conversions from 'Class' to the redefinition type |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5398 | if (RHSType->isObjCClassType() && |
| 5399 | Context.hasSameType(LHSType, |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5400 | Context.getObjCClassRedefinitionType())) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5401 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5402 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5403 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5404 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5405 | Kind = CK_BitCast; |
| 5406 | return IncompatiblePointer; |
| 5407 | } |
| 5408 | |
| 5409 | // U^ -> void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5410 | if (RHSType->getAs<BlockPointerType>()) { |
| 5411 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5412 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5413 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5414 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5415 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5416 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5417 | return Incompatible; |
| 5418 | } |
| 5419 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5420 | // Conversions to block pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5421 | if (isa<BlockPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5422 | // U^ -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5423 | if (RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5424 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5425 | return checkBlockPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | // int or null -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5429 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5430 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5431 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5432 | } |
| 5433 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5434 | // id -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5435 | if (getLangOptions().ObjC1 && RHSType->isObjCIdType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5436 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5437 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5438 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5439 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5440 | // void* -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5441 | if (const PointerType *RHSPT = RHSType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5442 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 5443 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5444 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5445 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5446 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5447 | return Incompatible; |
| 5448 | } |
| 5449 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5450 | // Conversions to Objective-C pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5451 | if (isa<ObjCObjectPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5452 | // A* -> B* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5453 | if (RHSType->isObjCObjectPointerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5454 | Kind = CK_BitCast; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5455 | Sema::AssignConvertType result = |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5456 | checkObjCPointerTypesForAssignment(*this, LHSType, RHSType); |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5457 | if (getLangOptions().ObjCAutoRefCount && |
| 5458 | result == Compatible && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5459 | !CheckObjCARCUnavailableWeakConversion(OrigLHSType, RHSType)) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5460 | result = IncompatibleObjCWeakRef; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5461 | return result; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5462 | } |
| 5463 | |
| 5464 | // int or null -> A* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5465 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5466 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5467 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5468 | } |
| 5469 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5470 | // In general, C pointers are not compatible with ObjC object pointers, |
| 5471 | // with two exceptions: |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5472 | if (isa<PointerType>(RHSType)) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5473 | Kind = CK_CPointerToObjCPointerCast; |
| 5474 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5475 | // - conversions from 'void*' |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5476 | if (RHSType->isVoidPointerType()) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5477 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | // - conversions to 'Class' from its redefinition type |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5481 | if (LHSType->isObjCClassType() && |
| 5482 | Context.hasSameType(RHSType, |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5483 | Context.getObjCClassRedefinitionType())) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5484 | return Compatible; |
| 5485 | } |
| 5486 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5487 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5488 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5489 | |
| 5490 | // T^ -> A* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5491 | if (RHSType->isBlockPointerType()) { |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame^] | 5492 | maybeExtendBlockObject(*this, RHS); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5493 | Kind = CK_BlockPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5494 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5495 | } |
| 5496 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5497 | return Incompatible; |
| 5498 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5499 | |
| 5500 | // Conversions from pointers that are not covered by the above. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5501 | if (isa<PointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5502 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5503 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5504 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5505 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5506 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5507 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5508 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5509 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5510 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5511 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5512 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5513 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5514 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5515 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5516 | |
| 5517 | // Conversions from Objective-C pointers that are not covered by the above. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5518 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5519 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5520 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5521 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5522 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5523 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5524 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5525 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5526 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5527 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5528 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5531 | return Incompatible; |
| 5532 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5533 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5534 | // struct A -> struct B |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5535 | if (isa<TagType>(LHSType) && isa<TagType>(RHSType)) { |
| 5536 | if (Context.typesAreCompatible(LHSType, RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5537 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5538 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5539 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5540 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5541 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5542 | return Incompatible; |
| 5543 | } |
| 5544 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5545 | /// \brief Constructs a transparent union from an expression that is |
| 5546 | /// used to initialize the transparent union. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5547 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, |
| 5548 | ExprResult &EResult, QualType UnionType, |
| 5549 | FieldDecl *Field) { |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5550 | // Build an initializer list that designates the appropriate member |
| 5551 | // of the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5552 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5553 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 5554 | &E, 1, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5555 | SourceLocation()); |
| 5556 | Initializer->setType(UnionType); |
| 5557 | Initializer->setInitializedFieldInUnion(Field); |
| 5558 | |
| 5559 | // Build a compound literal constructing a value of the transparent |
| 5560 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5561 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5562 | EResult = S.Owned( |
| 5563 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 5564 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5565 | } |
| 5566 | |
| 5567 | Sema::AssignConvertType |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5568 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5569 | ExprResult &RHS) { |
| 5570 | QualType RHSType = RHS.get()->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5571 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5572 | // 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] | 5573 | // transparent_union GCC extension. |
| 5574 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5575 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5576 | return Incompatible; |
| 5577 | |
| 5578 | // The field to initialize within the transparent union. |
| 5579 | RecordDecl *UD = UT->getDecl(); |
| 5580 | FieldDecl *InitField = 0; |
| 5581 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5582 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5583 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5584 | it != itend; ++it) { |
| 5585 | if (it->getType()->isPointerType()) { |
| 5586 | // If the transparent union contains a pointer type, we allow: |
| 5587 | // 1) void pointer |
| 5588 | // 2) null pointer constant |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5589 | if (RHSType->isPointerType()) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5590 | if (RHSType->castAs<PointerType>()->getPointeeType()->isVoidType()) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5591 | RHS = ImpCastExprToType(RHS.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5592 | InitField = *it; |
| 5593 | break; |
| 5594 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5595 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5596 | if (RHS.get()->isNullPointerConstant(Context, |
| 5597 | Expr::NPC_ValueDependentIsNull)) { |
| 5598 | RHS = ImpCastExprToType(RHS.take(), it->getType(), |
| 5599 | CK_NullToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5600 | InitField = *it; |
| 5601 | break; |
| 5602 | } |
| 5603 | } |
| 5604 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5605 | CastKind Kind = CK_Invalid; |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5606 | if (CheckAssignmentConstraints(it->getType(), RHS, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5607 | == Compatible) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5608 | RHS = ImpCastExprToType(RHS.take(), it->getType(), Kind); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5609 | InitField = *it; |
| 5610 | break; |
| 5611 | } |
| 5612 | } |
| 5613 | |
| 5614 | if (!InitField) |
| 5615 | return Incompatible; |
| 5616 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5617 | ConstructTransparentUnion(*this, Context, RHS, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5618 | return Compatible; |
| 5619 | } |
| 5620 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5621 | Sema::AssignConvertType |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5622 | Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &RHS) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5623 | if (getLangOptions().CPlusPlus) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5624 | if (!LHSType->isRecordType()) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5625 | // C++ 5.17p3: If the left operand is not of class type, the |
| 5626 | // expression is implicitly converted (C++ 4) to the |
| 5627 | // cv-unqualified type of the left operand. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5628 | ExprResult Res = PerformImplicitConversion(RHS.get(), |
| 5629 | LHSType.getUnqualifiedType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5630 | AA_Assigning); |
| 5631 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5632 | return Incompatible; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5633 | Sema::AssignConvertType result = Compatible; |
| 5634 | if (getLangOptions().ObjCAutoRefCount && |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5635 | !CheckObjCARCUnavailableWeakConversion(LHSType, |
| 5636 | RHS.get()->getType())) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5637 | result = IncompatibleObjCWeakRef; |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5638 | RHS = move(Res); |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5639 | return result; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5640 | } |
| 5641 | |
| 5642 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 5643 | // structures. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5644 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5645 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5646 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 5647 | // a null pointer constant. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5648 | if ((LHSType->isPointerType() || |
| 5649 | LHSType->isObjCObjectPointerType() || |
| 5650 | LHSType->isBlockPointerType()) |
| 5651 | && RHS.get()->isNullPointerConstant(Context, |
| 5652 | Expr::NPC_ValueDependentIsNull)) { |
| 5653 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5654 | return Compatible; |
| 5655 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5656 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5657 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5658 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 5659 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 5660 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5661 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5662 | // Suppress this for references: C++ 8.5.3p5. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5663 | if (!LHSType->isReferenceType()) { |
| 5664 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 5665 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5666 | return Incompatible; |
| 5667 | } |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5668 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5669 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5670 | Sema::AssignConvertType result = |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5671 | CheckAssignmentConstraints(LHSType, RHS, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5672 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5673 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 5674 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5675 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 5676 | // so that we can use references in built-in functions even in C. |
| 5677 | // The getNonReferenceType() call makes sure that the resulting expression |
| 5678 | // does not have reference type. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5679 | if (result != Incompatible && RHS.get()->getType() != LHSType) |
| 5680 | RHS = ImpCastExprToType(RHS.take(), |
| 5681 | LHSType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5682 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5683 | } |
| 5684 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5685 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &LHS, |
| 5686 | ExprResult &RHS) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5687 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5688 | << LHS.get()->getType() << RHS.get()->getType() |
| 5689 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5690 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5691 | } |
| 5692 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5693 | QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5694 | SourceLocation Loc, bool IsCompAssign) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5695 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5696 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5697 | QualType LHSType = |
| 5698 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 5699 | QualType RHSType = |
| 5700 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5701 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5702 | // If the vector types are identical, return. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5703 | if (LHSType == RHSType) |
| 5704 | return LHSType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5705 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5706 | // Handle the case of equivalent AltiVec and GCC vector types |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5707 | if (LHSType->isVectorType() && RHSType->isVectorType() && |
| 5708 | Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
| 5709 | if (LHSType->isExtVectorType()) { |
| 5710 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 5711 | return LHSType; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5712 | } |
| 5713 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5714 | if (!IsCompAssign) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5715 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
| 5716 | return RHSType; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5717 | } |
| 5718 | |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5719 | if (getLangOptions().LaxVectorConversions && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5720 | Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType)) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5721 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5722 | // vectors, the total size only needs to be the same. This is a |
| 5723 | // bitcast; no bits are changed but the result type is different. |
| 5724 | // FIXME: Should we really be allowing this? |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5725 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 5726 | return LHSType; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5727 | } |
| 5728 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5729 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 5730 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 5731 | bool swapped = false; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5732 | if (RHSType->isExtVectorType() && !IsCompAssign) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5733 | swapped = true; |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5734 | std::swap(RHS, LHS); |
| 5735 | std::swap(RHSType, LHSType); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5737 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5738 | // Handle the case of an ext vector and scalar. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5739 | if (const ExtVectorType *LV = LHSType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5740 | QualType EltTy = LV->getElementType(); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5741 | if (EltTy->isIntegralType(Context) && RHSType->isIntegralType(Context)) { |
| 5742 | int order = Context.getIntegerTypeOrder(EltTy, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5743 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5744 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5745 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5746 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 5747 | if (swapped) std::swap(RHS, LHS); |
| 5748 | return LHSType; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5749 | } |
| 5750 | } |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5751 | if (EltTy->isRealFloatingType() && RHSType->isScalarType() && |
| 5752 | RHSType->isRealFloatingType()) { |
| 5753 | int order = Context.getFloatingTypeOrder(EltTy, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5754 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5755 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5756 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5757 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 5758 | if (swapped) std::swap(RHS, LHS); |
| 5759 | return LHSType; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5760 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5761 | } |
| 5762 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5763 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5764 | // Vectors of different size or scalar and non-ext-vector are errors. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5765 | if (swapped) std::swap(RHS, LHS); |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5766 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5767 | << LHS.get()->getType() << RHS.get()->getType() |
| 5768 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5769 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5770 | } |
| 5771 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5772 | QualType Sema::CheckMultiplyDivideOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5773 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5774 | bool IsCompAssign, bool IsDiv) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5775 | if (LHS.get()->getType()->isVectorType() || |
| 5776 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5777 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5778 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5779 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5780 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5781 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5782 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5783 | if (!LHS.get()->getType()->isArithmeticType() || |
| 5784 | !RHS.get()->getType()->isArithmeticType()) |
| 5785 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5786 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5787 | // Check for division by zero. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5788 | if (IsDiv && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5789 | RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5790 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5791 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_division_by_zero) |
| 5792 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5793 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5794 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5795 | } |
| 5796 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5797 | QualType Sema::CheckRemainderOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5798 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5799 | if (LHS.get()->getType()->isVectorType() || |
| 5800 | RHS.get()->getType()->isVectorType()) { |
| 5801 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 5802 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5803 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5804 | return InvalidOperands(Loc, LHS, RHS); |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 5805 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5806 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5807 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5808 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5809 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5810 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5811 | if (!LHS.get()->getType()->isIntegerType() || |
| 5812 | !RHS.get()->getType()->isIntegerType()) |
| 5813 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5814 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5815 | // Check for remainder by zero. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5816 | if (RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5817 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5818 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_remainder_by_zero) |
| 5819 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5820 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5821 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5822 | } |
| 5823 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5824 | /// \brief Diagnose invalid arithmetic on two void pointers. |
| 5825 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5826 | Expr *LHSExpr, Expr *RHSExpr) { |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5827 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5828 | ? diag::err_typecheck_pointer_arith_void_type |
| 5829 | : diag::ext_gnu_void_ptr) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5830 | << 1 /* two pointers */ << LHSExpr->getSourceRange() |
| 5831 | << RHSExpr->getSourceRange(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5832 | } |
| 5833 | |
| 5834 | /// \brief Diagnose invalid arithmetic on a void pointer. |
| 5835 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, |
| 5836 | Expr *Pointer) { |
| 5837 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5838 | ? diag::err_typecheck_pointer_arith_void_type |
| 5839 | : diag::ext_gnu_void_ptr) |
| 5840 | << 0 /* one pointer */ << Pointer->getSourceRange(); |
| 5841 | } |
| 5842 | |
| 5843 | /// \brief Diagnose invalid arithmetic on two function pointers. |
| 5844 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, |
| 5845 | Expr *LHS, Expr *RHS) { |
| 5846 | assert(LHS->getType()->isAnyPointerType()); |
| 5847 | assert(RHS->getType()->isAnyPointerType()); |
| 5848 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5849 | ? diag::err_typecheck_pointer_arith_function_type |
| 5850 | : diag::ext_gnu_ptr_func_arith) |
| 5851 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() |
| 5852 | // We only show the second type if it differs from the first. |
| 5853 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), |
| 5854 | RHS->getType()) |
| 5855 | << RHS->getType()->getPointeeType() |
| 5856 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5857 | } |
| 5858 | |
| 5859 | /// \brief Diagnose invalid arithmetic on a function pointer. |
| 5860 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, |
| 5861 | Expr *Pointer) { |
| 5862 | assert(Pointer->getType()->isAnyPointerType()); |
| 5863 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5864 | ? diag::err_typecheck_pointer_arith_function_type |
| 5865 | : diag::ext_gnu_ptr_func_arith) |
| 5866 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() |
| 5867 | << 0 /* one pointer, so only one type */ |
| 5868 | << Pointer->getSourceRange(); |
| 5869 | } |
| 5870 | |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5871 | /// \brief Warn if Operand is incomplete pointer type |
| 5872 | /// |
| 5873 | /// \returns True if pointer has incomplete type |
| 5874 | static bool checkArithmeticIncompletePointerType(Sema &S, SourceLocation Loc, |
| 5875 | Expr *Operand) { |
| 5876 | if ((Operand->getType()->isPointerType() && |
| 5877 | !Operand->getType()->isDependentType()) || |
| 5878 | Operand->getType()->isObjCObjectPointerType()) { |
| 5879 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5880 | if (S.RequireCompleteType( |
| 5881 | Loc, PointeeTy, |
| 5882 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 5883 | << PointeeTy << Operand->getSourceRange())) |
| 5884 | return true; |
| 5885 | } |
| 5886 | return false; |
| 5887 | } |
| 5888 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5889 | /// \brief Check the validity of an arithmetic pointer operand. |
| 5890 | /// |
| 5891 | /// If the operand has pointer type, this code will check for pointer types |
| 5892 | /// which are invalid in arithmetic operations. These will be diagnosed |
| 5893 | /// appropriately, including whether or not the use is supported as an |
| 5894 | /// extension. |
| 5895 | /// |
| 5896 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5897 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, |
| 5898 | Expr *Operand) { |
| 5899 | if (!Operand->getType()->isAnyPointerType()) return true; |
| 5900 | |
| 5901 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5902 | if (PointeeTy->isVoidType()) { |
| 5903 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); |
| 5904 | return !S.getLangOptions().CPlusPlus; |
| 5905 | } |
| 5906 | if (PointeeTy->isFunctionType()) { |
| 5907 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); |
| 5908 | return !S.getLangOptions().CPlusPlus; |
| 5909 | } |
| 5910 | |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5911 | if (checkArithmeticIncompletePointerType(S, Loc, Operand)) return false; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5912 | |
| 5913 | return true; |
| 5914 | } |
| 5915 | |
| 5916 | /// \brief Check the validity of a binary arithmetic operation w.r.t. pointer |
| 5917 | /// operands. |
| 5918 | /// |
| 5919 | /// This routine will diagnose any invalid arithmetic on pointer operands much |
| 5920 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic |
| 5921 | /// for emitting a single diagnostic even for operations where both LHS and RHS |
| 5922 | /// are (potentially problematic) pointers. |
| 5923 | /// |
| 5924 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5925 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5926 | Expr *LHSExpr, Expr *RHSExpr) { |
| 5927 | bool isLHSPointer = LHSExpr->getType()->isAnyPointerType(); |
| 5928 | bool isRHSPointer = RHSExpr->getType()->isAnyPointerType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5929 | if (!isLHSPointer && !isRHSPointer) return true; |
| 5930 | |
| 5931 | QualType LHSPointeeTy, RHSPointeeTy; |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5932 | if (isLHSPointer) LHSPointeeTy = LHSExpr->getType()->getPointeeType(); |
| 5933 | if (isRHSPointer) RHSPointeeTy = RHSExpr->getType()->getPointeeType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5934 | |
| 5935 | // Check for arithmetic on pointers to incomplete types. |
| 5936 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); |
| 5937 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); |
| 5938 | if (isLHSVoidPtr || isRHSVoidPtr) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5939 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHSExpr); |
| 5940 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHSExpr); |
| 5941 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5942 | |
| 5943 | return !S.getLangOptions().CPlusPlus; |
| 5944 | } |
| 5945 | |
| 5946 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); |
| 5947 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); |
| 5948 | if (isLHSFuncPtr || isRHSFuncPtr) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5949 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHSExpr); |
| 5950 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, |
| 5951 | RHSExpr); |
| 5952 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5953 | |
| 5954 | return !S.getLangOptions().CPlusPlus; |
| 5955 | } |
| 5956 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5957 | if (checkArithmeticIncompletePointerType(S, Loc, LHSExpr)) return false; |
| 5958 | if (checkArithmeticIncompletePointerType(S, Loc, RHSExpr)) return false; |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5959 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5960 | return true; |
| 5961 | } |
| 5962 | |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 5963 | /// \brief Check bad cases where we step over interface counts. |
| 5964 | static bool checkArithmethicPointerOnNonFragileABI(Sema &S, |
| 5965 | SourceLocation OpLoc, |
| 5966 | Expr *Op) { |
| 5967 | assert(Op->getType()->isAnyPointerType()); |
| 5968 | QualType PointeeTy = Op->getType()->getPointeeType(); |
| 5969 | if (!PointeeTy->isObjCObjectType() || !S.LangOpts.ObjCNonFragileABI) |
| 5970 | return true; |
| 5971 | |
| 5972 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 5973 | << PointeeTy << Op->getSourceRange(); |
| 5974 | return false; |
| 5975 | } |
| 5976 | |
| 5977 | /// \brief Warn when two pointers are incompatible. |
| 5978 | static void diagnosePointerIncompatibility(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5979 | Expr *LHSExpr, Expr *RHSExpr) { |
| 5980 | assert(LHSExpr->getType()->isAnyPointerType()); |
| 5981 | assert(RHSExpr->getType()->isAnyPointerType()); |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 5982 | S.Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5983 | << LHSExpr->getType() << RHSExpr->getType() << LHSExpr->getSourceRange() |
| 5984 | << RHSExpr->getSourceRange(); |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 5985 | } |
| 5986 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5987 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5988 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, QualType* CompLHSTy) { |
| 5989 | if (LHS.get()->getType()->isVectorType() || |
| 5990 | RHS.get()->getType()->isVectorType()) { |
| 5991 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5992 | if (CompLHSTy) *CompLHSTy = compType; |
| 5993 | return compType; |
| 5994 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5995 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5996 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 5997 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5998 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5999 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6000 | // handle the common case first (both operands are arithmetic). |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6001 | if (LHS.get()->getType()->isArithmeticType() && |
| 6002 | RHS.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6003 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6004 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6005 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6006 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6007 | // Put any potential pointer into PExp |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6008 | Expr* PExp = LHS.get(), *IExp = RHS.get(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6009 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6010 | std::swap(PExp, IExp); |
| 6011 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6012 | if (PExp->getType()->isAnyPointerType()) { |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6013 | if (IExp->getType()->isIntegerType()) { |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6014 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) |
| 6015 | return QualType(); |
| 6016 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6017 | // Diagnose bad cases where we step over interface counts. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6018 | if (!checkArithmethicPointerOnNonFragileABI(*this, Loc, PExp)) |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6019 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6020 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6021 | // Check array bounds for pointer arithemtic |
| 6022 | CheckArrayAccess(PExp, IExp); |
| 6023 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6024 | if (CompLHSTy) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6025 | QualType LHSTy = Context.isPromotableBitField(LHS.get()); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 6026 | if (LHSTy.isNull()) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6027 | LHSTy = LHS.get()->getType(); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 6028 | if (LHSTy->isPromotableIntegerType()) |
| 6029 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 6030 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6031 | *CompLHSTy = LHSTy; |
| 6032 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6033 | return PExp->getType(); |
| 6034 | } |
| 6035 | } |
| 6036 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6037 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6038 | } |
| 6039 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6040 | // C99 6.5.6 |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6041 | QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6042 | SourceLocation Loc, |
| 6043 | QualType* CompLHSTy) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6044 | if (LHS.get()->getType()->isVectorType() || |
| 6045 | RHS.get()->getType()->isVectorType()) { |
| 6046 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6047 | if (CompLHSTy) *CompLHSTy = compType; |
| 6048 | return compType; |
| 6049 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6050 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6051 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 6052 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6053 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6054 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6055 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6056 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6057 | // Handle the common case first (both operands are arithmetic). |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6058 | if (LHS.get()->getType()->isArithmeticType() && |
| 6059 | RHS.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6060 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6061 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6062 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6063 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6064 | // Either ptr - int or ptr - ptr. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6065 | if (LHS.get()->getType()->isAnyPointerType()) { |
| 6066 | QualType lpointee = LHS.get()->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6067 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6068 | // Diagnose bad cases where we step over interface counts. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6069 | if (!checkArithmethicPointerOnNonFragileABI(*this, Loc, LHS.get())) |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6070 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6071 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6072 | // The result type of a pointer-int computation is the pointer type. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6073 | if (RHS.get()->getType()->isIntegerType()) { |
| 6074 | if (!checkArithmeticOpPointerOperand(*this, Loc, LHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6075 | return QualType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6076 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6077 | Expr *IExpr = RHS.get()->IgnoreParenCasts(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6078 | UnaryOperator negRex(IExpr, UO_Minus, IExpr->getType(), VK_RValue, |
| 6079 | OK_Ordinary, IExpr->getExprLoc()); |
| 6080 | // Check array bounds for pointer arithemtic |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6081 | CheckArrayAccess(LHS.get()->IgnoreParenCasts(), &negRex); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6082 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6083 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
| 6084 | return LHS.get()->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6085 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6086 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6087 | // Handle pointer-pointer subtractions. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6088 | if (const PointerType *RHSPTy |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6089 | = RHS.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6090 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6091 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6092 | if (getLangOptions().CPlusPlus) { |
| 6093 | // Pointee types must be the same: C++ [expr.add] |
| 6094 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6095 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6096 | } |
| 6097 | } else { |
| 6098 | // Pointee types must be compatible C99 6.5.6p3 |
| 6099 | if (!Context.typesAreCompatible( |
| 6100 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6101 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6102 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6103 | return QualType(); |
| 6104 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6105 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6106 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6107 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6108 | LHS.get(), RHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6109 | return QualType(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6110 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6111 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6112 | return Context.getPointerDiffType(); |
| 6113 | } |
| 6114 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6115 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6116 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6119 | static bool isScopedEnumerationType(QualType T) { |
| 6120 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6121 | return ET->getDecl()->isScoped(); |
| 6122 | return false; |
| 6123 | } |
| 6124 | |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6125 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6126 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6127 | QualType LHSType) { |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6128 | llvm::APSInt Right; |
| 6129 | // Check right/shifter operand |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6130 | if (RHS.get()->isValueDependent() || |
| 6131 | !RHS.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6132 | return; |
| 6133 | |
| 6134 | if (Right.isNegative()) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6135 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6136 | S.PDiag(diag::warn_shift_negative) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6137 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6138 | return; |
| 6139 | } |
| 6140 | llvm::APInt LeftBits(Right.getBitWidth(), |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6141 | S.Context.getTypeSize(LHS.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6142 | if (Right.uge(LeftBits)) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6143 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6144 | S.PDiag(diag::warn_shift_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6145 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6146 | return; |
| 6147 | } |
| 6148 | if (Opc != BO_Shl) |
| 6149 | return; |
| 6150 | |
| 6151 | // When left shifting an ICE which is signed, we can check for overflow which |
| 6152 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 6153 | // integers have defined behavior modulo one more than the maximum value |
| 6154 | // representable in the result type, so never warn for those. |
| 6155 | llvm::APSInt Left; |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6156 | if (LHS.get()->isValueDependent() || |
| 6157 | !LHS.get()->isIntegerConstantExpr(Left, S.Context) || |
| 6158 | LHSType->hasUnsignedIntegerRepresentation()) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6159 | return; |
| 6160 | llvm::APInt ResultBits = |
| 6161 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 6162 | if (LeftBits.uge(ResultBits)) |
| 6163 | return; |
| 6164 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 6165 | Result = Result.shl(Right); |
| 6166 | |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6167 | // Print the bit representation of the signed integer as an unsigned |
| 6168 | // hexadecimal number. |
| 6169 | llvm::SmallString<40> HexResult; |
| 6170 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 6171 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6172 | // If we are only missing a sign bit, this is less likely to result in actual |
| 6173 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 6174 | // expected value. Thus we place this behind a different warning that can be |
| 6175 | // turned off separately if needed. |
| 6176 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6177 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6178 | << HexResult.str() << LHSType |
| 6179 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6180 | return; |
| 6181 | } |
| 6182 | |
| 6183 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6184 | << HexResult.str() << Result.getMinSignedBits() << LHSType |
| 6185 | << Left.getBitWidth() << LHS.get()->getSourceRange() |
| 6186 | << RHS.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6187 | } |
| 6188 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6189 | // C99 6.5.7 |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6190 | QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6191 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6192 | bool IsCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6193 | // C99 6.5.7p2: Each of the operands shall have integer type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6194 | if (!LHS.get()->getType()->hasIntegerRepresentation() || |
| 6195 | !RHS.get()->getType()->hasIntegerRepresentation()) |
| 6196 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6197 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6198 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6199 | // hasIntegerRepresentation() above instead of this. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6200 | if (isScopedEnumerationType(LHS.get()->getType()) || |
| 6201 | isScopedEnumerationType(RHS.get()->getType())) { |
| 6202 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6203 | } |
| 6204 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6205 | // Vector shifts promote their scalar inputs to vector type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6206 | if (LHS.get()->getType()->isVectorType() || |
| 6207 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6208 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6209 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6210 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6211 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6212 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6213 | // For the LHS, do usual unary conversions, but then reset them away |
| 6214 | // if this is a compound assignment. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6215 | ExprResult OldLHS = LHS; |
| 6216 | LHS = UsualUnaryConversions(LHS.take()); |
| 6217 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6218 | return QualType(); |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6219 | QualType LHSType = LHS.get()->getType(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6220 | if (IsCompAssign) LHS = OldLHS; |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6221 | |
| 6222 | // The RHS is simpler. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6223 | RHS = UsualUnaryConversions(RHS.take()); |
| 6224 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6225 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6226 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6227 | // Sanity-check shift operands |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6228 | DiagnoseBadShiftValues(*this, LHS, RHS, Loc, Opc, LHSType); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6229 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6230 | // "The type of the result is that of the promoted left operand." |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6231 | return LHSType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6232 | } |
| 6233 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6234 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6235 | if (DeclContext *DC = D->getDeclContext()) { |
| 6236 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6237 | return true; |
| 6238 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6239 | return FD->isFunctionTemplateSpecialization(); |
| 6240 | } |
| 6241 | return false; |
| 6242 | } |
| 6243 | |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6244 | /// If two different enums are compared, raise a warning. |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6245 | static void checkEnumComparison(Sema &S, SourceLocation Loc, ExprResult &LHS, |
| 6246 | ExprResult &RHS) { |
| 6247 | QualType LHSStrippedType = LHS.get()->IgnoreParenImpCasts()->getType(); |
| 6248 | QualType RHSStrippedType = RHS.get()->IgnoreParenImpCasts()->getType(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6249 | |
| 6250 | const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>(); |
| 6251 | if (!LHSEnumType) |
| 6252 | return; |
| 6253 | const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>(); |
| 6254 | if (!RHSEnumType) |
| 6255 | return; |
| 6256 | |
| 6257 | // Ignore anonymous enums. |
| 6258 | if (!LHSEnumType->getDecl()->getIdentifier()) |
| 6259 | return; |
| 6260 | if (!RHSEnumType->getDecl()->getIdentifier()) |
| 6261 | return; |
| 6262 | |
| 6263 | if (S.Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) |
| 6264 | return; |
| 6265 | |
| 6266 | S.Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6267 | << LHSStrippedType << RHSStrippedType |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6268 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6269 | } |
| 6270 | |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6271 | /// \brief Diagnose bad pointer comparisons. |
| 6272 | static void diagnoseDistinctPointerComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6273 | ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6274 | bool IsError) { |
| 6275 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_distinct_pointers |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6276 | : diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6277 | << LHS.get()->getType() << RHS.get()->getType() |
| 6278 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6279 | } |
| 6280 | |
| 6281 | /// \brief Returns false if the pointers are converted to a composite type, |
| 6282 | /// true otherwise. |
| 6283 | static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6284 | ExprResult &LHS, ExprResult &RHS) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6285 | // C++ [expr.rel]p2: |
| 6286 | // [...] Pointer conversions (4.10) and qualification |
| 6287 | // conversions (4.4) are performed on pointer operands (or on |
| 6288 | // a pointer operand and a null pointer constant) to bring |
| 6289 | // them to their composite pointer type. [...] |
| 6290 | // |
| 6291 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
| 6292 | // comparisons of pointers. |
| 6293 | |
| 6294 | // C++ [expr.eq]p2: |
| 6295 | // In addition, pointers to members can be compared, or a pointer to |
| 6296 | // member and a null pointer constant. Pointer to member conversions |
| 6297 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6298 | // them to a common type. If one operand is a null pointer constant, |
| 6299 | // the common type is the type of the other operand. Otherwise, the |
| 6300 | // common type is a pointer to member type similar (4.4) to the type |
| 6301 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6302 | // that is the union of the cv-qualification signatures of the operand |
| 6303 | // types. |
| 6304 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6305 | QualType LHSType = LHS.get()->getType(); |
| 6306 | QualType RHSType = RHS.get()->getType(); |
| 6307 | assert((LHSType->isPointerType() && RHSType->isPointerType()) || |
| 6308 | (LHSType->isMemberPointerType() && RHSType->isMemberPointerType())); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6309 | |
| 6310 | bool NonStandardCompositeType = false; |
Richard Trieu | 43dff1b | 2011-09-02 21:44:27 +0000 | [diff] [blame] | 6311 | bool *BoolPtr = S.isSFINAEContext() ? 0 : &NonStandardCompositeType; |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6312 | QualType T = S.FindCompositePointerType(Loc, LHS, RHS, BoolPtr); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6313 | if (T.isNull()) { |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6314 | diagnoseDistinctPointerComparison(S, Loc, LHS, RHS, /*isError*/true); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6315 | return true; |
| 6316 | } |
| 6317 | |
| 6318 | if (NonStandardCompositeType) |
| 6319 | S.Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6320 | << LHSType << RHSType << T << LHS.get()->getSourceRange() |
| 6321 | << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6322 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6323 | LHS = S.ImpCastExprToType(LHS.take(), T, CK_BitCast); |
| 6324 | RHS = S.ImpCastExprToType(RHS.take(), T, CK_BitCast); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6325 | return false; |
| 6326 | } |
| 6327 | |
| 6328 | static void diagnoseFunctionPointerToVoidComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6329 | ExprResult &LHS, |
| 6330 | ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6331 | bool IsError) { |
| 6332 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_fptr_to_void |
| 6333 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6334 | << LHS.get()->getType() << RHS.get()->getType() |
| 6335 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6336 | } |
| 6337 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6338 | // C99 6.5.8, C++ [expr.rel] |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6339 | QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6340 | SourceLocation Loc, unsigned OpaqueOpc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6341 | bool IsRelational) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6342 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6343 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6344 | // Handle vector comparisons separately. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6345 | if (LHS.get()->getType()->isVectorType() || |
| 6346 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6347 | return CheckVectorCompareOperands(LHS, RHS, Loc, IsRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6348 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6349 | QualType LHSType = LHS.get()->getType(); |
| 6350 | QualType RHSType = RHS.get()->getType(); |
Benjamin Kramer | fec0959 | 2011-09-03 08:46:20 +0000 | [diff] [blame] | 6351 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6352 | Expr *LHSStripped = LHS.get()->IgnoreParenImpCasts(); |
| 6353 | Expr *RHSStripped = RHS.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6354 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6355 | checkEnumComparison(*this, Loc, LHS, RHS); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6356 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6357 | if (!LHSType->hasFloatingRepresentation() && |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6358 | !(LHSType->isBlockPointerType() && IsRelational) && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6359 | !LHS.get()->getLocStart().isMacroID() && |
| 6360 | !RHS.get()->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6361 | // For non-floating point types, check for self-comparisons of the form |
| 6362 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6363 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6364 | // |
| 6365 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6366 | // expansion. Also don't warn about comparisons which are only self |
| 6367 | // comparisons within a template specialization. The warnings should catch |
| 6368 | // obvious cases in the definition of the template anyways. The idea is to |
| 6369 | // warn when the typed comparison operator will always evaluate to the same |
| 6370 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6371 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6372 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6373 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6374 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6375 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6376 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6377 | << (Opc == BO_EQ |
| 6378 | || Opc == BO_LE |
| 6379 | || Opc == BO_GE)); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6380 | } else if (LHSType->isArrayType() && RHSType->isArrayType() && |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6381 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6382 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6383 | // what is it always going to eval to? |
| 6384 | char always_evals_to; |
| 6385 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6386 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6387 | always_evals_to = 0; // false |
| 6388 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6389 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6390 | always_evals_to = 1; // true |
| 6391 | break; |
| 6392 | default: |
| 6393 | // best we can say is 'a constant' |
| 6394 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6395 | break; |
| 6396 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6397 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6398 | << 1 // array |
| 6399 | << always_evals_to); |
| 6400 | } |
| 6401 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6402 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6403 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6404 | if (isa<CastExpr>(LHSStripped)) |
| 6405 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6406 | if (isa<CastExpr>(RHSStripped)) |
| 6407 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6408 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6409 | // Warn about comparisons against a string constant (unless the other |
| 6410 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6411 | Expr *literalString = 0; |
| 6412 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6413 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6414 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6415 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6416 | literalString = LHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6417 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6418 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 6419 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6420 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6421 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6422 | literalString = RHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6423 | literalStringStripped = RHSStripped; |
| 6424 | } |
| 6425 | |
| 6426 | if (literalString) { |
| 6427 | std::string resultComparison; |
| 6428 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6429 | case BO_LT: resultComparison = ") < 0"; break; |
| 6430 | case BO_GT: resultComparison = ") > 0"; break; |
| 6431 | case BO_LE: resultComparison = ") <= 0"; break; |
| 6432 | case BO_GE: resultComparison = ") >= 0"; break; |
| 6433 | case BO_EQ: resultComparison = ") == 0"; break; |
| 6434 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6435 | default: assert(false && "Invalid comparison operator"); |
| 6436 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6437 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6438 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 6439 | PDiag(diag::warn_stringcompare) |
| 6440 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 6441 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6442 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 6443 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6444 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6445 | // C99 6.5.8p3 / C99 6.5.9p4 |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6446 | if (LHS.get()->getType()->isArithmeticType() && |
| 6447 | RHS.get()->getType()->isArithmeticType()) { |
| 6448 | UsualArithmeticConversions(LHS, RHS); |
| 6449 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6450 | return QualType(); |
| 6451 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6452 | else { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6453 | LHS = UsualUnaryConversions(LHS.take()); |
| 6454 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6455 | return QualType(); |
| 6456 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6457 | RHS = UsualUnaryConversions(RHS.take()); |
| 6458 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6459 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6460 | } |
| 6461 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6462 | LHSType = LHS.get()->getType(); |
| 6463 | RHSType = RHS.get()->getType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6464 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6465 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 6466 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6467 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6468 | if (IsRelational) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6469 | if (LHSType->isRealType() && RHSType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6470 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6471 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 6472 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6473 | if (LHSType->hasFloatingRepresentation()) |
| 6474 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6475 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6476 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6477 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6478 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6479 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6480 | bool LHSIsNull = LHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6481 | Expr::NPC_ValueDependentIsNull); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6482 | bool RHSIsNull = RHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6483 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6484 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6485 | // All of the following pointer-related warnings are GCC extensions, except |
| 6486 | // when handling null pointer constants. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6487 | if (LHSType->isPointerType() && RHSType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6488 | QualType LCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6489 | LHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6490 | QualType RCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6491 | RHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6492 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6493 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6494 | if (LCanPointeeTy == RCanPointeeTy) |
| 6495 | return ResultTy; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6496 | if (!IsRelational && |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6497 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6498 | // Valid unless comparison between non-null pointer and function pointer |
| 6499 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6500 | // In a SFINAE context, we treat this as a hard error to maintain |
| 6501 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6502 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6503 | && !LHSIsNull && !RHSIsNull) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6504 | diagnoseFunctionPointerToVoidComparison( |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6505 | *this, Loc, LHS, RHS, /*isError*/ isSFINAEContext()); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6506 | |
| 6507 | if (isSFINAEContext()) |
| 6508 | return QualType(); |
| 6509 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6510 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6511 | return ResultTy; |
| 6512 | } |
| 6513 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6514 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6515 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6516 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6517 | else |
| 6518 | return ResultTy; |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6519 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6520 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 6521 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 6522 | RCanPointeeTy.getUnqualifiedType())) { |
| 6523 | // Valid unless a relational comparison of function pointers |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6524 | if (IsRelational && LCanPointeeTy->isFunctionType()) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6525 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6526 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6527 | << RHS.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6528 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6529 | } else if (!IsRelational && |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6530 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6531 | // Valid unless comparison between non-null pointer and function pointer |
| 6532 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6533 | && !LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6534 | diagnoseFunctionPointerToVoidComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6535 | /*isError*/false); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6536 | } else { |
| 6537 | // Invalid |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6538 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6539 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6540 | if (LCanPointeeTy != RCanPointeeTy) { |
| 6541 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6542 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6543 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6544 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6545 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6546 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 6547 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6548 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6549 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6550 | // Comparison of nullptr_t with itself. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6551 | if (LHSType->isNullPtrType() && RHSType->isNullPtrType()) |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6552 | return ResultTy; |
| 6553 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6554 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6555 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6556 | if (RHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6557 | ((LHSType->isAnyPointerType() || LHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6558 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6559 | (LHSType->isMemberPointerType() || LHSType->isBlockPointerType())))) { |
| 6560 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 6561 | LHSType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6562 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6563 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6564 | return ResultTy; |
| 6565 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6566 | if (LHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6567 | ((RHSType->isAnyPointerType() || RHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6568 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6569 | (RHSType->isMemberPointerType() || RHSType->isBlockPointerType())))) { |
| 6570 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 6571 | RHSType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6572 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6573 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6574 | return ResultTy; |
| 6575 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6576 | |
| 6577 | // Comparison of member pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6578 | if (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6579 | LHSType->isMemberPointerType() && RHSType->isMemberPointerType()) { |
| 6580 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6581 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6582 | else |
| 6583 | return ResultTy; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6584 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6585 | |
| 6586 | // Handle scoped enumeration types specifically, since they don't promote |
| 6587 | // to integers. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6588 | if (LHS.get()->getType()->isEnumeralType() && |
| 6589 | Context.hasSameUnqualifiedType(LHS.get()->getType(), |
| 6590 | RHS.get()->getType())) |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6591 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6592 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6593 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6594 | // Handle block pointer types. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6595 | if (!IsRelational && LHSType->isBlockPointerType() && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6596 | RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6597 | QualType lpointee = LHSType->castAs<BlockPointerType>()->getPointeeType(); |
| 6598 | QualType rpointee = RHSType->castAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6599 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6600 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 6601 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6602 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6603 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6604 | << RHS.get()->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6605 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6606 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6607 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6608 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6609 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6610 | // Allow block pointers to be compared with null pointer constants. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6611 | if (!IsRelational |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6612 | && ((LHSType->isBlockPointerType() && RHSType->isPointerType()) |
| 6613 | || (LHSType->isPointerType() && RHSType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6614 | if (!LHSIsNull && !RHSIsNull) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6615 | if (!((RHSType->isPointerType() && RHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6616 | ->getPointeeType()->isVoidType()) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6617 | || (LHSType->isPointerType() && LHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6618 | ->getPointeeType()->isVoidType()))) |
| 6619 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6620 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6621 | << RHS.get()->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6622 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6623 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6624 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 6625 | RHSType->isPointerType() ? CK_BitCast |
| 6626 | : CK_AnyPointerToBlockPointerCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6627 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6628 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 6629 | LHSType->isPointerType() ? CK_BitCast |
| 6630 | : CK_AnyPointerToBlockPointerCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6631 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6632 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6633 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6634 | if (LHSType->isObjCObjectPointerType() || |
| 6635 | RHSType->isObjCObjectPointerType()) { |
| 6636 | const PointerType *LPT = LHSType->getAs<PointerType>(); |
| 6637 | const PointerType *RPT = RHSType->getAs<PointerType>(); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6638 | if (LPT || RPT) { |
| 6639 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 6640 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6641 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 6642 | if (!LPtrToVoid && !RPtrToVoid && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6643 | !Context.typesAreCompatible(LHSType, RHSType)) { |
| 6644 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6645 | /*isError*/false); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 6646 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6647 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6648 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 6649 | RPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6650 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6651 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 6652 | LPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6653 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 6654 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6655 | if (LHSType->isObjCObjectPointerType() && |
| 6656 | RHSType->isObjCObjectPointerType()) { |
| 6657 | if (!Context.areComparableObjCPointerTypes(LHSType, RHSType)) |
| 6658 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6659 | /*isError*/false); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6660 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6661 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6662 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6663 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6664 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 6665 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 6666 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6667 | if ((LHSType->isAnyPointerType() && RHSType->isIntegerType()) || |
| 6668 | (LHSType->isIntegerType() && RHSType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6669 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6670 | bool isError = false; |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6671 | if ((LHSIsNull && LHSType->isIntegerType()) || |
| 6672 | (RHSIsNull && RHSType->isIntegerType())) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6673 | if (IsRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6674 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6675 | } else if (IsRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6676 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6677 | else if (getLangOptions().CPlusPlus) { |
| 6678 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 6679 | isError = true; |
| 6680 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6681 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6682 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6683 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6684 | Diag(Loc, DiagID) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6685 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6686 | << RHS.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6687 | if (isError) |
| 6688 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6689 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6690 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6691 | if (LHSType->isIntegerType()) |
| 6692 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6693 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6694 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6695 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6696 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6697 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6698 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6699 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6700 | // Handle block pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6701 | if (!IsRelational && RHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6702 | && LHSType->isBlockPointerType() && RHSType->isIntegerType()) { |
| 6703 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6704 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6705 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6706 | if (!IsRelational && LHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6707 | && LHSType->isIntegerType() && RHSType->isBlockPointerType()) { |
| 6708 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6709 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6710 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6711 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6712 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6713 | } |
| 6714 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6715 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6716 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6717 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 6718 | /// types. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6719 | QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6720 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6721 | bool IsRelational) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6722 | // Check to make sure we're operating on vectors of the same type and width, |
| 6723 | // Allowing one side to be a scalar of element type. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6724 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, /*isCompAssign*/false); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6725 | if (vType.isNull()) |
| 6726 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6727 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6728 | QualType LHSType = LHS.get()->getType(); |
| 6729 | QualType RHSType = RHS.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6730 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6731 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 6732 | // bool for C++, int for C |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 6733 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6734 | return Context.getLogicalOperationType(); |
| 6735 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6736 | // For non-floating point types, check for self-comparisons of the form |
| 6737 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6738 | // often indicate logic errors in the program. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6739 | if (!LHSType->hasFloatingRepresentation()) { |
| 6740 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHS.get()->IgnoreParens())) |
| 6741 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHS.get()->IgnoreParens())) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6742 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6743 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6744 | PDiag(diag::warn_comparison_always) |
| 6745 | << 0 // self- |
| 6746 | << 2 // "a constant" |
| 6747 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6748 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6749 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6750 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6751 | if (!IsRelational && LHSType->hasFloatingRepresentation()) { |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6752 | assert (RHSType->hasFloatingRepresentation()); |
| 6753 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6754 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6755 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6756 | // Return the type for the comparison, which is the same as vector type for |
| 6757 | // integer vectors, or an integer type of identical size and number of |
| 6758 | // elements for floating point vectors. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6759 | if (LHSType->hasIntegerRepresentation()) |
| 6760 | return LHSType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6761 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6762 | const VectorType *VTy = LHSType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6763 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6764 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6765 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 6766 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6767 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 6768 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6769 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6770 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6771 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 6772 | } |
| 6773 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6774 | inline QualType Sema::CheckBitwiseOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6775 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6776 | if (LHS.get()->getType()->isVectorType() || |
| 6777 | RHS.get()->getType()->isVectorType()) { |
| 6778 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 6779 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6780 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6781 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6782 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6783 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6784 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6785 | ExprResult LHSResult = Owned(LHS), RHSResult = Owned(RHS); |
| 6786 | QualType compType = UsualArithmeticConversions(LHSResult, RHSResult, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6787 | IsCompAssign); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6788 | if (LHSResult.isInvalid() || RHSResult.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6789 | return QualType(); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6790 | LHS = LHSResult.take(); |
| 6791 | RHS = RHSResult.take(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6792 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6793 | if (LHS.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 6794 | RHS.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6795 | return compType; |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6796 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6797 | } |
| 6798 | |
| 6799 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6800 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6801 | |
| 6802 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 6803 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 6804 | // is a constant. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6805 | if (LHS.get()->getType()->isIntegerType() && |
| 6806 | !LHS.get()->getType()->isBooleanType() && |
| 6807 | RHS.get()->getType()->isIntegerType() && !RHS.get()->isValueDependent() && |
Richard Trieu | e5adf59 | 2011-07-15 00:00:51 +0000 | [diff] [blame] | 6808 | // Don't warn in macros or template instantiations. |
| 6809 | !Loc.isMacroID() && ActiveTemplateInstantiations.empty()) { |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6810 | // If the RHS can be constant folded, and if it constant folds to something |
| 6811 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 6812 | // happened to fold to true/false) then warn. |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6813 | // Parens on the RHS are ignored. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6814 | Expr::EvalResult Result; |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6815 | if (RHS.get()->Evaluate(Result, Context) && !Result.HasSideEffects) |
| 6816 | if ((getLangOptions().Bool && !RHS.get()->getType()->isBooleanType()) || |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6817 | (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) { |
| 6818 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6819 | << RHS.get()->getSourceRange() |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6820 | << (Opc == BO_LAnd ? "&&" : "||"); |
| 6821 | // Suggest replacing the logical operator with the bitwise version |
| 6822 | Diag(Loc, diag::note_logical_instead_of_bitwise_change_operator) |
| 6823 | << (Opc == BO_LAnd ? "&" : "|") |
| 6824 | << FixItHint::CreateReplacement(SourceRange( |
| 6825 | Loc, Lexer::getLocForEndOfToken(Loc, 0, getSourceManager(), |
| 6826 | getLangOptions())), |
| 6827 | Opc == BO_LAnd ? "&" : "|"); |
| 6828 | if (Opc == BO_LAnd) |
| 6829 | // Suggest replacing "Foo() && kNonZero" with "Foo()" |
| 6830 | Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant) |
| 6831 | << FixItHint::CreateRemoval( |
| 6832 | SourceRange( |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6833 | Lexer::getLocForEndOfToken(LHS.get()->getLocEnd(), |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6834 | 0, getSourceManager(), |
| 6835 | getLangOptions()), |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6836 | RHS.get()->getLocEnd())); |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6837 | } |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6838 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6839 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6840 | if (!Context.getLangOptions().CPlusPlus) { |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6841 | LHS = UsualUnaryConversions(LHS.take()); |
| 6842 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6843 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6844 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6845 | RHS = UsualUnaryConversions(RHS.take()); |
| 6846 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6847 | return QualType(); |
| 6848 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6849 | if (!LHS.get()->getType()->isScalarType() || |
| 6850 | !RHS.get()->getType()->isScalarType()) |
| 6851 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6852 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6853 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 6854 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6855 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6856 | // The following is safe because we only use this method for |
| 6857 | // non-overloadable operands. |
| 6858 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6859 | // C++ [expr.log.and]p1 |
| 6860 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6861 | // The operands are both contextually converted to type bool. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6862 | ExprResult LHSRes = PerformContextuallyConvertToBool(LHS.get()); |
| 6863 | if (LHSRes.isInvalid()) |
| 6864 | return InvalidOperands(Loc, LHS, RHS); |
| 6865 | LHS = move(LHSRes); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6866 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6867 | ExprResult RHSRes = PerformContextuallyConvertToBool(RHS.get()); |
| 6868 | if (RHSRes.isInvalid()) |
| 6869 | return InvalidOperands(Loc, LHS, RHS); |
| 6870 | RHS = move(RHSRes); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6871 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6872 | // C++ [expr.log.and]p2 |
| 6873 | // C++ [expr.log.or]p2 |
| 6874 | // The result is a bool. |
| 6875 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6876 | } |
| 6877 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6878 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 6879 | /// is a read-only property; return true if so. A readonly property expression |
| 6880 | /// depends on various declarations and thus must be treated specially. |
| 6881 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6882 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6883 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6884 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6885 | if (PropExpr->isImplicitProperty()) return false; |
| 6886 | |
| 6887 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6888 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 6889 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 6890 | PropExpr->getBase()->getType(); |
| 6891 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6892 | if (const ObjCObjectPointerType *OPT = |
| 6893 | BaseType->getAsObjCInterfacePointerType()) |
| 6894 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 6895 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 6896 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6897 | } |
| 6898 | return false; |
| 6899 | } |
| 6900 | |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6901 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 6902 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6903 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 6904 | if (PropExpr->isImplicitProperty()) return false; |
| 6905 | |
| 6906 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6907 | QualType T = PDecl->getType(); |
| 6908 | if (T->isReferenceType()) |
Fariborz Jahanian | 61750f2 | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 6909 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6910 | CanQualType CT = S.Context.getCanonicalType(T); |
| 6911 | return CT.isConstQualified(); |
| 6912 | } |
| 6913 | return false; |
| 6914 | } |
| 6915 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6916 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 6917 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 6918 | return false; |
| 6919 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 6920 | NamedDecl *Member = ME->getMemberDecl(); |
| 6921 | if (isa<FieldDecl>(Member)) { |
| 6922 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 6923 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 6924 | return false; |
| 6925 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 6926 | } |
| 6927 | return false; |
| 6928 | } |
| 6929 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6930 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 6931 | /// emit an error and return true. If so, return false. |
| 6932 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6933 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6934 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6935 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6936 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 6937 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6938 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 6939 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6940 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 6941 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6942 | if (IsLV == Expr::MLV_Valid) |
| 6943 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6944 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6945 | unsigned Diag = 0; |
| 6946 | bool NeedType = false; |
| 6947 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6948 | case Expr::MLV_ConstQualified: |
| 6949 | Diag = diag::err_typecheck_assign_const; |
| 6950 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6951 | // In ARC, use some specialized diagnostics for occasions where we |
| 6952 | // infer 'const'. These are always pseudo-strong variables. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6953 | if (S.getLangOptions().ObjCAutoRefCount) { |
| 6954 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 6955 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 6956 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 6957 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6958 | // Use the normal diagnostic if it's pseudo-__strong but the |
| 6959 | // user actually wrote 'const'. |
| 6960 | if (var->isARCPseudoStrong() && |
| 6961 | (!var->getTypeSourceInfo() || |
| 6962 | !var->getTypeSourceInfo()->getType().isConstQualified())) { |
| 6963 | // There are two pseudo-strong cases: |
| 6964 | // - self |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6965 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 6966 | if (method && var == method->getSelfDecl()) |
| 6967 | Diag = diag::err_typecheck_arr_assign_self; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6968 | |
| 6969 | // - fast enumeration variables |
| 6970 | else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6971 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6972 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6973 | SourceRange Assign; |
| 6974 | if (Loc != OrigLoc) |
| 6975 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 6976 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 6977 | // We need to preserve the AST regardless, so migration tool |
| 6978 | // can do its job. |
| 6979 | return false; |
| 6980 | } |
| 6981 | } |
| 6982 | } |
| 6983 | |
| 6984 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6985 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6986 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 6987 | NeedType = true; |
| 6988 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6989 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6990 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 6991 | NeedType = true; |
| 6992 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 6993 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6994 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 6995 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6996 | case Expr::MLV_Valid: |
| 6997 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6998 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6999 | case Expr::MLV_MemberFunction: |
| 7000 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7001 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 7002 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7003 | case Expr::MLV_IncompleteType: |
| 7004 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 7005 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7006 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 7007 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7008 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7009 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 7010 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 7011 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7012 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 7013 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 7014 | case Expr::MLV_ReadonlyProperty: |
| 7015 | Diag = diag::error_readonly_property_assignment; |
| 7016 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 7017 | case Expr::MLV_NoSetterProperty: |
| 7018 | Diag = diag::error_nosetter_property_assignment; |
| 7019 | break; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7020 | case Expr::MLV_InvalidMessageExpression: |
| 7021 | Diag = diag::error_readonly_message_assignment; |
| 7022 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 7023 | case Expr::MLV_SubObjCPropertySetting: |
| 7024 | Diag = diag::error_no_subobject_property_setting; |
| 7025 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7026 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7027 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7028 | SourceRange Assign; |
| 7029 | if (Loc != OrigLoc) |
| 7030 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7031 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7032 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7033 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7034 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7035 | return true; |
| 7036 | } |
| 7037 | |
| 7038 | |
| 7039 | |
| 7040 | // C99 6.5.16.1 |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7041 | QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7042 | SourceLocation Loc, |
| 7043 | QualType CompoundType) { |
| 7044 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7045 | if (CheckForModifiableLvalue(LHSExpr, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7046 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7047 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7048 | QualType LHSType = LHSExpr->getType(); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7049 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : |
| 7050 | CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7051 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7052 | if (CompoundType.isNull()) { |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7053 | QualType LHSTy(LHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7054 | // Simple assignment "x = y". |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7055 | if (LHSExpr->getObjectKind() == OK_ObjCProperty) { |
| 7056 | ExprResult LHSResult = Owned(LHSExpr); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7057 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 7058 | if (LHSResult.isInvalid()) |
| 7059 | return QualType(); |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7060 | LHSExpr = LHSResult.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7061 | } |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7062 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7063 | if (RHS.isInvalid()) |
| 7064 | return QualType(); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7065 | // Special case of NSObject attributes on c-style pointer types. |
| 7066 | if (ConvTy == IncompatiblePointer && |
| 7067 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7068 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7069 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7070 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7071 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7072 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7073 | if (ConvTy == Compatible && |
| 7074 | getLangOptions().ObjCNonFragileABI && |
| 7075 | LHSType->isObjCObjectType()) |
| 7076 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 7077 | << LHSType; |
| 7078 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7079 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 7080 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 7081 | // instead of "x += 4". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7082 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7083 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 7084 | RHSCheck = ICE->getSubExpr(); |
| 7085 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7086 | if ((UO->getOpcode() == UO_Plus || |
| 7087 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7088 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7089 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7090 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 7091 | // And there is a space or other character before the subexpr of the |
| 7092 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7093 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 7094 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7095 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7096 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7097 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7098 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7099 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7100 | |
| 7101 | if (ConvTy == Compatible) { |
| 7102 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7103 | checkRetainCycles(LHSExpr, RHS.get()); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 7104 | else if (getLangOptions().ObjCAutoRefCount) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7105 | checkUnsafeExprAssigns(Loc, LHSExpr, RHS.get()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7106 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7107 | } else { |
| 7108 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7109 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7110 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7111 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7112 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7113 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7114 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7115 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7116 | CheckForNullPointerDereference(*this, LHSExpr); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7117 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7118 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7119 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7120 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7121 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7122 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7123 | // 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] | 7124 | // operand. |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7125 | return (getLangOptions().CPlusPlus |
| 7126 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7127 | } |
| 7128 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7129 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7130 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7131 | SourceLocation Loc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7132 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 7133 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7134 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 7135 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7136 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7137 | return QualType(); |
| 7138 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7139 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7140 | // operands, but not unary promotions. |
| 7141 | // 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] | 7142 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7143 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7144 | // containing site to determine what should be done with the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7145 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 7146 | if (LHS.isInvalid()) |
| 7147 | return QualType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7148 | |
| 7149 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7150 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 7151 | if (RHS.isInvalid()) |
| 7152 | return QualType(); |
| 7153 | if (!RHS.get()->getType()->isVoidType()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7154 | S.RequireCompleteType(Loc, RHS.get()->getType(), |
| 7155 | diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7156 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7157 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7158 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7159 | } |
| 7160 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7161 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7162 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7163 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7164 | ExprValueKind &VK, |
| 7165 | SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7166 | bool IsInc, bool IsPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7167 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7168 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7169 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7170 | QualType ResType = Op->getType(); |
| 7171 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7172 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7173 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7174 | // Decrement of bool is not allowed. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7175 | if (!IsInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7176 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7177 | return QualType(); |
| 7178 | } |
| 7179 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7180 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7181 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7182 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7183 | } else if (ResType->isAnyPointerType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7184 | // C99 6.5.2.4p2, 6.5.6p2 |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7185 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7186 | return QualType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7187 | |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7188 | // Diagnose bad cases where we step over interface counts. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 7189 | else if (!checkArithmethicPointerOnNonFragileABI(S, OpLoc, Op)) |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7190 | return QualType(); |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7191 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7192 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7193 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7194 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7195 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7196 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7197 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7198 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7199 | IsInc, IsPrefix); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7200 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 7201 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7202 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7203 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7204 | << ResType << int(IsInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7205 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7206 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7207 | // 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] | 7208 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7209 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7210 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7211 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7212 | // (in C or with postfix), the increment is the unqualified type of the |
| 7213 | // operand. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7214 | if (IsPrefix && S.getLangOptions().CPlusPlus) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7215 | VK = VK_LValue; |
| 7216 | return ResType; |
| 7217 | } else { |
| 7218 | VK = VK_RValue; |
| 7219 | return ResType.getUnqualifiedType(); |
| 7220 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7221 | } |
| 7222 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7223 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7224 | assert(E->getValueKind() == VK_LValue && |
| 7225 | E->getObjectKind() == OK_ObjCProperty); |
| 7226 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 7227 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7228 | QualType T = E->getType(); |
| 7229 | QualType ReceiverType; |
| 7230 | if (PRE->isObjectReceiver()) |
| 7231 | ReceiverType = PRE->getBase()->getType(); |
| 7232 | else if (PRE->isSuperReceiver()) |
| 7233 | ReceiverType = PRE->getSuperReceiverType(); |
| 7234 | else |
| 7235 | ReceiverType = Context.getObjCInterfaceType(PRE->getClassReceiver()); |
| 7236 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7237 | ExprValueKind VK = VK_RValue; |
| 7238 | if (PRE->isImplicitProperty()) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7239 | if (ObjCMethodDecl *GetterMethod = |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7240 | PRE->getImplicitPropertyGetter()) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7241 | T = getMessageSendResultType(ReceiverType, GetterMethod, |
| 7242 | PRE->isClassReceiver(), |
| 7243 | PRE->isSuperReceiver()); |
| 7244 | VK = Expr::getValueKindForType(GetterMethod->getResultType()); |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7245 | } |
| 7246 | else { |
| 7247 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 7248 | << PRE->getBase()->getType(); |
| 7249 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7250 | } |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7251 | |
| 7252 | E = ImplicitCastExpr::Create(Context, T, CK_GetObjCProperty, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7253 | E, 0, VK); |
John McCall | db67e2f | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 7254 | |
| 7255 | ExprResult Result = MaybeBindToTemporary(E); |
| 7256 | if (!Result.isInvalid()) |
| 7257 | E = Result.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7258 | |
| 7259 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7260 | } |
| 7261 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7262 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, |
| 7263 | QualType &LHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7264 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 7265 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 7266 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7267 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7268 | bool Consumed = false; |
| 7269 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7270 | if (PropRef->isImplicitProperty()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7271 | // If using property-dot syntax notation for assignment, and there is a |
| 7272 | // setter, RHS expression is being passed to the setter argument. So, |
| 7273 | // type conversion (and comparison) is RHS to setter's argument type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7274 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7275 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 7276 | LHSTy = (*P)->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7277 | Consumed = (getLangOptions().ObjCAutoRefCount && |
| 7278 | (*P)->hasAttr<NSConsumedAttr>()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7279 | |
| 7280 | // Otherwise, if the getter returns an l-value, just call that. |
| 7281 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7282 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7283 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 7284 | if (VK == VK_LValue) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7285 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 7286 | CK_GetObjCProperty, LHS.take(), 0, VK); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7287 | return; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7288 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7289 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7290 | } else if (getLangOptions().ObjCAutoRefCount) { |
| 7291 | const ObjCMethodDecl *setter |
| 7292 | = PropRef->getExplicitProperty()->getSetterMethodDecl(); |
| 7293 | if (setter) { |
| 7294 | ObjCMethodDecl::param_iterator P = setter->param_begin(); |
| 7295 | LHSTy = (*P)->getType(); |
| 7296 | Consumed = (*P)->hasAttr<NSConsumedAttr>(); |
| 7297 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7298 | } |
| 7299 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7300 | if ((getLangOptions().CPlusPlus && LHSTy->isRecordType()) || |
| 7301 | getLangOptions().ObjCAutoRefCount) { |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7302 | InitializedEntity Entity = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7303 | InitializedEntity::InitializeParameter(Context, LHSTy, Consumed); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7304 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7305 | if (!ArgE.isInvalid()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7306 | RHS = ArgE; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7307 | if (getLangOptions().ObjCAutoRefCount && !PropRef->isSuperReceiver()) |
| 7308 | checkRetainCycles(const_cast<Expr*>(PropRef->getBase()), RHS.get()); |
| 7309 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7310 | } |
| 7311 | } |
| 7312 | |
| 7313 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7314 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7315 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7316 | /// where the declaration is needed for type checking. We only need to |
| 7317 | /// handle cases when the expression references a function designator |
| 7318 | /// or is an lvalue. Here are some examples: |
| 7319 | /// - &(x) => x |
| 7320 | /// - &*****f => f for f a function designator. |
| 7321 | /// - &s.xx => s |
| 7322 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7323 | /// - *(x + 1) -> x, if x is an array |
| 7324 | /// - &"123"[2] -> 0 |
| 7325 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7326 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7327 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7328 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7329 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7330 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7331 | // If this is an arrow operator, the address is an offset from |
| 7332 | // the base's value, so the object the base refers to is |
| 7333 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7334 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7335 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7336 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7337 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7338 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7339 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7340 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7341 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7342 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7343 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7344 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7345 | } |
| 7346 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7347 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7348 | case Stmt::UnaryOperatorClass: { |
| 7349 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7350 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7351 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7352 | case UO_Real: |
| 7353 | case UO_Imag: |
| 7354 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7355 | return getPrimaryDecl(UO->getSubExpr()); |
| 7356 | default: |
| 7357 | return 0; |
| 7358 | } |
| 7359 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7360 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7361 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7362 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7363 | // If the result of an implicit cast is an l-value, we care about |
| 7364 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7365 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7366 | default: |
| 7367 | return 0; |
| 7368 | } |
| 7369 | } |
| 7370 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7371 | namespace { |
| 7372 | enum { |
| 7373 | AO_Bit_Field = 0, |
| 7374 | AO_Vector_Element = 1, |
| 7375 | AO_Property_Expansion = 2, |
| 7376 | AO_Register_Variable = 3, |
| 7377 | AO_No_Error = 4 |
| 7378 | }; |
| 7379 | } |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7380 | /// \brief Diagnose invalid operand for address of operations. |
| 7381 | /// |
| 7382 | /// \param Type The type of operand which cannot have its address taken. |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7383 | static void diagnoseAddressOfInvalidType(Sema &S, SourceLocation Loc, |
| 7384 | Expr *E, unsigned Type) { |
| 7385 | S.Diag(Loc, diag::err_typecheck_address_of) << Type << E->getSourceRange(); |
| 7386 | } |
| 7387 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7388 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7389 | /// 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] | 7390 | /// 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] | 7391 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7392 | /// 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] | 7393 | /// 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] | 7394 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7395 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 7396 | SourceLocation OpLoc) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7397 | if (OrigOp->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7398 | return S.Context.DependentTy; |
| 7399 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 7400 | return S.Context.OverloadTy; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7401 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 7402 | return S.Context.UnknownAnyTy; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7403 | if (OrigOp->getType() == S.Context.BoundMemberTy) { |
| 7404 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 7405 | << OrigOp->getSourceRange(); |
| 7406 | return QualType(); |
| 7407 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7408 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7409 | assert(!OrigOp->getType()->isPlaceholderType()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7410 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7411 | // Make sure to ignore parentheses in subsequent checks |
| 7412 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 7413 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7414 | if (S.getLangOptions().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7415 | // Implement C99-only parts of addressof rules. |
| 7416 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7417 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7418 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 7419 | // (assuming the deref expression is valid). |
| 7420 | return uOp->getSubExpr()->getType(); |
| 7421 | } |
| 7422 | // Technically, there should be a check for array subscript |
| 7423 | // expressions here, but the result of one is always an lvalue anyway. |
| 7424 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7425 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7426 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7427 | unsigned AddressOfError = AO_No_Error; |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 7428 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7429 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7430 | bool sfinae = S.isSFINAEContext(); |
| 7431 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 7432 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7433 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7434 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7435 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7436 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7437 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7438 | } else if (lval == Expr::LV_MemberFunction) { |
| 7439 | // If it's an instance method, make a member pointer. |
| 7440 | // The expression must have exactly the form &A::foo. |
| 7441 | |
| 7442 | // If the underlying expression isn't a decl ref, give up. |
| 7443 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7444 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7445 | << OrigOp->getSourceRange(); |
| 7446 | return QualType(); |
| 7447 | } |
| 7448 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 7449 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 7450 | |
| 7451 | // The id-expression was parenthesized. |
| 7452 | if (OrigOp != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7453 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7454 | << OrigOp->getSourceRange(); |
| 7455 | |
| 7456 | // The method was named without a qualifier. |
| 7457 | } else if (!DRE->getQualifier()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7458 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7459 | << op->getSourceRange(); |
| 7460 | } |
| 7461 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7462 | return S.Context.getMemberPointerType(op->getType(), |
| 7463 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7464 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7465 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7466 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7467 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7468 | // FIXME: emit more specific diag... |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7469 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 7470 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7471 | return QualType(); |
| 7472 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7473 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7474 | // The operand cannot be a bit-field |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7475 | AddressOfError = AO_Bit_Field; |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7476 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7477 | // The operand cannot be an element of a vector |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7478 | AddressOfError = AO_Vector_Element; |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7479 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7480 | // cannot take address of a property expression. |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7481 | AddressOfError = AO_Property_Expansion; |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7482 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7483 | // 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] | 7484 | // with the register storage-class specifier. |
| 7485 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 7486 | // in C++ it is not error to take address of a register |
| 7487 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7488 | if (vd->getStorageClass() == SC_Register && |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7489 | !S.getLangOptions().CPlusPlus) { |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7490 | AddressOfError = AO_Register_Variable; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7491 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7492 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7493 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7494 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 7495 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7496 | // Could be a pointer to member, though, if there is an explicit |
| 7497 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 7498 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7499 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7500 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7501 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7502 | S.Diag(OpLoc, |
| 7503 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7504 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7505 | return QualType(); |
| 7506 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7507 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 7508 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 7509 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7510 | return S.Context.getMemberPointerType(op->getType(), |
| 7511 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7512 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7513 | } |
Eli Friedman | 7b2f51c | 2011-08-26 20:28:17 +0000 | [diff] [blame] | 7514 | } else if (!isa<FunctionDecl>(dcl) && !isa<NonTypeTemplateParmDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7515 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7516 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7517 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7518 | if (AddressOfError != AO_No_Error) { |
| 7519 | diagnoseAddressOfInvalidType(S, OpLoc, op, AddressOfError); |
| 7520 | return QualType(); |
| 7521 | } |
| 7522 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7523 | if (lval == Expr::LV_IncompleteVoidType) { |
| 7524 | // Taking the address of a void variable is technically illegal, but we |
| 7525 | // allow it in cases which are otherwise valid. |
| 7526 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7527 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7528 | } |
| 7529 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7530 | // 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] | 7531 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7532 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 7533 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7534 | } |
| 7535 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7536 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7537 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 7538 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7539 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7540 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7541 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7542 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 7543 | if (ConvResult.isInvalid()) |
| 7544 | return QualType(); |
| 7545 | Op = ConvResult.take(); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7546 | QualType OpTy = Op->getType(); |
| 7547 | QualType Result; |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 7548 | |
| 7549 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 7550 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 7551 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 7552 | Op->getSourceRange()); |
| 7553 | } |
| 7554 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7555 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 7556 | // is an incomplete type or void. It would be possible to warn about |
| 7557 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 7558 | // warning is unlikely to catch any mistakes. |
| 7559 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 7560 | Result = PT->getPointeeType(); |
| 7561 | else if (const ObjCObjectPointerType *OPT = |
| 7562 | OpTy->getAs<ObjCObjectPointerType>()) |
| 7563 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7564 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7565 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7566 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7567 | if (PR.take() != Op) |
| 7568 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7569 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7570 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7571 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7572 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7573 | << OpTy << Op->getSourceRange(); |
| 7574 | return QualType(); |
| 7575 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7576 | |
| 7577 | // Dereferences are usually l-values... |
| 7578 | VK = VK_LValue; |
| 7579 | |
| 7580 | // ...except that certain expressions are never l-values in C. |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 7581 | if (!S.getLangOptions().CPlusPlus && Result.isCForbiddenLValueType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7582 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7583 | |
| 7584 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7585 | } |
| 7586 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7587 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7588 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7589 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7590 | switch (Kind) { |
| 7591 | default: assert(0 && "Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7592 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 7593 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 7594 | case tok::star: Opc = BO_Mul; break; |
| 7595 | case tok::slash: Opc = BO_Div; break; |
| 7596 | case tok::percent: Opc = BO_Rem; break; |
| 7597 | case tok::plus: Opc = BO_Add; break; |
| 7598 | case tok::minus: Opc = BO_Sub; break; |
| 7599 | case tok::lessless: Opc = BO_Shl; break; |
| 7600 | case tok::greatergreater: Opc = BO_Shr; break; |
| 7601 | case tok::lessequal: Opc = BO_LE; break; |
| 7602 | case tok::less: Opc = BO_LT; break; |
| 7603 | case tok::greaterequal: Opc = BO_GE; break; |
| 7604 | case tok::greater: Opc = BO_GT; break; |
| 7605 | case tok::exclaimequal: Opc = BO_NE; break; |
| 7606 | case tok::equalequal: Opc = BO_EQ; break; |
| 7607 | case tok::amp: Opc = BO_And; break; |
| 7608 | case tok::caret: Opc = BO_Xor; break; |
| 7609 | case tok::pipe: Opc = BO_Or; break; |
| 7610 | case tok::ampamp: Opc = BO_LAnd; break; |
| 7611 | case tok::pipepipe: Opc = BO_LOr; break; |
| 7612 | case tok::equal: Opc = BO_Assign; break; |
| 7613 | case tok::starequal: Opc = BO_MulAssign; break; |
| 7614 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 7615 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 7616 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 7617 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 7618 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 7619 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 7620 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 7621 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 7622 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 7623 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7624 | } |
| 7625 | return Opc; |
| 7626 | } |
| 7627 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7628 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7629 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7630 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7631 | switch (Kind) { |
| 7632 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7633 | case tok::plusplus: Opc = UO_PreInc; break; |
| 7634 | case tok::minusminus: Opc = UO_PreDec; break; |
| 7635 | case tok::amp: Opc = UO_AddrOf; break; |
| 7636 | case tok::star: Opc = UO_Deref; break; |
| 7637 | case tok::plus: Opc = UO_Plus; break; |
| 7638 | case tok::minus: Opc = UO_Minus; break; |
| 7639 | case tok::tilde: Opc = UO_Not; break; |
| 7640 | case tok::exclaim: Opc = UO_LNot; break; |
| 7641 | case tok::kw___real: Opc = UO_Real; break; |
| 7642 | case tok::kw___imag: Opc = UO_Imag; break; |
| 7643 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7644 | } |
| 7645 | return Opc; |
| 7646 | } |
| 7647 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7648 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 7649 | /// This warning is only emitted for builtin assignment operations. It is also |
| 7650 | /// suppressed in the event of macro expansions. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7651 | static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7652 | SourceLocation OpLoc) { |
| 7653 | if (!S.ActiveTemplateInstantiations.empty()) |
| 7654 | return; |
| 7655 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 7656 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7657 | LHSExpr = LHSExpr->IgnoreParenImpCasts(); |
| 7658 | RHSExpr = RHSExpr->IgnoreParenImpCasts(); |
| 7659 | const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); |
| 7660 | const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); |
| 7661 | if (!LHSDeclRef || !RHSDeclRef || |
| 7662 | LHSDeclRef->getLocation().isMacroID() || |
| 7663 | RHSDeclRef->getLocation().isMacroID()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7664 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7665 | const ValueDecl *LHSDecl = |
| 7666 | cast<ValueDecl>(LHSDeclRef->getDecl()->getCanonicalDecl()); |
| 7667 | const ValueDecl *RHSDecl = |
| 7668 | cast<ValueDecl>(RHSDeclRef->getDecl()->getCanonicalDecl()); |
| 7669 | if (LHSDecl != RHSDecl) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7670 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7671 | if (LHSDecl->getType().isVolatileQualified()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7672 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7673 | if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7674 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 7675 | return; |
| 7676 | |
| 7677 | S.Diag(OpLoc, diag::warn_self_assignment) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7678 | << LHSDeclRef->getType() |
| 7679 | << LHSExpr->getSourceRange() << RHSExpr->getSourceRange(); |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7680 | } |
| 7681 | |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7682 | // checkArithmeticNull - Detect when a NULL constant is used improperly in an |
| 7683 | // expression. These are mainly cases where the null pointer is used as an |
| 7684 | // integer instead of a pointer. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7685 | static void checkArithmeticNull(Sema &S, ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7686 | SourceLocation Loc, bool IsCompare) { |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7687 | // The canonical way to check for a GNU null is with isNullPointerConstant, |
| 7688 | // but we use a bit of a hack here for speed; this is a relatively |
| 7689 | // hot path, and isNullPointerConstant is slow. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7690 | bool LHSNull = isa<GNUNullExpr>(LHS.get()->IgnoreParenImpCasts()); |
| 7691 | bool RHSNull = isa<GNUNullExpr>(RHS.get()->IgnoreParenImpCasts()); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7692 | |
| 7693 | // Detect when a NULL constant is used improperly in an expression. These |
| 7694 | // are mainly cases where the null pointer is used as an integer instead |
| 7695 | // of a pointer. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7696 | if (!LHSNull && !RHSNull) |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7697 | return; |
| 7698 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7699 | QualType LHSType = LHS.get()->getType(); |
| 7700 | QualType RHSType = RHS.get()->getType(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7701 | |
| 7702 | // Avoid analyzing cases where the result will either be invalid (and |
| 7703 | // diagnosed as such) or entirely valid and not something to warn about. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7704 | if (LHSType->isBlockPointerType() || LHSType->isMemberPointerType() || |
| 7705 | LHSType->isFunctionType() || RHSType->isBlockPointerType() || |
| 7706 | RHSType->isMemberPointerType() || RHSType->isFunctionType()) |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7707 | return; |
| 7708 | |
| 7709 | // Comparison operations would not make sense with a null pointer no matter |
| 7710 | // what the other expression is. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7711 | if (!IsCompare) { |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7712 | S.Diag(Loc, diag::warn_null_in_arithmetic_operation) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7713 | << (LHSNull ? LHS.get()->getSourceRange() : SourceRange()) |
| 7714 | << (RHSNull ? RHS.get()->getSourceRange() : SourceRange()); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7715 | return; |
| 7716 | } |
| 7717 | |
| 7718 | // The rest of the operations only make sense with a null pointer |
| 7719 | // if the other expression is a pointer. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7720 | if (LHSNull == RHSNull || LHSType->isAnyPointerType() || |
| 7721 | LHSType->canDecayToPointerType() || RHSType->isAnyPointerType() || |
| 7722 | RHSType->canDecayToPointerType()) |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7723 | return; |
| 7724 | |
| 7725 | S.Diag(Loc, diag::warn_null_in_comparison_operation) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7726 | << LHSNull /* LHS is NULL */ |
| 7727 | << (LHSNull ? RHS.get()->getType() : LHS.get()->getType()) |
| 7728 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7729 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7730 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 7731 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 7732 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7733 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7734 | BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7735 | Expr *LHSExpr, Expr *RHSExpr) { |
| 7736 | ExprResult LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7737 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7738 | // The following two variables are used for compound assignment operators |
| 7739 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 7740 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7741 | ExprValueKind VK = VK_RValue; |
| 7742 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7743 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7744 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 7745 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 7746 | // But since an assignment can trigger target based overload, exclude it in |
| 7747 | // our blind search. i.e: |
| 7748 | // template<class T> void f(); template<class T, class U> void f(U); |
| 7749 | // f<int> == 0; // resolve f<int> blindly |
| 7750 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 7751 | if (Opc != BO_Assign) { |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7752 | ExprResult resolvedLHS = CheckPlaceholderExpr(LHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7753 | if (!resolvedLHS.isUsable()) return ExprError(); |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7754 | LHS = move(resolvedLHS); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7755 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7756 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7757 | if (!resolvedRHS.isUsable()) return ExprError(); |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7758 | RHS = move(resolvedRHS); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7759 | } |
| 7760 | |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7761 | if (Opc == BO_Mul || Opc == BO_Div || Opc == BO_Rem || Opc == BO_Add || |
| 7762 | Opc == BO_Sub || Opc == BO_Shl || Opc == BO_Shr || Opc == BO_And || |
| 7763 | Opc == BO_Xor || Opc == BO_Or || Opc == BO_MulAssign || |
| 7764 | Opc == BO_DivAssign || Opc == BO_AddAssign || Opc == BO_SubAssign || |
| 7765 | Opc == BO_RemAssign || Opc == BO_ShlAssign || Opc == BO_ShrAssign || |
| 7766 | Opc == BO_AndAssign || Opc == BO_OrAssign || Opc == BO_XorAssign) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7767 | checkArithmeticNull(*this, LHS, RHS, OpLoc, /*isCompare=*/false); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7768 | else if (Opc == BO_LE || Opc == BO_LT || Opc == BO_GE || Opc == BO_GT || |
| 7769 | Opc == BO_EQ || Opc == BO_NE) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7770 | checkArithmeticNull(*this, LHS, RHS, OpLoc, /*isCompare=*/true); |
Richard Trieu | 3e95ba9 | 2011-06-16 21:36:56 +0000 | [diff] [blame] | 7771 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7772 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7773 | case BO_Assign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7774 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, QualType()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7775 | if (getLangOptions().CPlusPlus && |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7776 | LHS.get()->getObjectKind() != OK_ObjCProperty) { |
| 7777 | VK = LHS.get()->getValueKind(); |
| 7778 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7779 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7780 | if (!ResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7781 | DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7782 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7783 | case BO_PtrMemD: |
| 7784 | case BO_PtrMemI: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7785 | ResultTy = CheckPointerToMemberOperands(LHS, RHS, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7786 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7787 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7788 | case BO_Mul: |
| 7789 | case BO_Div: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7790 | ResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7791 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7792 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7793 | case BO_Rem: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7794 | ResultTy = CheckRemainderOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7795 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7796 | case BO_Add: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7797 | ResultTy = CheckAdditionOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7798 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7799 | case BO_Sub: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7800 | ResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7801 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7802 | case BO_Shl: |
| 7803 | case BO_Shr: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7804 | ResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7805 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7806 | case BO_LE: |
| 7807 | case BO_LT: |
| 7808 | case BO_GE: |
| 7809 | case BO_GT: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7810 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7811 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7812 | case BO_EQ: |
| 7813 | case BO_NE: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7814 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7815 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7816 | case BO_And: |
| 7817 | case BO_Xor: |
| 7818 | case BO_Or: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7819 | ResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7820 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7821 | case BO_LAnd: |
| 7822 | case BO_LOr: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7823 | ResultTy = CheckLogicalOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7824 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7825 | case BO_MulAssign: |
| 7826 | case BO_DivAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7827 | CompResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7828 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7829 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7830 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7831 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7832 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7833 | case BO_RemAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7834 | CompResultTy = CheckRemainderOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7835 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7836 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7837 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
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_AddAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7840 | CompResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, &CompLHSTy); |
| 7841 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7842 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7843 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7844 | case BO_SubAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7845 | CompResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc, &CompLHSTy); |
| 7846 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7847 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7848 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7849 | case BO_ShlAssign: |
| 7850 | case BO_ShrAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7851 | CompResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7852 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7853 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7854 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7855 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7856 | case BO_AndAssign: |
| 7857 | case BO_XorAssign: |
| 7858 | case BO_OrAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7859 | CompResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7860 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7861 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7862 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7863 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7864 | case BO_Comma: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7865 | ResultTy = CheckCommaOperands(*this, LHS, RHS, OpLoc); |
| 7866 | if (getLangOptions().CPlusPlus && !RHS.isInvalid()) { |
| 7867 | VK = RHS.get()->getValueKind(); |
| 7868 | OK = RHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7869 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7870 | break; |
| 7871 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7872 | if (ResultTy.isNull() || LHS.isInvalid() || RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7873 | return ExprError(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7874 | |
| 7875 | // Check for array bounds violations for both sides of the BinaryOperator |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7876 | CheckArrayAccess(LHS.get()); |
| 7877 | CheckArrayAccess(RHS.get()); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7878 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7879 | if (CompResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7880 | return Owned(new (Context) BinaryOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7881 | ResultTy, VK, OK, OpLoc)); |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7882 | if (getLangOptions().CPlusPlus && LHS.get()->getObjectKind() != |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7883 | OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7884 | VK = VK_LValue; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7885 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7886 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7887 | return Owned(new (Context) CompoundAssignOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7888 | ResultTy, VK, OK, CompLHSTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7889 | CompResultTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7890 | } |
| 7891 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7892 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 7893 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 7894 | /// comparison operators have higher precedence. The most typical example of |
| 7895 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7896 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7897 | SourceLocation OpLoc, Expr *LHSExpr, |
| 7898 | Expr *RHSExpr) { |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7899 | typedef BinaryOperator BinOp; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7900 | BinOp::Opcode LHSopc = static_cast<BinOp::Opcode>(-1), |
| 7901 | RHSopc = static_cast<BinOp::Opcode>(-1); |
| 7902 | if (BinOp *BO = dyn_cast<BinOp>(LHSExpr)) |
| 7903 | LHSopc = BO->getOpcode(); |
| 7904 | if (BinOp *BO = dyn_cast<BinOp>(RHSExpr)) |
| 7905 | RHSopc = BO->getOpcode(); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7906 | |
| 7907 | // Subs are not binary operators. |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7908 | if (LHSopc == -1 && RHSopc == -1) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7909 | return; |
| 7910 | |
| 7911 | // Bitwise operations are sometimes used as eager logical ops. |
| 7912 | // Don't diagnose this. |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7913 | if ((BinOp::isComparisonOp(LHSopc) || BinOp::isBitwiseOp(LHSopc)) && |
| 7914 | (BinOp::isComparisonOp(RHSopc) || BinOp::isBitwiseOp(RHSopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7915 | return; |
| 7916 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7917 | bool isLeftComp = BinOp::isComparisonOp(LHSopc); |
| 7918 | bool isRightComp = BinOp::isComparisonOp(RHSopc); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7919 | if (!isLeftComp && !isRightComp) return; |
| 7920 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7921 | SourceRange DiagRange = isLeftComp ? SourceRange(LHSExpr->getLocStart(), |
| 7922 | OpLoc) |
| 7923 | : SourceRange(OpLoc, RHSExpr->getLocEnd()); |
| 7924 | std::string OpStr = isLeftComp ? BinOp::getOpcodeStr(LHSopc) |
| 7925 | : BinOp::getOpcodeStr(RHSopc); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7926 | SourceRange ParensRange = isLeftComp ? |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7927 | SourceRange(cast<BinOp>(LHSExpr)->getRHS()->getLocStart(), |
| 7928 | RHSExpr->getLocEnd()) |
| 7929 | : SourceRange(LHSExpr->getLocStart(), |
| 7930 | cast<BinOp>(RHSExpr)->getLHS()->getLocStart()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7931 | |
| 7932 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 7933 | << DiagRange << BinOp::getOpcodeStr(Opc) << OpStr; |
| 7934 | SuggestParentheses(Self, OpLoc, |
| 7935 | Self.PDiag(diag::note_precedence_bitwise_silence) << OpStr, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7936 | RHSExpr->getSourceRange()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7937 | SuggestParentheses(Self, OpLoc, |
| 7938 | Self.PDiag(diag::note_precedence_bitwise_first) << BinOp::getOpcodeStr(Opc), |
| 7939 | ParensRange); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7940 | } |
| 7941 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7942 | /// \brief It accepts a '&' expr that is inside a '|' one. |
| 7943 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression |
| 7944 | /// in parentheses. |
| 7945 | static void |
| 7946 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, |
| 7947 | BinaryOperator *Bop) { |
| 7948 | assert(Bop->getOpcode() == BO_And); |
| 7949 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) |
| 7950 | << Bop->getSourceRange() << OpLoc; |
| 7951 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
| 7952 | Self.PDiag(diag::note_bitwise_and_in_bitwise_or_silence), |
| 7953 | Bop->getSourceRange()); |
| 7954 | } |
| 7955 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7956 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 7957 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 7958 | /// in parentheses. |
| 7959 | static void |
| 7960 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7961 | BinaryOperator *Bop) { |
| 7962 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7963 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 7964 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7965 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7966 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7967 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7968 | } |
| 7969 | |
| 7970 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7971 | /// 'true'. |
| 7972 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 7973 | bool Res; |
| 7974 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 7975 | } |
| 7976 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7977 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7978 | /// 'false'. |
| 7979 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 7980 | bool Res; |
| 7981 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 7982 | } |
| 7983 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7984 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 7985 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7986 | Expr *LHSExpr, Expr *RHSExpr) { |
| 7987 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(LHSExpr)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7988 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7989 | // 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] | 7990 | if (EvaluatesAsFalse(S, RHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7991 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7992 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 7993 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 7994 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 7995 | } else if (Bop->getOpcode() == BO_LOr) { |
| 7996 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 7997 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 7998 | // "a || b && 1", but warn now. |
| 7999 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 8000 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 8001 | } |
| 8002 | } |
| 8003 | } |
| 8004 | } |
| 8005 | |
| 8006 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 8007 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8008 | Expr *LHSExpr, Expr *RHSExpr) { |
| 8009 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(RHSExpr)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8010 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8011 | // 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] | 8012 | if (EvaluatesAsFalse(S, LHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8013 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8014 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 8015 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 8016 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8017 | } |
| 8018 | } |
| 8019 | } |
| 8020 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8021 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
| 8022 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, |
| 8023 | Expr *OrArg) { |
| 8024 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { |
| 8025 | if (Bop->getOpcode() == BO_And) |
| 8026 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); |
| 8027 | } |
| 8028 | } |
| 8029 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8030 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8031 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8032 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8033 | SourceLocation OpLoc, Expr *LHSExpr, |
| 8034 | Expr *RHSExpr){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8035 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8036 | if (BinaryOperator::isBitwiseOp(Opc)) |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8037 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8038 | |
| 8039 | // Diagnose "arg1 & arg2 | arg3" |
| 8040 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8041 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, LHSExpr); |
| 8042 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8043 | } |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8044 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8045 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 8046 | // 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] | 8047 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8048 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, LHSExpr, RHSExpr); |
| 8049 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8050 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8051 | } |
| 8052 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8053 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8054 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8055 | tok::TokenKind Kind, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8056 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8057 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8058 | assert((LHSExpr != 0) && "ActOnBinOp(): missing left expression"); |
| 8059 | assert((RHSExpr != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8060 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8061 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8062 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, LHSExpr, RHSExpr); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8063 | |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8064 | return BuildBinOp(S, TokLoc, Opc, LHSExpr, RHSExpr); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8065 | } |
| 8066 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8067 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8068 | BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8069 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8070 | if (getLangOptions().CPlusPlus) { |
| 8071 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8072 | |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8073 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8074 | UseBuiltinOperator = false; |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8075 | } else if (Opc == BO_Assign && |
| 8076 | LHSExpr->getObjectKind() == OK_ObjCProperty) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8077 | UseBuiltinOperator = true; |
| 8078 | } else { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8079 | UseBuiltinOperator = !LHSExpr->getType()->isOverloadableType() && |
| 8080 | !RHSExpr->getType()->isOverloadableType(); |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8081 | } |
| 8082 | |
| 8083 | if (!UseBuiltinOperator) { |
| 8084 | // Find all of the overloaded operators visible from this |
| 8085 | // point. We perform both an operator-name lookup from the local |
| 8086 | // scope and an argument-dependent lookup based on the types of |
| 8087 | // the arguments. |
| 8088 | UnresolvedSet<16> Functions; |
| 8089 | OverloadedOperatorKind OverOp |
| 8090 | = BinaryOperator::getOverloadedOperator(Opc); |
| 8091 | if (S && OverOp != OO_None) |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8092 | LookupOverloadedOperatorName(OverOp, S, LHSExpr->getType(), |
| 8093 | RHSExpr->getType(), Functions); |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8094 | |
| 8095 | // Build the (potentially-overloaded, potentially-dependent) |
| 8096 | // binary operation. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8097 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, LHSExpr, RHSExpr); |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8098 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8099 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8100 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8101 | // Build a built-in binary operation. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8102 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8103 | } |
| 8104 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8105 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8106 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8107 | Expr *InputExpr) { |
| 8108 | ExprResult Input = Owned(InputExpr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8109 | ExprValueKind VK = VK_RValue; |
| 8110 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8111 | QualType resultType; |
| 8112 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8113 | case UO_PreInc: |
| 8114 | case UO_PreDec: |
| 8115 | case UO_PostInc: |
| 8116 | case UO_PostDec: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8117 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8118 | Opc == UO_PreInc || |
| 8119 | Opc == UO_PostInc, |
| 8120 | Opc == UO_PreInc || |
| 8121 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8122 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8123 | case UO_AddrOf: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8124 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8125 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8126 | case UO_Deref: { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8127 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8128 | if (!resolved.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8129 | Input = move(resolved); |
| 8130 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8131 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8132 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8133 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8134 | case UO_Plus: |
| 8135 | case UO_Minus: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8136 | Input = UsualUnaryConversions(Input.take()); |
| 8137 | if (Input.isInvalid()) return ExprError(); |
| 8138 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8139 | if (resultType->isDependentType()) |
| 8140 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8141 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8142 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8143 | break; |
| 8144 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 8145 | resultType->isEnumeralType()) |
| 8146 | break; |
| 8147 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8148 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8149 | resultType->isPointerType()) |
| 8150 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8151 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8152 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8153 | if (Input.isInvalid()) return ExprError(); |
| 8154 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8155 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8156 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8157 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8158 | << resultType << Input.get()->getSourceRange()); |
| 8159 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8160 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8161 | Input = UsualUnaryConversions(Input.take()); |
| 8162 | if (Input.isInvalid()) return ExprError(); |
| 8163 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8164 | if (resultType->isDependentType()) |
| 8165 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8166 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8167 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8168 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8169 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8170 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8171 | else if (resultType->hasIntegerRepresentation()) |
| 8172 | break; |
| 8173 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8174 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8175 | if (Input.isInvalid()) return ExprError(); |
| 8176 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8177 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8178 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8179 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8180 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8181 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8182 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8183 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8184 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8185 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8186 | if (Input.isInvalid()) return ExprError(); |
| 8187 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8188 | if (resultType->isDependentType()) |
| 8189 | break; |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8190 | if (resultType->isScalarType()) { |
| 8191 | // C99 6.5.3.3p1: ok, fallthrough; |
| 8192 | if (Context.getLangOptions().CPlusPlus) { |
| 8193 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 8194 | // operand contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8195 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 8196 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8197 | } |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8198 | } else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8199 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8200 | if (Input.isInvalid()) return ExprError(); |
| 8201 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8202 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8203 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8204 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8205 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8206 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8207 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8208 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8209 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8210 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8211 | case UO_Real: |
| 8212 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8213 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8214 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8215 | if (Input.isInvalid()) return ExprError(); |
| 8216 | if (Input.get()->getValueKind() != VK_RValue && |
| 8217 | Input.get()->getObjectKind() == OK_Ordinary) |
| 8218 | VK = Input.get()->getValueKind(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8219 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8220 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8221 | resultType = Input.get()->getType(); |
| 8222 | VK = Input.get()->getValueKind(); |
| 8223 | OK = Input.get()->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8224 | break; |
| 8225 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8226 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8227 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8228 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8229 | // Check for array bounds violations in the operand of the UnaryOperator, |
| 8230 | // except for the '*' and '&' operators that have to be handled specially |
| 8231 | // by CheckArrayAccess (as there are special cases like &array[arraysize] |
| 8232 | // that are explicitly defined as valid by the standard). |
| 8233 | if (Opc != UO_AddrOf && Opc != UO_Deref) |
| 8234 | CheckArrayAccess(Input.get()); |
| 8235 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8236 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8237 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8238 | } |
| 8239 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8240 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8241 | UnaryOperatorKind Opc, Expr *Input) { |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 8242 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 957c094 | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 8243 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8244 | // Find all of the overloaded operators visible from this |
| 8245 | // point. We perform both an operator-name lookup from the local |
| 8246 | // scope and an argument-dependent lookup based on the types of |
| 8247 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8248 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8249 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8250 | if (S && OverOp != OO_None) |
| 8251 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 8252 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8253 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8254 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8255 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8256 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8257 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8258 | } |
| 8259 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8260 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8261 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8262 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8263 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8264 | } |
| 8265 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8266 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8267 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8268 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8269 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8270 | // 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] | 8271 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8272 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8273 | } |
| 8274 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8275 | /// Given the last statement in a statement-expression, check whether |
| 8276 | /// the result is a producing expression (like a call to an |
| 8277 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 8278 | /// release out of the full-expression. Otherwise, return null. |
| 8279 | /// Cannot fail. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8280 | static Expr *maybeRebuildARCConsumingStmt(Stmt *Statement) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8281 | // Should always be wrapped with one of these. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8282 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(Statement); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8283 | if (!cleanups) return 0; |
| 8284 | |
| 8285 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
| 8286 | if (!cast || cast->getCastKind() != CK_ObjCConsumeObject) |
| 8287 | return 0; |
| 8288 | |
| 8289 | // Splice out the cast. This shouldn't modify any interesting |
| 8290 | // features of the statement. |
| 8291 | Expr *producer = cast->getSubExpr(); |
| 8292 | assert(producer->getType() == cast->getType()); |
| 8293 | assert(producer->getValueKind() == cast->getValueKind()); |
| 8294 | cleanups->setSubExpr(producer); |
| 8295 | return cleanups; |
| 8296 | } |
| 8297 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8298 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8299 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8300 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8301 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8302 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8303 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8304 | bool isFileScope |
| 8305 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8306 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8307 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8308 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8309 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8310 | // example, it is not possible to goto into a stmt expression apparently. |
| 8311 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8312 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8313 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8314 | // as the type of the stmtexpr. |
| 8315 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8316 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8317 | if (!Compound->body_empty()) { |
| 8318 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8319 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8320 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8321 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8322 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8323 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8324 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8325 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8326 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8327 | // Do function/array conversion on the last expression, but not |
| 8328 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8329 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 8330 | if (LastExpr.isInvalid()) |
| 8331 | return ExprError(); |
| 8332 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8333 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8334 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8335 | // In ARC, if the final expression ends in a consume, splice |
| 8336 | // the consume out and bind it later. In the alternate case |
| 8337 | // (when dealing with a retainable type), the result |
| 8338 | // initialization will create a produce. In both cases the |
| 8339 | // result will be +1, and we'll need to balance that out with |
| 8340 | // a bind. |
| 8341 | if (Expr *rebuiltLastStmt |
| 8342 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 8343 | LastExpr = rebuiltLastStmt; |
| 8344 | } else { |
| 8345 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8346 | InitializedEntity::InitializeResult(LPLoc, |
| 8347 | Ty, |
| 8348 | false), |
| 8349 | SourceLocation(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8350 | LastExpr); |
| 8351 | } |
| 8352 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8353 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8354 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8355 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8356 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8357 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8358 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8359 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8360 | StmtExprMayBindToTemp = true; |
| 8361 | } |
| 8362 | } |
| 8363 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8364 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8365 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8366 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8367 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8368 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8369 | if (StmtExprMayBindToTemp) |
| 8370 | return MaybeBindToTemporary(ResStmtExpr); |
| 8371 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8372 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8373 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8374 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8375 | TypeSourceInfo *TInfo, |
| 8376 | OffsetOfComponent *CompPtr, |
| 8377 | unsigned NumComponents, |
| 8378 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8379 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8380 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8381 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8382 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8383 | // We must have at least one component that refers to the type, and the first |
| 8384 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8385 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8386 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8387 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 8388 | << ArgTy << TypeRange); |
| 8389 | |
| 8390 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 8391 | // with an incomplete type would be ill-formed. |
| 8392 | if (!Dependent |
| 8393 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 8394 | PDiag(diag::err_offsetof_incomplete_type) |
| 8395 | << TypeRange)) |
| 8396 | return ExprError(); |
| 8397 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8398 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 8399 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 8400 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 8401 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8402 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8403 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 8404 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8405 | |
| 8406 | bool DidWarnAboutNonPOD = false; |
| 8407 | QualType CurrentType = ArgTy; |
| 8408 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8409 | SmallVector<OffsetOfNode, 4> Comps; |
| 8410 | SmallVector<Expr*, 4> Exprs; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8411 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 8412 | const OffsetOfComponent &OC = CompPtr[i]; |
| 8413 | if (OC.isBrackets) { |
| 8414 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 8415 | if (!CurrentType->isDependentType()) { |
| 8416 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 8417 | if(!AT) |
| 8418 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 8419 | << CurrentType); |
| 8420 | CurrentType = AT->getElementType(); |
| 8421 | } else |
| 8422 | CurrentType = Context.DependentTy; |
| 8423 | |
| 8424 | // The expression must be an integral expression. |
| 8425 | // FIXME: An integral constant expression? |
| 8426 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 8427 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 8428 | !Idx->getType()->isIntegerType()) |
| 8429 | return ExprError(Diag(Idx->getLocStart(), |
| 8430 | diag::err_typecheck_subscript_not_integer) |
| 8431 | << Idx->getSourceRange()); |
| 8432 | |
| 8433 | // Record this array index. |
| 8434 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 8435 | Exprs.push_back(Idx); |
| 8436 | continue; |
| 8437 | } |
| 8438 | |
| 8439 | // Offset of a field. |
| 8440 | if (CurrentType->isDependentType()) { |
| 8441 | // We have the offset of a field, but we can't look into the dependent |
| 8442 | // type. Just record the identifier of the field. |
| 8443 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 8444 | CurrentType = Context.DependentTy; |
| 8445 | continue; |
| 8446 | } |
| 8447 | |
| 8448 | // We need to have a complete type to look into. |
| 8449 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 8450 | diag::err_offsetof_incomplete_type)) |
| 8451 | return ExprError(); |
| 8452 | |
| 8453 | // Look for the designated field. |
| 8454 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 8455 | if (!RC) |
| 8456 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 8457 | << CurrentType); |
| 8458 | RecordDecl *RD = RC->getDecl(); |
| 8459 | |
| 8460 | // C++ [lib.support.types]p5: |
| 8461 | // The macro offsetof accepts a restricted set of type arguments in this |
| 8462 | // International Standard. type shall be a POD structure or a POD union |
| 8463 | // (clause 9). |
| 8464 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 8465 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 8466 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8467 | PDiag(diag::warn_offsetof_non_pod_type) |
| 8468 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 8469 | << CurrentType)) |
| 8470 | DidWarnAboutNonPOD = true; |
| 8471 | } |
| 8472 | |
| 8473 | // Look for the field. |
| 8474 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 8475 | LookupQualifiedName(R, RD); |
| 8476 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8477 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 8478 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 8479 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8480 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 8481 | } |
| 8482 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8483 | if (!MemberDecl) |
| 8484 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 8485 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 8486 | OC.LocEnd)); |
| 8487 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 8488 | // C99 7.17p3: |
| 8489 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 8490 | // |
| 8491 | // We diagnose this as an error. |
| 8492 | if (MemberDecl->getBitWidth()) { |
| 8493 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 8494 | << MemberDecl->getDeclName() |
| 8495 | << SourceRange(BuiltinLoc, RParenLoc); |
| 8496 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 8497 | return ExprError(); |
| 8498 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8499 | |
| 8500 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8501 | if (IndirectMemberDecl) |
| 8502 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8503 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8504 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 8505 | // the base class indirections. |
| 8506 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8507 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8508 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8509 | CXXBasePath &Path = Paths.front(); |
| 8510 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 8511 | B != BEnd; ++B) |
| 8512 | Comps.push_back(OffsetOfNode(B->Base)); |
| 8513 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8514 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8515 | if (IndirectMemberDecl) { |
| 8516 | for (IndirectFieldDecl::chain_iterator FI = |
| 8517 | IndirectMemberDecl->chain_begin(), |
| 8518 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 8519 | assert(isa<FieldDecl>(*FI)); |
| 8520 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 8521 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 8522 | } |
| 8523 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8524 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8525 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8526 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 8527 | } |
| 8528 | |
| 8529 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 8530 | TInfo, Comps.data(), Comps.size(), |
| 8531 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 8532 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8533 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8534 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8535 | SourceLocation BuiltinLoc, |
| 8536 | SourceLocation TypeLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8537 | ParsedType ParsedArgTy, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8538 | OffsetOfComponent *CompPtr, |
| 8539 | unsigned NumComponents, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8540 | SourceLocation RParenLoc) { |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8541 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8542 | TypeSourceInfo *ArgTInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8543 | QualType ArgTy = GetTypeFromParser(ParsedArgTy, &ArgTInfo); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8544 | if (ArgTy.isNull()) |
| 8545 | return ExprError(); |
| 8546 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 8547 | if (!ArgTInfo) |
| 8548 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 8549 | |
| 8550 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8551 | RParenLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8552 | } |
| 8553 | |
| 8554 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8555 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8556 | Expr *CondExpr, |
| 8557 | Expr *LHSExpr, Expr *RHSExpr, |
| 8558 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8559 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 8560 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8561 | ExprValueKind VK = VK_RValue; |
| 8562 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8563 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8564 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 8565 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8566 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8567 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8568 | } else { |
| 8569 | // The conditional expression is required to be a constant expression. |
| 8570 | llvm::APSInt condEval(32); |
| 8571 | SourceLocation ExpLoc; |
| 8572 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8573 | return ExprError(Diag(ExpLoc, |
| 8574 | diag::err_typecheck_choose_expr_requires_constant) |
| 8575 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8576 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8577 | // 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] | 8578 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 8579 | |
| 8580 | resType = ActiveExpr->getType(); |
| 8581 | ValueDependent = ActiveExpr->isValueDependent(); |
| 8582 | VK = ActiveExpr->getValueKind(); |
| 8583 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8584 | } |
| 8585 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8586 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8587 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8588 | resType->isDependentType(), |
| 8589 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8590 | } |
| 8591 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8592 | //===----------------------------------------------------------------------===// |
| 8593 | // Clang Extensions. |
| 8594 | //===----------------------------------------------------------------------===// |
| 8595 | |
| 8596 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8597 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *CurScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8598 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8599 | PushBlockScope(CurScope, Block); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8600 | CurContext->addDecl(Block); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8601 | if (CurScope) |
| 8602 | PushDeclContext(CurScope, Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8603 | else |
| 8604 | CurContext = Block; |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8605 | } |
| 8606 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8607 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 8608 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8609 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8610 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8611 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8612 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8613 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8614 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8615 | // GetTypeForDeclarator always produces a function type for a block |
| 8616 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 8617 | // unless the function was written with a typedef. |
| 8618 | assert(T->isFunctionType() && |
| 8619 | "GetTypeForDeclarator made a non-function block signature"); |
| 8620 | |
| 8621 | // Look for an explicit signature in that function type. |
| 8622 | FunctionProtoTypeLoc ExplicitSignature; |
| 8623 | |
| 8624 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 8625 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 8626 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 8627 | |
| 8628 | // Check whether that explicit signature was synthesized by |
| 8629 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 8630 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8631 | if (ExplicitSignature.getLocalRangeBegin() == |
| 8632 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8633 | // This would be much cheaper if we stored TypeLocs instead of |
| 8634 | // TypeSourceInfos. |
| 8635 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 8636 | unsigned Size = Result.getFullDataSize(); |
| 8637 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 8638 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 8639 | |
| 8640 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 8641 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8642 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8643 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8644 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 8645 | CurBlock->FunctionType = T; |
| 8646 | |
| 8647 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 8648 | QualType RetTy = Fn->getResultType(); |
| 8649 | bool isVariadic = |
| 8650 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 8651 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8652 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8653 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8654 | // Don't allow returning a objc interface by value. |
| 8655 | if (RetTy->isObjCObjectType()) { |
| 8656 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 8657 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 8658 | return; |
| 8659 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8660 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8661 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8662 | // return type. TODO: what should we do with declarators like: |
| 8663 | // ^ * { ... } |
| 8664 | // If the answer is "apply template argument deduction".... |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8665 | if (RetTy != Context.DependentTy) |
| 8666 | CurBlock->ReturnType = RetTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8667 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8668 | // Push block parameters from the declarator if we had them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8669 | SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8670 | if (ExplicitSignature) { |
| 8671 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 8672 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8673 | if (Param->getIdentifier() == 0 && |
| 8674 | !Param->isImplicit() && |
| 8675 | !Param->isInvalidDecl() && |
| 8676 | !getLangOptions().CPlusPlus) |
| 8677 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8678 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8679 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8680 | |
| 8681 | // Fake up parameter variables if we have a typedef, like |
| 8682 | // ^ fntype { ... } |
| 8683 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 8684 | for (FunctionProtoType::arg_type_iterator |
| 8685 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 8686 | ParmVarDecl *Param = |
| 8687 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 8688 | ParamInfo.getSourceRange().getBegin(), |
| 8689 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8690 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8691 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8692 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8693 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8694 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8695 | if (!Params.empty()) { |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8696 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8697 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 8698 | CurBlock->TheDecl->param_end(), |
| 8699 | /*CheckParameterNames=*/false); |
| 8700 | } |
| 8701 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8702 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 8703 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8704 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8705 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8706 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 8707 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 8708 | // FIXME: remove the attribute. |
| 8709 | } |
| 8710 | |
| 8711 | // Put the parameter variables in scope. We can bail out immediately |
| 8712 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8713 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8714 | return; |
| 8715 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8716 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8717 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 8718 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 8719 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8720 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8721 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8722 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8723 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8724 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8725 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8726 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8727 | } |
| 8728 | |
| 8729 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 8730 | /// is invoked to pop the information about the block from the action impl. |
| 8731 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8732 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 8733 | PopDeclContext(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8734 | PopFunctionOrBlockScope(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8735 | } |
| 8736 | |
| 8737 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 8738 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8739 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8740 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 8741 | // If blocks are disabled, emit an error. |
| 8742 | if (!LangOpts.Blocks) |
| 8743 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8744 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8745 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8746 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8747 | PopDeclContext(); |
| 8748 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8749 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 8750 | if (!BSI->ReturnType.isNull()) |
| 8751 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8752 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 8753 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8754 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8755 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8756 | // Set the captured variables on the block. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 8757 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 8758 | BSI->CapturesCXXThis); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8759 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8760 | // If the user wrote a function type in some form, try to use that. |
| 8761 | if (!BSI->FunctionType.isNull()) { |
| 8762 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 8763 | |
| 8764 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 8765 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 8766 | |
| 8767 | // Turn protoless block types into nullary block types. |
| 8768 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8769 | FunctionProtoType::ExtProtoInfo EPI; |
| 8770 | EPI.ExtInfo = Ext; |
| 8771 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8772 | |
| 8773 | // Otherwise, if we don't need to change anything about the function type, |
| 8774 | // preserve its sugar structure. |
| 8775 | } else if (FTy->getResultType() == RetTy && |
| 8776 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 8777 | BlockTy = BSI->FunctionType; |
| 8778 | |
| 8779 | // Otherwise, make the minimal modifications to the function type. |
| 8780 | } else { |
| 8781 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8782 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8783 | EPI.TypeQuals = 0; // FIXME: silently? |
| 8784 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8785 | BlockTy = Context.getFunctionType(RetTy, |
| 8786 | FPT->arg_type_begin(), |
| 8787 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8788 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8789 | } |
| 8790 | |
| 8791 | // If we don't have a function type, just build one from nothing. |
| 8792 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8793 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8794 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8795 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8796 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8797 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8798 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 8799 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8800 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8801 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 8802 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8803 | if (getCurFunction()->NeedsScopeChecking() && |
| 8804 | !hasAnyUnrecoverableErrorsInThisFunction()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8805 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8806 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8807 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8808 | |
Fariborz Jahanian | 4e7c7f2 | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 8809 | for (BlockDecl::capture_const_iterator ci = BSI->TheDecl->capture_begin(), |
| 8810 | ce = BSI->TheDecl->capture_end(); ci != ce; ++ci) { |
| 8811 | const VarDecl *variable = ci->getVariable(); |
| 8812 | QualType T = variable->getType(); |
| 8813 | QualType::DestructionKind destructKind = T.isDestructedType(); |
| 8814 | if (destructKind != QualType::DK_none) |
| 8815 | getCurFunction()->setHasBranchProtectedScope(); |
| 8816 | } |
| 8817 | |
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 8818 | computeNRVO(Body, getCurBlock()); |
| 8819 | |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 8820 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
| 8821 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 8822 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
| 8823 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8824 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8825 | } |
| 8826 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8827 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8828 | Expr *E, ParsedType Ty, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8829 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8830 | TypeSourceInfo *TInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8831 | GetTypeFromParser(Ty, &TInfo); |
| 8832 | return BuildVAArgExpr(BuiltinLoc, E, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8833 | } |
| 8834 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8835 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8836 | Expr *E, TypeSourceInfo *TInfo, |
| 8837 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8838 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8839 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8840 | // Get the va_list type |
| 8841 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8842 | if (VaListType->isArrayType()) { |
| 8843 | // Deal with implicit array decay; for example, on x86-64, |
| 8844 | // va_list is an array, but it's supposed to decay to |
| 8845 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8846 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8847 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8848 | ExprResult Result = UsualUnaryConversions(E); |
| 8849 | if (Result.isInvalid()) |
| 8850 | return ExprError(); |
| 8851 | E = Result.take(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8852 | } else { |
| 8853 | // Otherwise, the va_list argument must be an l-value because |
| 8854 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8855 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8856 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8857 | return ExprError(); |
| 8858 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8859 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8860 | if (!E->isTypeDependent() && |
| 8861 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8862 | return ExprError(Diag(E->getLocStart(), |
| 8863 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8864 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 8865 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8866 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8867 | if (!TInfo->getType()->isDependentType()) { |
| 8868 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
| 8869 | PDiag(diag::err_second_parameter_to_va_arg_incomplete) |
| 8870 | << TInfo->getTypeLoc().getSourceRange())) |
| 8871 | return ExprError(); |
David Majnemer | db11b01 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 8872 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8873 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
| 8874 | TInfo->getType(), |
| 8875 | PDiag(diag::err_second_parameter_to_va_arg_abstract) |
| 8876 | << TInfo->getTypeLoc().getSourceRange())) |
| 8877 | return ExprError(); |
| 8878 | |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8879 | if (!TInfo->getType().isPODType(Context)) { |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8880 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8881 | TInfo->getType()->isObjCLifetimeType() |
| 8882 | ? diag::warn_second_parameter_to_va_arg_ownership_qualified |
| 8883 | : diag::warn_second_parameter_to_va_arg_not_pod) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8884 | << TInfo->getType() |
| 8885 | << TInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8886 | } |
Eli Friedman | 46d37c1 | 2011-07-11 21:45:59 +0000 | [diff] [blame] | 8887 | |
| 8888 | // Check for va_arg where arguments of the given type will be promoted |
| 8889 | // (i.e. this va_arg is guaranteed to have undefined behavior). |
| 8890 | QualType PromoteType; |
| 8891 | if (TInfo->getType()->isPromotableIntegerType()) { |
| 8892 | PromoteType = Context.getPromotedIntegerType(TInfo->getType()); |
| 8893 | if (Context.typesAreCompatible(PromoteType, TInfo->getType())) |
| 8894 | PromoteType = QualType(); |
| 8895 | } |
| 8896 | if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) |
| 8897 | PromoteType = Context.DoubleTy; |
| 8898 | if (!PromoteType.isNull()) |
| 8899 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 8900 | diag::warn_second_parameter_to_va_arg_never_compatible) |
| 8901 | << TInfo->getType() |
| 8902 | << PromoteType |
| 8903 | << TInfo->getTypeLoc().getSourceRange(); |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8904 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8905 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8906 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 8907 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 8908 | } |
| 8909 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8910 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8911 | // The type of __null will be int or long, depending on the size of |
| 8912 | // pointers on the target. |
| 8913 | QualType Ty; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8914 | unsigned pw = Context.getTargetInfo().getPointerWidth(0); |
| 8915 | if (pw == Context.getTargetInfo().getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8916 | Ty = Context.IntTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8917 | else if (pw == Context.getTargetInfo().getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8918 | Ty = Context.LongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8919 | else if (pw == Context.getTargetInfo().getLongLongWidth()) |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8920 | Ty = Context.LongLongTy; |
| 8921 | else { |
| 8922 | assert(!"I don't know size of pointer!"); |
| 8923 | Ty = Context.IntTy; |
| 8924 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8925 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8926 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8927 | } |
| 8928 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8929 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8930 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8931 | if (!SemaRef.getLangOptions().ObjC1) |
| 8932 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8933 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8934 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 8935 | if (!PT) |
| 8936 | return; |
| 8937 | |
| 8938 | // Check if the destination is of type 'id'. |
| 8939 | if (!PT->isObjCIdType()) { |
| 8940 | // Check if the destination is the 'NSString' interface. |
| 8941 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 8942 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 8943 | return; |
| 8944 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8945 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8946 | // Strip off any parens and casts. |
| 8947 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 8948 | if (!SL || !SL->isAscii()) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8949 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8950 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8951 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8952 | } |
| 8953 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8954 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 8955 | SourceLocation Loc, |
| 8956 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8957 | Expr *SrcExpr, AssignmentAction Action, |
| 8958 | bool *Complained) { |
| 8959 | if (Complained) |
| 8960 | *Complained = false; |
| 8961 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8962 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8963 | bool CheckInferredResultType = false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8964 | bool isInvalid = false; |
| 8965 | unsigned DiagKind; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8966 | FixItHint Hint; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8967 | ConversionFixItGenerator ConvHints; |
| 8968 | bool MayHaveConvFixit = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8969 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8970 | switch (ConvTy) { |
| 8971 | default: assert(0 && "Unknown conversion type"); |
| 8972 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8973 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8974 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8975 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8976 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8977 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8978 | case IntToPointer: |
| 8979 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8980 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8981 | MayHaveConvFixit = true; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8982 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8983 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8984 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8985 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8986 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 8987 | SrcType->isObjCObjectPointerType(); |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8988 | if (Hint.isNull() && !CheckInferredResultType) { |
| 8989 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8990 | } |
| 8991 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8992 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 8993 | case IncompatiblePointerSign: |
| 8994 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 8995 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8996 | case FunctionVoidPointer: |
| 8997 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 8998 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8999 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9000 | // Perform array-to-pointer decay if necessary. |
| 9001 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9002 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9003 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9004 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9005 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9006 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9007 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9008 | |
| 9009 | |
| 9010 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 9011 | DiagKind = diag::err_typecheck_incompatible_ownership; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9012 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9013 | } |
| 9014 | |
| 9015 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9016 | // fallthrough |
| 9017 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9018 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9019 | // If the qualifiers lost were because we were applying the |
| 9020 | // (deprecated) C++ conversion from a string literal to a char* |
| 9021 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9022 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9023 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9024 | // bit of refactoring (so that the second argument is an |
| 9025 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9026 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9027 | // C++ semantics. |
| 9028 | if (getLangOptions().CPlusPlus && |
| 9029 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9030 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9031 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9032 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9033 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9034 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9035 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9036 | case IntToBlockPointer: |
| 9037 | DiagKind = diag::err_int_to_block_pointer; |
| 9038 | break; |
| 9039 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9040 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9041 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9042 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9043 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9044 | // it can give a more specific diagnostic. |
| 9045 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9046 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9047 | case IncompatibleVectors: |
| 9048 | DiagKind = diag::warn_incompatible_vectors; |
| 9049 | break; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 9050 | case IncompatibleObjCWeakRef: |
| 9051 | DiagKind = diag::err_arc_weak_unavailable_assign; |
| 9052 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9053 | case Incompatible: |
| 9054 | DiagKind = diag::err_typecheck_convert_incompatible; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9055 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9056 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9057 | isInvalid = true; |
| 9058 | break; |
| 9059 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9060 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9061 | QualType FirstType, SecondType; |
| 9062 | switch (Action) { |
| 9063 | case AA_Assigning: |
| 9064 | case AA_Initializing: |
| 9065 | // The destination type comes first. |
| 9066 | FirstType = DstType; |
| 9067 | SecondType = SrcType; |
| 9068 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9069 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9070 | case AA_Returning: |
| 9071 | case AA_Passing: |
| 9072 | case AA_Converting: |
| 9073 | case AA_Sending: |
| 9074 | case AA_Casting: |
| 9075 | // The source type comes first. |
| 9076 | FirstType = SrcType; |
| 9077 | SecondType = DstType; |
| 9078 | break; |
| 9079 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9080 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9081 | PartialDiagnostic FDiag = PDiag(DiagKind); |
| 9082 | FDiag << FirstType << SecondType << Action << SrcExpr->getSourceRange(); |
| 9083 | |
| 9084 | // If we can fix the conversion, suggest the FixIts. |
| 9085 | assert(ConvHints.isNull() || Hint.isNull()); |
| 9086 | if (!ConvHints.isNull()) { |
| 9087 | for (llvm::SmallVector<FixItHint, 1>::iterator |
| 9088 | HI = ConvHints.Hints.begin(), HE = ConvHints.Hints.end(); |
| 9089 | HI != HE; ++HI) |
| 9090 | FDiag << *HI; |
| 9091 | } else { |
| 9092 | FDiag << Hint; |
| 9093 | } |
| 9094 | if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } |
| 9095 | |
| 9096 | Diag(Loc, FDiag); |
| 9097 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9098 | if (CheckInferredResultType) |
| 9099 | EmitRelatedResultTypeNote(SrcExpr); |
| 9100 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9101 | if (Complained) |
| 9102 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9103 | return isInvalid; |
| 9104 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9105 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 9106 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9107 | llvm::APSInt ICEResult; |
| 9108 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 9109 | if (Result) |
| 9110 | *Result = ICEResult; |
| 9111 | return false; |
| 9112 | } |
| 9113 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9114 | Expr::EvalResult EvalResult; |
| 9115 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9116 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9117 | EvalResult.HasSideEffects) { |
| 9118 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 9119 | |
| 9120 | if (EvalResult.Diag) { |
| 9121 | // We only show the note if it's not the usual "invalid subexpression" |
| 9122 | // or if it's actually in a subexpression. |
| 9123 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 9124 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 9125 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 9126 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9127 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9128 | return true; |
| 9129 | } |
| 9130 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9131 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 9132 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9133 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9134 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9135 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 9136 | != Diagnostic::Ignored) |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9137 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9138 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9139 | if (Result) |
| 9140 | *Result = EvalResult.Val.getInt(); |
| 9141 | return false; |
| 9142 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9143 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9144 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9145 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9146 | ExprEvalContexts.push_back( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9147 | ExpressionEvaluationContextRecord(NewContext, |
| 9148 | ExprTemporaries.size(), |
| 9149 | ExprNeedsCleanups)); |
| 9150 | ExprNeedsCleanups = false; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9151 | } |
| 9152 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9153 | void Sema::PopExpressionEvaluationContext() { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9154 | // Pop the current expression evaluation context off the stack. |
| 9155 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 9156 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9157 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9158 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 9159 | if (Rec.PotentiallyReferenced) { |
| 9160 | // Mark any remaining declarations in the current position of the stack |
| 9161 | // as "referenced". If they were not meant to be referenced, semantic |
| 9162 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9163 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9164 | I = Rec.PotentiallyReferenced->begin(), |
| 9165 | IEnd = Rec.PotentiallyReferenced->end(); |
| 9166 | I != IEnd; ++I) |
| 9167 | MarkDeclarationReferenced(I->first, I->second); |
| 9168 | } |
| 9169 | |
| 9170 | if (Rec.PotentiallyDiagnosed) { |
| 9171 | // Emit any pending diagnostics. |
| 9172 | for (PotentiallyEmittedDiagnostics::iterator |
| 9173 | I = Rec.PotentiallyDiagnosed->begin(), |
| 9174 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 9175 | I != IEnd; ++I) |
| 9176 | Diag(I->first, I->second); |
| 9177 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9178 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9179 | |
| 9180 | // When are coming out of an unevaluated context, clear out any |
| 9181 | // temporaries that we may have created as part of the evaluation of |
| 9182 | // the expression in that context: they aren't relevant because they |
| 9183 | // will never be constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9184 | if (Rec.Context == Unevaluated) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9185 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 9186 | ExprTemporaries.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9187 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
| 9188 | |
| 9189 | // Otherwise, merge the contexts together. |
| 9190 | } else { |
| 9191 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
| 9192 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9193 | |
| 9194 | // Destroy the popped expression evaluation record. |
| 9195 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9196 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9197 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9198 | void Sema::DiscardCleanupsInEvaluationContext() { |
| 9199 | ExprTemporaries.erase( |
| 9200 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 9201 | ExprTemporaries.end()); |
| 9202 | ExprNeedsCleanups = false; |
| 9203 | } |
| 9204 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9205 | /// \brief Note that the given declaration was referenced in the source code. |
| 9206 | /// |
| 9207 | /// This routine should be invoke whenever a given declaration is referenced |
| 9208 | /// in the source code, and where that reference occurred. If this declaration |
| 9209 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 9210 | /// C99 6.9p3), then the declaration will be marked as used. |
| 9211 | /// |
| 9212 | /// \param Loc the location where the declaration was referenced. |
| 9213 | /// |
| 9214 | /// \param D the declaration that has been referenced by the source code. |
| 9215 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 9216 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9217 | |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 9218 | D->setReferenced(); |
| 9219 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9220 | if (D->isUsed(false)) |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9221 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9222 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9223 | // Mark a parameter or variable declaration "used", regardless of whether |
| 9224 | // we're in a template or not. The reason for this is that unevaluated |
| 9225 | // expressions (e.g. (void)sizeof()) constitute a use for warning purposes |
| 9226 | // (-Wunused-variables and -Wunused-parameters) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9227 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9228 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 9229 | D->setUsed(); |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9230 | return; |
| 9231 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9232 | |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9233 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 9234 | return; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9235 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9236 | // Do not mark anything as "used" within a dependent context; wait for |
| 9237 | // an instantiation. |
| 9238 | if (CurContext->isDependentContext()) |
| 9239 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9240 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9241 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9242 | case Unevaluated: |
| 9243 | // We are in an expression that is not potentially evaluated; do nothing. |
| 9244 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9245 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9246 | case PotentiallyEvaluated: |
| 9247 | // We are in a potentially-evaluated expression, so this declaration is |
| 9248 | // "used"; handle this below. |
| 9249 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9250 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9251 | case PotentiallyPotentiallyEvaluated: |
| 9252 | // We are in an expression that may be potentially evaluated; queue this |
| 9253 | // declaration reference until we know whether the expression is |
| 9254 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9255 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9256 | return; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9257 | |
| 9258 | case PotentiallyEvaluatedIfUsed: |
| 9259 | // Referenced declarations will only be used if the construct in the |
| 9260 | // containing expression is used. |
| 9261 | return; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9262 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9263 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9264 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 9265 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9266 | if (Constructor->isDefaulted()) { |
| 9267 | if (Constructor->isDefaultConstructor()) { |
| 9268 | if (Constructor->isTrivial()) |
| 9269 | return; |
| 9270 | if (!Constructor->isUsed(false)) |
| 9271 | DefineImplicitDefaultConstructor(Loc, Constructor); |
| 9272 | } else if (Constructor->isCopyConstructor()) { |
| 9273 | if (!Constructor->isUsed(false)) |
| 9274 | DefineImplicitCopyConstructor(Loc, Constructor); |
| 9275 | } else if (Constructor->isMoveConstructor()) { |
| 9276 | if (!Constructor->isUsed(false)) |
| 9277 | DefineImplicitMoveConstructor(Loc, Constructor); |
| 9278 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9279 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9280 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9281 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9282 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9283 | if (Destructor->isDefaulted() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9284 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9285 | if (Destructor->isVirtual()) |
| 9286 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9287 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 9288 | if (MethodDecl->isDefaulted() && MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9289 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9290 | if (!MethodDecl->isUsed(false)) { |
| 9291 | if (MethodDecl->isCopyAssignmentOperator()) |
| 9292 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
| 9293 | else |
| 9294 | DefineImplicitMoveAssignment(Loc, MethodDecl); |
| 9295 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9296 | } else if (MethodDecl->isVirtual()) |
| 9297 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9298 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 9299 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9300 | // Recursive functions should be marked when used from another function. |
| 9301 | if (CurContext == Function) return; |
| 9302 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9303 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 9304 | // class templates. |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 9305 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9306 | bool AlreadyInstantiated = false; |
| 9307 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 9308 | = Function->getTemplateSpecializationInfo()) { |
| 9309 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 9310 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9311 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9312 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9313 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9314 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9315 | = Function->getMemberSpecializationInfo()) { |
| 9316 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 9317 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9318 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9319 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9320 | AlreadyInstantiated = true; |
| 9321 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9322 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9323 | if (!AlreadyInstantiated) { |
| 9324 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 9325 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 9326 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 9327 | Loc)); |
| 9328 | else |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9329 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9330 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9331 | } else { |
| 9332 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9333 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 9334 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | be9ebe3 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 9335 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9336 | MarkDeclarationReferenced(Loc, *i); |
| 9337 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9338 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9339 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9340 | // Keep track of used but undefined functions. |
| 9341 | if (!Function->isPure() && !Function->hasBody() && |
| 9342 | Function->getLinkage() != ExternalLinkage) { |
| 9343 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 9344 | if (old.isInvalid()) old = Loc; |
| 9345 | } |
Argyrios Kyrtzidis | 58b5259 | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 9346 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9347 | Function->setUsed(true); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9348 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9349 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9350 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9351 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9352 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9353 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9354 | Var->getInstantiatedFromStaticDataMember()) { |
| 9355 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 9356 | assert(MSInfo && "Missing member specialization information?"); |
| 9357 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 9358 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 9359 | MSInfo->setPointOfInstantiation(Loc); |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 9360 | // This is a modification of an existing AST node. Notify listeners. |
| 9361 | if (ASTMutationListener *L = getASTMutationListener()) |
| 9362 | L->StaticDataMemberInstantiated(Var); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9363 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9364 | } |
| 9365 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9366 | |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9367 | // Keep track of used but undefined variables. We make a hole in |
| 9368 | // the warning for static const data members with in-line |
| 9369 | // initializers. |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9370 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9371 | && Var->getLinkage() != ExternalLinkage |
| 9372 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9373 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 9374 | if (old.isInvalid()) old = Loc; |
| 9375 | } |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9376 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9377 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9378 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 9379 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9380 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9381 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9382 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9383 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9384 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9385 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9386 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 9387 | Sema &S; |
| 9388 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9389 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9390 | public: |
| 9391 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9392 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9393 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9394 | |
| 9395 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 9396 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9397 | }; |
| 9398 | } |
| 9399 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9400 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 9401 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9402 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 9403 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 9404 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9405 | |
| 9406 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9407 | } |
| 9408 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9409 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9410 | if (ClassTemplateSpecializationDecl *Spec |
| 9411 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 9412 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9413 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9414 | } |
| 9415 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 9416 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9417 | } |
| 9418 | |
| 9419 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 9420 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9421 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9422 | } |
| 9423 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9424 | namespace { |
| 9425 | /// \brief Helper class that marks all of the declarations referenced by |
| 9426 | /// potentially-evaluated subexpressions as "referenced". |
| 9427 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 9428 | Sema &S; |
| 9429 | |
| 9430 | public: |
| 9431 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 9432 | |
| 9433 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 9434 | |
| 9435 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 9436 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9437 | } |
| 9438 | |
| 9439 | void VisitMemberExpr(MemberExpr *E) { |
| 9440 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9441 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9442 | } |
| 9443 | |
| 9444 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 9445 | if (E->getConstructor()) |
| 9446 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 9447 | if (E->getOperatorNew()) |
| 9448 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 9449 | if (E->getOperatorDelete()) |
| 9450 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9451 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9452 | } |
| 9453 | |
| 9454 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 9455 | if (E->getOperatorDelete()) |
| 9456 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 9457 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 9458 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 9459 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 9460 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 9461 | S.LookupDestructor(Record)); |
| 9462 | } |
| 9463 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9464 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9465 | } |
| 9466 | |
| 9467 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 9468 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9469 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9470 | } |
| 9471 | |
| 9472 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 9473 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9474 | } |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 9475 | |
| 9476 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 9477 | Visit(E->getExpr()); |
| 9478 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9479 | }; |
| 9480 | } |
| 9481 | |
| 9482 | /// \brief Mark any declarations that appear within this expression or any |
| 9483 | /// potentially-evaluated subexpressions as "referenced". |
| 9484 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 9485 | EvaluatedExprMarker(*this).Visit(E); |
| 9486 | } |
| 9487 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9488 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 9489 | /// of the program being compiled. |
| 9490 | /// |
| 9491 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9492 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9493 | /// possibility that the code will actually be executable. Code in sizeof() |
| 9494 | /// expressions, code used only during overload resolution, etc., are not |
| 9495 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 9496 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9497 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9498 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9499 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9500 | /// This routine should be used for all diagnostics that describe the run-time |
| 9501 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 9502 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 9503 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9504 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *Statement, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9505 | const PartialDiagnostic &PD) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9506 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9507 | case Unevaluated: |
| 9508 | // The argument will never be evaluated, so don't complain. |
| 9509 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9510 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9511 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9512 | case PotentiallyEvaluatedIfUsed: |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9513 | if (Statement && getCurFunctionOrMethodDecl()) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 9514 | FunctionScopes.back()->PossiblyUnreachableDiags. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9515 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, Statement)); |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 9516 | } |
| 9517 | else |
| 9518 | Diag(Loc, PD); |
| 9519 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9520 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9521 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9522 | case PotentiallyPotentiallyEvaluated: |
| 9523 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 9524 | break; |
| 9525 | } |
| 9526 | |
| 9527 | return false; |
| 9528 | } |
| 9529 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9530 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 9531 | CallExpr *CE, FunctionDecl *FD) { |
| 9532 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 9533 | return false; |
| 9534 | |
| 9535 | PartialDiagnostic Note = |
| 9536 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 9537 | << FD->getDeclName() : PDiag(); |
| 9538 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9539 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9540 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9541 | FD ? |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9542 | PDiag(diag::err_call_function_incomplete_return) |
| 9543 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9544 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9545 | << CE->getSourceRange(), |
| 9546 | std::make_pair(NoteLoc, Note))) |
| 9547 | return true; |
| 9548 | |
| 9549 | return false; |
| 9550 | } |
| 9551 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9552 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9553 | // will prevent this condition from triggering, which is what we want. |
| 9554 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 9555 | SourceLocation Loc; |
| 9556 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9557 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9558 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9559 | |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 9560 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9561 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9562 | return; |
| 9563 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9564 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 9565 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9566 | // Greylist some idioms by putting them into a warning subcategory. |
| 9567 | if (ObjCMessageExpr *ME |
| 9568 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 9569 | Selector Sel = ME->getSelector(); |
| 9570 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9571 | // self = [<foo> init...] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9572 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9573 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9574 | |
| 9575 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9576 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9577 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9578 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9579 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9580 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 9581 | } else if (CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9582 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9583 | return; |
| 9584 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9585 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9586 | Loc = Op->getOperatorLoc(); |
| 9587 | } else { |
| 9588 | // Not an assignment. |
| 9589 | return; |
| 9590 | } |
| 9591 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9592 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9593 | |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9594 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 9595 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 9596 | Diag(Loc, diag::note_condition_assign_silence) |
| 9597 | << FixItHint::CreateInsertion(Open, "(") |
| 9598 | << FixItHint::CreateInsertion(Close, ")"); |
| 9599 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9600 | if (IsOrAssign) |
| 9601 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 9602 | << FixItHint::CreateReplacement(Loc, "!="); |
| 9603 | else |
| 9604 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 9605 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9606 | } |
| 9607 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9608 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 9609 | /// that the user intended an assignment used as condition. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9610 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *ParenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9611 | // Don't warn if the parens came from a macro. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9612 | SourceLocation parenLoc = ParenE->getLocStart(); |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9613 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 9614 | return; |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 9615 | // Don't warn for dependent expressions. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9616 | if (ParenE->isTypeDependent()) |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 9617 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9618 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9619 | Expr *E = ParenE->IgnoreParens(); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9620 | |
| 9621 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 9622 | if (opE->getOpcode() == BO_EQ && |
| 9623 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 9624 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9625 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 9626 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9627 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9628 | Diag(Loc, diag::note_equality_comparison_silence) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9629 | << FixItHint::CreateRemoval(ParenE->getSourceRange().getBegin()) |
| 9630 | << FixItHint::CreateRemoval(ParenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9631 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 9632 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9633 | } |
| 9634 | } |
| 9635 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9636 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9637 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9638 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 9639 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9640 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9641 | ExprResult result = CheckPlaceholderExpr(E); |
| 9642 | if (result.isInvalid()) return ExprError(); |
| 9643 | E = result.take(); |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 9644 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9645 | if (!E->isTypeDependent()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9646 | if (getLangOptions().CPlusPlus) |
| 9647 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 9648 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9649 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 9650 | if (ERes.isInvalid()) |
| 9651 | return ExprError(); |
| 9652 | E = ERes.take(); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 9653 | |
| 9654 | QualType T = E->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9655 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 9656 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 9657 | << T << E->getSourceRange(); |
| 9658 | return ExprError(); |
| 9659 | } |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9660 | } |
| 9661 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9662 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9663 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9664 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9665 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9666 | Expr *SubExpr) { |
| 9667 | if (!SubExpr) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9668 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9669 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9670 | return CheckBooleanCondition(SubExpr, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9671 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9672 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9673 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9674 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 9675 | /// to have an appropriate type. |
| 9676 | struct RebuildUnknownAnyFunction |
| 9677 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 9678 | |
| 9679 | Sema &S; |
| 9680 | |
| 9681 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 9682 | |
| 9683 | ExprResult VisitStmt(Stmt *S) { |
| 9684 | llvm_unreachable("unexpected statement!"); |
| 9685 | return ExprError(); |
| 9686 | } |
| 9687 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9688 | ExprResult VisitExpr(Expr *E) { |
| 9689 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 9690 | << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9691 | return ExprError(); |
| 9692 | } |
| 9693 | |
| 9694 | /// Rebuild an expression which simply semantically wraps another |
| 9695 | /// expression which it shares the type and value kind of. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9696 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 9697 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 9698 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9699 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9700 | Expr *SubExpr = SubResult.take(); |
| 9701 | E->setSubExpr(SubExpr); |
| 9702 | E->setType(SubExpr->getType()); |
| 9703 | E->setValueKind(SubExpr->getValueKind()); |
| 9704 | assert(E->getObjectKind() == OK_Ordinary); |
| 9705 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9706 | } |
| 9707 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9708 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 9709 | return rebuildSugarExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9710 | } |
| 9711 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9712 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 9713 | return rebuildSugarExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9714 | } |
| 9715 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9716 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 9717 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 9718 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9719 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9720 | Expr *SubExpr = SubResult.take(); |
| 9721 | E->setSubExpr(SubExpr); |
| 9722 | E->setType(S.Context.getPointerType(SubExpr->getType())); |
| 9723 | assert(E->getValueKind() == VK_RValue); |
| 9724 | assert(E->getObjectKind() == OK_Ordinary); |
| 9725 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9726 | } |
| 9727 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9728 | ExprResult resolveDecl(Expr *E, ValueDecl *VD) { |
| 9729 | if (!isa<FunctionDecl>(VD)) return VisitExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9730 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9731 | E->setType(VD->getType()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9732 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9733 | assert(E->getValueKind() == VK_RValue); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9734 | if (S.getLangOptions().CPlusPlus && |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9735 | !(isa<CXXMethodDecl>(VD) && |
| 9736 | cast<CXXMethodDecl>(VD)->isInstance())) |
| 9737 | E->setValueKind(VK_LValue); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9738 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9739 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9740 | } |
| 9741 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9742 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 9743 | return resolveDecl(E, E->getMemberDecl()); |
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 VisitDeclRefExpr(DeclRefExpr *E) { |
| 9747 | return resolveDecl(E, E->getDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9748 | } |
| 9749 | }; |
| 9750 | } |
| 9751 | |
| 9752 | /// Given a function expression of unknown-any type, try to rebuild it |
| 9753 | /// to have a function type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9754 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *FunctionExpr) { |
| 9755 | ExprResult Result = RebuildUnknownAnyFunction(S).Visit(FunctionExpr); |
| 9756 | if (Result.isInvalid()) return ExprError(); |
| 9757 | return S.DefaultFunctionArrayConversion(Result.take()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9758 | } |
| 9759 | |
| 9760 | namespace { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9761 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 9762 | /// into one which resolves the type directly on the referring |
| 9763 | /// expression. Strict preservation of the original source |
| 9764 | /// structure is not a goal. |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9765 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9766 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9767 | |
| 9768 | Sema &S; |
| 9769 | |
| 9770 | /// The current destination type. |
| 9771 | QualType DestType; |
| 9772 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9773 | RebuildUnknownAnyExpr(Sema &S, QualType CastType) |
| 9774 | : S(S), DestType(CastType) {} |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9775 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9776 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9777 | llvm_unreachable("unexpected statement!"); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9778 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9779 | } |
| 9780 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9781 | ExprResult VisitExpr(Expr *E) { |
| 9782 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 9783 | << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9784 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9785 | } |
| 9786 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9787 | ExprResult VisitCallExpr(CallExpr *E); |
| 9788 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9789 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9790 | /// Rebuild an expression which simply semantically wraps another |
| 9791 | /// expression which it shares the type and value kind of. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9792 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 9793 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 9794 | if (SubResult.isInvalid()) return ExprError(); |
| 9795 | Expr *SubExpr = SubResult.take(); |
| 9796 | E->setSubExpr(SubExpr); |
| 9797 | E->setType(SubExpr->getType()); |
| 9798 | E->setValueKind(SubExpr->getValueKind()); |
| 9799 | assert(E->getObjectKind() == OK_Ordinary); |
| 9800 | return E; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9801 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9802 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9803 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 9804 | return rebuildSugarExpr(E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9805 | } |
| 9806 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9807 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 9808 | return rebuildSugarExpr(E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9809 | } |
| 9810 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9811 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 9812 | const PointerType *Ptr = DestType->getAs<PointerType>(); |
| 9813 | if (!Ptr) { |
| 9814 | S.Diag(E->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 9815 | << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9816 | return ExprError(); |
| 9817 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9818 | assert(E->getValueKind() == VK_RValue); |
| 9819 | assert(E->getObjectKind() == OK_Ordinary); |
| 9820 | E->setType(DestType); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9821 | |
| 9822 | // 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] | 9823 | DestType = Ptr->getPointeeType(); |
| 9824 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 9825 | if (SubResult.isInvalid()) return ExprError(); |
| 9826 | E->setSubExpr(SubResult.take()); |
| 9827 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9828 | } |
| 9829 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9830 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9831 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9832 | ExprResult resolveDecl(Expr *E, ValueDecl *VD); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9833 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9834 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 9835 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9836 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9837 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9838 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 9839 | return resolveDecl(E, E->getDecl()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9840 | } |
| 9841 | }; |
| 9842 | } |
| 9843 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9844 | /// Rebuilds a call expression which yielded __unknown_anytype. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9845 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) { |
| 9846 | Expr *CalleeExpr = E->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9847 | |
| 9848 | enum FnKind { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9849 | FK_MemberFunction, |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9850 | FK_FunctionPointer, |
| 9851 | FK_BlockPointer |
| 9852 | }; |
| 9853 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9854 | FnKind Kind; |
| 9855 | QualType CalleeType = CalleeExpr->getType(); |
| 9856 | if (CalleeType == S.Context.BoundMemberTy) { |
| 9857 | assert(isa<CXXMemberCallExpr>(E) || isa<CXXOperatorCallExpr>(E)); |
| 9858 | Kind = FK_MemberFunction; |
| 9859 | CalleeType = Expr::findBoundMemberType(CalleeExpr); |
| 9860 | } else if (const PointerType *Ptr = CalleeType->getAs<PointerType>()) { |
| 9861 | CalleeType = Ptr->getPointeeType(); |
| 9862 | Kind = FK_FunctionPointer; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9863 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9864 | CalleeType = CalleeType->castAs<BlockPointerType>()->getPointeeType(); |
| 9865 | Kind = FK_BlockPointer; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9866 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9867 | const FunctionType *FnType = CalleeType->castAs<FunctionType>(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9868 | |
| 9869 | // Verify that this is a legal result type of a function. |
| 9870 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 9871 | unsigned diagID = diag::err_func_returning_array_function; |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9872 | if (Kind == FK_BlockPointer) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9873 | diagID = diag::err_block_returning_array_function; |
| 9874 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9875 | S.Diag(E->getExprLoc(), diagID) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9876 | << DestType->isFunctionType() << DestType; |
| 9877 | return ExprError(); |
| 9878 | } |
| 9879 | |
| 9880 | // Otherwise, go ahead and set DestType as the call's result. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9881 | E->setType(DestType.getNonLValueExprType(S.Context)); |
| 9882 | E->setValueKind(Expr::getValueKindForType(DestType)); |
| 9883 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9884 | |
| 9885 | // Rebuild the function type, replacing the result type with DestType. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9886 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType)) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9887 | DestType = S.Context.getFunctionType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9888 | Proto->arg_type_begin(), |
| 9889 | Proto->getNumArgs(), |
| 9890 | Proto->getExtProtoInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9891 | else |
| 9892 | DestType = S.Context.getFunctionNoProtoType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9893 | FnType->getExtInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9894 | |
| 9895 | // Rebuild the appropriate pointer-to-function type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9896 | switch (Kind) { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9897 | case FK_MemberFunction: |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9898 | // Nothing to do. |
| 9899 | break; |
| 9900 | |
| 9901 | case FK_FunctionPointer: |
| 9902 | DestType = S.Context.getPointerType(DestType); |
| 9903 | break; |
| 9904 | |
| 9905 | case FK_BlockPointer: |
| 9906 | DestType = S.Context.getBlockPointerType(DestType); |
| 9907 | break; |
| 9908 | } |
| 9909 | |
| 9910 | // Finally, we can recurse. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9911 | ExprResult CalleeResult = Visit(CalleeExpr); |
| 9912 | if (!CalleeResult.isUsable()) return ExprError(); |
| 9913 | E->setCallee(CalleeResult.take()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9914 | |
| 9915 | // Bind a temporary if necessary. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9916 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9917 | } |
| 9918 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9919 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9920 | // Verify that this is a legal result type of a call. |
| 9921 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9922 | S.Diag(E->getExprLoc(), diag::err_func_returning_array_function) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9923 | << DestType->isFunctionType() << DestType; |
| 9924 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9925 | } |
| 9926 | |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 9927 | // Rewrite the method result type if available. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9928 | if (ObjCMethodDecl *Method = E->getMethodDecl()) { |
| 9929 | assert(Method->getResultType() == S.Context.UnknownAnyTy); |
| 9930 | Method->setResultType(DestType); |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 9931 | } |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9932 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9933 | // Change the type of the message. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9934 | E->setType(DestType.getNonReferenceType()); |
| 9935 | E->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9936 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9937 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9938 | } |
| 9939 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9940 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9941 | // 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] | 9942 | assert(E->getCastKind() == CK_FunctionToPointerDecay); |
| 9943 | assert(E->getValueKind() == VK_RValue); |
| 9944 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9945 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9946 | E->setType(DestType); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9947 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9948 | // Rebuild the sub-expression as the pointee (function) type. |
| 9949 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 9950 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9951 | ExprResult Result = Visit(E->getSubExpr()); |
| 9952 | if (!Result.isUsable()) return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9953 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9954 | E->setSubExpr(Result.take()); |
| 9955 | return S.Owned(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::resolveDecl(Expr *E, ValueDecl *VD) { |
| 9959 | ExprValueKind ValueKind = VK_LValue; |
| 9960 | QualType Type = DestType; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9961 | |
| 9962 | // We know how to make this work for certain kinds of decls: |
| 9963 | |
| 9964 | // - functions |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9965 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(VD)) { |
| 9966 | if (const PointerType *Ptr = Type->getAs<PointerType>()) { |
| 9967 | DestType = Ptr->getPointeeType(); |
| 9968 | ExprResult Result = resolveDecl(E, VD); |
| 9969 | if (Result.isInvalid()) return ExprError(); |
| 9970 | return S.ImpCastExprToType(Result.take(), Type, |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 9971 | CK_FunctionToPointerDecay, VK_RValue); |
| 9972 | } |
| 9973 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9974 | if (!Type->isFunctionType()) { |
| 9975 | S.Diag(E->getExprLoc(), diag::err_unknown_any_function) |
| 9976 | << VD << E->getSourceRange(); |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 9977 | return ExprError(); |
| 9978 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9979 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9980 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 9981 | if (MD->isInstance()) { |
| 9982 | ValueKind = VK_RValue; |
| 9983 | Type = S.Context.BoundMemberTy; |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9984 | } |
| 9985 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9986 | // Function references aren't l-values in C. |
| 9987 | if (!S.getLangOptions().CPlusPlus) |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9988 | ValueKind = VK_RValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9989 | |
| 9990 | // - variables |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9991 | } else if (isa<VarDecl>(VD)) { |
| 9992 | if (const ReferenceType *RefTy = Type->getAs<ReferenceType>()) { |
| 9993 | Type = RefTy->getPointeeType(); |
| 9994 | } else if (Type->isFunctionType()) { |
| 9995 | S.Diag(E->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 9996 | << VD << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9997 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9998 | } |
| 9999 | |
| 10000 | // - nothing else |
| 10001 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10002 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 10003 | << VD << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10004 | return ExprError(); |
| 10005 | } |
| 10006 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10007 | VD->setType(DestType); |
| 10008 | E->setType(Type); |
| 10009 | E->setValueKind(ValueKind); |
| 10010 | return S.Owned(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10011 | } |
| 10012 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10013 | /// Check a cast of an unknown-any type. We intentionally only |
| 10014 | /// trigger this for C-style casts. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10015 | ExprResult Sema::checkUnknownAnyCast(SourceRange TypeRange, QualType CastType, |
| 10016 | Expr *CastExpr, CastKind &CastKind, |
| 10017 | ExprValueKind &VK, CXXCastPath &Path) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10018 | // Rewrite the casted expression from scratch. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10019 | ExprResult result = RebuildUnknownAnyExpr(*this, CastType).Visit(CastExpr); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10020 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10021 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10022 | CastExpr = result.take(); |
| 10023 | VK = CastExpr->getValueKind(); |
| 10024 | CastKind = CK_NoOp; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10025 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10026 | return CastExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10027 | } |
| 10028 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10029 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *E) { |
| 10030 | Expr *orig = E; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10031 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10032 | while (true) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10033 | E = E->IgnoreParenImpCasts(); |
| 10034 | if (CallExpr *call = dyn_cast<CallExpr>(E)) { |
| 10035 | E = call->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10036 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10037 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10038 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10039 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10040 | } |
| 10041 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10042 | SourceLocation loc; |
| 10043 | NamedDecl *d; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10044 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10045 | loc = ref->getLocation(); |
| 10046 | d = ref->getDecl(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10047 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10048 | loc = mem->getMemberLoc(); |
| 10049 | d = mem->getMemberDecl(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10050 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10051 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10052 | loc = msg->getSelectorLoc(); |
| 10053 | d = msg->getMethodDecl(); |
John McCall | 819e745 | 2011-08-31 20:57:36 +0000 | [diff] [blame] | 10054 | if (!d) { |
| 10055 | S.Diag(loc, diag::err_uncasted_send_to_unknown_any_method) |
| 10056 | << static_cast<unsigned>(msg->isClassMessage()) << msg->getSelector() |
| 10057 | << orig->getSourceRange(); |
| 10058 | return ExprError(); |
| 10059 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10060 | } else { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10061 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10062 | << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10063 | return ExprError(); |
| 10064 | } |
| 10065 | |
| 10066 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10067 | |
| 10068 | // Never recoverable. |
| 10069 | return ExprError(); |
| 10070 | } |
| 10071 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10072 | /// Check for operands with placeholder types and complain if found. |
| 10073 | /// Returns true if there was an error and no recovery was possible. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 10074 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10075 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 10076 | QualType type = E->getType(); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10077 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10078 | // Overloaded expressions. |
| 10079 | if (type == Context.OverloadTy) |
| 10080 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 10081 | E->getSourceRange(), |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10082 | QualType(), |
| 10083 | diag::err_ovl_unresolvable); |
| 10084 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10085 | // Bound member functions. |
| 10086 | if (type == Context.BoundMemberTy) { |
| 10087 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 10088 | << E->getSourceRange(); |
| 10089 | return ExprError(); |
| 10090 | } |
| 10091 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10092 | // Expressions of unknown type. |
| 10093 | if (type == Context.UnknownAnyTy) |
| 10094 | return diagnoseUnknownAnyExpr(*this, E); |
| 10095 | |
| 10096 | assert(!type->isPlaceholderType()); |
| 10097 | return Owned(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10098 | } |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10099 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10100 | bool Sema::CheckCaseExpression(Expr *E) { |
| 10101 | if (E->isTypeDependent()) |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10102 | return true; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10103 | if (E->isValueDependent() || E->isIntegerConstantExpr(Context)) |
| 10104 | return E->getType()->isIntegralOrEnumerationType(); |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10105 | return false; |
| 10106 | } |